blob: 80c392ae1ce6137b68c72f5653fc5a8d03af8eee [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);
Hal Finkele9150472013-03-27 19:10:42 +0000473 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Michael Liao6c0e04c2012-10-15 22:39:43 +0000474 // 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
Michael Liao5c5f1902013-03-20 02:28:20 +00001056 // In the customized shift lowering, the legal cases in AVX2 will be
1057 // recognized.
1058 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1059 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001060
Michael Liao5c5f1902013-03-20 02:28:20 +00001061 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1062 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001063
Michael Liao5c5f1902013-03-20 02:28:20 +00001064 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001065
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001066 setOperationAction(ISD::SDIV, MVT::v8i16, Custom);
1067 setOperationAction(ISD::SDIV, MVT::v4i32, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +00001068 }
1069
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001070 if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) {
Craig Topperc9099502012-04-20 06:31:50 +00001071 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1072 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1073 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1074 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1075 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1076 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001077
Owen Anderson825b72b2009-08-11 20:47:22 +00001078 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001079 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1080 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001081
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1083 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1084 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1085 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1086 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001087 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001088 setOperationAction(ISD::FCEIL, MVT::v8f32, Legal);
1089 setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal);
1090 setOperationAction(ISD::FRINT, MVT::v8f32, Legal);
1091 setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001092 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001093 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001094
Owen Anderson825b72b2009-08-11 20:47:22 +00001095 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1096 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1097 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1098 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1099 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001100 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Craig Topperd5775522012-11-16 06:37:56 +00001101 setOperationAction(ISD::FCEIL, MVT::v4f64, Legal);
1102 setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal);
1103 setOperationAction(ISD::FRINT, MVT::v4f64, Legal);
1104 setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001105 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001106 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001107
Michael Liaobedcbd42012-10-16 18:14:11 +00001108 setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom);
Nadav Rotem3c22a442012-12-27 07:45:10 +00001109 setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom);
Michael Liaobedcbd42012-10-16 18:14:11 +00001110
1111 setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom);
1112
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001113 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1114 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001115 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001116
Michael Liaoa7554632012-10-23 17:36:08 +00001117 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1118 setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom);
1119 setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom);
1120
Michael Liaob8150d82012-09-10 18:33:51 +00001121 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1122
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001123 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1124 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1125
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001126 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1127 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1128
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001129 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001130 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001131
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001132 setOperationAction(ISD::SDIV, MVT::v16i16, Custom);
1133
Duncan Sands28b77e92011-09-06 19:07:46 +00001134 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1135 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1136 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1137 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001138
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001139 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1140 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1141 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1142
Craig Topperaaa643c2011-11-09 07:28:55 +00001143 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1144 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1145 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1146 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001147
Nadav Rotem0509db22012-12-28 05:45:24 +00001148 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom);
1149 setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom);
1150 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom);
1151 setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom);
1152 setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom);
1153 setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom);
Nadav Rotem1a330af2012-12-27 22:47:16 +00001154
Craig Topperbf404372012-08-31 15:40:30 +00001155 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Craig Topper3dcefc82012-11-21 05:36:24 +00001156 setOperationAction(ISD::FMA, MVT::v8f32, Legal);
1157 setOperationAction(ISD::FMA, MVT::v4f64, Legal);
1158 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
1159 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
1160 setOperationAction(ISD::FMA, MVT::f32, Legal);
1161 setOperationAction(ISD::FMA, MVT::f64, Legal);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001162 }
Craig Topper880ef452012-08-11 22:34:26 +00001163
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001164 if (Subtarget->hasInt256()) {
Craig Topperaaa643c2011-11-09 07:28:55 +00001165 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1166 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1167 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1168 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001169
Craig Topperaaa643c2011-11-09 07:28:55 +00001170 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1171 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1172 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1173 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001174
Craig Topperaaa643c2011-11-09 07:28:55 +00001175 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1176 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1177 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001178 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001179
1180 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001181
Nadav Rotem13f8cf52013-01-09 05:14:33 +00001182 setOperationAction(ISD::SDIV, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001183 } else {
1184 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1185 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1186 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1187 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1188
1189 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1190 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1191 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1192 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1193
1194 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1195 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1196 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1197 // Don't lower v32i8 because there is no 128-bit byte mul
1198 }
Craig Topper13894fa2011-08-24 06:14:18 +00001199
Michael Liao5c5f1902013-03-20 02:28:20 +00001200 // In the customized shift lowering, the legal cases in AVX2 will be
1201 // recognized.
1202 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1203 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1204
1205 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1206 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1207
1208 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
1209
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001210 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001211 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1212 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001213 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001214
1215 // Extract subvector is special because the value type
1216 // (result) is 128-bit but the source is 256-bit wide.
1217 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001218 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001219
1220 // Do not attempt to custom lower other non-256-bit vectors
1221 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001222 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001223
Craig Topper0d1f1762012-08-12 00:34:56 +00001224 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1225 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1226 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1227 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1228 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1229 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1230 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001231 }
1232
David Greene54d8eba2011-01-27 22:38:56 +00001233 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001234 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001235 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001236
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001237 // Do not attempt to promote non-256-bit vectors
1238 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001239 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001240
Craig Topper0d1f1762012-08-12 00:34:56 +00001241 setOperationAction(ISD::AND, VT, Promote);
1242 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1243 setOperationAction(ISD::OR, VT, Promote);
1244 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1245 setOperationAction(ISD::XOR, VT, Promote);
1246 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1247 setOperationAction(ISD::LOAD, VT, Promote);
1248 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1249 setOperationAction(ISD::SELECT, VT, Promote);
1250 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001251 }
David Greene9b9838d2009-06-29 16:47:10 +00001252 }
1253
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001254 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1255 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001256 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1257 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001258 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1259 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001260 }
1261
Evan Cheng6be2c582006-04-05 23:38:46 +00001262 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001264 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001265
Eli Friedman962f5492010-06-02 19:35:46 +00001266 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1267 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001268 //
Eli Friedman962f5492010-06-02 19:35:46 +00001269 // FIXME: We really should do custom legalization for addition and
1270 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1271 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001272 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1273 // Add/Sub/Mul with overflow operations are custom lowered.
1274 MVT VT = IntVTs[i];
1275 setOperationAction(ISD::SADDO, VT, Custom);
1276 setOperationAction(ISD::UADDO, VT, Custom);
1277 setOperationAction(ISD::SSUBO, VT, Custom);
1278 setOperationAction(ISD::USUBO, VT, Custom);
1279 setOperationAction(ISD::SMULO, VT, Custom);
1280 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001281 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001282
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001283 // There are no 8-bit 3-address imul/mul instructions
1284 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1285 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001286
Evan Chengd54f2d52009-03-31 19:38:51 +00001287 if (!Subtarget->is64Bit()) {
1288 // These libcalls are not available in 32-bit.
1289 setLibcallName(RTLIB::SHL_I128, 0);
1290 setLibcallName(RTLIB::SRL_I128, 0);
1291 setLibcallName(RTLIB::SRA_I128, 0);
1292 }
1293
Evan Cheng8688a582013-01-29 02:32:37 +00001294 // Combine sin / cos into one node or libcall if possible.
1295 if (Subtarget->hasSinCos()) {
1296 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1297 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Evan Chenga66f40a2013-01-30 22:56:35 +00001298 if (Subtarget->isTargetDarwin()) {
Evan Cheng8688a582013-01-29 02:32:37 +00001299 // For MacOSX, we don't want to the normal expansion of a libcall to
1300 // sincos. We want to issue a libcall to __sincos_stret to avoid memory
1301 // traffic.
1302 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1303 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1304 }
1305 }
1306
Evan Cheng206ee9d2006-07-07 08:33:52 +00001307 // We have target-specific dag combine patterns for the following nodes:
1308 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001309 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001310 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001311 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001312 setTargetDAGCombine(ISD::SHL);
1313 setTargetDAGCombine(ISD::SRA);
1314 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001315 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001316 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001317 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001318 setTargetDAGCombine(ISD::FADD);
1319 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001320 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001321 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001322 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001323 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001324 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001325 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001326 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky52981c42013-02-20 12:42:54 +00001327 setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001328 setTargetDAGCombine(ISD::TRUNCATE);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001329 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001330 setTargetDAGCombine(ISD::SETCC);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001331 if (Subtarget->is64Bit())
1332 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001333 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001334
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001335 computeRegisterProperties();
1336
Evan Cheng05219282011-01-06 06:52:41 +00001337 // On Darwin, -Os means optimize for size without hurting performance,
1338 // do not reduce the limit.
Jim Grosbach3450f802013-02-20 21:13:59 +00001339 MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1340 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
1341 MaxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
1342 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1343 MaxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1344 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001345 setPrefLoopAlignment(4); // 2^4 bytes.
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001346
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001347 // Predictable cmov don't hurt on atom because it's in-order.
Jim Grosbach3450f802013-02-20 21:13:59 +00001348 PredictableSelectIsExpensive = !Subtarget->isAtom();
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001349
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001350 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001351}
1352
Duncan Sands28b77e92011-09-06 19:07:46 +00001353EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1354 if (!VT.isVector()) return MVT::i8;
1355 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001356}
1357
Evan Cheng29286502008-01-23 23:17:41 +00001358/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1359/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001360static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001361 if (MaxAlign == 16)
1362 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001363 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001364 if (VTy->getBitWidth() == 128)
1365 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001366 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001367 unsigned EltAlign = 0;
1368 getMaxByValAlign(ATy->getElementType(), EltAlign);
1369 if (EltAlign > MaxAlign)
1370 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001371 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001372 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1373 unsigned EltAlign = 0;
1374 getMaxByValAlign(STy->getElementType(i), EltAlign);
1375 if (EltAlign > MaxAlign)
1376 MaxAlign = EltAlign;
1377 if (MaxAlign == 16)
1378 break;
1379 }
1380 }
Evan Cheng29286502008-01-23 23:17:41 +00001381}
1382
1383/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1384/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001385/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1386/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001387unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001388 if (Subtarget->is64Bit()) {
1389 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001390 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001391 if (TyAlign > 8)
1392 return TyAlign;
1393 return 8;
1394 }
1395
Evan Cheng29286502008-01-23 23:17:41 +00001396 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001397 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001398 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001399 return Align;
1400}
Chris Lattner2b02a442007-02-25 08:29:00 +00001401
Evan Chengf0df0312008-05-15 08:39:06 +00001402/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001403/// and store operations as a result of memset, memcpy, and memmove
1404/// lowering. If DstAlign is zero that means it's safe to destination
1405/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1406/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001407/// probably because the source does not need to be loaded. If 'IsMemset' is
1408/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1409/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1410/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001411/// It returns EVT::Other if the type should be determined using generic
1412/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001413EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001414X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1415 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001416 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001417 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001418 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001419 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001420 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001421 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1422 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001423 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001424 (Subtarget->isUnalignedMemAccessFast() ||
1425 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001426 (SrcAlign == 0 || SrcAlign >= 16)))) {
1427 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001428 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001429 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001430 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001431 return MVT::v8f32;
1432 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001433 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001434 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001435 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001436 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001437 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001438 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001439 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001440 // Do not use f64 to lower memcpy if source is string constant. It's
1441 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001442 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001443 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001444 }
Evan Chengf0df0312008-05-15 08:39:06 +00001445 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001446 return MVT::i64;
1447 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001448}
1449
Evan Cheng7d342672012-12-12 01:32:07 +00001450bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001451 if (VT == MVT::f32)
1452 return X86ScalarSSEf32;
1453 else if (VT == MVT::f64)
1454 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001455 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001456}
1457
Evan Cheng376642e2012-12-10 23:21:26 +00001458bool
1459X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1460 if (Fast)
1461 *Fast = Subtarget->isUnalignedMemAccessFast();
1462 return true;
1463}
1464
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001465/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1466/// current function. The returned value is a member of the
1467/// MachineJumpTableInfo::JTEntryKind enum.
1468unsigned X86TargetLowering::getJumpTableEncoding() const {
1469 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1470 // symbol.
1471 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1472 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001473 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001474
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001475 // Otherwise, use the normal jump table encoding heuristics.
1476 return TargetLowering::getJumpTableEncoding();
1477}
1478
Chris Lattnerc64daab2010-01-26 05:02:42 +00001479const MCExpr *
1480X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1481 const MachineBasicBlock *MBB,
1482 unsigned uid,MCContext &Ctx) const{
1483 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1484 Subtarget->isPICStyleGOT());
1485 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1486 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001487 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1488 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001489}
1490
Evan Chengcc415862007-11-09 01:32:10 +00001491/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1492/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001493SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001494 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001495 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001496 // This doesn't have DebugLoc associated with it, but is not really the
1497 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001498 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001499 return Table;
1500}
1501
Chris Lattner589c6f62010-01-26 06:28:43 +00001502/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1503/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1504/// MCExpr.
1505const MCExpr *X86TargetLowering::
1506getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1507 MCContext &Ctx) const {
1508 // X86-64 uses RIP relative addressing based on the jump table label.
1509 if (Subtarget->isPICStyleRIPRel())
1510 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1511
1512 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001513 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001514}
1515
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001516// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001517std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001518X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001519 const TargetRegisterClass *RRC = 0;
1520 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001521 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001522 default:
1523 return TargetLowering::findRepresentativeClass(VT);
1524 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001525 RRC = Subtarget->is64Bit() ?
1526 (const TargetRegisterClass*)&X86::GR64RegClass :
1527 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001528 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001529 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001530 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001531 break;
1532 case MVT::f32: case MVT::f64:
1533 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1534 case MVT::v4f32: case MVT::v2f64:
1535 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1536 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001537 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001538 break;
1539 }
1540 return std::make_pair(RRC, Cost);
1541}
1542
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001543bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1544 unsigned &Offset) const {
1545 if (!Subtarget->isTargetLinux())
1546 return false;
1547
1548 if (Subtarget->is64Bit()) {
1549 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1550 Offset = 0x28;
1551 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1552 AddressSpace = 256;
1553 else
1554 AddressSpace = 257;
1555 } else {
1556 // %gs:0x14 on i386
1557 Offset = 0x14;
1558 AddressSpace = 256;
1559 }
1560 return true;
1561}
1562
Chris Lattner2b02a442007-02-25 08:29:00 +00001563//===----------------------------------------------------------------------===//
1564// Return Value Calling Convention Implementation
1565//===----------------------------------------------------------------------===//
1566
Chris Lattner59ed56b2007-02-28 04:55:35 +00001567#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001568
Michael J. Spencerec38de22010-10-10 22:04:20 +00001569bool
Eric Christopher471e4222011-06-08 23:55:35 +00001570X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001571 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001572 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001573 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001574 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001575 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001576 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001577 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001578}
1579
Dan Gohman98ca4f22009-08-05 01:29:28 +00001580SDValue
1581X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001582 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001583 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001584 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001585 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001586 MachineFunction &MF = DAG.getMachineFunction();
1587 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001588
Chris Lattner9774c912007-02-27 05:28:59 +00001589 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001590 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 RVLocs, *DAG.getContext());
1592 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001593
Dan Gohman475871a2008-07-27 21:46:04 +00001594 SDValue Flag;
Dan Gohman475871a2008-07-27 21:46:04 +00001595 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001596 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1597 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001598 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1599 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001601 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001602 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1603 CCValAssign &VA = RVLocs[i];
1604 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001605 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001606 EVT ValVT = ValToCopy.getValueType();
1607
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001608 // Promote values to the appropriate types
1609 if (VA.getLocInfo() == CCValAssign::SExt)
1610 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1611 else if (VA.getLocInfo() == CCValAssign::ZExt)
1612 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1613 else if (VA.getLocInfo() == CCValAssign::AExt)
1614 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1615 else if (VA.getLocInfo() == CCValAssign::BCvt)
1616 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1617
Dale Johannesenc4510512010-09-24 19:05:48 +00001618 // If this is x86-64, and we disabled SSE, we can't return FP values,
1619 // or SSE or MMX vectors.
1620 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1621 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001622 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001623 report_fatal_error("SSE register return with SSE disabled");
1624 }
1625 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1626 // llvm-gcc has never done it right and no one has noticed, so this
1627 // should be OK for now.
1628 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001629 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001630 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001631
Chris Lattner447ff682008-03-11 03:23:40 +00001632 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1633 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001634 if (VA.getLocReg() == X86::ST0 ||
1635 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001636 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1637 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001638 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001639 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001640 RetOps.push_back(ValToCopy);
1641 // Don't emit a copytoreg.
1642 continue;
1643 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001644
Evan Cheng242b38b2009-02-23 09:03:22 +00001645 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1646 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001647 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001648 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001649 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001650 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001651 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1652 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001653 // If we don't have SSE2 available, convert to v4f32 so the generated
1654 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001655 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001656 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001657 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001658 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001659 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001660
Dale Johannesendd64c412009-02-04 00:33:20 +00001661 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001662 Flag = Chain.getValue(1);
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001663 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001664 }
Dan Gohman61a92132008-04-21 23:59:07 +00001665
Eli Benderskya5597f02013-01-25 22:07:43 +00001666 // The x86-64 ABIs require that for returning structs by value we copy
1667 // the sret argument into %rax/%eax (depending on ABI) for the return.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001668 // Win32 requires us to put the sret argument to %eax as well.
Eli Benderskya5597f02013-01-25 22:07:43 +00001669 // We saved the argument into a virtual register in the entry block,
1670 // so now we copy the value out and into %rax/%eax.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001671 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr() &&
1672 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Dan Gohman61a92132008-04-21 23:59:07 +00001673 MachineFunction &MF = DAG.getMachineFunction();
1674 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1675 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001676 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001677 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001678 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001679
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001680 unsigned RetValReg
1681 = (Subtarget->is64Bit() && !Subtarget->isTarget64BitILP32()) ?
1682 X86::RAX : X86::EAX;
Eli Benderskya5597f02013-01-25 22:07:43 +00001683 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001684 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001685
Eli Benderskya5597f02013-01-25 22:07:43 +00001686 // RAX/EAX now acts like a return value.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001687 RetOps.push_back(DAG.getRegister(RetValReg, getPointerTy()));
Dan Gohman61a92132008-04-21 23:59:07 +00001688 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001689
Chris Lattner447ff682008-03-11 03:23:40 +00001690 RetOps[0] = Chain; // Update chain.
1691
1692 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001693 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001694 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001695
1696 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001697 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001698}
1699
Evan Chengbf010eb2012-04-10 01:51:00 +00001700bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001701 if (N->getNumValues() != 1)
1702 return false;
1703 if (!N->hasNUsesOfValue(1, 0))
1704 return false;
1705
Evan Chengbf010eb2012-04-10 01:51:00 +00001706 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001707 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001708 if (Copy->getOpcode() == ISD::CopyToReg) {
1709 // If the copy has a glue operand, we conservatively assume it isn't safe to
1710 // perform a tail call.
1711 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1712 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001713 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001714 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001715 return false;
1716
Evan Cheng1bf891a2010-12-01 22:59:46 +00001717 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001718 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001719 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001720 if (UI->getOpcode() != X86ISD::RET_FLAG)
1721 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001722 HasRet = true;
1723 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001724
Evan Chengbf010eb2012-04-10 01:51:00 +00001725 if (!HasRet)
1726 return false;
1727
1728 Chain = TCChain;
1729 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001730}
1731
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001732MVT
1733X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001734 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001735 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001736 // TODO: Is this also valid on 32-bit?
1737 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001738 ReturnMVT = MVT::i8;
1739 else
1740 ReturnMVT = MVT::i32;
1741
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001742 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001743 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001744}
1745
Dan Gohman98ca4f22009-08-05 01:29:28 +00001746/// LowerCallResult - Lower the result values of a call into the
1747/// appropriate copies out of appropriate physical registers.
1748///
1749SDValue
1750X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001751 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752 const SmallVectorImpl<ISD::InputArg> &Ins,
1753 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001754 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001755
Chris Lattnere32bbf62007-02-28 07:09:55 +00001756 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001757 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001758 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001759 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001760 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001762
Chris Lattner3085e152007-02-25 08:59:22 +00001763 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001764 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001765 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001766 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001767
Torok Edwin3f142c32009-02-01 18:15:56 +00001768 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001770 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001771 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001772 }
1773
Evan Cheng79fb3b42009-02-20 20:43:02 +00001774 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001775
1776 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001777 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001778 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001779 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001780 // instead.
1781 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1782 // If we prefer to use the value in xmm registers, copy it out as f80 and
1783 // use a truncate to move it from fp stack reg to xmm reg.
1784 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001785 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001786 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1787 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001788 Val = Chain.getValue(0);
1789
1790 // Round the f80 to the right size, which also moves it to the appropriate
1791 // xmm register.
1792 if (CopyVT != VA.getValVT())
1793 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1794 // This truncation won't change the value.
1795 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001796 } else {
1797 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1798 CopyVT, InFlag).getValue(1);
1799 Val = Chain.getValue(0);
1800 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001801 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001803 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001804
Dan Gohman98ca4f22009-08-05 01:29:28 +00001805 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001806}
1807
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001808//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001809// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001810//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001811// StdCall calling convention seems to be standard for many Windows' API
1812// routines and around. It differs from C calling convention just a little:
1813// callee should clean up the stack, not caller. Symbols should be also
1814// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001815// For info on fast calling convention see Fast Calling Convention (tail call)
1816// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001817
Dan Gohman98ca4f22009-08-05 01:29:28 +00001818/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001819/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001820enum StructReturnType {
1821 NotStructReturn,
1822 RegStructReturn,
1823 StackStructReturn
1824};
1825static StructReturnType
1826callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001827 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001828 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001829
Rafael Espindola1cee7102012-07-25 13:41:10 +00001830 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1831 if (!Flags.isSRet())
1832 return NotStructReturn;
1833 if (Flags.isInReg())
1834 return RegStructReturn;
1835 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001836}
1837
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001838/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001839/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001840static StructReturnType
1841argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001842 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001843 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001844
Rafael Espindola1cee7102012-07-25 13:41:10 +00001845 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1846 if (!Flags.isSRet())
1847 return NotStructReturn;
1848 if (Flags.isInReg())
1849 return RegStructReturn;
1850 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001851}
1852
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001853/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1854/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001855/// the specific parameter attribute. The copy will be passed as a byval
1856/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001857static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001858CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001859 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1860 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001861 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001862
Dale Johannesendd64c412009-02-04 00:33:20 +00001863 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001864 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001865 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001866}
1867
Chris Lattner29689432010-03-11 00:22:57 +00001868/// IsTailCallConvention - Return true if the calling convention is one that
1869/// supports tail call optimization.
1870static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001871 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1872 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001873}
1874
Evan Cheng485fafc2011-03-21 01:19:09 +00001875bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001876 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001877 return false;
1878
1879 CallSite CS(CI);
1880 CallingConv::ID CalleeCC = CS.getCallingConv();
1881 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1882 return false;
1883
1884 return true;
1885}
1886
Evan Cheng0c439eb2010-01-27 00:07:07 +00001887/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1888/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001889static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1890 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001891 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001892}
1893
Dan Gohman98ca4f22009-08-05 01:29:28 +00001894SDValue
1895X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001896 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001897 const SmallVectorImpl<ISD::InputArg> &Ins,
1898 DebugLoc dl, SelectionDAG &DAG,
1899 const CCValAssign &VA,
1900 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001901 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001902 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001904 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1905 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001906 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001907 EVT ValVT;
1908
1909 // If value is passed by pointer we have address passed instead of the value
1910 // itself.
1911 if (VA.getLocInfo() == CCValAssign::Indirect)
1912 ValVT = VA.getLocVT();
1913 else
1914 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001915
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001916 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001917 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001918 // In case of tail call optimization mark all arguments mutable. Since they
1919 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001920 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001921 unsigned Bytes = Flags.getByValSize();
1922 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1923 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001924 return DAG.getFrameIndex(FI, getPointerTy());
1925 } else {
1926 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001927 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001928 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1929 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001930 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001931 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001932 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001933}
1934
Dan Gohman475871a2008-07-27 21:46:04 +00001935SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001936X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001937 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938 bool isVarArg,
1939 const SmallVectorImpl<ISD::InputArg> &Ins,
1940 DebugLoc dl,
1941 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001942 SmallVectorImpl<SDValue> &InVals)
1943 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001944 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001945 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001946
Gordon Henriksen86737662008-01-05 16:56:59 +00001947 const Function* Fn = MF.getFunction();
1948 if (Fn->hasExternalLinkage() &&
1949 Subtarget->isTargetCygMing() &&
1950 Fn->getName() == "main")
1951 FuncInfo->setForceFramePointer(true);
1952
Evan Cheng1bc78042006-04-26 01:20:17 +00001953 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001954 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001955 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001956 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001957
Chris Lattner29689432010-03-11 00:22:57 +00001958 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001959 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001960
Chris Lattner638402b2007-02-28 07:00:42 +00001961 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001962 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001963 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001965
1966 // Allocate shadow area for Win64
1967 if (IsWin64) {
1968 CCInfo.AllocateStack(32, 8);
1969 }
1970
Duncan Sands45907662010-10-31 13:21:44 +00001971 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001972
Chris Lattnerf39f7712007-02-28 05:46:49 +00001973 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001974 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001975 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1976 CCValAssign &VA = ArgLocs[i];
1977 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1978 // places.
1979 assert(VA.getValNo() != LastVal &&
1980 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001981 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001982 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001983
Chris Lattnerf39f7712007-02-28 05:46:49 +00001984 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001985 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001986 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001988 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001989 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001990 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001992 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001994 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001995 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001996 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001997 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001998 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001999 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00002000 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002001 else
Torok Edwinc23197a2009-07-14 16:55:14 +00002002 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002003
Devang Patel68e6bee2011-02-21 23:21:26 +00002004 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002005 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002006
Chris Lattnerf39f7712007-02-28 05:46:49 +00002007 // If this is an 8 or 16-bit value, it is really passed promoted to 32
2008 // bits. Insert an assert[sz]ext to capture this, then truncate to the
2009 // right size.
2010 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002011 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002012 DAG.getValueType(VA.getValVT()));
2013 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002014 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002015 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002016 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002017 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002018
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002019 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002020 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002021 if (RegVT.isVector())
2022 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2023 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002024 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002025 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002026 } else {
2027 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002028 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002029 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002030
2031 // If value is passed via pointer - do a load.
2032 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002033 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002034 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002035
Dan Gohman98ca4f22009-08-05 01:29:28 +00002036 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002037 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002038
Eli Benderskya5597f02013-01-25 22:07:43 +00002039 // The x86-64 ABIs require that for returning structs by value we copy
2040 // the sret argument into %rax/%eax (depending on ABI) for the return.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00002041 // Win32 requires us to put the sret argument to %eax as well.
Eli Benderskya5597f02013-01-25 22:07:43 +00002042 // Save the argument into a virtual register so that we can access it
2043 // from the return points.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00002044 if (MF.getFunction()->hasStructRetAttr() &&
2045 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Dan Gohman61a92132008-04-21 23:59:07 +00002046 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2047 unsigned Reg = FuncInfo->getSRetReturnReg();
2048 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002049 MVT PtrTy = getPointerTy();
2050 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002051 FuncInfo->setSRetReturnReg(Reg);
2052 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002053 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002054 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002055 }
2056
Chris Lattnerf39f7712007-02-28 05:46:49 +00002057 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002058 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002059 if (FuncIsMadeTailCallSafe(CallConv,
2060 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002061 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002062
Evan Cheng1bc78042006-04-26 01:20:17 +00002063 // If the function takes variable number of arguments, make a frame index for
2064 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002065 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002066 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2067 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002068 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002069 }
2070 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002071 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2072
2073 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002074 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002075 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002076 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002077 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002078 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2079 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002080 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002081 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2082 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2083 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002084 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002085 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002086
2087 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002088 // The XMM registers which might contain var arg parameters are shadowed
2089 // in their paired GPR. So we only need to save the GPR to their home
2090 // slots.
2091 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002092 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002093 } else {
2094 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2095 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002096
Chad Rosier30450e82011-12-22 22:35:21 +00002097 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2098 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002099 }
2100 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2101 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002102
Bill Wendling831737d2012-12-30 10:32:01 +00002103 bool NoImplicitFloatOps = Fn->getAttributes().
2104 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002105 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002106 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002107 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2108 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002109 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002110 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002111 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002112 // Kernel mode asks for SSE to be disabled, so don't push them
2113 // on the stack.
2114 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002115
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002116 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002117 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002118 // Get to the caller-allocated home save location. Add 8 to account
2119 // for the return address.
2120 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002121 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002122 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002123 // Fixup to set vararg frame on shadow area (4 x i64).
2124 if (NumIntRegs < 4)
2125 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002126 } else {
2127 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002128 // registers, then we must store them to their spots on the stack so
2129 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002130 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2131 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2132 FuncInfo->setRegSaveFrameIndex(
2133 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002134 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002135 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002136
Gordon Henriksen86737662008-01-05 16:56:59 +00002137 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002138 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002139 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2140 getPointerTy());
2141 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002142 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002143 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2144 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002145 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002146 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002147 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002148 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002149 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002150 MachinePointerInfo::getFixedStack(
2151 FuncInfo->getRegSaveFrameIndex(), Offset),
2152 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002153 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002154 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002155 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002156
Dan Gohmanface41a2009-08-16 21:24:25 +00002157 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2158 // Now store the XMM (fp + vector) parameter registers.
2159 SmallVector<SDValue, 11> SaveXMMOps;
2160 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002161
Craig Topperc9099502012-04-20 06:31:50 +00002162 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002163 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2164 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002165
Dan Gohman1e93df62010-04-17 14:41:14 +00002166 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2167 FuncInfo->getRegSaveFrameIndex()));
2168 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2169 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002170
Dan Gohmanface41a2009-08-16 21:24:25 +00002171 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002172 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002173 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002174 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2175 SaveXMMOps.push_back(Val);
2176 }
2177 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2178 MVT::Other,
2179 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002180 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002181
2182 if (!MemOps.empty())
2183 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2184 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002185 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002186 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002187
Gordon Henriksen86737662008-01-05 16:56:59 +00002188 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002189 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2190 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002191 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002192 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002193 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002194 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002195 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002196 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002197 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002198 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002199
Gordon Henriksen86737662008-01-05 16:56:59 +00002200 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002201 // RegSaveFrameIndex is X86-64 only.
2202 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002203 if (CallConv == CallingConv::X86_FastCall ||
2204 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002205 // fastcc functions can't have varargs.
2206 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002207 }
Evan Cheng25caf632006-05-23 21:06:34 +00002208
Rafael Espindola76927d752011-08-30 19:39:58 +00002209 FuncInfo->setArgumentStackSize(StackSize);
2210
Dan Gohman98ca4f22009-08-05 01:29:28 +00002211 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002212}
2213
Dan Gohman475871a2008-07-27 21:46:04 +00002214SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002215X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2216 SDValue StackPtr, SDValue Arg,
2217 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002218 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002219 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002220 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002221 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002222 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002223 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002224 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002225
2226 return DAG.getStore(Chain, dl, Arg, PtrOff,
2227 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002228 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002229}
2230
Bill Wendling64e87322009-01-16 19:25:27 +00002231/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002232/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002233SDValue
2234X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002235 SDValue &OutRetAddr, SDValue Chain,
2236 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002237 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002238 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002239 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002240 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002241
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002242 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002243 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002244 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002245 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002246}
2247
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002248/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002249/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002250static SDValue
2251EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002252 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2253 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002254 // Store the return address to the appropriate stack slot.
2255 if (!FPDiff) return Chain;
2256 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002257 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002258 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002259 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002260 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002261 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002262 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002263 return Chain;
2264}
2265
Dan Gohman98ca4f22009-08-05 01:29:28 +00002266SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002267X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002268 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002269 SelectionDAG &DAG = CLI.DAG;
2270 DebugLoc &dl = CLI.DL;
2271 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2272 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2273 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2274 SDValue Chain = CLI.Chain;
2275 SDValue Callee = CLI.Callee;
2276 CallingConv::ID CallConv = CLI.CallConv;
2277 bool &isTailCall = CLI.IsTailCall;
2278 bool isVarArg = CLI.IsVarArg;
2279
Dan Gohman98ca4f22009-08-05 01:29:28 +00002280 MachineFunction &MF = DAG.getMachineFunction();
2281 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002282 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002283 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002284 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002285 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002286
Nick Lewycky22de16d2012-01-19 00:34:10 +00002287 if (MF.getTarget().Options.DisableTailCalls)
2288 isTailCall = false;
2289
Evan Cheng5f941932010-02-05 02:21:12 +00002290 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002291 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002292 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002293 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002294 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002295 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002296
2297 // Sibcalls are automatically detected tailcalls which do not require
2298 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002299 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002300 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002301
2302 if (isTailCall)
2303 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002304 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002305
Chris Lattner29689432010-03-11 00:22:57 +00002306 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002307 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002308
Chris Lattner638402b2007-02-28 07:00:42 +00002309 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002310 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002311 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002312 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002313
2314 // Allocate shadow area for Win64
2315 if (IsWin64) {
2316 CCInfo.AllocateStack(32, 8);
2317 }
2318
Duncan Sands45907662010-10-31 13:21:44 +00002319 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002320
Chris Lattner423c5f42007-02-28 05:31:48 +00002321 // Get a count of how many bytes are to be pushed on the stack.
2322 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002323 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002324 // This is a sibcall. The memory operands are available in caller's
2325 // own caller's stack.
2326 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002327 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2328 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002329 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002330
Gordon Henriksen86737662008-01-05 16:56:59 +00002331 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002332 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002333 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002334 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2335 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2336
Gordon Henriksen86737662008-01-05 16:56:59 +00002337 FPDiff = NumBytesCallerPushed - NumBytes;
2338
2339 // Set the delta of movement of the returnaddr stackslot.
2340 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002341 if (FPDiff < X86Info->getTCReturnAddrDelta())
2342 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002343 }
2344
Evan Chengf22f9b32010-02-06 03:28:46 +00002345 if (!IsSibcall)
2346 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002347
Dan Gohman475871a2008-07-27 21:46:04 +00002348 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002349 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002350 if (isTailCall && FPDiff)
2351 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2352 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002353
Dan Gohman475871a2008-07-27 21:46:04 +00002354 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2355 SmallVector<SDValue, 8> MemOpChains;
2356 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002357
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002358 // Walk the register/memloc assignments, inserting copies/loads. In the case
2359 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002360 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2361 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002362 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002363 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002364 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002365 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002366
Chris Lattner423c5f42007-02-28 05:31:48 +00002367 // Promote the value if needed.
2368 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002369 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002370 case CCValAssign::Full: break;
2371 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002372 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002373 break;
2374 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002375 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002376 break;
2377 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002378 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002379 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002380 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002381 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2382 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002383 } else
2384 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2385 break;
2386 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002387 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002388 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002389 case CCValAssign::Indirect: {
2390 // Store the argument.
2391 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002392 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002393 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002394 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002395 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002396 Arg = SpillSlot;
2397 break;
2398 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002399 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002400
Chris Lattner423c5f42007-02-28 05:31:48 +00002401 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002402 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2403 if (isVarArg && IsWin64) {
2404 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2405 // shadow reg if callee is a varargs function.
2406 unsigned ShadowReg = 0;
2407 switch (VA.getLocReg()) {
2408 case X86::XMM0: ShadowReg = X86::RCX; break;
2409 case X86::XMM1: ShadowReg = X86::RDX; break;
2410 case X86::XMM2: ShadowReg = X86::R8; break;
2411 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002412 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002413 if (ShadowReg)
2414 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002415 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002416 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002417 assert(VA.isMemLoc());
2418 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002419 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2420 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002421 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2422 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002423 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002424 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002425
Evan Cheng32fe1032006-05-25 00:59:30 +00002426 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002427 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002428 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002429
Chris Lattner88e1fd52009-07-09 04:24:46 +00002430 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002431 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2432 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002433 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002434 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2435 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002436 } else {
2437 // If we are tail calling and generating PIC/GOT style code load the
2438 // address of the callee into ECX. The value in ecx is used as target of
2439 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2440 // for tail calls on PIC/GOT architectures. Normally we would just put the
2441 // address of GOT into ebx and then call target@PLT. But for tail calls
2442 // ebx would be restored (since ebx is callee saved) before jumping to the
2443 // target@PLT.
2444
2445 // Note: The actual moving to ECX is done further down.
2446 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2447 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2448 !G->getGlobal()->hasProtectedVisibility())
2449 Callee = LowerGlobalAddress(Callee, DAG);
2450 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002451 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002452 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002453 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002454
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002455 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002456 // From AMD64 ABI document:
2457 // For calls that may call functions that use varargs or stdargs
2458 // (prototype-less calls or calls to functions containing ellipsis (...) in
2459 // the declaration) %al is used as hidden argument to specify the number
2460 // of SSE registers used. The contents of %al do not need to match exactly
2461 // the number of registers, but must be an ubound on the number of SSE
2462 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002463
Gordon Henriksen86737662008-01-05 16:56:59 +00002464 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002465 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002466 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2467 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2468 };
2469 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002470 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002471 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002472
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002473 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2474 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002475 }
2476
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002477 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002478 if (isTailCall) {
2479 // Force all the incoming stack arguments to be loaded from the stack
2480 // before any new outgoing arguments are stored to the stack, because the
2481 // outgoing stack slots may alias the incoming argument stack slots, and
2482 // the alias isn't otherwise explicit. This is slightly more conservative
2483 // than necessary, because it means that each store effectively depends
2484 // on every argument instead of just those arguments it would clobber.
2485 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2486
Dan Gohman475871a2008-07-27 21:46:04 +00002487 SmallVector<SDValue, 8> MemOpChains2;
2488 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002489 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002490 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002491 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2492 CCValAssign &VA = ArgLocs[i];
2493 if (VA.isRegLoc())
2494 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002495 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002496 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002497 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002498 // Create frame index.
2499 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002500 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002501 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002502 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002503
Duncan Sands276dcbd2008-03-21 09:14:45 +00002504 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002505 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002506 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002507 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002508 StackPtr = DAG.getCopyFromReg(Chain, dl,
2509 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002510 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002511 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002512
Dan Gohman98ca4f22009-08-05 01:29:28 +00002513 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2514 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002515 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002516 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002517 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002518 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002519 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002520 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002521 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002522 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002523 }
2524 }
2525
2526 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002527 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002528 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002529
2530 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002531 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2532 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002533 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002534 }
2535
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002536 // Build a sequence of copy-to-reg nodes chained together with token chain
2537 // and flag operands which copy the outgoing args into registers.
2538 SDValue InFlag;
2539 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2540 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2541 RegsToPass[i].second, InFlag);
2542 InFlag = Chain.getValue(1);
2543 }
2544
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002545 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2546 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2547 // In the 64-bit large code model, we have to make all calls
2548 // through a register, since the call instruction's 32-bit
2549 // pc-relative offset may not be large enough to hold the whole
2550 // address.
2551 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002552 // If the callee is a GlobalAddress node (quite common, every direct call
2553 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2554 // it.
2555
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002556 // We should use extra load for direct calls to dllimported functions in
2557 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002558 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002559 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002560 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002561 bool ExtraLoad = false;
2562 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002563
Chris Lattner48a7d022009-07-09 05:02:21 +00002564 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2565 // external symbols most go through the PLT in PIC mode. If the symbol
2566 // has hidden or protected visibility, or if it is static or local, then
2567 // we don't need to use the PLT - we can directly call it.
2568 if (Subtarget->isTargetELF() &&
2569 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002570 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002571 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002572 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002573 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002574 (!Subtarget->getTargetTriple().isMacOSX() ||
2575 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002576 // PC-relative references to external symbols should go through $stub,
2577 // unless we're building with the leopard linker or later, which
2578 // automatically synthesizes these stubs.
2579 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002580 } else if (Subtarget->isPICStyleRIPRel() &&
2581 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002582 cast<Function>(GV)->getAttributes().
2583 hasAttribute(AttributeSet::FunctionIndex,
2584 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002585 // If the function is marked as non-lazy, generate an indirect call
2586 // which loads from the GOT directly. This avoids runtime overhead
2587 // at the cost of eager binding (and one extra byte of encoding).
2588 OpFlags = X86II::MO_GOTPCREL;
2589 WrapperKind = X86ISD::WrapperRIP;
2590 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002591 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002592
Devang Patel0d881da2010-07-06 22:08:15 +00002593 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002594 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002595
2596 // Add a wrapper if needed.
2597 if (WrapperKind != ISD::DELETED_NODE)
2598 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2599 // Add extra indirection if needed.
2600 if (ExtraLoad)
2601 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2602 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002603 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002604 }
Bill Wendling056292f2008-09-16 21:48:12 +00002605 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002606 unsigned char OpFlags = 0;
2607
Evan Cheng1bf891a2010-12-01 22:59:46 +00002608 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2609 // external symbols should go through the PLT.
2610 if (Subtarget->isTargetELF() &&
2611 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2612 OpFlags = X86II::MO_PLT;
2613 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002614 (!Subtarget->getTargetTriple().isMacOSX() ||
2615 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002616 // PC-relative references to external symbols should go through $stub,
2617 // unless we're building with the leopard linker or later, which
2618 // automatically synthesizes these stubs.
2619 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002620 }
Eric Christopherfd179292009-08-27 18:07:15 +00002621
Chris Lattner48a7d022009-07-09 05:02:21 +00002622 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2623 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002624 }
2625
Chris Lattnerd96d0722007-02-25 06:40:16 +00002626 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002627 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002628 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002629
Evan Chengf22f9b32010-02-06 03:28:46 +00002630 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002631 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2632 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002633 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002634 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002635
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002636 Ops.push_back(Chain);
2637 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002638
Dan Gohman98ca4f22009-08-05 01:29:28 +00002639 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002640 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002641
Gordon Henriksen86737662008-01-05 16:56:59 +00002642 // Add argument registers to the end of the list so that they are known live
2643 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002644 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2645 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2646 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002647
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002648 // Add a register mask operand representing the call-preserved registers.
2649 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2650 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2651 assert(Mask && "Missing call preserved mask for calling convention");
2652 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002653
Gabor Greifba36cb52008-08-28 21:40:38 +00002654 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002655 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002656
Dan Gohman98ca4f22009-08-05 01:29:28 +00002657 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002658 // We used to do:
2659 //// If this is the first return lowered for this function, add the regs
2660 //// to the liveout set for the function.
2661 // This isn't right, although it's probably harmless on x86; liveouts
2662 // should be computed from returns not tail calls. Consider a void
2663 // function making a tail call to a function returning int.
Jakub Staszak30fcfc32013-02-16 13:34:26 +00002664 return DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002665 }
2666
Dale Johannesenace16102009-02-03 19:33:06 +00002667 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002668 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002669
Chris Lattner2d297092006-05-23 18:50:38 +00002670 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002671 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002672 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2673 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002674 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002675 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002676 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002677 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002678 // pops the hidden struct pointer, so we have to push it back.
2679 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002680 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002681 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002682 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002683 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002684
Gordon Henriksenae636f82008-01-03 16:47:34 +00002685 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002686 if (!IsSibcall) {
2687 Chain = DAG.getCALLSEQ_END(Chain,
2688 DAG.getIntPtrConstant(NumBytes, true),
2689 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2690 true),
2691 InFlag);
2692 InFlag = Chain.getValue(1);
2693 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002694
Chris Lattner3085e152007-02-25 08:59:22 +00002695 // Handle result values, copying them out of physregs into vregs that we
2696 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002697 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2698 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002699}
2700
Evan Cheng25ab6902006-09-08 06:48:29 +00002701//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002702// Fast Calling Convention (tail call) implementation
2703//===----------------------------------------------------------------------===//
2704
2705// Like std call, callee cleans arguments, convention except that ECX is
2706// reserved for storing the tail called function address. Only 2 registers are
2707// free for argument passing (inreg). Tail call optimization is performed
2708// provided:
2709// * tailcallopt is enabled
2710// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002711// On X86_64 architecture with GOT-style position independent code only local
2712// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002713// To keep the stack aligned according to platform abi the function
2714// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2715// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002716// If a tail called function callee has more arguments than the caller the
2717// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002718// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002719// original REtADDR, but before the saved framepointer or the spilled registers
2720// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2721// stack layout:
2722// arg1
2723// arg2
2724// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002725// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002726// move area ]
2727// (possible EBP)
2728// ESI
2729// EDI
2730// local1 ..
2731
2732/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2733/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002734unsigned
2735X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2736 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002737 MachineFunction &MF = DAG.getMachineFunction();
2738 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002739 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002740 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002741 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002742 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002743 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002744 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2745 // Number smaller than 12 so just add the difference.
2746 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2747 } else {
2748 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002749 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002750 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002751 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002752 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002753}
2754
Evan Cheng5f941932010-02-05 02:21:12 +00002755/// MatchingStackOffset - Return true if the given stack call argument is
2756/// already available in the same position (relatively) of the caller's
2757/// incoming argument stack.
2758static
2759bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2760 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2761 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002762 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2763 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002764 if (Arg.getOpcode() == ISD::CopyFromReg) {
2765 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002766 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002767 return false;
2768 MachineInstr *Def = MRI->getVRegDef(VR);
2769 if (!Def)
2770 return false;
2771 if (!Flags.isByVal()) {
2772 if (!TII->isLoadFromStackSlot(Def, FI))
2773 return false;
2774 } else {
2775 unsigned Opcode = Def->getOpcode();
2776 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2777 Def->getOperand(1).isFI()) {
2778 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002779 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002780 } else
2781 return false;
2782 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002783 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2784 if (Flags.isByVal())
2785 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002786 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002787 // define @foo(%struct.X* %A) {
2788 // tail call @bar(%struct.X* byval %A)
2789 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002790 return false;
2791 SDValue Ptr = Ld->getBasePtr();
2792 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2793 if (!FINode)
2794 return false;
2795 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002796 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002797 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002798 FI = FINode->getIndex();
2799 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002800 } else
2801 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002802
Evan Cheng4cae1332010-03-05 08:38:04 +00002803 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002804 if (!MFI->isFixedObjectIndex(FI))
2805 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002806 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002807}
2808
Dan Gohman98ca4f22009-08-05 01:29:28 +00002809/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2810/// for tail call optimization. Targets which want to do tail call
2811/// optimization should implement this function.
2812bool
2813X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002814 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002815 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002816 bool isCalleeStructRet,
2817 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002818 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002819 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002820 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002821 const SmallVectorImpl<ISD::InputArg> &Ins,
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002822 SelectionDAG &DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002823 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002824 CalleeCC != CallingConv::C)
2825 return false;
2826
Evan Cheng7096ae42010-01-29 06:45:59 +00002827 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002828 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002829 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002830
2831 // If the function return type is x86_fp80 and the callee return type is not,
2832 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2833 // perform a tailcall optimization here.
2834 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2835 return false;
2836
Evan Cheng13617962010-04-30 01:12:32 +00002837 CallingConv::ID CallerCC = CallerF->getCallingConv();
2838 bool CCMatch = CallerCC == CalleeCC;
2839
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002840 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002841 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002842 return true;
2843 return false;
2844 }
2845
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002846 // Look for obvious safe cases to perform tail call optimization that do not
2847 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002848
Evan Cheng2c12cb42010-03-26 16:26:03 +00002849 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2850 // emit a special epilogue.
2851 if (RegInfo->needsStackRealignment(MF))
2852 return false;
2853
Evan Chenga375d472010-03-15 18:54:48 +00002854 // Also avoid sibcall optimization if either caller or callee uses struct
2855 // return semantics.
2856 if (isCalleeStructRet || isCallerStructRet)
2857 return false;
2858
Chad Rosier2416da32011-06-24 21:15:36 +00002859 // An stdcall caller is expected to clean up its arguments; the callee
2860 // isn't going to do that.
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002861 if (!CCMatch && CallerCC == CallingConv::X86_StdCall)
Chad Rosier2416da32011-06-24 21:15:36 +00002862 return false;
2863
Chad Rosier871f6642011-05-18 19:59:50 +00002864 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002865 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002866 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002867
2868 // Optimizing for varargs on Win64 is unlikely to be safe without
2869 // additional testing.
2870 if (Subtarget->isTargetWin64())
2871 return false;
2872
Chad Rosier871f6642011-05-18 19:59:50 +00002873 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002874 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002875 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002876
Chad Rosier871f6642011-05-18 19:59:50 +00002877 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2878 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2879 if (!ArgLocs[i].isRegLoc())
2880 return false;
2881 }
2882
Chad Rosier30450e82011-12-22 22:35:21 +00002883 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2884 // stack. Therefore, if it's not used by the call it is not safe to optimize
2885 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002886 bool Unused = false;
2887 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2888 if (!Ins[i].Used) {
2889 Unused = true;
2890 break;
2891 }
2892 }
2893 if (Unused) {
2894 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002895 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002896 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002897 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002898 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002899 CCValAssign &VA = RVLocs[i];
2900 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2901 return false;
2902 }
2903 }
2904
Evan Cheng13617962010-04-30 01:12:32 +00002905 // If the calling conventions do not match, then we'd better make sure the
2906 // results are returned in the same way as what the caller expects.
2907 if (!CCMatch) {
2908 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002909 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002910 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002911 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2912
2913 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002914 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002915 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002916 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2917
2918 if (RVLocs1.size() != RVLocs2.size())
2919 return false;
2920 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2921 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2922 return false;
2923 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2924 return false;
2925 if (RVLocs1[i].isRegLoc()) {
2926 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2927 return false;
2928 } else {
2929 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2930 return false;
2931 }
2932 }
2933 }
2934
Evan Chenga6bff982010-01-30 01:22:00 +00002935 // If the callee takes no arguments then go on to check the results of the
2936 // call.
2937 if (!Outs.empty()) {
2938 // Check if stack adjustment is needed. For now, do not do this if any
2939 // argument is passed on the stack.
2940 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002941 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002942 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002943
2944 // Allocate shadow area for Win64
2945 if (Subtarget->isTargetWin64()) {
2946 CCInfo.AllocateStack(32, 8);
2947 }
2948
Duncan Sands45907662010-10-31 13:21:44 +00002949 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002950 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002951 MachineFunction &MF = DAG.getMachineFunction();
2952 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2953 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002954
2955 // Check if the arguments are already laid out in the right way as
2956 // the caller's fixed stack objects.
2957 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002958 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2959 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002960 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002961 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2962 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002963 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002964 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002965 if (VA.getLocInfo() == CCValAssign::Indirect)
2966 return false;
2967 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002968 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2969 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002970 return false;
2971 }
2972 }
2973 }
Evan Cheng9c044672010-05-29 01:35:22 +00002974
2975 // If the tailcall address may be in a register, then make sure it's
2976 // possible to register allocate for it. In 32-bit, the call address can
2977 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002978 // callee-saved registers are restored. These happen to be the same
2979 // registers used to pass 'inreg' arguments so watch out for those.
2980 if (!Subtarget->is64Bit() &&
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002981 ((!isa<GlobalAddressSDNode>(Callee) &&
2982 !isa<ExternalSymbolSDNode>(Callee)) ||
2983 getTargetMachine().getRelocationModel() == Reloc::PIC_)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002984 unsigned NumInRegs = 0;
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002985 // In PIC we need an extra register to formulate the address computation
2986 // for the callee.
2987 unsigned MaxInRegs =
2988 (getTargetMachine().getRelocationModel() == Reloc::PIC_) ? 2 : 3;
2989
Evan Cheng9c044672010-05-29 01:35:22 +00002990 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2991 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002992 if (!VA.isRegLoc())
2993 continue;
2994 unsigned Reg = VA.getLocReg();
2995 switch (Reg) {
2996 default: break;
2997 case X86::EAX: case X86::EDX: case X86::ECX:
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002998 if (++NumInRegs == MaxInRegs)
Evan Cheng9c044672010-05-29 01:35:22 +00002999 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00003000 break;
Evan Cheng9c044672010-05-29 01:35:22 +00003001 }
3002 }
3003 }
Evan Chenga6bff982010-01-30 01:22:00 +00003004 }
Evan Chengb1712452010-01-27 06:25:16 +00003005
Evan Cheng86809cc2010-02-03 03:28:02 +00003006 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003007}
3008
Dan Gohman3df24e62008-09-03 23:12:08 +00003009FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00003010X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3011 const TargetLibraryInfo *libInfo) const {
3012 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00003013}
3014
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003015//===----------------------------------------------------------------------===//
3016// Other Lowering Hooks
3017//===----------------------------------------------------------------------===//
3018
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00003019static bool MayFoldLoad(SDValue Op) {
3020 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3021}
3022
3023static bool MayFoldIntoStore(SDValue Op) {
3024 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3025}
3026
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003027static bool isTargetShuffle(unsigned Opcode) {
3028 switch(Opcode) {
3029 default: return false;
3030 case X86ISD::PSHUFD:
3031 case X86ISD::PSHUFHW:
3032 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003033 case X86ISD::SHUFP:
Craig Topper4aee1bb2013-01-28 06:48:25 +00003034 case X86ISD::PALIGNR:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003035 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003036 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003037 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003038 case X86ISD::MOVLPS:
3039 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003040 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003041 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003042 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003043 case X86ISD::MOVSS:
3044 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003045 case X86ISD::UNPCKL:
3046 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003047 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003048 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003049 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003050 return true;
3051 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003052}
3053
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003054static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003055 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003056 switch(Opc) {
3057 default: llvm_unreachable("Unknown x86 shuffle node");
3058 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003059 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003060 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003061 return DAG.getNode(Opc, dl, VT, V1);
3062 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003063}
3064
3065static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003066 SDValue V1, unsigned TargetMask,
3067 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003068 switch(Opc) {
3069 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003070 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003071 case X86ISD::PSHUFHW:
3072 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003073 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003074 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003075 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3076 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003077}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003078
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003079static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003080 SDValue V1, SDValue V2, unsigned TargetMask,
3081 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003082 switch(Opc) {
3083 default: llvm_unreachable("Unknown x86 shuffle node");
Craig Topper4aee1bb2013-01-28 06:48:25 +00003084 case X86ISD::PALIGNR:
Craig Topperb3982da2011-12-31 23:50:21 +00003085 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003086 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003087 return DAG.getNode(Opc, dl, VT, V1, V2,
3088 DAG.getConstant(TargetMask, MVT::i8));
3089 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003090}
3091
3092static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3093 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3094 switch(Opc) {
3095 default: llvm_unreachable("Unknown x86 shuffle node");
3096 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003097 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003098 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003099 case X86ISD::MOVLPS:
3100 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003101 case X86ISD::MOVSS:
3102 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003103 case X86ISD::UNPCKL:
3104 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003105 return DAG.getNode(Opc, dl, VT, V1, V2);
3106 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003107}
3108
Dan Gohmand858e902010-04-17 15:26:15 +00003109SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003110 MachineFunction &MF = DAG.getMachineFunction();
3111 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3112 int ReturnAddrIndex = FuncInfo->getRAIndex();
3113
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003114 if (ReturnAddrIndex == 0) {
3115 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003116 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003117 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003118 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003119 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003120 }
3121
Evan Cheng25ab6902006-09-08 06:48:29 +00003122 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003123}
3124
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003125bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3126 bool hasSymbolicDisplacement) {
3127 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003128 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003129 return false;
3130
3131 // If we don't have a symbolic displacement - we don't have any extra
3132 // restrictions.
3133 if (!hasSymbolicDisplacement)
3134 return true;
3135
3136 // FIXME: Some tweaks might be needed for medium code model.
3137 if (M != CodeModel::Small && M != CodeModel::Kernel)
3138 return false;
3139
3140 // For small code model we assume that latest object is 16MB before end of 31
3141 // bits boundary. We may also accept pretty large negative constants knowing
3142 // that all objects are in the positive half of address space.
3143 if (M == CodeModel::Small && Offset < 16*1024*1024)
3144 return true;
3145
3146 // For kernel code model we know that all object resist in the negative half
3147 // of 32bits address space. We may not accept negative offsets, since they may
3148 // be just off and we may accept pretty large positive ones.
3149 if (M == CodeModel::Kernel && Offset > 0)
3150 return true;
3151
3152 return false;
3153}
3154
Evan Chengef41ff62011-06-23 17:54:54 +00003155/// isCalleePop - Determines whether the callee is required to pop its
3156/// own arguments. Callee pop is necessary to support tail calls.
3157bool X86::isCalleePop(CallingConv::ID CallingConv,
3158 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3159 if (IsVarArg)
3160 return false;
3161
3162 switch (CallingConv) {
3163 default:
3164 return false;
3165 case CallingConv::X86_StdCall:
3166 return !is64Bit;
3167 case CallingConv::X86_FastCall:
3168 return !is64Bit;
3169 case CallingConv::X86_ThisCall:
3170 return !is64Bit;
3171 case CallingConv::Fast:
3172 return TailCallOpt;
3173 case CallingConv::GHC:
3174 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003175 case CallingConv::HiPE:
3176 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003177 }
3178}
3179
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003180/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3181/// specific condition code, returning the condition code and the LHS/RHS of the
3182/// comparison to make.
3183static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3184 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003185 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003186 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3187 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3188 // X > -1 -> X == 0, jump !sign.
3189 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003190 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003191 }
3192 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003193 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003194 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003195 }
3196 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003197 // X < 1 -> X <= 0
3198 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003199 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003200 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003201 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003202
Evan Chengd9558e02006-01-06 00:43:03 +00003203 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003204 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003205 case ISD::SETEQ: return X86::COND_E;
3206 case ISD::SETGT: return X86::COND_G;
3207 case ISD::SETGE: return X86::COND_GE;
3208 case ISD::SETLT: return X86::COND_L;
3209 case ISD::SETLE: return X86::COND_LE;
3210 case ISD::SETNE: return X86::COND_NE;
3211 case ISD::SETULT: return X86::COND_B;
3212 case ISD::SETUGT: return X86::COND_A;
3213 case ISD::SETULE: return X86::COND_BE;
3214 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003215 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003216 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003217
Chris Lattner4c78e022008-12-23 23:42:27 +00003218 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003219
Chris Lattner4c78e022008-12-23 23:42:27 +00003220 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003221 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3222 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003223 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3224 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003225 }
3226
Chris Lattner4c78e022008-12-23 23:42:27 +00003227 switch (SetCCOpcode) {
3228 default: break;
3229 case ISD::SETOLT:
3230 case ISD::SETOLE:
3231 case ISD::SETUGT:
3232 case ISD::SETUGE:
3233 std::swap(LHS, RHS);
3234 break;
3235 }
3236
3237 // On a floating point condition, the flags are set as follows:
3238 // ZF PF CF op
3239 // 0 | 0 | 0 | X > Y
3240 // 0 | 0 | 1 | X < Y
3241 // 1 | 0 | 0 | X == Y
3242 // 1 | 1 | 1 | unordered
3243 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003244 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003245 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003246 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003247 case ISD::SETOLT: // flipped
3248 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003249 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003250 case ISD::SETOLE: // flipped
3251 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003252 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003253 case ISD::SETUGT: // flipped
3254 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003255 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003256 case ISD::SETUGE: // flipped
3257 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003258 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003259 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003260 case ISD::SETNE: return X86::COND_NE;
3261 case ISD::SETUO: return X86::COND_P;
3262 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003263 case ISD::SETOEQ:
3264 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003265 }
Evan Chengd9558e02006-01-06 00:43:03 +00003266}
3267
Evan Cheng4a460802006-01-11 00:33:36 +00003268/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3269/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003270/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003271static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003272 switch (X86CC) {
3273 default:
3274 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003275 case X86::COND_B:
3276 case X86::COND_BE:
3277 case X86::COND_E:
3278 case X86::COND_P:
3279 case X86::COND_A:
3280 case X86::COND_AE:
3281 case X86::COND_NE:
3282 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003283 return true;
3284 }
3285}
3286
Evan Chengeb2f9692009-10-27 19:56:55 +00003287/// isFPImmLegal - Returns true if the target can instruction select the
3288/// specified FP immediate natively. If false, the legalizer will
3289/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003290bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003291 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3292 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3293 return true;
3294 }
3295 return false;
3296}
3297
Nate Begeman9008ca62009-04-27 18:41:29 +00003298/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3299/// the specified range (L, H].
3300static bool isUndefOrInRange(int Val, int Low, int Hi) {
3301 return (Val < 0) || (Val >= Low && Val < Hi);
3302}
3303
3304/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3305/// specified value.
3306static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003307 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003308}
3309
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003310/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003311/// from position Pos and ending in Pos+Size, falls within the specified
3312/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003313static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003314 unsigned Pos, unsigned Size, int Low) {
3315 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003316 if (!isUndefOrEqual(Mask[i], Low))
3317 return false;
3318 return true;
3319}
3320
Nate Begeman9008ca62009-04-27 18:41:29 +00003321/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3322/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3323/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003324static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003325 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003326 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003327 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003328 return (Mask[0] < 2 && Mask[1] < 2);
3329 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003330}
3331
Nate Begeman9008ca62009-04-27 18:41:29 +00003332/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3333/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003334static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3335 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003336 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003337
Nate Begeman9008ca62009-04-27 18:41:29 +00003338 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003339 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3340 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003341
Evan Cheng506d3df2006-03-29 23:07:14 +00003342 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003343 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003344 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003345 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003346
Craig Toppera9a568a2012-05-02 08:03:44 +00003347 if (VT == MVT::v16i16) {
3348 // Lower quadword copied in order or undef.
3349 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3350 return false;
3351
3352 // Upper quadword shuffled.
3353 for (unsigned i = 12; i != 16; ++i)
3354 if (!isUndefOrInRange(Mask[i], 12, 16))
3355 return false;
3356 }
3357
Evan Cheng506d3df2006-03-29 23:07:14 +00003358 return true;
3359}
3360
Nate Begeman9008ca62009-04-27 18:41:29 +00003361/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3362/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003363static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3364 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003365 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003366
Rafael Espindola15684b22009-04-24 12:40:33 +00003367 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003368 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3369 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003370
Rafael Espindola15684b22009-04-24 12:40:33 +00003371 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003372 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003373 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003374 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003375
Craig Toppera9a568a2012-05-02 08:03:44 +00003376 if (VT == MVT::v16i16) {
3377 // Upper quadword copied in order.
3378 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3379 return false;
3380
3381 // Lower quadword shuffled.
3382 for (unsigned i = 8; i != 12; ++i)
3383 if (!isUndefOrInRange(Mask[i], 8, 12))
3384 return false;
3385 }
3386
Rafael Espindola15684b22009-04-24 12:40:33 +00003387 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003388}
3389
Nate Begemana09008b2009-10-19 02:17:23 +00003390/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3391/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003392static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3393 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003394 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3395 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003396 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003397
Craig Topper0e2037b2012-01-20 05:53:00 +00003398 unsigned NumElts = VT.getVectorNumElements();
3399 unsigned NumLanes = VT.getSizeInBits()/128;
3400 unsigned NumLaneElts = NumElts/NumLanes;
3401
3402 // Do not handle 64-bit element shuffles with palignr.
3403 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003404 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003405
Craig Topper0e2037b2012-01-20 05:53:00 +00003406 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3407 unsigned i;
3408 for (i = 0; i != NumLaneElts; ++i) {
3409 if (Mask[i+l] >= 0)
3410 break;
3411 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003412
Craig Topper0e2037b2012-01-20 05:53:00 +00003413 // Lane is all undef, go to next lane
3414 if (i == NumLaneElts)
3415 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003416
Craig Topper0e2037b2012-01-20 05:53:00 +00003417 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003418
Craig Topper0e2037b2012-01-20 05:53:00 +00003419 // Make sure its in this lane in one of the sources
3420 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3421 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003422 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003423
3424 // If not lane 0, then we must match lane 0
3425 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3426 return false;
3427
3428 // Correct second source to be contiguous with first source
3429 if (Start >= (int)NumElts)
3430 Start -= NumElts - NumLaneElts;
3431
3432 // Make sure we're shifting in the right direction.
3433 if (Start <= (int)(i+l))
3434 return false;
3435
3436 Start -= i;
3437
3438 // Check the rest of the elements to see if they are consecutive.
3439 for (++i; i != NumLaneElts; ++i) {
3440 int Idx = Mask[i+l];
3441
3442 // Make sure its in this lane
3443 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3444 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3445 return false;
3446
3447 // If not lane 0, then we must match lane 0
3448 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3449 return false;
3450
3451 if (Idx >= (int)NumElts)
3452 Idx -= NumElts - NumLaneElts;
3453
3454 if (!isUndefOrEqual(Idx, Start+i))
3455 return false;
3456
3457 }
Nate Begemana09008b2009-10-19 02:17:23 +00003458 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003459
Nate Begemana09008b2009-10-19 02:17:23 +00003460 return true;
3461}
3462
Craig Topper1a7700a2012-01-19 08:19:12 +00003463/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3464/// the two vector operands have swapped position.
3465static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3466 unsigned NumElems) {
3467 for (unsigned i = 0; i != NumElems; ++i) {
3468 int idx = Mask[i];
3469 if (idx < 0)
3470 continue;
3471 else if (idx < (int)NumElems)
3472 Mask[i] = idx + NumElems;
3473 else
3474 Mask[i] = idx - NumElems;
3475 }
3476}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003477
Craig Topper1a7700a2012-01-19 08:19:12 +00003478/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3479/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3480/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3481/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003482static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003483 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003484 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003485 return false;
3486
Craig Topper1a7700a2012-01-19 08:19:12 +00003487 unsigned NumElems = VT.getVectorNumElements();
3488 unsigned NumLanes = VT.getSizeInBits()/128;
3489 unsigned NumLaneElems = NumElems/NumLanes;
3490
3491 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003492 return false;
3493
3494 // VSHUFPSY divides the resulting vector into 4 chunks.
3495 // The sources are also splitted into 4 chunks, and each destination
3496 // chunk must come from a different source chunk.
3497 //
3498 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3499 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3500 //
3501 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3502 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3503 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003504 // VSHUFPDY divides the resulting vector into 4 chunks.
3505 // The sources are also splitted into 4 chunks, and each destination
3506 // chunk must come from a different source chunk.
3507 //
3508 // SRC1 => X3 X2 X1 X0
3509 // SRC2 => Y3 Y2 Y1 Y0
3510 //
3511 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3512 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003513 unsigned HalfLaneElems = NumLaneElems/2;
3514 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3515 for (unsigned i = 0; i != NumLaneElems; ++i) {
3516 int Idx = Mask[i+l];
3517 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3518 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3519 return false;
3520 // For VSHUFPSY, the mask of the second half must be the same as the
3521 // first but with the appropriate offsets. This works in the same way as
3522 // VPERMILPS works with masks.
3523 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3524 continue;
3525 if (!isUndefOrEqual(Idx, Mask[i]+l))
3526 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003527 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003528 }
3529
3530 return true;
3531}
3532
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003533/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3534/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003535static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003536 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003537 return false;
3538
Craig Topper7a9a28b2012-08-12 02:23:29 +00003539 unsigned NumElems = VT.getVectorNumElements();
3540
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003541 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003542 return false;
3543
Evan Cheng2064a2b2006-03-28 06:50:32 +00003544 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003545 return isUndefOrEqual(Mask[0], 6) &&
3546 isUndefOrEqual(Mask[1], 7) &&
3547 isUndefOrEqual(Mask[2], 2) &&
3548 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003549}
3550
Nate Begeman0b10b912009-11-07 23:17:15 +00003551/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3552/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3553/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003554static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003555 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003556 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003557
Craig Topper7a9a28b2012-08-12 02:23:29 +00003558 unsigned NumElems = VT.getVectorNumElements();
3559
Nate Begeman0b10b912009-11-07 23:17:15 +00003560 if (NumElems != 4)
3561 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003562
Craig Topperdd637ae2012-02-19 05:41:45 +00003563 return isUndefOrEqual(Mask[0], 2) &&
3564 isUndefOrEqual(Mask[1], 3) &&
3565 isUndefOrEqual(Mask[2], 2) &&
3566 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003567}
3568
Evan Cheng5ced1d82006-04-06 23:23:56 +00003569/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3570/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003571static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003572 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003573 return false;
3574
Craig Topperdd637ae2012-02-19 05:41:45 +00003575 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003576
Evan Cheng5ced1d82006-04-06 23:23:56 +00003577 if (NumElems != 2 && NumElems != 4)
3578 return false;
3579
Chad Rosier238ae312012-04-30 17:47:15 +00003580 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003581 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003582 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003583
Chad Rosier238ae312012-04-30 17:47:15 +00003584 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003585 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003586 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003587
3588 return true;
3589}
3590
Nate Begeman0b10b912009-11-07 23:17:15 +00003591/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3592/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003593static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003594 if (!VT.is128BitVector())
3595 return false;
3596
Craig Topperdd637ae2012-02-19 05:41:45 +00003597 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003598
Craig Topper7a9a28b2012-08-12 02:23:29 +00003599 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003600 return false;
3601
Chad Rosier238ae312012-04-30 17:47:15 +00003602 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003603 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003604 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003605
Chad Rosier238ae312012-04-30 17:47:15 +00003606 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3607 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003608 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003609
3610 return true;
3611}
3612
Elena Demikhovsky15963732012-06-26 08:04:10 +00003613//
3614// Some special combinations that can be optimized.
3615//
3616static
3617SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3618 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003619 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky15963732012-06-26 08:04:10 +00003620 DebugLoc dl = SVOp->getDebugLoc();
3621
3622 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3623 return SDValue();
3624
3625 ArrayRef<int> Mask = SVOp->getMask();
3626
3627 // These are the special masks that may be optimized.
3628 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3629 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3630 bool MatchEvenMask = true;
3631 bool MatchOddMask = true;
3632 for (int i=0; i<8; ++i) {
3633 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3634 MatchEvenMask = false;
3635 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3636 MatchOddMask = false;
3637 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003638
Elena Demikhovsky32510202012-09-04 12:49:02 +00003639 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003640 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003641
Elena Demikhovsky15963732012-06-26 08:04:10 +00003642 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3643
Elena Demikhovsky32510202012-09-04 12:49:02 +00003644 SDValue Op0 = SVOp->getOperand(0);
3645 SDValue Op1 = SVOp->getOperand(1);
3646
3647 if (MatchEvenMask) {
3648 // Shift the second operand right to 32 bits.
3649 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3650 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3651 } else {
3652 // Shift the first operand left to 32 bits.
3653 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3654 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3655 }
3656 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3657 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003658}
3659
Evan Cheng0038e592006-03-28 00:39:58 +00003660/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3661/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003662static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003663 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003664 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003665
3666 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3667 "Unsupported vector type for unpckh");
3668
Craig Topper5a529e42013-01-18 06:44:29 +00003669 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003670 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003671 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003672
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003673 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3674 // independently on 128-bit lanes.
3675 unsigned NumLanes = VT.getSizeInBits()/128;
3676 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003677
Craig Topper94438ba2011-12-16 08:06:31 +00003678 for (unsigned l = 0; l != NumLanes; ++l) {
3679 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3680 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003681 i += 2, ++j) {
3682 int BitI = Mask[i];
3683 int BitI1 = Mask[i+1];
3684 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003685 return false;
David Greenea20244d2011-03-02 17:23:43 +00003686 if (V2IsSplat) {
3687 if (!isUndefOrEqual(BitI1, NumElts))
3688 return false;
3689 } else {
3690 if (!isUndefOrEqual(BitI1, j + NumElts))
3691 return false;
3692 }
Evan Cheng39623da2006-04-20 08:58:49 +00003693 }
Evan Cheng0038e592006-03-28 00:39:58 +00003694 }
David Greenea20244d2011-03-02 17:23:43 +00003695
Evan Cheng0038e592006-03-28 00:39:58 +00003696 return true;
3697}
3698
Evan Cheng4fcb9222006-03-28 02:43:26 +00003699/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3700/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003701static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003702 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003703 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003704
3705 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3706 "Unsupported vector type for unpckh");
3707
Craig Topper5a529e42013-01-18 06:44:29 +00003708 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003709 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003710 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003711
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003712 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3713 // independently on 128-bit lanes.
3714 unsigned NumLanes = VT.getSizeInBits()/128;
3715 unsigned NumLaneElts = NumElts/NumLanes;
3716
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003717 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003718 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3719 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003720 int BitI = Mask[i];
3721 int BitI1 = Mask[i+1];
3722 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003723 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003724 if (V2IsSplat) {
3725 if (isUndefOrEqual(BitI1, NumElts))
3726 return false;
3727 } else {
3728 if (!isUndefOrEqual(BitI1, j+NumElts))
3729 return false;
3730 }
Evan Cheng39623da2006-04-20 08:58:49 +00003731 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003732 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003733 return true;
3734}
3735
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003736/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3737/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3738/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003739static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003740 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003741 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003742
3743 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3744 "Unsupported vector type for unpckh");
3745
Craig Topper5a529e42013-01-18 06:44:29 +00003746 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003747 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003748 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003749
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003750 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3751 // FIXME: Need a better way to get rid of this, there's no latency difference
3752 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3753 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003754 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003755 return false;
3756
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003757 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3758 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003759 unsigned NumLanes = VT.getSizeInBits()/128;
3760 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003761
Craig Topper94438ba2011-12-16 08:06:31 +00003762 for (unsigned l = 0; l != NumLanes; ++l) {
3763 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3764 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003765 i += 2, ++j) {
3766 int BitI = Mask[i];
3767 int BitI1 = Mask[i+1];
3768
3769 if (!isUndefOrEqual(BitI, j))
3770 return false;
3771 if (!isUndefOrEqual(BitI1, j))
3772 return false;
3773 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003774 }
David Greenea20244d2011-03-02 17:23:43 +00003775
Rafael Espindola15684b22009-04-24 12:40:33 +00003776 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003777}
3778
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003779/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3780/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3781/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003782static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003783 unsigned NumElts = VT.getVectorNumElements();
3784
3785 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3786 "Unsupported vector type for unpckh");
3787
Craig Topper5a529e42013-01-18 06:44:29 +00003788 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003789 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003790 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003791
Craig Topper94438ba2011-12-16 08:06:31 +00003792 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3793 // independently on 128-bit lanes.
3794 unsigned NumLanes = VT.getSizeInBits()/128;
3795 unsigned NumLaneElts = NumElts/NumLanes;
3796
3797 for (unsigned l = 0; l != NumLanes; ++l) {
3798 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3799 i != (l+1)*NumLaneElts; i += 2, ++j) {
3800 int BitI = Mask[i];
3801 int BitI1 = Mask[i+1];
3802 if (!isUndefOrEqual(BitI, j))
3803 return false;
3804 if (!isUndefOrEqual(BitI1, j))
3805 return false;
3806 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003807 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003808 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003809}
3810
Evan Cheng017dcc62006-04-21 01:05:10 +00003811/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3812/// specifies a shuffle of elements that is suitable for input to MOVSS,
3813/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003814static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003815 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003816 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003817 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003818 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003819
Craig Topperc612d792012-01-02 09:17:37 +00003820 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003821
Nate Begeman9008ca62009-04-27 18:41:29 +00003822 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003823 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003824
Craig Topperc612d792012-01-02 09:17:37 +00003825 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003826 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003827 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003828
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003829 return true;
3830}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003831
Craig Topper70b883b2011-11-28 10:14:51 +00003832/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003833/// as permutations between 128-bit chunks or halves. As an example: this
3834/// shuffle bellow:
3835/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3836/// The first half comes from the second half of V1 and the second half from the
3837/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003838static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3839 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003840 return false;
3841
3842 // The shuffle result is divided into half A and half B. In total the two
3843 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3844 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003845 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003846 bool MatchA = false, MatchB = false;
3847
3848 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003849 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003850 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3851 MatchA = true;
3852 break;
3853 }
3854 }
3855
3856 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003857 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003858 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3859 MatchB = true;
3860 break;
3861 }
3862 }
3863
3864 return MatchA && MatchB;
3865}
3866
Craig Topper70b883b2011-11-28 10:14:51 +00003867/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3868/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003869static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003870 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003871
Craig Topperc612d792012-01-02 09:17:37 +00003872 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003873
Craig Topperc612d792012-01-02 09:17:37 +00003874 unsigned FstHalf = 0, SndHalf = 0;
3875 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003876 if (SVOp->getMaskElt(i) > 0) {
3877 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3878 break;
3879 }
3880 }
Craig Topperc612d792012-01-02 09:17:37 +00003881 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003882 if (SVOp->getMaskElt(i) > 0) {
3883 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3884 break;
3885 }
3886 }
3887
3888 return (FstHalf | (SndHalf << 4));
3889}
3890
Craig Topper70b883b2011-11-28 10:14:51 +00003891/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003892/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3893/// Note that VPERMIL mask matching is different depending whether theunderlying
3894/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3895/// to the same elements of the low, but to the higher half of the source.
3896/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003897/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003898static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3899 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003900 return false;
3901
Craig Topperc612d792012-01-02 09:17:37 +00003902 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003903 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003904 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003905 return false;
3906
Craig Topperc612d792012-01-02 09:17:37 +00003907 unsigned NumLanes = VT.getSizeInBits()/128;
3908 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003909 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003910 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003911 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003912 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003913 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003914 continue;
3915 // VPERMILPS handling
3916 if (Mask[i] < 0)
3917 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003918 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003919 return false;
3920 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003921 }
3922
3923 return true;
3924}
3925
Craig Topper5aaffa82012-02-19 02:53:47 +00003926/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003927/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003928/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003929static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003930 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003931 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003932 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003933
3934 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003935 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003936 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003937
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003939 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003940
Craig Topperc612d792012-01-02 09:17:37 +00003941 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003942 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3943 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3944 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003945 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003946
Evan Cheng39623da2006-04-20 08:58:49 +00003947 return true;
3948}
3949
Evan Chengd9539472006-04-14 21:59:03 +00003950/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3951/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003952/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003953static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003954 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003955 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003956 return false;
3957
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003958 unsigned NumElems = VT.getVectorNumElements();
3959
Craig Topper5a529e42013-01-18 06:44:29 +00003960 if ((VT.is128BitVector() && NumElems != 4) ||
3961 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003962 return false;
3963
3964 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003965 for (unsigned i = 0; i != NumElems; i += 2)
3966 if (!isUndefOrEqual(Mask[i], i+1) ||
3967 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003969
3970 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003971}
3972
3973/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3974/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003975/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003976static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003977 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003978 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003979 return false;
3980
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003981 unsigned NumElems = VT.getVectorNumElements();
3982
Craig Topper5a529e42013-01-18 06:44:29 +00003983 if ((VT.is128BitVector() && NumElems != 4) ||
3984 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003985 return false;
3986
3987 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003988 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003989 if (!isUndefOrEqual(Mask[i], i) ||
3990 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003991 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003992
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003993 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003994}
3995
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003996/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3997/// specifies a shuffle of elements that is suitable for input to 256-bit
3998/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003999static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
4000 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00004001 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004002
Craig Topper7a9a28b2012-08-12 02:23:29 +00004003 unsigned NumElts = VT.getVectorNumElements();
4004 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004005 return false;
4006
Craig Topperc612d792012-01-02 09:17:37 +00004007 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004008 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004009 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004010 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004011 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004012 return false;
4013 return true;
4014}
4015
Evan Cheng0b457f02008-09-25 20:50:48 +00004016/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004017/// specifies a shuffle of elements that is suitable for input to 128-bit
4018/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00004019static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004020 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004021 return false;
4022
Craig Topperc612d792012-01-02 09:17:37 +00004023 unsigned e = VT.getVectorNumElements() / 2;
4024 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004025 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004026 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004027 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004028 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004029 return false;
4030 return true;
4031}
4032
David Greenec38a03e2011-02-03 15:50:00 +00004033/// isVEXTRACTF128Index - Return true if the specified
4034/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4035/// suitable for input to VEXTRACTF128.
4036bool X86::isVEXTRACTF128Index(SDNode *N) {
4037 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4038 return false;
4039
4040 // The index should be aligned on a 128-bit boundary.
4041 uint64_t Index =
4042 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4043
Craig Topper5141d972013-01-18 08:41:28 +00004044 MVT VT = N->getValueType(0).getSimpleVT();
4045 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004046 bool Result = (Index * ElSize) % 128 == 0;
4047
4048 return Result;
4049}
4050
David Greeneccacdc12011-02-04 16:08:29 +00004051/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4052/// operand specifies a subvector insert that is suitable for input to
4053/// VINSERTF128.
4054bool X86::isVINSERTF128Index(SDNode *N) {
4055 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4056 return false;
4057
4058 // The index should be aligned on a 128-bit boundary.
4059 uint64_t Index =
4060 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4061
Craig Topper5141d972013-01-18 08:41:28 +00004062 MVT VT = N->getValueType(0).getSimpleVT();
4063 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004064 bool Result = (Index * ElSize) % 128 == 0;
4065
4066 return Result;
4067}
4068
Evan Cheng63d33002006-03-22 08:01:21 +00004069/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004070/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004071/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004072static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004073 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004074
Craig Topper1a7700a2012-01-19 08:19:12 +00004075 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4076 "Unsupported vector type for PSHUF/SHUFP");
4077
4078 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4079 // independently on 128-bit lanes.
4080 unsigned NumElts = VT.getVectorNumElements();
4081 unsigned NumLanes = VT.getSizeInBits()/128;
4082 unsigned NumLaneElts = NumElts/NumLanes;
4083
4084 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4085 "Only supports 2 or 4 elements per lane");
4086
4087 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004088 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004089 for (unsigned i = 0; i != NumElts; ++i) {
4090 int Elt = N->getMaskElt(i);
4091 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004092 Elt &= NumLaneElts - 1;
4093 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004094 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004095 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004096
Evan Cheng63d33002006-03-22 08:01:21 +00004097 return Mask;
4098}
4099
Evan Cheng506d3df2006-03-29 23:07:14 +00004100/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004101/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004102static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004103 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004104
4105 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4106 "Unsupported vector type for PSHUFHW");
4107
4108 unsigned NumElts = VT.getVectorNumElements();
4109
Evan Cheng506d3df2006-03-29 23:07:14 +00004110 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004111 for (unsigned l = 0; l != NumElts; l += 8) {
4112 // 8 nodes per lane, but we only care about the last 4.
4113 for (unsigned i = 0; i < 4; ++i) {
4114 int Elt = N->getMaskElt(l+i+4);
4115 if (Elt < 0) continue;
4116 Elt &= 0x3; // only 2-bits.
4117 Mask |= Elt << (i * 2);
4118 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004119 }
Craig Topper6b28d352012-05-03 07:12:59 +00004120
Evan Cheng506d3df2006-03-29 23:07:14 +00004121 return Mask;
4122}
4123
4124/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004125/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004126static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004127 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004128
4129 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4130 "Unsupported vector type for PSHUFHW");
4131
4132 unsigned NumElts = VT.getVectorNumElements();
4133
Evan Cheng506d3df2006-03-29 23:07:14 +00004134 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004135 for (unsigned l = 0; l != NumElts; l += 8) {
4136 // 8 nodes per lane, but we only care about the first 4.
4137 for (unsigned i = 0; i < 4; ++i) {
4138 int Elt = N->getMaskElt(l+i);
4139 if (Elt < 0) continue;
4140 Elt &= 0x3; // only 2-bits
4141 Mask |= Elt << (i * 2);
4142 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004143 }
Craig Topper6b28d352012-05-03 07:12:59 +00004144
Evan Cheng506d3df2006-03-29 23:07:14 +00004145 return Mask;
4146}
4147
Nate Begemana09008b2009-10-19 02:17:23 +00004148/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4149/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004150static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004151 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004152 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004153
Craig Topper0e2037b2012-01-20 05:53:00 +00004154 unsigned NumElts = VT.getVectorNumElements();
4155 unsigned NumLanes = VT.getSizeInBits()/128;
4156 unsigned NumLaneElts = NumElts/NumLanes;
4157
4158 int Val = 0;
4159 unsigned i;
4160 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004161 Val = SVOp->getMaskElt(i);
4162 if (Val >= 0)
4163 break;
4164 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004165 if (Val >= (int)NumElts)
4166 Val -= NumElts - NumLaneElts;
4167
Eli Friedman63f8dde2011-07-25 21:36:45 +00004168 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004169 return (Val - i) * EltSize;
4170}
4171
David Greenec38a03e2011-02-03 15:50:00 +00004172/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4173/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4174/// instructions.
4175unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4176 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4177 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4178
4179 uint64_t Index =
4180 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4181
Craig Toppercfcab212013-01-19 08:27:45 +00004182 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4183 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004184
4185 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004186 return Index / NumElemsPerChunk;
4187}
4188
David Greeneccacdc12011-02-04 16:08:29 +00004189/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4190/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4191/// instructions.
4192unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4193 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4194 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4195
4196 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004197 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004198
Craig Toppercfcab212013-01-19 08:27:45 +00004199 MVT VecVT = N->getValueType(0).getSimpleVT();
4200 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004201
4202 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004203 return Index / NumElemsPerChunk;
4204}
4205
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004206/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4207/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4208/// Handles 256-bit.
4209static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004210 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004211
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004212 unsigned NumElts = VT.getVectorNumElements();
4213
Craig Topper095c5282012-04-15 23:48:57 +00004214 assert((VT.is256BitVector() && NumElts == 4) &&
4215 "Unsupported vector type for VPERMQ/VPERMPD");
4216
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004217 unsigned Mask = 0;
4218 for (unsigned i = 0; i != NumElts; ++i) {
4219 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004220 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004221 continue;
4222 Mask |= Elt << (i*2);
4223 }
4224
4225 return Mask;
4226}
Evan Cheng37b73872009-07-30 08:33:02 +00004227/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4228/// constant +0.0.
4229bool X86::isZeroNode(SDValue Elt) {
Jakub Staszak30fcfc32013-02-16 13:34:26 +00004230 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Elt))
4231 return CN->isNullValue();
4232 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Elt))
4233 return CFP->getValueAPF().isPosZero();
4234 return false;
Evan Cheng37b73872009-07-30 08:33:02 +00004235}
4236
Nate Begeman9008ca62009-04-27 18:41:29 +00004237/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4238/// their permute mask.
4239static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4240 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004241 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004242 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004244
Nate Begeman5a5ca152009-04-29 05:20:52 +00004245 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004246 int Idx = SVOp->getMaskElt(i);
4247 if (Idx >= 0) {
4248 if (Idx < (int)NumElems)
4249 Idx += NumElems;
4250 else
4251 Idx -= NumElems;
4252 }
4253 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004254 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004255 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4256 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004257}
4258
Evan Cheng533a0aa2006-04-19 20:35:22 +00004259/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4260/// match movhlps. The lower half elements should come from upper half of
4261/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004262/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004263static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004264 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004265 return false;
4266 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004267 return false;
4268 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004269 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004270 return false;
4271 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004272 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004273 return false;
4274 return true;
4275}
4276
Evan Cheng5ced1d82006-04-06 23:23:56 +00004277/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004278/// is promoted to a vector. It also returns the LoadSDNode by reference if
4279/// required.
4280static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004281 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4282 return false;
4283 N = N->getOperand(0).getNode();
4284 if (!ISD::isNON_EXTLoad(N))
4285 return false;
4286 if (LD)
4287 *LD = cast<LoadSDNode>(N);
4288 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004289}
4290
Dan Gohman65fd6562011-11-03 21:49:52 +00004291// Test whether the given value is a vector value which will be legalized
4292// into a load.
4293static bool WillBeConstantPoolLoad(SDNode *N) {
4294 if (N->getOpcode() != ISD::BUILD_VECTOR)
4295 return false;
4296
4297 // Check for any non-constant elements.
4298 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4299 switch (N->getOperand(i).getNode()->getOpcode()) {
4300 case ISD::UNDEF:
4301 case ISD::ConstantFP:
4302 case ISD::Constant:
4303 break;
4304 default:
4305 return false;
4306 }
4307
4308 // Vectors of all-zeros and all-ones are materialized with special
4309 // instructions rather than being loaded.
4310 return !ISD::isBuildVectorAllZeros(N) &&
4311 !ISD::isBuildVectorAllOnes(N);
4312}
4313
Evan Cheng533a0aa2006-04-19 20:35:22 +00004314/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4315/// match movlp{s|d}. The lower half elements should come from lower half of
4316/// V1 (and in order), and the upper half elements should come from the upper
4317/// half of V2 (and in order). And since V1 will become the source of the
4318/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004319static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004320 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004321 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004322 return false;
4323
Evan Cheng466685d2006-10-09 20:57:25 +00004324 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004325 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004326 // Is V2 is a vector load, don't do this transformation. We will try to use
4327 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004328 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004329 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004330
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004331 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004332
Evan Cheng533a0aa2006-04-19 20:35:22 +00004333 if (NumElems != 2 && NumElems != 4)
4334 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004335 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004336 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004337 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004338 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004339 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004340 return false;
4341 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004342}
4343
Evan Cheng39623da2006-04-20 08:58:49 +00004344/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4345/// all the same.
4346static bool isSplatVector(SDNode *N) {
4347 if (N->getOpcode() != ISD::BUILD_VECTOR)
4348 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004349
Dan Gohman475871a2008-07-27 21:46:04 +00004350 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004351 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4352 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004353 return false;
4354 return true;
4355}
4356
Evan Cheng213d2cf2007-05-17 18:45:50 +00004357/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004358/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004359/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004360static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004361 SDValue V1 = N->getOperand(0);
4362 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004363 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4364 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004366 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004367 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004368 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4369 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004370 if (Opc != ISD::BUILD_VECTOR ||
4371 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004372 return false;
4373 } else if (Idx >= 0) {
4374 unsigned Opc = V1.getOpcode();
4375 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4376 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004377 if (Opc != ISD::BUILD_VECTOR ||
4378 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004379 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004380 }
4381 }
4382 return true;
4383}
4384
4385/// getZeroVector - Returns a vector of specified type with all zero elements.
4386///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004387static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004388 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004389 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004390
Dale Johannesen0488fb62010-09-30 23:57:10 +00004391 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004392 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004393 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004394 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004395 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004396 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4397 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4398 } else { // SSE1
4399 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4400 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4401 }
Craig Topper5a529e42013-01-18 06:44:29 +00004402 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004403 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004404 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4405 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4406 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4407 } else {
4408 // 256-bit logic and arithmetic instructions in AVX are all
4409 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4410 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4411 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4412 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4413 }
Craig Topper9d352402012-04-23 07:24:41 +00004414 } else
4415 llvm_unreachable("Unexpected vector type");
4416
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004417 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004418}
4419
Chris Lattner8a594482007-11-25 00:24:49 +00004420/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004421/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4422/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4423/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004424static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004425 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004426 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004427
Owen Anderson825b72b2009-08-11 20:47:22 +00004428 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004429 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004430 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004431 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004432 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4433 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4434 } else { // AVX
4435 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004436 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004437 }
Craig Topper5a529e42013-01-18 06:44:29 +00004438 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004439 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004440 } else
4441 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004442
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004443 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004444}
4445
Evan Cheng39623da2006-04-20 08:58:49 +00004446/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4447/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004448static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004449 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004450 if (Mask[i] > (int)NumElems) {
4451 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004452 }
Evan Cheng39623da2006-04-20 08:58:49 +00004453 }
Evan Cheng39623da2006-04-20 08:58:49 +00004454}
4455
Evan Cheng017dcc62006-04-21 01:05:10 +00004456/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4457/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004458static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004459 SDValue V2) {
4460 unsigned NumElems = VT.getVectorNumElements();
4461 SmallVector<int, 8> Mask;
4462 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004463 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004464 Mask.push_back(i);
4465 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004466}
4467
Nate Begeman9008ca62009-04-27 18:41:29 +00004468/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004469static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004470 SDValue V2) {
4471 unsigned NumElems = VT.getVectorNumElements();
4472 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004473 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004474 Mask.push_back(i);
4475 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004476 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004477 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004478}
4479
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004480/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004481static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 SDValue V2) {
4483 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004485 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004486 Mask.push_back(i + Half);
4487 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004488 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004489 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004490}
4491
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004492// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004493// a generic shuffle instruction because the target has no such instructions.
4494// Generate shuffles which repeat i16 and i8 several times until they can be
4495// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004496static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004497 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004498 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004499 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004500
Nate Begeman9008ca62009-04-27 18:41:29 +00004501 while (NumElems > 4) {
4502 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004503 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004504 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004505 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004506 EltNo -= NumElems/2;
4507 }
4508 NumElems >>= 1;
4509 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004510 return V;
4511}
Eric Christopherfd179292009-08-27 18:07:15 +00004512
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004513/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4514static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4515 EVT VT = V.getValueType();
4516 DebugLoc dl = V.getDebugLoc();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004517
Craig Topper5a529e42013-01-18 06:44:29 +00004518 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004519 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004520 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004521 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4522 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004523 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004524 // To use VPERMILPS to splat scalars, the second half of indicies must
4525 // refer to the higher part, which is a duplication of the lower one,
4526 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004527 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4528 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004529
4530 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4531 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4532 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004533 } else
4534 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004535
4536 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4537}
4538
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004539/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004540static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4541 EVT SrcVT = SV->getValueType(0);
4542 SDValue V1 = SV->getOperand(0);
4543 DebugLoc dl = SV->getDebugLoc();
4544
4545 int EltNo = SV->getSplatIndex();
4546 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004547 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004548
Craig Topper5a529e42013-01-18 06:44:29 +00004549 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4550 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004551
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004552 // Extract the 128-bit part containing the splat element and update
4553 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004554 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004555 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4556 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004557 EltNo -= NumElems/2;
4558 }
4559
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004560 // All i16 and i8 vector types can't be used directly by a generic shuffle
4561 // instruction because the target has no such instruction. Generate shuffles
4562 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004563 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004564 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004565 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004566 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004567
4568 // Recreate the 256-bit vector and place the same 128-bit vector
4569 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004570 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004571 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004572 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004573 }
4574
4575 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004576}
4577
Evan Chengba05f722006-04-21 23:03:30 +00004578/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004579/// vector of zero or undef vector. This produces a shuffle where the low
4580/// element of V2 is swizzled into the zero/undef vector, landing at element
4581/// 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 +00004582static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004583 bool IsZero,
4584 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004585 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004586 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004587 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004588 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004589 unsigned NumElems = VT.getVectorNumElements();
4590 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004591 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004592 // If this is the insertion idx, put the low elt of V2 here.
4593 MaskVec.push_back(i == Idx ? NumElems : i);
4594 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004595}
4596
Craig Toppera1ffc682012-03-20 06:42:26 +00004597/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4598/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004599/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004600static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004601 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004602 unsigned NumElems = VT.getVectorNumElements();
4603 SDValue ImmN;
4604
Craig Topper89f4e662012-03-20 07:17:59 +00004605 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004606 switch(N->getOpcode()) {
4607 case X86ISD::SHUFP:
4608 ImmN = N->getOperand(N->getNumOperands()-1);
4609 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4610 break;
4611 case X86ISD::UNPCKH:
4612 DecodeUNPCKHMask(VT, Mask);
4613 break;
4614 case X86ISD::UNPCKL:
4615 DecodeUNPCKLMask(VT, Mask);
4616 break;
4617 case X86ISD::MOVHLPS:
4618 DecodeMOVHLPSMask(NumElems, Mask);
4619 break;
4620 case X86ISD::MOVLHPS:
4621 DecodeMOVLHPSMask(NumElems, Mask);
4622 break;
Craig Topper4aee1bb2013-01-28 06:48:25 +00004623 case X86ISD::PALIGNR:
Benjamin Kramer200b3062013-01-26 13:31:37 +00004624 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper4aee1bb2013-01-28 06:48:25 +00004625 DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Benjamin Kramer200b3062013-01-26 13:31:37 +00004626 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004627 case X86ISD::PSHUFD:
4628 case X86ISD::VPERMILP:
4629 ImmN = N->getOperand(N->getNumOperands()-1);
4630 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004631 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004632 break;
4633 case X86ISD::PSHUFHW:
4634 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004635 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004636 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004637 break;
4638 case X86ISD::PSHUFLW:
4639 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004640 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004641 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004642 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004643 case X86ISD::VPERMI:
4644 ImmN = N->getOperand(N->getNumOperands()-1);
4645 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4646 IsUnary = true;
4647 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004648 case X86ISD::MOVSS:
4649 case X86ISD::MOVSD: {
4650 // The index 0 always comes from the first element of the second source,
4651 // this is why MOVSS and MOVSD are used in the first place. The other
4652 // elements come from the other positions of the first source vector
4653 Mask.push_back(NumElems);
4654 for (unsigned i = 1; i != NumElems; ++i) {
4655 Mask.push_back(i);
4656 }
4657 break;
4658 }
4659 case X86ISD::VPERM2X128:
4660 ImmN = N->getOperand(N->getNumOperands()-1);
4661 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004662 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004663 break;
4664 case X86ISD::MOVDDUP:
4665 case X86ISD::MOVLHPD:
4666 case X86ISD::MOVLPD:
4667 case X86ISD::MOVLPS:
4668 case X86ISD::MOVSHDUP:
4669 case X86ISD::MOVSLDUP:
Craig Toppera1ffc682012-03-20 06:42:26 +00004670 // Not yet implemented
4671 return false;
4672 default: llvm_unreachable("unknown target shuffle node");
4673 }
4674
4675 return true;
4676}
4677
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004678/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4679/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004680static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004681 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004682 if (Depth == 6)
4683 return SDValue(); // Limit search depth.
4684
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004685 SDValue V = SDValue(N, 0);
4686 EVT VT = V.getValueType();
4687 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004688
4689 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4690 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004691 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004692
Craig Topper3d092db2012-03-21 02:14:01 +00004693 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004694 return DAG.getUNDEF(VT.getVectorElementType());
4695
Craig Topperd156dc12012-02-06 07:17:51 +00004696 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004697 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4698 : SV->getOperand(1);
4699 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004700 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004701
4702 // Recurse into target specific vector shuffles to find scalars.
4703 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004704 MVT ShufVT = V.getValueType().getSimpleVT();
4705 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004706 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004707 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004708
Craig Topperd978c542012-05-06 19:46:21 +00004709 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004710 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004711
Craig Topper3d092db2012-03-21 02:14:01 +00004712 int Elt = ShuffleMask[Index];
4713 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004714 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004715
Craig Topper3d092db2012-03-21 02:14:01 +00004716 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004717 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004718 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004719 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004720 }
4721
4722 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004723 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004724 V = V.getOperand(0);
4725 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004726 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004727
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004728 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004729 return SDValue();
4730 }
4731
4732 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4733 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004734 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004735
4736 if (V.getOpcode() == ISD::BUILD_VECTOR)
4737 return V.getOperand(Index);
4738
4739 return SDValue();
4740}
4741
4742/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4743/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004744/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004745static
Craig Topper3d092db2012-03-21 02:14:01 +00004746unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004747 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004748 unsigned i;
4749 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004750 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004751 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004752 if (!(Elt.getNode() &&
4753 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4754 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004755 }
4756
4757 return i;
4758}
4759
Craig Topper3d092db2012-03-21 02:14:01 +00004760/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4761/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004762/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4763static
Craig Topper3d092db2012-03-21 02:14:01 +00004764bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4765 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4766 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004767 bool SeenV1 = false;
4768 bool SeenV2 = false;
4769
Craig Topper3d092db2012-03-21 02:14:01 +00004770 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004771 int Idx = SVOp->getMaskElt(i);
4772 // Ignore undef indicies
4773 if (Idx < 0)
4774 continue;
4775
Craig Topper3d092db2012-03-21 02:14:01 +00004776 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004777 SeenV1 = true;
4778 else
4779 SeenV2 = true;
4780
4781 // Only accept consecutive elements from the same vector
4782 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4783 return false;
4784 }
4785
4786 OpNum = SeenV1 ? 0 : 1;
4787 return true;
4788}
4789
4790/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4791/// logical left shift of a vector.
4792static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4793 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4794 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4795 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4796 false /* check zeros from right */, DAG);
4797 unsigned OpSrc;
4798
4799 if (!NumZeros)
4800 return false;
4801
4802 // Considering the elements in the mask that are not consecutive zeros,
4803 // check if they consecutively come from only one of the source vectors.
4804 //
4805 // V1 = {X, A, B, C} 0
4806 // \ \ \ /
4807 // vector_shuffle V1, V2 <1, 2, 3, X>
4808 //
4809 if (!isShuffleMaskConsecutive(SVOp,
4810 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004811 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004812 NumZeros, // Where to start looking in the src vector
4813 NumElems, // Number of elements in vector
4814 OpSrc)) // Which source operand ?
4815 return false;
4816
4817 isLeft = false;
4818 ShAmt = NumZeros;
4819 ShVal = SVOp->getOperand(OpSrc);
4820 return true;
4821}
4822
4823/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4824/// logical left shift of a vector.
4825static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4826 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4827 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4828 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4829 true /* check zeros from left */, DAG);
4830 unsigned OpSrc;
4831
4832 if (!NumZeros)
4833 return false;
4834
4835 // Considering the elements in the mask that are not consecutive zeros,
4836 // check if they consecutively come from only one of the source vectors.
4837 //
4838 // 0 { A, B, X, X } = V2
4839 // / \ / /
4840 // vector_shuffle V1, V2 <X, X, 4, 5>
4841 //
4842 if (!isShuffleMaskConsecutive(SVOp,
4843 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004844 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004845 0, // Where to start looking in the src vector
4846 NumElems, // Number of elements in vector
4847 OpSrc)) // Which source operand ?
4848 return false;
4849
4850 isLeft = true;
4851 ShAmt = NumZeros;
4852 ShVal = SVOp->getOperand(OpSrc);
4853 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004854}
4855
4856/// isVectorShift - Returns true if the shuffle can be implemented as a
4857/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004858static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004859 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004860 // Although the logic below support any bitwidth size, there are no
4861 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004862 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004863 return false;
4864
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004865 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4866 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4867 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004868
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004869 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004870}
4871
Evan Chengc78d3b42006-04-24 18:01:45 +00004872/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4873///
Dan Gohman475871a2008-07-27 21:46:04 +00004874static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004875 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004876 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004877 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004878 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004879 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004880 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004881
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004882 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004883 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004884 bool First = true;
4885 for (unsigned i = 0; i < 16; ++i) {
4886 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4887 if (ThisIsNonZero && First) {
4888 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004889 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004890 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004891 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004892 First = false;
4893 }
4894
4895 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004896 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004897 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4898 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004899 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004900 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004901 }
4902 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004903 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4904 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4905 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004906 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004907 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004908 } else
4909 ThisElt = LastElt;
4910
Gabor Greifba36cb52008-08-28 21:40:38 +00004911 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004912 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004913 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004914 }
4915 }
4916
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004917 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004918}
4919
Bill Wendlinga348c562007-03-22 18:42:45 +00004920/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004921///
Dan Gohman475871a2008-07-27 21:46:04 +00004922static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004923 unsigned NumNonZero, unsigned NumZero,
4924 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004925 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004926 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004927 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004928 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004929
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004930 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004931 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004932 bool First = true;
4933 for (unsigned i = 0; i < 8; ++i) {
4934 bool isNonZero = (NonZeros & (1 << i)) != 0;
4935 if (isNonZero) {
4936 if (First) {
4937 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004938 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004939 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004941 First = false;
4942 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004943 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004944 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004945 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004946 }
4947 }
4948
4949 return V;
4950}
4951
Evan Chengf26ffe92008-05-29 08:22:04 +00004952/// getVShift - Return a vector logical shift node.
4953///
Owen Andersone50ed302009-08-10 22:56:29 +00004954static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004955 unsigned NumBits, SelectionDAG &DAG,
4956 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004957 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004958 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004959 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004960 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4961 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004962 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004963 DAG.getConstant(NumBits,
Michael Liaoa6b20ce2013-03-01 18:40:30 +00004964 TLI.getScalarShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004965}
4966
Dan Gohman475871a2008-07-27 21:46:04 +00004967SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004968X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004969 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004970
Evan Chengc3630942009-12-09 21:00:30 +00004971 // Check if the scalar load can be widened into a vector load. And if
4972 // the address is "base + cst" see if the cst can be "absorbed" into
4973 // the shuffle mask.
4974 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4975 SDValue Ptr = LD->getBasePtr();
4976 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4977 return SDValue();
4978 EVT PVT = LD->getValueType(0);
4979 if (PVT != MVT::i32 && PVT != MVT::f32)
4980 return SDValue();
4981
4982 int FI = -1;
4983 int64_t Offset = 0;
4984 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4985 FI = FINode->getIndex();
4986 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004987 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004988 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4989 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4990 Offset = Ptr.getConstantOperandVal(1);
4991 Ptr = Ptr.getOperand(0);
4992 } else {
4993 return SDValue();
4994 }
4995
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004996 // FIXME: 256-bit vector instructions don't require a strict alignment,
4997 // improve this code to support it better.
4998 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004999 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005000 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00005001 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005002 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00005003 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005004 // Can't change the alignment. FIXME: It's possible to compute
5005 // the exact stack offset and reference FI + adjust offset instead.
5006 // If someone *really* cares about this. That's the way to implement it.
5007 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005008 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005009 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005010 }
5011 }
5012
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005013 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005014 // Ptr + (Offset & ~15).
5015 if (Offset < 0)
5016 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005017 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005018 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005019 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005020 if (StartOffset)
5021 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5022 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5023
5024 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00005025 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005026
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005027 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5028 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005029 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005030 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005031
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005032 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005033 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005034 Mask.push_back(EltNo);
5035
Craig Toppercc3000632012-01-30 07:50:31 +00005036 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005037 }
5038
5039 return SDValue();
5040}
5041
Michael J. Spencerec38de22010-10-10 22:04:20 +00005042/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5043/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005044/// load which has the same value as a build_vector whose operands are 'elts'.
5045///
5046/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005047///
Nate Begeman1449f292010-03-24 22:19:06 +00005048/// FIXME: we'd also like to handle the case where the last elements are zero
5049/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5050/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005051static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005052 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005053 EVT EltVT = VT.getVectorElementType();
5054 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005055
Nate Begemanfdea31a2010-03-24 20:49:50 +00005056 LoadSDNode *LDBase = NULL;
5057 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005058
Nate Begeman1449f292010-03-24 22:19:06 +00005059 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005060 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005061 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005062 for (unsigned i = 0; i < NumElems; ++i) {
5063 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005064
Nate Begemanfdea31a2010-03-24 20:49:50 +00005065 if (!Elt.getNode() ||
5066 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5067 return SDValue();
5068 if (!LDBase) {
5069 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5070 return SDValue();
5071 LDBase = cast<LoadSDNode>(Elt.getNode());
5072 LastLoadedElt = i;
5073 continue;
5074 }
5075 if (Elt.getOpcode() == ISD::UNDEF)
5076 continue;
5077
5078 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5079 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5080 return SDValue();
5081 LastLoadedElt = i;
5082 }
Nate Begeman1449f292010-03-24 22:19:06 +00005083
5084 // If we have found an entire vector of loads and undefs, then return a large
5085 // load of the entire vector width starting at the base pointer. If we found
5086 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005087 if (LastLoadedElt == NumElems - 1) {
5088 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005089 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005090 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005091 LDBase->isVolatile(), LDBase->isNonTemporal(),
5092 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005093 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005094 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005095 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005096 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005097 }
5098 if (NumElems == 4 && LastLoadedElt == 1 &&
5099 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005100 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5101 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005102 SDValue ResNode =
5103 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5104 LDBase->getPointerInfo(),
5105 LDBase->getAlignment(),
5106 false/*isVolatile*/, true/*ReadMem*/,
5107 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005108
5109 // Make sure the newly-created LOAD is in the same position as LDBase in
5110 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5111 // update uses of LDBase's output chain to use the TokenFactor.
5112 if (LDBase->hasAnyUseOfValue(1)) {
5113 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5114 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5115 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5116 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5117 SDValue(ResNode.getNode(), 1));
5118 }
5119
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005120 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005121 }
5122 return SDValue();
5123}
5124
Nadav Rotem9d68b062012-04-08 12:54:54 +00005125/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5126/// to generate a splat value for the following cases:
5127/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005128/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005129/// a scalar load, or a constant.
5130/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005131/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005132SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005133X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005134 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005135 return SDValue();
5136
Craig Topper45e1c752013-01-20 00:38:18 +00005137 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem154819d2012-04-09 07:45:58 +00005138 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005139
Craig Topper5da8a802012-05-04 05:49:51 +00005140 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5141 "Unsupported vector type for broadcast.");
5142
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005143 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005144 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005145
Nadav Rotem9d68b062012-04-08 12:54:54 +00005146 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005147 default:
5148 // Unknown pattern found.
5149 return SDValue();
5150
5151 case ISD::BUILD_VECTOR: {
5152 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005153 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005154 return SDValue();
5155
Nadav Rotem9d68b062012-04-08 12:54:54 +00005156 Ld = Op.getOperand(0);
5157 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5158 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005159
5160 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005161 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005162 // Constants may have multiple users.
5163 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005164 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005165 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005166 }
5167
5168 case ISD::VECTOR_SHUFFLE: {
5169 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5170
5171 // Shuffles must have a splat mask where the first element is
5172 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005173 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005174 return SDValue();
5175
5176 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005177 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005178 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5179
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005180 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005181 return SDValue();
5182
5183 // Use the register form of the broadcast instruction available on AVX2.
5184 if (VT.is256BitVector())
5185 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5186 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5187 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005188
5189 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005190 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005191 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005192
5193 // The scalar_to_vector node and the suspected
5194 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005195 // Constants may have multiple users.
5196 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005197 return SDValue();
5198 break;
5199 }
5200 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005201
Craig Topper7a9a28b2012-08-12 02:23:29 +00005202 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005203
5204 // Handle the broadcasting a single constant scalar from the constant pool
5205 // into a vector. On Sandybridge it is still better to load a constant vector
5206 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005207 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005208 EVT CVT = Ld.getValueType();
5209 assert(!CVT.isVector() && "Must not broadcast a vector type");
5210 unsigned ScalarSize = CVT.getSizeInBits();
5211
Craig Topper5da8a802012-05-04 05:49:51 +00005212 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005213 const Constant *C = 0;
5214 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5215 C = CI->getConstantIntValue();
5216 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5217 C = CF->getConstantFPValue();
5218
5219 assert(C && "Invalid constant type");
5220
Nadav Rotem154819d2012-04-09 07:45:58 +00005221 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005222 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005223 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005224 MachinePointerInfo::getConstantPool(),
5225 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005226
Nadav Rotem9d68b062012-04-08 12:54:54 +00005227 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5228 }
5229 }
5230
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005231 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005232 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5233
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005234 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005235 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005236 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5237 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5238
5239 // The scalar source must be a normal load.
5240 if (!IsLoad)
5241 return SDValue();
5242
Craig Topper5da8a802012-05-04 05:49:51 +00005243 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005244 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005245
Craig Toppera9376332012-01-10 08:23:59 +00005246 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005247 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005248 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005249 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005250 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005251 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005252
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005253 // Unsupported broadcast.
5254 return SDValue();
5255}
5256
Evan Chengc3630942009-12-09 21:00:30 +00005257SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005258X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5259 EVT VT = Op.getValueType();
5260
5261 // Skip if insert_vec_elt is not supported.
5262 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5263 return SDValue();
5264
5265 DebugLoc DL = Op.getDebugLoc();
5266 unsigned NumElems = Op.getNumOperands();
5267
5268 SDValue VecIn1;
5269 SDValue VecIn2;
5270 SmallVector<unsigned, 4> InsertIndices;
5271 SmallVector<int, 8> Mask(NumElems, -1);
5272
5273 for (unsigned i = 0; i != NumElems; ++i) {
5274 unsigned Opc = Op.getOperand(i).getOpcode();
5275
5276 if (Opc == ISD::UNDEF)
5277 continue;
5278
5279 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5280 // Quit if more than 1 elements need inserting.
5281 if (InsertIndices.size() > 1)
5282 return SDValue();
5283
5284 InsertIndices.push_back(i);
5285 continue;
5286 }
5287
5288 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5289 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5290
5291 // Quit if extracted from vector of different type.
5292 if (ExtractedFromVec.getValueType() != VT)
5293 return SDValue();
5294
5295 // Quit if non-constant index.
5296 if (!isa<ConstantSDNode>(ExtIdx))
5297 return SDValue();
5298
5299 if (VecIn1.getNode() == 0)
5300 VecIn1 = ExtractedFromVec;
5301 else if (VecIn1 != ExtractedFromVec) {
5302 if (VecIn2.getNode() == 0)
5303 VecIn2 = ExtractedFromVec;
5304 else if (VecIn2 != ExtractedFromVec)
5305 // Quit if more than 2 vectors to shuffle
5306 return SDValue();
5307 }
5308
5309 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5310
5311 if (ExtractedFromVec == VecIn1)
5312 Mask[i] = Idx;
5313 else if (ExtractedFromVec == VecIn2)
5314 Mask[i] = Idx + NumElems;
5315 }
5316
5317 if (VecIn1.getNode() == 0)
5318 return SDValue();
5319
5320 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5321 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5322 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5323 unsigned Idx = InsertIndices[i];
5324 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5325 DAG.getIntPtrConstant(Idx));
5326 }
5327
5328 return NV;
5329}
5330
5331SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005332X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005333 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005334
Craig Topper45e1c752013-01-20 00:38:18 +00005335 MVT VT = Op.getValueType().getSimpleVT();
5336 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005337 unsigned NumElems = Op.getNumOperands();
5338
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005339 // Vectors containing all zeros can be matched by pxor and xorps later
5340 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5341 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5342 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005343 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005344 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005345
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005346 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005347 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005348
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005349 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005350 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5351 // vpcmpeqd on 256-bit vectors.
Michael Liaod09318f2013-02-25 23:16:36 +00005352 if (Subtarget->hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005353 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005354 return Op;
5355
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005356 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005357 }
5358
Nadav Rotem154819d2012-04-09 07:45:58 +00005359 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005360 if (Broadcast.getNode())
5361 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005362
Owen Andersone50ed302009-08-10 22:56:29 +00005363 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005364
Evan Cheng0db9fe62006-04-25 20:13:52 +00005365 unsigned NumZero = 0;
5366 unsigned NumNonZero = 0;
5367 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005368 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005369 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005370 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005371 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005372 if (Elt.getOpcode() == ISD::UNDEF)
5373 continue;
5374 Values.insert(Elt);
5375 if (Elt.getOpcode() != ISD::Constant &&
5376 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005377 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005378 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005379 NumZero++;
5380 else {
5381 NonZeros |= (1 << i);
5382 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005383 }
5384 }
5385
Chris Lattner97a2a562010-08-26 05:24:29 +00005386 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5387 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005388 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005389
Chris Lattner67f453a2008-03-09 05:42:06 +00005390 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005391 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005392 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005393 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005394
Chris Lattner62098042008-03-09 01:05:04 +00005395 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5396 // the value are obviously zero, truncate the value to i32 and do the
5397 // insertion that way. Only do this if the value is non-constant or if the
5398 // value is a constant being inserted into element 0. It is cheaper to do
5399 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005400 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005401 (!IsAllConstants || Idx == 0)) {
5402 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005403 // Handle SSE only.
5404 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5405 EVT VecVT = MVT::v4i32;
5406 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005407
Chris Lattner62098042008-03-09 01:05:04 +00005408 // Truncate the value (which may itself be a constant) to i32, and
5409 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005410 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005411 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005412 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005413
Chris Lattner62098042008-03-09 01:05:04 +00005414 // Now we have our 32-bit value zero extended in the low element of
5415 // a vector. If Idx != 0, swizzle it into place.
5416 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005417 SmallVector<int, 4> Mask;
5418 Mask.push_back(Idx);
5419 for (unsigned i = 1; i != VecElts; ++i)
5420 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005421 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005422 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005423 }
Craig Topper07a27622012-01-22 03:07:48 +00005424 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005425 }
5426 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005427
Chris Lattner19f79692008-03-08 22:59:52 +00005428 // If we have a constant or non-constant insertion into the low element of
5429 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5430 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005431 // depending on what the source datatype is.
5432 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005433 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005434 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005435
5436 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005437 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005438 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005439 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005440 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5441 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005442 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005443 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005444 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5445 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005446 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005447 }
5448
5449 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005450 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005451 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005452 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005453 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005454 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005455 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005456 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005457 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005458 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005459 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005460 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005461 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005462
5463 // Is it a vector logical left shift?
5464 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005465 X86::isZeroNode(Op.getOperand(0)) &&
5466 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005467 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005468 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005469 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005470 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005471 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005472 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005473
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005474 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005475 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005476
Chris Lattner19f79692008-03-08 22:59:52 +00005477 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5478 // is a non-constant being inserted into an element other than the low one,
5479 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5480 // movd/movss) to move this into the low element, then shuffle it into
5481 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005482 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005483 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005484
Evan Cheng0db9fe62006-04-25 20:13:52 +00005485 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005486 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005487 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005488 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005489 MaskVec.push_back(i == Idx ? 0 : 1);
5490 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005491 }
5492 }
5493
Chris Lattner67f453a2008-03-09 05:42:06 +00005494 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005495 if (Values.size() == 1) {
5496 if (EVTBits == 32) {
5497 // Instead of a shuffle like this:
5498 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5499 // Check if it's possible to issue this instead.
5500 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5501 unsigned Idx = CountTrailingZeros_32(NonZeros);
5502 SDValue Item = Op.getOperand(Idx);
5503 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5504 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5505 }
Dan Gohman475871a2008-07-27 21:46:04 +00005506 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005507 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005508
Dan Gohmana3941172007-07-24 22:55:08 +00005509 // A vector full of immediates; various special cases are already
5510 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005511 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005512 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005513
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005514 // For AVX-length vectors, build the individual 128-bit pieces and use
5515 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005516 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005517 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005518 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005519 V.push_back(Op.getOperand(i));
5520
5521 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5522
5523 // Build both the lower and upper subvector.
5524 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5525 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5526 NumElems/2);
5527
5528 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005529 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005530 }
5531
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005532 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005533 if (EVTBits == 64) {
5534 if (NumNonZero == 1) {
5535 // One half is zero or undef.
5536 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005537 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005538 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005539 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005540 }
Dan Gohman475871a2008-07-27 21:46:04 +00005541 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005542 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005543
5544 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005545 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005546 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005547 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005548 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005549 }
5550
Bill Wendling826f36f2007-03-28 00:57:11 +00005551 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005552 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005553 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005554 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005555 }
5556
5557 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005558 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005559 if (NumElems == 4 && NumZero > 0) {
5560 for (unsigned i = 0; i < 4; ++i) {
5561 bool isZero = !(NonZeros & (1 << i));
5562 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005563 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005564 else
Dale Johannesenace16102009-02-03 19:33:06 +00005565 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005566 }
5567
5568 for (unsigned i = 0; i < 2; ++i) {
5569 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5570 default: break;
5571 case 0:
5572 V[i] = V[i*2]; // Must be a zero vector.
5573 break;
5574 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005575 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005576 break;
5577 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005578 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005579 break;
5580 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005581 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005582 break;
5583 }
5584 }
5585
Benjamin Kramer9c683542012-01-30 15:16:21 +00005586 bool Reverse1 = (NonZeros & 0x3) == 2;
5587 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5588 int MaskVec[] = {
5589 Reverse1 ? 1 : 0,
5590 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005591 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5592 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005593 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005594 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005595 }
5596
Craig Topper7a9a28b2012-08-12 02:23:29 +00005597 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005598 // Check for a build vector of consecutive loads.
5599 for (unsigned i = 0; i < NumElems; ++i)
5600 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005601
Nate Begemanfdea31a2010-03-24 20:49:50 +00005602 // Check for elements which are consecutive loads.
5603 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5604 if (LD.getNode())
5605 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005606
Michael Liaofacace82012-10-19 17:15:18 +00005607 // Check for a build vector from mostly shuffle plus few inserting.
5608 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5609 if (Sh.getNode())
5610 return Sh;
5611
Michael J. Spencerec38de22010-10-10 22:04:20 +00005612 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005613 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005614 SDValue Result;
5615 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5616 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5617 else
5618 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005619
Chris Lattner24faf612010-08-28 17:59:08 +00005620 for (unsigned i = 1; i < NumElems; ++i) {
5621 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5622 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005623 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005624 }
5625 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005626 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005627
Chris Lattner6e80e442010-08-28 17:15:43 +00005628 // Otherwise, expand into a number of unpckl*, start by extending each of
5629 // our (non-undef) elements to the full vector width with the element in the
5630 // bottom slot of the vector (which generates no code for SSE).
5631 for (unsigned i = 0; i < NumElems; ++i) {
5632 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5633 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5634 else
5635 V[i] = DAG.getUNDEF(VT);
5636 }
5637
5638 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005639 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5640 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5641 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005642 unsigned EltStride = NumElems >> 1;
5643 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005644 for (unsigned i = 0; i < EltStride; ++i) {
5645 // If V[i+EltStride] is undef and this is the first round of mixing,
5646 // then it is safe to just drop this shuffle: V[i] is already in the
5647 // right place, the one element (since it's the first round) being
5648 // inserted as undef can be dropped. This isn't safe for successive
5649 // rounds because they will permute elements within both vectors.
5650 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5651 EltStride == NumElems/2)
5652 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005653
Chris Lattner6e80e442010-08-28 17:15:43 +00005654 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005655 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005656 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005657 }
5658 return V[0];
5659 }
Dan Gohman475871a2008-07-27 21:46:04 +00005660 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005661}
5662
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005663// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5664// to create 256-bit vectors from two other 128-bit ones.
5665static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5666 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00005667 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005668
Craig Topper7a9a28b2012-08-12 02:23:29 +00005669 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005670
5671 SDValue V1 = Op.getOperand(0);
5672 SDValue V2 = Op.getOperand(1);
5673 unsigned NumElems = ResVT.getVectorNumElements();
5674
Craig Topper4c7972d2012-04-22 18:15:59 +00005675 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005676}
5677
Craig Topper55b24052012-09-11 06:15:32 +00005678static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005679 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005680
5681 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5682 // from two other 128-bit ones.
5683 return LowerAVXCONCAT_VECTORS(Op, DAG);
5684}
5685
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005686// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005687static SDValue
5688LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5689 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005690 SDValue V1 = SVOp->getOperand(0);
5691 SDValue V2 = SVOp->getOperand(1);
5692 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00005693 MVT VT = SVOp->getValueType(0).getSimpleVT();
5694 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005695 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005696
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005697 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5698 return SDValue();
5699 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005700 return SDValue();
5701
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005702 // Check the mask for BLEND and build the value.
5703 unsigned MaskValue = 0;
5704 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005705 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005706 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005707
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005708 // Blend for v16i16 should be symetric for the both lanes.
5709 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005710
Craig Topper9b33ef72013-01-21 06:57:59 +00005711 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005712 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005713 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005714
Craig Topper04f74a12013-01-21 07:25:16 +00005715 if ((EltIdx < 0 || EltIdx == (int)i) &&
5716 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005717 continue;
5718
Craig Topper9b33ef72013-01-21 06:57:59 +00005719 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005720 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005721 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5722 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005723 else
Craig Topper1842ba02012-04-23 06:38:28 +00005724 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005725 }
5726
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005727 // Convert i32 vectors to floating point if it is not AVX2.
5728 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005729 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005730 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005731 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
5732 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005733 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5734 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5735 }
Craig Topper9b33ef72013-01-21 06:57:59 +00005736
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005737 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5738 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005739 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005740}
5741
Nate Begemanb9a47b82009-02-23 08:49:38 +00005742// v8i16 shuffles - Prefer shuffles in the following order:
5743// 1. [all] pshuflw, pshufhw, optional move
5744// 2. [ssse3] 1 x pshufb
5745// 3. [ssse3] 2 x pshufb + 1 x por
5746// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005747static SDValue
5748LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5749 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005750 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005751 SDValue V1 = SVOp->getOperand(0);
5752 SDValue V2 = SVOp->getOperand(1);
5753 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005754 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005755
Nate Begemanb9a47b82009-02-23 08:49:38 +00005756 // Determine if more than 1 of the words in each of the low and high quadwords
5757 // of the result come from the same quadword of one of the two inputs. Undef
5758 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005759 unsigned LoQuad[] = { 0, 0, 0, 0 };
5760 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005761 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005762 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005763 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005764 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005765 MaskVals.push_back(EltIdx);
5766 if (EltIdx < 0) {
5767 ++Quad[0];
5768 ++Quad[1];
5769 ++Quad[2];
5770 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005771 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005772 }
5773 ++Quad[EltIdx / 4];
5774 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005775 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005776
Nate Begemanb9a47b82009-02-23 08:49:38 +00005777 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005778 unsigned MaxQuad = 1;
5779 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005780 if (LoQuad[i] > MaxQuad) {
5781 BestLoQuad = i;
5782 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005783 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005784 }
5785
Nate Begemanb9a47b82009-02-23 08:49:38 +00005786 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005787 MaxQuad = 1;
5788 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005789 if (HiQuad[i] > MaxQuad) {
5790 BestHiQuad = i;
5791 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005792 }
5793 }
5794
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005796 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005797 // single pshufb instruction is necessary. If There are more than 2 input
5798 // quads, disable the next transformation since it does not help SSSE3.
5799 bool V1Used = InputQuads[0] || InputQuads[1];
5800 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005801 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005802 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005803 BestLoQuad = InputQuads[0] ? 0 : 1;
5804 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 }
5806 if (InputQuads.count() > 2) {
5807 BestLoQuad = -1;
5808 BestHiQuad = -1;
5809 }
5810 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005811
Nate Begemanb9a47b82009-02-23 08:49:38 +00005812 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5813 // the shuffle mask. If a quad is scored as -1, that means that it contains
5814 // words from all 4 input quadwords.
5815 SDValue NewV;
5816 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005817 int MaskV[] = {
5818 BestLoQuad < 0 ? 0 : BestLoQuad,
5819 BestHiQuad < 0 ? 1 : BestHiQuad
5820 };
Eric Christopherfd179292009-08-27 18:07:15 +00005821 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005822 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5823 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5824 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005825
Nate Begemanb9a47b82009-02-23 08:49:38 +00005826 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5827 // source words for the shuffle, to aid later transformations.
5828 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005829 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005830 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005831 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005832 if (idx != (int)i)
5833 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005835 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005836 AllWordsInNewV = false;
5837 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005838 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005839
Nate Begemanb9a47b82009-02-23 08:49:38 +00005840 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5841 if (AllWordsInNewV) {
5842 for (int i = 0; i != 8; ++i) {
5843 int idx = MaskVals[i];
5844 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005845 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005846 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005847 if ((idx != i) && idx < 4)
5848 pshufhw = false;
5849 if ((idx != i) && idx > 3)
5850 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005851 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005852 V1 = NewV;
5853 V2Used = false;
5854 BestLoQuad = 0;
5855 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005856 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005857
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5859 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005860 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005861 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5862 unsigned TargetMask = 0;
5863 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005864 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005865 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5866 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5867 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005868 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005869 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005870 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005871 }
Eric Christopherfd179292009-08-27 18:07:15 +00005872
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005873 // Promote splats to a larger type which usually leads to more efficient code.
5874 // FIXME: Is this true if pshufb is available?
5875 if (SVOp->isSplat())
5876 return PromoteSplat(SVOp, DAG);
5877
Nate Begemanb9a47b82009-02-23 08:49:38 +00005878 // If we have SSSE3, and all words of the result are from 1 input vector,
5879 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5880 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005881 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005883
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005885 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005886 // mask, and elements that come from V1 in the V2 mask, so that the two
5887 // results can be OR'd together.
5888 bool TwoInputs = V1Used && V2Used;
5889 for (unsigned i = 0; i != 8; ++i) {
5890 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005891 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5892 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005893 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005894 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005895 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005896 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005897 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005898 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005899 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005900 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005901 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005902
Nate Begemanb9a47b82009-02-23 08:49:38 +00005903 // Calculate the shuffle mask for the second input, shuffle it, and
5904 // OR it with the first shuffled input.
5905 pshufbMask.clear();
5906 for (unsigned i = 0; i != 8; ++i) {
5907 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005908 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5909 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5910 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5911 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005912 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005913 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005914 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005915 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005916 MVT::v16i8, &pshufbMask[0], 16));
5917 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005918 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005919 }
5920
5921 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5922 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005923 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005924 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005925 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005926 for (int i = 0; i != 4; ++i) {
5927 int idx = MaskVals[i];
5928 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005929 InOrder.set(i);
5930 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005931 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005932 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005933 }
5934 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005935 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005936 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005937
Craig Topperdd637ae2012-02-19 05:41:45 +00005938 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5939 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005940 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005941 NewV.getOperand(0),
5942 getShufflePSHUFLWImmediate(SVOp), DAG);
5943 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005944 }
Eric Christopherfd179292009-08-27 18:07:15 +00005945
Nate Begemanb9a47b82009-02-23 08:49:38 +00005946 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5947 // and update MaskVals with the new element order.
5948 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005949 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005950 for (unsigned i = 4; i != 8; ++i) {
5951 int idx = MaskVals[i];
5952 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005953 InOrder.set(i);
5954 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005955 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005956 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005957 }
5958 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005959 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005960 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005961
Craig Topperdd637ae2012-02-19 05:41:45 +00005962 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5963 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005964 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005965 NewV.getOperand(0),
5966 getShufflePSHUFHWImmediate(SVOp), DAG);
5967 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 }
Eric Christopherfd179292009-08-27 18:07:15 +00005969
Nate Begemanb9a47b82009-02-23 08:49:38 +00005970 // In case BestHi & BestLo were both -1, which means each quadword has a word
5971 // from each of the four input quadwords, calculate the InOrder bitvector now
5972 // before falling through to the insert/extract cleanup.
5973 if (BestLoQuad == -1 && BestHiQuad == -1) {
5974 NewV = V1;
5975 for (int i = 0; i != 8; ++i)
5976 if (MaskVals[i] < 0 || MaskVals[i] == i)
5977 InOrder.set(i);
5978 }
Eric Christopherfd179292009-08-27 18:07:15 +00005979
Nate Begemanb9a47b82009-02-23 08:49:38 +00005980 // The other elements are put in the right place using pextrw and pinsrw.
5981 for (unsigned i = 0; i != 8; ++i) {
5982 if (InOrder[i])
5983 continue;
5984 int EltIdx = MaskVals[i];
5985 if (EltIdx < 0)
5986 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005987 SDValue ExtOp = (EltIdx < 8) ?
5988 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5989 DAG.getIntPtrConstant(EltIdx)) :
5990 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005991 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005992 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005993 DAG.getIntPtrConstant(i));
5994 }
5995 return NewV;
5996}
5997
5998// v16i8 shuffles - Prefer shuffles in the following order:
5999// 1. [ssse3] 1 x pshufb
6000// 2. [ssse3] 2 x pshufb + 1 x por
6001// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
6002static
Nate Begeman9008ca62009-04-27 18:41:29 +00006003SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00006004 SelectionDAG &DAG,
6005 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006006 SDValue V1 = SVOp->getOperand(0);
6007 SDValue V2 = SVOp->getOperand(1);
6008 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006009 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00006010
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00006011 // Promote splats to a larger type which usually leads to more efficient code.
6012 // FIXME: Is this true if pshufb is available?
6013 if (SVOp->isSplat())
6014 return PromoteSplat(SVOp, DAG);
6015
Nate Begemanb9a47b82009-02-23 08:49:38 +00006016 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00006017 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00006018 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00006019
Nate Begemanb9a47b82009-02-23 08:49:38 +00006020 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00006021 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006022 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006023
Nate Begemanb9a47b82009-02-23 08:49:38 +00006024 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00006025 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006026 //
6027 // Otherwise, we have elements from both input vectors, and must zero out
6028 // elements that come from V2 in the first mask, and V1 in the second mask
6029 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006030 for (unsigned i = 0; i != 16; ++i) {
6031 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006032 if (EltIdx < 0 || EltIdx >= 16)
6033 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006034 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006035 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006036 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006037 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006038 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006039
6040 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6041 // the 2nd operand if it's undefined or zero.
6042 if (V2.getOpcode() == ISD::UNDEF ||
6043 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006044 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006045
Nate Begemanb9a47b82009-02-23 08:49:38 +00006046 // Calculate the shuffle mask for the second input, shuffle it, and
6047 // OR it with the first shuffled input.
6048 pshufbMask.clear();
6049 for (unsigned i = 0; i != 16; ++i) {
6050 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006051 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006052 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006053 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006054 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006055 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006056 MVT::v16i8, &pshufbMask[0], 16));
6057 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006058 }
Eric Christopherfd179292009-08-27 18:07:15 +00006059
Nate Begemanb9a47b82009-02-23 08:49:38 +00006060 // No SSSE3 - Calculate in place words and then fix all out of place words
6061 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6062 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006063 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6064 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006065 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006066 for (int i = 0; i != 8; ++i) {
6067 int Elt0 = MaskVals[i*2];
6068 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006069
Nate Begemanb9a47b82009-02-23 08:49:38 +00006070 // This word of the result is all undef, skip it.
6071 if (Elt0 < 0 && Elt1 < 0)
6072 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006073
Nate Begemanb9a47b82009-02-23 08:49:38 +00006074 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006075 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006076 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006077
Nate Begemanb9a47b82009-02-23 08:49:38 +00006078 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6079 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6080 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006081
6082 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6083 // using a single extract together, load it and store it.
6084 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006085 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006086 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006087 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006088 DAG.getIntPtrConstant(i));
6089 continue;
6090 }
6091
Nate Begemanb9a47b82009-02-23 08:49:38 +00006092 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006093 // source byte is not also odd, shift the extracted word left 8 bits
6094 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006095 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006096 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006097 DAG.getIntPtrConstant(Elt1 / 2));
6098 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006099 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006100 DAG.getConstant(8,
6101 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006102 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006103 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6104 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006105 }
6106 // If Elt0 is defined, extract it from the appropriate source. If the
6107 // source byte is not also even, shift the extracted word right 8 bits. If
6108 // Elt1 was also defined, OR the extracted values together before
6109 // inserting them in the result.
6110 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006111 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006112 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6113 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006114 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006115 DAG.getConstant(8,
6116 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006117 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006118 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6119 DAG.getConstant(0x00FF, MVT::i16));
6120 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006121 : InsElt0;
6122 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006123 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006124 DAG.getIntPtrConstant(i));
6125 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006126 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006127}
6128
Elena Demikhovsky41789462012-09-06 12:42:01 +00006129// v32i8 shuffles - Translate to VPSHUFB if possible.
6130static
6131SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006132 const X86Subtarget *Subtarget,
6133 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006134 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006135 SDValue V1 = SVOp->getOperand(0);
6136 SDValue V2 = SVOp->getOperand(1);
6137 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006138 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006139
6140 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006141 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6142 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006143
Michael Liao471b9172012-10-03 23:43:52 +00006144 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006145 // (1) one of input vector is undefined or zeroinitializer.
6146 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6147 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006148 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006149 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006150 return SDValue();
6151
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006152 if (V1IsAllZero && !V2IsAllZero) {
6153 CommuteVectorShuffleMask(MaskVals, 32);
6154 V1 = V2;
6155 }
6156 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006157 for (unsigned i = 0; i != 32; i++) {
6158 int EltIdx = MaskVals[i];
6159 if (EltIdx < 0 || EltIdx >= 32)
6160 EltIdx = 0x80;
6161 else {
6162 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6163 // Cross lane is not allowed.
6164 return SDValue();
6165 EltIdx &= 0xf;
6166 }
6167 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6168 }
6169 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6170 DAG.getNode(ISD::BUILD_VECTOR, dl,
6171 MVT::v32i8, &pshufbMask[0], 32));
6172}
6173
Evan Cheng7a831ce2007-12-15 03:00:47 +00006174/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006175/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006176/// done when every pair / quad of shuffle mask elements point to elements in
6177/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006178/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006179static
Nate Begeman9008ca62009-04-27 18:41:29 +00006180SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006181 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006182 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper3b2aba02013-01-20 00:43:42 +00006183 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006184 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006185 MVT NewVT;
6186 unsigned Scale;
6187 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006188 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006189 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6190 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6191 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6192 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6193 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6194 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006195 }
6196
Nate Begeman9008ca62009-04-27 18:41:29 +00006197 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006198 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006199 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006200 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006201 int EltIdx = SVOp->getMaskElt(i+j);
6202 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006203 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006204 if (StartIdx < 0)
6205 StartIdx = (EltIdx / Scale);
6206 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006207 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006208 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006209 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006210 }
6211
Craig Topper11ac1f82012-05-04 04:08:44 +00006212 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6213 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006214 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006215}
6216
Evan Chengd880b972008-05-09 21:53:03 +00006217/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006218///
Craig Topperf84b7502013-01-20 00:50:58 +00006219static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006220 SDValue SrcOp, SelectionDAG &DAG,
6221 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006222 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006223 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006224 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006225 LD = dyn_cast<LoadSDNode>(SrcOp);
6226 if (!LD) {
6227 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6228 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006229 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006230 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006231 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006232 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006233 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006234 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006235 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006236 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006237 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6238 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6239 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006240 SrcOp.getOperand(0)
6241 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006242 }
6243 }
6244 }
6245
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006246 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006247 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006248 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006249 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006250}
6251
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006252/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6253/// which could not be matched by any known target speficic shuffle
6254static SDValue
6255LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006256
6257 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6258 if (NewOp.getNode())
6259 return NewOp;
6260
Craig Topper657a99c2013-01-19 23:36:09 +00006261 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006262
Craig Topper8f35c132012-01-20 09:29:03 +00006263 unsigned NumElems = VT.getVectorNumElements();
6264 unsigned NumLaneElems = NumElems / 2;
6265
Craig Topper8f35c132012-01-20 09:29:03 +00006266 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006267 MVT EltVT = VT.getVectorElementType();
6268 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006269 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006270
Craig Topper9a2b6e12012-04-06 07:45:23 +00006271 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006272 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006273 // Build a shuffle mask for the output, discovering on the fly which
6274 // input vectors to use as shuffle operands (recorded in InputUsed).
6275 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006276 // out with UseBuildVector set.
6277 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006278 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006279 unsigned LaneStart = l * NumLaneElems;
6280 for (unsigned i = 0; i != NumLaneElems; ++i) {
6281 // The mask element. This indexes into the input.
6282 int Idx = SVOp->getMaskElt(i+LaneStart);
6283 if (Idx < 0) {
6284 // the mask element does not index into any input vector.
6285 Mask.push_back(-1);
6286 continue;
6287 }
Craig Topper8f35c132012-01-20 09:29:03 +00006288
Craig Topper9a2b6e12012-04-06 07:45:23 +00006289 // The input vector this mask element indexes into.
6290 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006291
Craig Topper9a2b6e12012-04-06 07:45:23 +00006292 // Turn the index into an offset from the start of the input vector.
6293 Idx -= Input * NumLaneElems;
6294
6295 // Find or create a shuffle vector operand to hold this input.
6296 unsigned OpNo;
6297 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6298 if (InputUsed[OpNo] == Input)
6299 // This input vector is already an operand.
6300 break;
6301 if (InputUsed[OpNo] < 0) {
6302 // Create a new operand for this input vector.
6303 InputUsed[OpNo] = Input;
6304 break;
6305 }
6306 }
6307
6308 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006309 // More than two input vectors used! Give up on trying to create a
6310 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6311 UseBuildVector = true;
6312 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006313 }
6314
6315 // Add the mask index for the new shuffle vector.
6316 Mask.push_back(Idx + OpNo * NumLaneElems);
6317 }
6318
Craig Topper8ae97ba2012-05-21 06:40:16 +00006319 if (UseBuildVector) {
6320 SmallVector<SDValue, 16> SVOps;
6321 for (unsigned i = 0; i != NumLaneElems; ++i) {
6322 // The mask element. This indexes into the input.
6323 int Idx = SVOp->getMaskElt(i+LaneStart);
6324 if (Idx < 0) {
6325 SVOps.push_back(DAG.getUNDEF(EltVT));
6326 continue;
6327 }
6328
6329 // The input vector this mask element indexes into.
6330 int Input = Idx / NumElems;
6331
6332 // Turn the index into an offset from the start of the input vector.
6333 Idx -= Input * NumElems;
6334
6335 // Extract the vector element by hand.
6336 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6337 SVOp->getOperand(Input),
6338 DAG.getIntPtrConstant(Idx)));
6339 }
6340
6341 // Construct the output using a BUILD_VECTOR.
6342 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6343 SVOps.size());
6344 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006345 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006346 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006347 } else {
6348 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006349 (InputUsed[0] % 2) * NumLaneElems,
6350 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006351 // If only one input was used, use an undefined vector for the other.
6352 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6353 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006354 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006355 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006356 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006357 }
6358
6359 Mask.clear();
6360 }
Craig Topper8f35c132012-01-20 09:29:03 +00006361
6362 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006363 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006364}
6365
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006366/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6367/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006368static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006369LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006370 SDValue V1 = SVOp->getOperand(0);
6371 SDValue V2 = SVOp->getOperand(1);
6372 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006373 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006374
Craig Topper7a9a28b2012-08-12 02:23:29 +00006375 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006376
Benjamin Kramer9c683542012-01-30 15:16:21 +00006377 std::pair<int, int> Locs[4];
6378 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006379 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006380
Evan Chengace3c172008-07-22 21:13:36 +00006381 unsigned NumHi = 0;
6382 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006383 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006384 int Idx = PermMask[i];
6385 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006386 Locs[i] = std::make_pair(-1, -1);
6387 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006388 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6389 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006390 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006391 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006392 NumLo++;
6393 } else {
6394 Locs[i] = std::make_pair(1, NumHi);
6395 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006396 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006397 NumHi++;
6398 }
6399 }
6400 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006401
Evan Chengace3c172008-07-22 21:13:36 +00006402 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006403 // If no more than two elements come from either vector. This can be
6404 // implemented with two shuffles. First shuffle gather the elements.
6405 // The second shuffle, which takes the first shuffle as both of its
6406 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006407 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006408
Benjamin Kramer9c683542012-01-30 15:16:21 +00006409 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006410
Benjamin Kramer9c683542012-01-30 15:16:21 +00006411 for (unsigned i = 0; i != 4; ++i)
6412 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006413 unsigned Idx = (i < 2) ? 0 : 4;
6414 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006415 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006416 }
Evan Chengace3c172008-07-22 21:13:36 +00006417
Nate Begeman9008ca62009-04-27 18:41:29 +00006418 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006419 }
6420
6421 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006422 // Otherwise, we must have three elements from one vector, call it X, and
6423 // one element from the other, call it Y. First, use a shufps to build an
6424 // intermediate vector with the one element from Y and the element from X
6425 // that will be in the same half in the final destination (the indexes don't
6426 // matter). Then, use a shufps to build the final vector, taking the half
6427 // containing the element from Y from the intermediate, and the other half
6428 // from X.
6429 if (NumHi == 3) {
6430 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006431 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006432 std::swap(V1, V2);
6433 }
6434
6435 // Find the element from V2.
6436 unsigned HiIndex;
6437 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006438 int Val = PermMask[HiIndex];
6439 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006440 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006441 if (Val >= 4)
6442 break;
6443 }
6444
Nate Begeman9008ca62009-04-27 18:41:29 +00006445 Mask1[0] = PermMask[HiIndex];
6446 Mask1[1] = -1;
6447 Mask1[2] = PermMask[HiIndex^1];
6448 Mask1[3] = -1;
6449 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006450
6451 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006452 Mask1[0] = PermMask[0];
6453 Mask1[1] = PermMask[1];
6454 Mask1[2] = HiIndex & 1 ? 6 : 4;
6455 Mask1[3] = HiIndex & 1 ? 4 : 6;
6456 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006457 }
Craig Topper69947b92012-04-23 06:57:04 +00006458
6459 Mask1[0] = HiIndex & 1 ? 2 : 0;
6460 Mask1[1] = HiIndex & 1 ? 0 : 2;
6461 Mask1[2] = PermMask[2];
6462 Mask1[3] = PermMask[3];
6463 if (Mask1[2] >= 0)
6464 Mask1[2] += 4;
6465 if (Mask1[3] >= 0)
6466 Mask1[3] += 4;
6467 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006468 }
6469
6470 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006471 int LoMask[] = { -1, -1, -1, -1 };
6472 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006473
Benjamin Kramer9c683542012-01-30 15:16:21 +00006474 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006475 unsigned MaskIdx = 0;
6476 unsigned LoIdx = 0;
6477 unsigned HiIdx = 2;
6478 for (unsigned i = 0; i != 4; ++i) {
6479 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006480 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006481 MaskIdx = 1;
6482 LoIdx = 0;
6483 HiIdx = 2;
6484 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006485 int Idx = PermMask[i];
6486 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006487 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006488 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006489 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006490 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006491 LoIdx++;
6492 } else {
6493 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006494 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006495 HiIdx++;
6496 }
6497 }
6498
Nate Begeman9008ca62009-04-27 18:41:29 +00006499 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6500 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006501 int MaskOps[] = { -1, -1, -1, -1 };
6502 for (unsigned i = 0; i != 4; ++i)
6503 if (Locs[i].first != -1)
6504 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006505 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006506}
6507
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006508static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006509 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006510 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006511
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006512 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6513 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006514 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6515 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6516 // BUILD_VECTOR (load), undef
6517 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006518
6519 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006520}
6521
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006522static
Evan Cheng835580f2010-10-07 20:50:20 +00006523SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6524 EVT VT = Op.getValueType();
6525
6526 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006527 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6528 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006529 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6530 V1, DAG));
6531}
6532
6533static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006534SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006535 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006536 SDValue V1 = Op.getOperand(0);
6537 SDValue V2 = Op.getOperand(1);
6538 EVT VT = Op.getValueType();
6539
6540 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6541
Craig Topper1accb7e2012-01-10 06:54:16 +00006542 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006543 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6544
Evan Cheng0899f5c2011-08-31 02:05:24 +00006545 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6546 return DAG.getNode(ISD::BITCAST, dl, VT,
6547 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6548 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6549 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006550}
6551
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006552static
6553SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6554 SDValue V1 = Op.getOperand(0);
6555 SDValue V2 = Op.getOperand(1);
6556 EVT VT = Op.getValueType();
6557
6558 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6559 "unsupported shuffle type");
6560
6561 if (V2.getOpcode() == ISD::UNDEF)
6562 V2 = V1;
6563
6564 // v4i32 or v4f32
6565 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6566}
6567
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006568static
Craig Topper1accb7e2012-01-10 06:54:16 +00006569SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006570 SDValue V1 = Op.getOperand(0);
6571 SDValue V2 = Op.getOperand(1);
6572 EVT VT = Op.getValueType();
6573 unsigned NumElems = VT.getVectorNumElements();
6574
6575 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6576 // operand of these instructions is only memory, so check if there's a
6577 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6578 // same masks.
6579 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006580
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006581 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006582 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006583 CanFoldLoad = true;
6584
6585 // When V1 is a load, it can be folded later into a store in isel, example:
6586 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6587 // turns into:
6588 // (MOVLPSmr addr:$src1, VR128:$src2)
6589 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006590 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006591 CanFoldLoad = true;
6592
Dan Gohman65fd6562011-11-03 21:49:52 +00006593 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006594 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006595 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006596 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6597
6598 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006599 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006600 if (SVOp->getMaskElt(1) != -1)
6601 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006602 }
6603
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006604 // movl and movlp will both match v2i64, but v2i64 is never matched by
6605 // movl earlier because we make it strict to avoid messing with the movlp load
6606 // folding logic (see the code above getMOVLP call). Match it here then,
6607 // this is horrible, but will stay like this until we move all shuffle
6608 // matching to x86 specific nodes. Note that for the 1st condition all
6609 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006610 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006611 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6612 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006613 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006614 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006615 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006616 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006617
6618 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6619
6620 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006621 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006622 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006623}
6624
Michael Liaod9d09602012-10-23 17:34:00 +00006625// Reduce a vector shuffle to zext.
6626SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006627X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006628 // PMOVZX is only available from SSE41.
6629 if (!Subtarget->hasSSE41())
6630 return SDValue();
6631
6632 EVT VT = Op.getValueType();
6633
6634 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006635 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006636 return SDValue();
6637
6638 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6639 DebugLoc DL = Op.getDebugLoc();
6640 SDValue V1 = Op.getOperand(0);
6641 SDValue V2 = Op.getOperand(1);
6642 unsigned NumElems = VT.getVectorNumElements();
6643
6644 // Extending is an unary operation and the element type of the source vector
6645 // won't be equal to or larger than i64.
6646 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6647 VT.getVectorElementType() == MVT::i64)
6648 return SDValue();
6649
6650 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6651 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006652 while ((1U << Shift) < NumElems) {
6653 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006654 break;
6655 Shift += 1;
6656 // The maximal ratio is 8, i.e. from i8 to i64.
6657 if (Shift > 3)
6658 return SDValue();
6659 }
6660
6661 // Check the shuffle mask.
6662 unsigned Mask = (1U << Shift) - 1;
6663 for (unsigned i = 0; i != NumElems; ++i) {
6664 int EltIdx = SVOp->getMaskElt(i);
6665 if ((i & Mask) != 0 && EltIdx != -1)
6666 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006667 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006668 return SDValue();
6669 }
6670
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006671 LLVMContext *Context = DAG.getContext();
Michael Liaod9d09602012-10-23 17:34:00 +00006672 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006673 EVT NeVT = EVT::getIntegerVT(*Context, NBits);
6674 EVT NVT = EVT::getVectorVT(*Context, NeVT, NumElems >> Shift);
Michael Liaod9d09602012-10-23 17:34:00 +00006675
6676 if (!isTypeLegal(NVT))
6677 return SDValue();
6678
6679 // Simplify the operand as it's prepared to be fed into shuffle.
6680 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6681 if (V1.getOpcode() == ISD::BITCAST &&
6682 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6683 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6684 V1.getOperand(0)
6685 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6686 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6687 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006688 ConstantSDNode *CIdx =
6689 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006690 // If it's foldable, i.e. normal load with single use, we will let code
6691 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006692 if (CIdx && CIdx->getZExtValue() == 0 &&
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006693 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse())) {
6694 if (V.getValueSizeInBits() > V1.getValueSizeInBits()) {
6695 // The "ext_vec_elt" node is wider than the result node.
6696 // In this case we should extract subvector from V.
6697 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast (extract_subvector x)).
6698 unsigned Ratio = V.getValueSizeInBits() / V1.getValueSizeInBits();
6699 EVT FullVT = V.getValueType();
6700 EVT SubVecVT = EVT::getVectorVT(*Context,
6701 FullVT.getVectorElementType(),
6702 FullVT.getVectorNumElements()/Ratio);
6703 V = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, V,
6704 DAG.getIntPtrConstant(0));
6705 }
Michael Liaod9d09602012-10-23 17:34:00 +00006706 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006707 }
Michael Liaod9d09602012-10-23 17:34:00 +00006708 }
6709
6710 return DAG.getNode(ISD::BITCAST, DL, VT,
6711 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6712}
6713
Nadav Rotem154819d2012-04-09 07:45:58 +00006714SDValue
6715X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006716 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006717 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006718 DebugLoc dl = Op.getDebugLoc();
6719 SDValue V1 = Op.getOperand(0);
6720 SDValue V2 = Op.getOperand(1);
6721
6722 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006723 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006724
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006725 // Handle splat operations
6726 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006727 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006728 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006729 if (Broadcast.getNode())
6730 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006731 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006732
Michael Liaod9d09602012-10-23 17:34:00 +00006733 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006734 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006735 if (NewOp.getNode())
6736 return NewOp;
6737
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006738 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6739 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006740 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6741 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006742 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006743 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006744 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006745 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006746 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006747 // FIXME: Figure out a cleaner way to do this.
6748 // Try to make use of movq to zero out the top part.
6749 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006750 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006751 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006752 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006753 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6754 NewVT, true, false))
6755 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006756 DAG, Subtarget, dl);
6757 }
6758 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006759 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +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 (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6763 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6764 DAG, Subtarget, dl);
6765 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006766 }
6767 }
6768 return SDValue();
6769}
6770
Dan Gohman475871a2008-07-27 21:46:04 +00006771SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006772X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006773 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006774 SDValue V1 = Op.getOperand(0);
6775 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006776 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006777 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006778 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006779 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006780 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006781 bool V1IsSplat = false;
6782 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006783 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006784 bool HasFp256 = Subtarget->hasFp256();
6785 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006786 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006787 bool OptForSize = MF.getFunction()->getAttributes().
6788 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006789
Craig Topper3426a3e2011-11-14 06:46:21 +00006790 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006791
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006792 if (V1IsUndef && V2IsUndef)
6793 return DAG.getUNDEF(VT);
6794
6795 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006796
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006797 // Vector shuffle lowering takes 3 steps:
6798 //
6799 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6800 // narrowing and commutation of operands should be handled.
6801 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6802 // shuffle nodes.
6803 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6804 // so the shuffle can be broken into other shuffles and the legalizer can
6805 // try the lowering again.
6806 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006807 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006808 // be matched during isel, all of them must be converted to a target specific
6809 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006810
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006811 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6812 // narrowing and commutation of operands should be handled. The actual code
6813 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006814 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006815 if (NewOp.getNode())
6816 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006817
Craig Topper5aaffa82012-02-19 02:53:47 +00006818 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6819
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006820 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6821 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006822 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006823 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006824 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006825 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006826
Craig Topperdd637ae2012-02-19 05:41:45 +00006827 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006828 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006829 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006830
Craig Topperdd637ae2012-02-19 05:41:45 +00006831 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006832 return getMOVHighToLow(Op, dl, DAG);
6833
6834 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006835 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006836 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006837 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006838
Craig Topper5aaffa82012-02-19 02:53:47 +00006839 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006840 // The actual implementation will match the mask in the if above and then
6841 // during isel it can match several different instructions, not only pshufd
6842 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006843 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6844 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006845
Craig Topper5aaffa82012-02-19 02:53:47 +00006846 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006847
Craig Topper1accb7e2012-01-10 06:54:16 +00006848 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006849 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6850
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006851 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6852 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6853 DAG);
6854
Craig Topperb3982da2011-12-31 23:50:21 +00006855 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006856 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006857 }
Eric Christopherfd179292009-08-27 18:07:15 +00006858
Evan Chengf26ffe92008-05-29 08:22:04 +00006859 // Check if this can be converted into a logical shift.
6860 bool isLeft = false;
6861 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006862 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006863 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006864 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006865 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006866 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006867 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006868 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006869 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006870 }
Eric Christopherfd179292009-08-27 18:07:15 +00006871
Craig Topper5aaffa82012-02-19 02:53:47 +00006872 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006873 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006874 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006875 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006876 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006877 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6878
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006879 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006880 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6881 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006882 }
Eric Christopherfd179292009-08-27 18:07:15 +00006883
Nate Begeman9008ca62009-04-27 18:41:29 +00006884 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006885 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006886 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006887
Craig Topperdd637ae2012-02-19 05:41:45 +00006888 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006889 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006890
Craig Topperdd637ae2012-02-19 05:41:45 +00006891 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006892 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006893
Craig Topperdd637ae2012-02-19 05:41:45 +00006894 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006895 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006896
Craig Topperdd637ae2012-02-19 05:41:45 +00006897 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006898 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006899
Craig Topperdd637ae2012-02-19 05:41:45 +00006900 if (ShouldXformToMOVHLPS(M, VT) ||
6901 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006902 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006903
Evan Chengf26ffe92008-05-29 08:22:04 +00006904 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006905 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00006906 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006907 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006908 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006909 }
Eric Christopherfd179292009-08-27 18:07:15 +00006910
Evan Cheng9eca5e82006-10-25 21:49:50 +00006911 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006912 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6913 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006914 V1IsSplat = isSplatVector(V1.getNode());
6915 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006916
Chris Lattner8a594482007-11-25 00:24:49 +00006917 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006918 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6919 CommuteVectorShuffleMask(M, NumElems);
6920 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006921 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006922 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006923 }
6924
Craig Topperbeabc6c2011-12-05 06:56:46 +00006925 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006926 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006927 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006928 return V1;
6929 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6930 // the instruction selector will not match, so get a canonical MOVL with
6931 // swapped operands to undo the commute.
6932 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006933 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006934
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006935 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006936 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006937
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006938 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006939 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006940
Evan Cheng9bbbb982006-10-25 20:48:19 +00006941 if (V2IsSplat) {
6942 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006943 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006944 // new vector_shuffle with the corrected mask.p
6945 SmallVector<int, 8> NewMask(M.begin(), M.end());
6946 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006947 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006948 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006949 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006950 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006951 }
6952
Evan Cheng9eca5e82006-10-25 21:49:50 +00006953 if (Commuted) {
6954 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006955 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006956 CommuteVectorShuffleMask(M, NumElems);
6957 std::swap(V1, V2);
6958 std::swap(V1IsSplat, V2IsSplat);
6959 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006960
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006961 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006962 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006963
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006964 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006965 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006966 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006967
Nate Begeman9008ca62009-04-27 18:41:29 +00006968 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006969 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006970 return CommuteVectorShuffle(SVOp, DAG);
6971
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006972 // The checks below are all present in isShuffleMaskLegal, but they are
6973 // inlined here right now to enable us to directly emit target specific
6974 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006975
Craig Topper0e2037b2012-01-20 05:53:00 +00006976 if (isPALIGNRMask(M, VT, Subtarget))
Craig Topper4aee1bb2013-01-28 06:48:25 +00006977 return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006978 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006979 DAG);
6980
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006981 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6982 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006983 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006984 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006985 }
6986
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006987 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006988 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006989 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006990 DAG);
6991
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006992 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006993 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006994 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006995 DAG);
6996
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006997 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006998 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006999 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00007000
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007001 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007002 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007003 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007004 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00007005
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007006 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007007 // Generate target specific nodes for 128 or 256-bit shuffles only
7008 // supported in the AVX instruction set.
7009 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007010
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007011 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007012 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007013 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
7014
Craig Topper70b883b2011-11-28 10:14:51 +00007015 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007016 if (isVPERMILPMask(M, VT, HasFp256)) {
7017 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00007018 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007019 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00007020 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007021 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00007022 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007023
Craig Topper70b883b2011-11-28 10:14:51 +00007024 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007025 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00007026 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00007027 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007028
Craig Topper1842ba02012-04-23 06:38:28 +00007029 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007030 if (BlendOp.getNode())
7031 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00007032
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007033 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00007034 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007035 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007036 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007037 }
Craig Topper92040742012-04-16 06:43:40 +00007038 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7039 &permclMask[0], 8);
7040 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007041 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007042 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007043 }
Craig Topper095c5282012-04-15 23:48:57 +00007044
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007045 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007046 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007047 getShuffleCLImmediate(SVOp), DAG);
7048
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007049 //===--------------------------------------------------------------------===//
7050 // Since no target specific shuffle was selected for this generic one,
7051 // lower it into other known shuffles. FIXME: this isn't true yet, but
7052 // this is the plan.
7053 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007054
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007055 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7056 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007057 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007058 if (NewOp.getNode())
7059 return NewOp;
7060 }
7061
7062 if (VT == MVT::v16i8) {
7063 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7064 if (NewOp.getNode())
7065 return NewOp;
7066 }
7067
Elena Demikhovsky41789462012-09-06 12:42:01 +00007068 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007069 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007070 if (NewOp.getNode())
7071 return NewOp;
7072 }
7073
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007074 // Handle all 128-bit wide vectors with 4 elements, and match them with
7075 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007076 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007077 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7078
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007079 // Handle general 256-bit shuffles
7080 if (VT.is256BitVector())
7081 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7082
Dan Gohman475871a2008-07-27 21:46:04 +00007083 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007084}
7085
Craig Topperf84b7502013-01-20 00:50:58 +00007086static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007087 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007088 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007089
Craig Topper45e1c752013-01-20 00:38:18 +00007090 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007091 return SDValue();
7092
Duncan Sands83ec4b62008-06-06 12:08:01 +00007093 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007094 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007095 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007096 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007097 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007098 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007099 }
7100
7101 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007102 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7103 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7104 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007105 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7106 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007107 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007108 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007109 Op.getOperand(0)),
7110 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007111 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007112 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007114 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007115 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007116 }
7117
7118 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007119 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7120 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007121 // result has a single use which is a store or a bitcast to i32. And in
7122 // the case of a store, it's not worth it if the index is a constant 0,
7123 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007124 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007125 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007126 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007127 if ((User->getOpcode() != ISD::STORE ||
7128 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7129 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007130 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007131 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007132 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007133 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007134 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007135 Op.getOperand(0)),
7136 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007137 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007138 }
7139
7140 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007141 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007142 if (isa<ConstantSDNode>(Op.getOperand(1)))
7143 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007144 }
Dan Gohman475871a2008-07-27 21:46:04 +00007145 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007146}
7147
Dan Gohman475871a2008-07-27 21:46:04 +00007148SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007149X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7150 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007151 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007152 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007153
David Greene74a579d2011-02-10 16:57:36 +00007154 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007155 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007156
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007157 // If this is a 256-bit vector result, first extract the 128-bit vector and
7158 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007159 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007160 DebugLoc dl = Op.getNode()->getDebugLoc();
7161 unsigned NumElems = VecVT.getVectorNumElements();
7162 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007163 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7164
7165 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007166 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007167
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007168 if (IdxVal >= NumElems/2)
7169 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007170 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007171 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007172 }
7173
Craig Topper7a9a28b2012-08-12 02:23:29 +00007174 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007175
Craig Topperd0a31172012-01-10 06:37:29 +00007176 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007177 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007178 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007179 return Res;
7180 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007181
Craig Topper45e1c752013-01-20 00:38:18 +00007182 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007183 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007184 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007185 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007186 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007187 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007188 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007189 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7190 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007191 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007192 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007193 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007194 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007195 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007196 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007197 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007198 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007199 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007200 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007201 }
7202
7203 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007204 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007205 if (Idx == 0)
7206 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007207
Evan Cheng0db9fe62006-04-25 20:13:52 +00007208 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007209 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007210 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007211 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007212 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007213 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007214 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007215 }
7216
7217 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007218 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7219 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7220 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007221 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007222 if (Idx == 0)
7223 return Op;
7224
7225 // UNPCKHPD the element to the lowest double word, then movsd.
7226 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7227 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007228 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007229 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007230 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007231 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007232 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007233 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007234 }
7235
Dan Gohman475871a2008-07-27 21:46:04 +00007236 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007237}
7238
Craig Topperf84b7502013-01-20 00:50:58 +00007239static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007240 MVT VT = Op.getValueType().getSimpleVT();
7241 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007242 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007243
Dan Gohman475871a2008-07-27 21:46:04 +00007244 SDValue N0 = Op.getOperand(0);
7245 SDValue N1 = Op.getOperand(1);
7246 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007247
Craig Topper7a9a28b2012-08-12 02:23:29 +00007248 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007249 return SDValue();
7250
Dan Gohman8a55ce42009-09-23 21:02:20 +00007251 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007252 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007253 unsigned Opc;
7254 if (VT == MVT::v8i16)
7255 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007256 else if (VT == MVT::v16i8)
7257 Opc = X86ISD::PINSRB;
7258 else
7259 Opc = X86ISD::PINSRB;
7260
Nate Begeman14d12ca2008-02-11 04:19:36 +00007261 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7262 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007263 if (N1.getValueType() != MVT::i32)
7264 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7265 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007266 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007267 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007268 }
7269
7270 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007271 // Bits [7:6] of the constant are the source select. This will always be
7272 // zero here. The DAG Combiner may combine an extract_elt index into these
7273 // bits. For example (insert (extract, 3), 2) could be matched by putting
7274 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007275 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007276 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007277 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007278 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007279 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007280 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007281 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007282 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007283 }
7284
7285 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007286 // PINSR* works with constant index.
7287 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007288 }
Dan Gohman475871a2008-07-27 21:46:04 +00007289 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007290}
7291
Dan Gohman475871a2008-07-27 21:46:04 +00007292SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007293X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007294 MVT VT = Op.getValueType().getSimpleVT();
7295 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007296
David Greene6b381262011-02-09 15:32:06 +00007297 DebugLoc dl = Op.getDebugLoc();
7298 SDValue N0 = Op.getOperand(0);
7299 SDValue N1 = Op.getOperand(1);
7300 SDValue N2 = Op.getOperand(2);
7301
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007302 // If this is a 256-bit vector result, first extract the 128-bit vector,
7303 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007304 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007305 if (!isa<ConstantSDNode>(N2))
7306 return SDValue();
7307
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007308 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007309 unsigned NumElems = VT.getVectorNumElements();
7310 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007311 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007312
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007313 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007314 bool Upper = IdxVal >= NumElems/2;
7315 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7316 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007317
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007318 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007319 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007320 }
7321
Craig Topperd0a31172012-01-10 06:37:29 +00007322 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007323 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7324
Dan Gohman8a55ce42009-09-23 21:02:20 +00007325 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007326 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007327
Dan Gohman8a55ce42009-09-23 21:02:20 +00007328 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007329 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7330 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007331 if (N1.getValueType() != MVT::i32)
7332 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7333 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007334 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007335 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007336 }
Dan Gohman475871a2008-07-27 21:46:04 +00007337 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007338}
7339
Craig Topper55b24052012-09-11 06:15:32 +00007340static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007341 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007342 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00007343 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007344
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007345 // If this is a 256-bit vector result, first insert into a 128-bit
7346 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007347 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007348 // Insert into a 128-bit vector.
7349 EVT VT128 = EVT::getVectorVT(*Context,
7350 OpVT.getVectorElementType(),
7351 OpVT.getVectorNumElements() / 2);
7352
7353 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7354
7355 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007356 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007357 }
7358
Craig Topperd77d2fe2012-04-29 20:22:05 +00007359 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007360 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007361 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007362
Owen Anderson825b72b2009-08-11 20:47:22 +00007363 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007364 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007365 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007366 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007367}
7368
David Greene91585092011-01-26 15:38:49 +00007369// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7370// a simple subregister reference or explicit instructions to grab
7371// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007372static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7373 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007374 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007375 DebugLoc dl = Op.getNode()->getDebugLoc();
7376 SDValue Vec = Op.getNode()->getOperand(0);
7377 SDValue Idx = Op.getNode()->getOperand(1);
7378
Craig Topper7a9a28b2012-08-12 02:23:29 +00007379 if (Op.getNode()->getValueType(0).is128BitVector() &&
7380 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007381 isa<ConstantSDNode>(Idx)) {
7382 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7383 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007384 }
David Greene91585092011-01-26 15:38:49 +00007385 }
7386 return SDValue();
7387}
7388
David Greenecfe33c42011-01-26 19:13:22 +00007389// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7390// simple superregister reference or explicit instructions to insert
7391// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007392static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7393 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007394 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007395 DebugLoc dl = Op.getNode()->getDebugLoc();
7396 SDValue Vec = Op.getNode()->getOperand(0);
7397 SDValue SubVec = Op.getNode()->getOperand(1);
7398 SDValue Idx = Op.getNode()->getOperand(2);
7399
Craig Topper7a9a28b2012-08-12 02:23:29 +00007400 if (Op.getNode()->getValueType(0).is256BitVector() &&
7401 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007402 isa<ConstantSDNode>(Idx)) {
7403 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7404 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007405 }
7406 }
7407 return SDValue();
7408}
7409
Bill Wendling056292f2008-09-16 21:48:12 +00007410// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7411// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7412// one of the above mentioned nodes. It has to be wrapped because otherwise
7413// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7414// be used to form addressing mode. These wrapped nodes will be selected
7415// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007416SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007417X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007418 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007419
Chris Lattner41621a22009-06-26 19:22:52 +00007420 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7421 // global base reg.
7422 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007423 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007424 CodeModel::Model M = getTargetMachine().getCodeModel();
7425
Chris Lattner4f066492009-07-11 20:29:19 +00007426 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007427 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007428 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007429 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007430 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007431 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007432 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007433
Evan Cheng1606e8e2009-03-13 07:51:59 +00007434 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007435 CP->getAlignment(),
7436 CP->getOffset(), OpFlag);
7437 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007438 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007439 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007440 if (OpFlag) {
7441 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007442 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007443 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007444 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007445 }
7446
7447 return Result;
7448}
7449
Dan Gohmand858e902010-04-17 15:26:15 +00007450SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007451 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007452
Chris Lattner18c59872009-06-27 04:16:01 +00007453 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7454 // global base reg.
7455 unsigned char OpFlag = 0;
7456 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007457 CodeModel::Model M = getTargetMachine().getCodeModel();
7458
Chris Lattner4f066492009-07-11 20:29:19 +00007459 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007460 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007461 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007462 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007463 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007464 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007465 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007466
Chris Lattner18c59872009-06-27 04:16:01 +00007467 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7468 OpFlag);
7469 DebugLoc DL = JT->getDebugLoc();
7470 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007471
Chris Lattner18c59872009-06-27 04:16:01 +00007472 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007473 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007474 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7475 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007476 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007477 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007478
Chris Lattner18c59872009-06-27 04:16:01 +00007479 return Result;
7480}
7481
7482SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007483X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007484 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007485
Chris Lattner18c59872009-06-27 04:16:01 +00007486 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7487 // global base reg.
7488 unsigned char OpFlag = 0;
7489 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007490 CodeModel::Model M = getTargetMachine().getCodeModel();
7491
Chris Lattner4f066492009-07-11 20:29:19 +00007492 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007493 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7494 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7495 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007496 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007497 } else if (Subtarget->isPICStyleGOT()) {
7498 OpFlag = X86II::MO_GOT;
7499 } else if (Subtarget->isPICStyleStubPIC()) {
7500 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7501 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7502 OpFlag = X86II::MO_DARWIN_NONLAZY;
7503 }
Eric Christopherfd179292009-08-27 18:07:15 +00007504
Chris Lattner18c59872009-06-27 04:16:01 +00007505 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007506
Chris Lattner18c59872009-06-27 04:16:01 +00007507 DebugLoc DL = Op.getDebugLoc();
7508 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007509
Chris Lattner18c59872009-06-27 04:16:01 +00007510 // With PIC, the address is actually $g + Offset.
7511 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007512 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007513 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7514 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007515 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007516 Result);
7517 }
Eric Christopherfd179292009-08-27 18:07:15 +00007518
Eli Friedman586272d2011-08-11 01:48:05 +00007519 // For symbols that require a load from a stub to get the address, emit the
7520 // load.
7521 if (isGlobalStubReference(OpFlag))
7522 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007523 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007524
Chris Lattner18c59872009-06-27 04:16:01 +00007525 return Result;
7526}
7527
Dan Gohman475871a2008-07-27 21:46:04 +00007528SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007529X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007530 // Create the TargetBlockAddressAddress node.
7531 unsigned char OpFlags =
7532 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007533 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007534 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007535 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007536 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007537 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7538 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007539
Dan Gohmanf705adb2009-10-30 01:28:02 +00007540 if (Subtarget->isPICStyleRIPRel() &&
7541 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007542 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7543 else
7544 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007545
Dan Gohman29cbade2009-11-20 23:18:13 +00007546 // With PIC, the address is actually $g + Offset.
7547 if (isGlobalRelativeToPICBase(OpFlags)) {
7548 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7549 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7550 Result);
7551 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007552
7553 return Result;
7554}
7555
7556SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007557X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007558 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007559 // Create the TargetGlobalAddress node, folding in the constant
7560 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007561 unsigned char OpFlags =
7562 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007563 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007564 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007565 if (OpFlags == X86II::MO_NO_FLAG &&
7566 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007567 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007568 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007569 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007570 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007571 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007572 }
Eric Christopherfd179292009-08-27 18:07:15 +00007573
Chris Lattner4f066492009-07-11 20:29:19 +00007574 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007575 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007576 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7577 else
7578 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007579
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007580 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007581 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007582 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7583 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007584 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007585 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007586
Chris Lattner36c25012009-07-10 07:34:39 +00007587 // For globals that require a load from a stub to get the address, emit the
7588 // load.
7589 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007590 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007591 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007592
Dan Gohman6520e202008-10-18 02:06:02 +00007593 // If there was a non-zero offset that we didn't fold, create an explicit
7594 // addition for it.
7595 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007596 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007597 DAG.getConstant(Offset, getPointerTy()));
7598
Evan Cheng0db9fe62006-04-25 20:13:52 +00007599 return Result;
7600}
7601
Evan Chengda43bcf2008-09-24 00:05:32 +00007602SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007603X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007604 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007605 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007606 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007607}
7608
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007609static SDValue
7610GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007611 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007612 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007613 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007614 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007615 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007616 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007617 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007618 GA->getOffset(),
7619 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007620
7621 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7622 : X86ISD::TLSADDR;
7623
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007624 if (InFlag) {
7625 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007626 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007627 } else {
7628 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007629 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007630 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007631
7632 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007633 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007634
Rafael Espindola15f1b662009-04-24 12:59:40 +00007635 SDValue Flag = Chain.getValue(1);
7636 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007637}
7638
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007639// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007640static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007641LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007642 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007643 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007644 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7645 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007646 DAG.getNode(X86ISD::GlobalBaseReg,
7647 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007648 InFlag = Chain.getValue(1);
7649
Chris Lattnerb903bed2009-06-26 21:20:29 +00007650 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007651}
7652
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007653// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007654static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007655LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007656 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007657 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7658 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007659}
7660
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007661static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7662 SelectionDAG &DAG,
7663 const EVT PtrVT,
7664 bool is64Bit) {
7665 DebugLoc dl = GA->getDebugLoc();
7666
7667 // Get the start address of the TLS block for this module.
7668 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7669 .getInfo<X86MachineFunctionInfo>();
7670 MFI->incNumLocalDynamicTLSAccesses();
7671
7672 SDValue Base;
7673 if (is64Bit) {
7674 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7675 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7676 } else {
7677 SDValue InFlag;
7678 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7679 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7680 InFlag = Chain.getValue(1);
7681 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7682 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7683 }
7684
7685 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7686 // of Base.
7687
7688 // Build x@dtpoff.
7689 unsigned char OperandFlags = X86II::MO_DTPOFF;
7690 unsigned WrapperKind = X86ISD::Wrapper;
7691 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7692 GA->getValueType(0),
7693 GA->getOffset(), OperandFlags);
7694 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7695
7696 // Add x@dtpoff with the base.
7697 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7698}
7699
Hans Wennborg228756c2012-05-11 10:11:01 +00007700// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007701static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007702 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007703 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007704 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007705
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007706 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7707 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7708 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007709
Michael J. Spencerec38de22010-10-10 22:04:20 +00007710 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007711 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007712 MachinePointerInfo(Ptr),
7713 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007714
Chris Lattnerb903bed2009-06-26 21:20:29 +00007715 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007716 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7717 // initialexec.
7718 unsigned WrapperKind = X86ISD::Wrapper;
7719 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007720 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007721 } else if (model == TLSModel::InitialExec) {
7722 if (is64Bit) {
7723 OperandFlags = X86II::MO_GOTTPOFF;
7724 WrapperKind = X86ISD::WrapperRIP;
7725 } else {
7726 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7727 }
Chris Lattner18c59872009-06-27 04:16:01 +00007728 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007729 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007730 }
Eric Christopherfd179292009-08-27 18:07:15 +00007731
Hans Wennborg228756c2012-05-11 10:11:01 +00007732 // emit "addl x@ntpoff,%eax" (local exec)
7733 // or "addl x@indntpoff,%eax" (initial exec)
7734 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007735 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007736 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007737 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007738 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007739
Hans Wennborg228756c2012-05-11 10:11:01 +00007740 if (model == TLSModel::InitialExec) {
7741 if (isPIC && !is64Bit) {
7742 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7743 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7744 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007745 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007746
7747 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7748 MachinePointerInfo::getGOT(), false, false, false,
7749 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007750 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007751
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007752 // The address of the thread local variable is the add of the thread
7753 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007754 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007755}
7756
Dan Gohman475871a2008-07-27 21:46:04 +00007757SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007758X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007759
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007760 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007761 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007762
Eric Christopher30ef0e52010-06-03 04:07:48 +00007763 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007764 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007765
Eric Christopher30ef0e52010-06-03 04:07:48 +00007766 switch (model) {
7767 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007768 if (Subtarget->is64Bit())
7769 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7770 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007771 case TLSModel::LocalDynamic:
7772 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7773 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007774 case TLSModel::InitialExec:
7775 case TLSModel::LocalExec:
7776 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007777 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00007778 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007779 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007780 llvm_unreachable("Unknown TLS model.");
7781 }
7782
7783 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007784 // Darwin only has one model of TLS. Lower to that.
7785 unsigned char OpFlag = 0;
7786 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7787 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007788
Eric Christopher30ef0e52010-06-03 04:07:48 +00007789 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7790 // global base reg.
7791 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7792 !Subtarget->is64Bit();
7793 if (PIC32)
7794 OpFlag = X86II::MO_TLVP_PIC_BASE;
7795 else
7796 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007797 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007798 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007799 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007800 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007801 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007802
Eric Christopher30ef0e52010-06-03 04:07:48 +00007803 // With PIC32, the address is actually $g + Offset.
7804 if (PIC32)
7805 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7806 DAG.getNode(X86ISD::GlobalBaseReg,
7807 DebugLoc(), getPointerTy()),
7808 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007809
Eric Christopher30ef0e52010-06-03 04:07:48 +00007810 // Lowering the machine isd will make sure everything is in the right
7811 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007812 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007813 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007814 SDValue Args[] = { Chain, Offset };
7815 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007816
Eric Christopher30ef0e52010-06-03 04:07:48 +00007817 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7818 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7819 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007820
Eric Christopher30ef0e52010-06-03 04:07:48 +00007821 // And our return value (tls address) is in the standard call return value
7822 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007823 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007824 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7825 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007826 }
7827
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007828 if (Subtarget->isTargetWindows() || Subtarget->isTargetMingw()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007829 // Just use the implicit TLS architecture
7830 // Need to generate someting similar to:
7831 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7832 // ; from TEB
7833 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7834 // mov rcx, qword [rdx+rcx*8]
7835 // mov eax, .tls$:tlsvar
7836 // [rax+rcx] contains the address
7837 // Windows 64bit: gs:0x58
7838 // Windows 32bit: fs:__tls_array
7839
7840 // If GV is an alias then use the aliasee for determining
7841 // thread-localness.
7842 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7843 GV = GA->resolveAliasedGlobal(false);
7844 DebugLoc dl = GA->getDebugLoc();
7845 SDValue Chain = DAG.getEntryNode();
7846
7847 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007848 // %gs:0x58 (64-bit). On MinGW, __tls_array is not available, so directly
7849 // use its literal value of 0x2C.
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007850 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7851 ? Type::getInt8PtrTy(*DAG.getContext(),
7852 256)
7853 : Type::getInt32PtrTy(*DAG.getContext(),
7854 257));
7855
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007856 SDValue TlsArray = Subtarget->is64Bit() ? DAG.getIntPtrConstant(0x58) :
7857 (Subtarget->isTargetMingw() ? DAG.getIntPtrConstant(0x2C) :
7858 DAG.getExternalSymbol("_tls_array", getPointerTy()));
7859
7860 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain, TlsArray,
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007861 MachinePointerInfo(Ptr),
7862 false, false, false, 0);
7863
7864 // Load the _tls_index variable
7865 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7866 if (Subtarget->is64Bit())
7867 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7868 IDX, MachinePointerInfo(), MVT::i32,
7869 false, false, 0);
7870 else
7871 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7872 false, false, false, 0);
7873
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007874 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007875 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007876 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7877
7878 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7879 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7880 false, false, false, 0);
7881
7882 // Get the offset of start of .tls section
7883 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7884 GA->getValueType(0),
7885 GA->getOffset(), X86II::MO_SECREL);
7886 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7887
7888 // The address of the thread local variable is the add of the thread
7889 // pointer with the offset of the variable.
7890 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007891 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007892
David Blaikie4d6ccb52012-01-20 21:51:11 +00007893 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007894}
7895
Chad Rosierb90d2a92012-01-03 23:19:12 +00007896/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7897/// and take a 2 x i32 value to shift plus a shift amount.
7898SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007899 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007900 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007901 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007902 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007903 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007904 SDValue ShOpLo = Op.getOperand(0);
7905 SDValue ShOpHi = Op.getOperand(1);
7906 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007907 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007908 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007909 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007910
Dan Gohman475871a2008-07-27 21:46:04 +00007911 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007912 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007913 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7914 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007915 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007916 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7917 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007918 }
Evan Chenge3413162006-01-09 18:33:28 +00007919
Owen Anderson825b72b2009-08-11 20:47:22 +00007920 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7921 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007922 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007923 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007924
Dan Gohman475871a2008-07-27 21:46:04 +00007925 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007926 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007927 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7928 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007929
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007930 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007931 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7932 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007933 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007934 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7935 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007936 }
7937
Dan Gohman475871a2008-07-27 21:46:04 +00007938 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007939 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007940}
Evan Chenga3195e82006-01-12 22:54:21 +00007941
Dan Gohmand858e902010-04-17 15:26:15 +00007942SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7943 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007944 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007945
Dale Johannesen0488fb62010-09-30 23:57:10 +00007946 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007947 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007948
Owen Anderson825b72b2009-08-11 20:47:22 +00007949 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007950 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007951
Eli Friedman36df4992009-05-27 00:47:34 +00007952 // These are really Legal; return the operand so the caller accepts it as
7953 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007954 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007955 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007956 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007957 Subtarget->is64Bit()) {
7958 return Op;
7959 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007960
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007961 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007962 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007963 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007964 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007965 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007966 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007967 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007968 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007969 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007970 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7971}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007972
Owen Andersone50ed302009-08-10 22:56:29 +00007973SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007974 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007975 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007976 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007977 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007978 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007979 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007980 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007981 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007982 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007983 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007984
Chris Lattner492a43e2010-09-22 01:28:21 +00007985 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007986
Stuart Hastings84be9582011-06-02 15:57:11 +00007987 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7988 MachineMemOperand *MMO;
7989 if (FI) {
7990 int SSFI = FI->getIndex();
7991 MMO =
7992 DAG.getMachineFunction()
7993 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7994 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7995 } else {
7996 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7997 StackSlot = StackSlot.getOperand(1);
7998 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007999 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008000 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
8001 X86ISD::FILD, DL,
8002 Tys, Ops, array_lengthof(Ops),
8003 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008004
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008005 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008006 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008007 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008008
8009 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
8010 // shouldn't be necessary except that RFP cannot be live across
8011 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008012 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008013 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
8014 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008015 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00008016 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008017 SDValue Ops[] = {
8018 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
8019 };
Chris Lattner492a43e2010-09-22 01:28:21 +00008020 MachineMemOperand *MMO =
8021 DAG.getMachineFunction()
8022 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008023 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008024
Chris Lattner492a43e2010-09-22 01:28:21 +00008025 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
8026 Ops, array_lengthof(Ops),
8027 Op.getValueType(), MMO);
8028 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008029 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008030 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008031 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008032
Evan Cheng0db9fe62006-04-25 20:13:52 +00008033 return Result;
8034}
8035
Bill Wendling8b8a6362009-01-17 03:56:04 +00008036// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008037SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8038 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008039 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008040 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008041 movq %rax, %xmm0
8042 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8043 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8044 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008045 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008046 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008047 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008048 addpd %xmm1, %xmm0
8049 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008050 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008051
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008052 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008053 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008054
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008055 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008056 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8057 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008058 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008059
Chris Lattner97484792012-01-25 09:56:22 +00008060 SmallVector<Constant*,2> CV1;
8061 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008062 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8063 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008064 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008065 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8066 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008067 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008068 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008069
Bill Wendling397ae212012-01-05 02:13:20 +00008070 // Load the 64-bit value into an XMM register.
8071 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8072 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008073 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008074 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008075 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008076 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8077 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8078 CLod0);
8079
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008081 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008082 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008083 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008084 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008085 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008086
Craig Topperd0a31172012-01-10 06:37:29 +00008087 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008088 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8089 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8090 } else {
8091 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8092 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8093 S2F, 0x4E, DAG);
8094 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8095 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8096 Sub);
8097 }
8098
8099 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008100 DAG.getIntPtrConstant(0));
8101}
8102
Bill Wendling8b8a6362009-01-17 03:56:04 +00008103// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008104SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8105 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008106 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008107 // FP constant to bias correct the final result.
8108 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008109 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008110
8111 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008112 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008113 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008114
Eli Friedmanf3704762011-08-29 21:15:46 +00008115 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008116 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008117
Owen Anderson825b72b2009-08-11 20:47:22 +00008118 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008119 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008120 DAG.getIntPtrConstant(0));
8121
8122 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008123 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008124 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008125 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008126 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008127 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008128 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008129 MVT::v2f64, Bias)));
8130 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008131 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008132 DAG.getIntPtrConstant(0));
8133
8134 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008135 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008136
8137 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008138 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008139
Craig Topper69947b92012-04-23 06:57:04 +00008140 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008141 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008142 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008143 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008144 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008145
8146 // Handle final rounding.
8147 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008148}
8149
Michael Liaoa7554632012-10-23 17:36:08 +00008150SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8151 SelectionDAG &DAG) const {
8152 SDValue N0 = Op.getOperand(0);
8153 EVT SVT = N0.getValueType();
8154 DebugLoc dl = Op.getDebugLoc();
8155
8156 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8157 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8158 "Custom UINT_TO_FP is not supported!");
8159
Craig Topperb99bafe2013-01-21 06:21:54 +00008160 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8161 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008162 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8163 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8164}
8165
Dan Gohmand858e902010-04-17 15:26:15 +00008166SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8167 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008168 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008169 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008170
Michael Liaoa7554632012-10-23 17:36:08 +00008171 if (Op.getValueType().isVector())
8172 return lowerUINT_TO_FP_vec(Op, DAG);
8173
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008174 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008175 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8176 // the optimization here.
8177 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008178 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008179
Owen Andersone50ed302009-08-10 22:56:29 +00008180 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008181 EVT DstVT = Op.getValueType();
8182 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008183 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008184 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008185 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008186 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008187 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008188
8189 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008190 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008191 if (SrcVT == MVT::i32) {
8192 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8193 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8194 getPointerTy(), StackSlot, WordOff);
8195 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008196 StackSlot, MachinePointerInfo(),
8197 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008198 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008199 OffsetSlot, MachinePointerInfo(),
8200 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008201 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8202 return Fild;
8203 }
8204
8205 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8206 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008207 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008208 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008209 // For i64 source, we need to add the appropriate power of 2 if the input
8210 // was negative. This is the same as the optimization in
8211 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8212 // we must be careful to do the computation in x87 extended precision, not
8213 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008214 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8215 MachineMemOperand *MMO =
8216 DAG.getMachineFunction()
8217 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8218 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008219
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008220 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8221 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008222 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8223 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008224
8225 APInt FF(32, 0x5F800000ULL);
8226
8227 // Check whether the sign bit is set.
8228 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8229 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8230 ISD::SETLT);
8231
8232 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8233 SDValue FudgePtr = DAG.getConstantPool(
8234 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8235 getPointerTy());
8236
8237 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8238 SDValue Zero = DAG.getIntPtrConstant(0);
8239 SDValue Four = DAG.getIntPtrConstant(4);
8240 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8241 Zero, Four);
8242 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8243
8244 // Load the value out, extending it from f32 to f80.
8245 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008246 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008247 FudgePtr, MachinePointerInfo::getConstantPool(),
8248 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008249 // Extend everything to 80 bits to force it to be done on x87.
8250 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8251 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008252}
8253
Craig Topperb99bafe2013-01-21 06:21:54 +00008254std::pair<SDValue,SDValue>
8255X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8256 bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008257 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008258
Owen Andersone50ed302009-08-10 22:56:29 +00008259 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008260
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008261 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008262 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8263 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008264 }
8265
Owen Anderson825b72b2009-08-11 20:47:22 +00008266 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8267 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008268 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008269
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008270 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008271 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008272 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008273 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008274 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008275 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008276 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008277 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008278
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008279 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8280 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008281 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008282 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008283 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008284 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008285
Evan Cheng0db9fe62006-04-25 20:13:52 +00008286 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008287 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8288 Opc = X86ISD::WIN_FTOL;
8289 else
8290 switch (DstTy.getSimpleVT().SimpleTy) {
8291 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8292 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8293 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8294 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8295 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008296
Dan Gohman475871a2008-07-27 21:46:04 +00008297 SDValue Chain = DAG.getEntryNode();
8298 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008299 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008300 // FIXME This causes a redundant load/store if the SSE-class value is already
8301 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008302 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008303 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008304 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008305 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008306 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008307 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008308 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008309 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008310 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008311
Chris Lattner492a43e2010-09-22 01:28:21 +00008312 MachineMemOperand *MMO =
8313 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8314 MachineMemOperand::MOLoad, MemSize, MemSize);
8315 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8316 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008317 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008318 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008319 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8320 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008321
Chris Lattner07290932010-09-22 01:05:16 +00008322 MachineMemOperand *MMO =
8323 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8324 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008325
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008326 if (Opc != X86ISD::WIN_FTOL) {
8327 // Build the FP_TO_INT*_IN_MEM
8328 SDValue Ops[] = { Chain, Value, StackSlot };
8329 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8330 Ops, 3, DstTy, MMO);
8331 return std::make_pair(FIST, StackSlot);
8332 } else {
8333 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8334 DAG.getVTList(MVT::Other, MVT::Glue),
8335 Chain, Value);
8336 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8337 MVT::i32, ftol.getValue(1));
8338 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8339 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008340 SDValue Ops[] = { eax, edx };
8341 SDValue pair = IsReplace
8342 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8343 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008344 return std::make_pair(pair, SDValue());
8345 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008346}
8347
Nadav Rotem0509db22012-12-28 05:45:24 +00008348static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8349 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008350 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008351 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008352 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008353 DebugLoc dl = Op->getDebugLoc();
8354
8355 // Optimize vectors in AVX mode:
8356 //
8357 // v8i16 -> v8i32
8358 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8359 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8360 // Concat upper and lower parts.
8361 //
8362 // v4i32 -> v4i64
8363 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8364 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8365 // Concat upper and lower parts.
8366 //
8367
8368 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8369 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8370 return SDValue();
8371
8372 if (Subtarget->hasInt256())
8373 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8374
8375 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8376 SDValue Undef = DAG.getUNDEF(InVT);
8377 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8378 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8379 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8380
Craig Toppera080daf2013-01-20 21:50:27 +00008381 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008382 VT.getVectorNumElements()/2);
8383
8384 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8385 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8386
8387 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8388}
8389
8390SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8391 SelectionDAG &DAG) const {
8392 if (Subtarget->hasFp256()) {
8393 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8394 if (Res.getNode())
8395 return Res;
8396 }
8397
8398 return SDValue();
8399}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008400SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8401 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008402 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008403 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008404 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008405 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008406
Nadav Rotem0509db22012-12-28 05:45:24 +00008407 if (Subtarget->hasFp256()) {
8408 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8409 if (Res.getNode())
8410 return Res;
8411 }
8412
Michael Liaoa7554632012-10-23 17:36:08 +00008413 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8414 VT.getVectorNumElements() != SVT.getVectorNumElements())
8415 return SDValue();
8416
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008417 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008418
8419 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008420 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008421 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8422
8423 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8424 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8425 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008426 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8427 DAG.getUNDEF(MVT::v8i16),
8428 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008429
8430 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8431}
8432
Craig Topperd713c0f2013-01-20 21:34:37 +00008433SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008434 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008435 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008436 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008437 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008438
Nadav Rotem3c22a442012-12-27 07:45:10 +00008439 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8440 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8441 if (Subtarget->hasInt256()) {
8442 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8443 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8444 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8445 ShufMask);
8446 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8447 DAG.getIntPtrConstant(0));
8448 }
8449
8450 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8451 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8452 DAG.getIntPtrConstant(0));
8453 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8454 DAG.getIntPtrConstant(2));
8455
8456 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8457 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8458
8459 // The PSHUFD mask:
8460 static const int ShufMask1[] = {0, 2, 0, 0};
8461 SDValue Undef = DAG.getUNDEF(VT);
8462 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8463 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8464
8465 // The MOVLHPS mask:
8466 static const int ShufMask2[] = {0, 1, 4, 5};
8467 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8468 }
8469
8470 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8471 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8472 if (Subtarget->hasInt256()) {
8473 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8474
8475 SmallVector<SDValue,32> pshufbMask;
8476 for (unsigned i = 0; i < 2; ++i) {
8477 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8478 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8479 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8480 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8481 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8482 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8483 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8484 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8485 for (unsigned j = 0; j < 8; ++j)
8486 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8487 }
8488 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8489 &pshufbMask[0], 32);
8490 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8491 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8492
8493 static const int ShufMask[] = {0, 2, -1, -1};
8494 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8495 &ShufMask[0]);
8496 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8497 DAG.getIntPtrConstant(0));
8498 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8499 }
8500
8501 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8502 DAG.getIntPtrConstant(0));
8503
8504 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8505 DAG.getIntPtrConstant(4));
8506
8507 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8508 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8509
8510 // The PSHUFB mask:
8511 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8512 -1, -1, -1, -1, -1, -1, -1, -1};
8513
8514 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8515 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8516 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8517
8518 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8519 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8520
8521 // The MOVLHPS Mask:
8522 static const int ShufMask2[] = {0, 1, 4, 5};
8523 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8524 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8525 }
8526
8527 // Handle truncation of V256 to V128 using shuffles.
8528 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008529 return SDValue();
8530
Nadav Rotem3c22a442012-12-27 07:45:10 +00008531 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8532 "Invalid op");
8533 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008534
8535 unsigned NumElems = VT.getVectorNumElements();
8536 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8537 NumElems * 2);
8538
Michael Liaobedcbd42012-10-16 18:14:11 +00008539 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8540 // Prepare truncation shuffle mask
8541 for (unsigned i = 0; i != NumElems; ++i)
8542 MaskVec[i] = i * 2;
8543 SDValue V = DAG.getVectorShuffle(NVT, DL,
8544 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8545 DAG.getUNDEF(NVT), &MaskVec[0]);
8546 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8547 DAG.getIntPtrConstant(0));
8548}
8549
Dan Gohmand858e902010-04-17 15:26:15 +00008550SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8551 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008552 MVT VT = Op.getValueType().getSimpleVT();
8553 if (VT.isVector()) {
8554 if (VT == MVT::v8i16)
8555 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), VT,
Michael Liaobedcbd42012-10-16 18:14:11 +00008556 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8557 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008558 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008559 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008560
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008561 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8562 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008563 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008564 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8565 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008566
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008567 if (StackSlot.getNode())
8568 // Load the result.
8569 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8570 FIST, StackSlot, MachinePointerInfo(),
8571 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008572
8573 // The node is the result.
8574 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008575}
8576
Dan Gohmand858e902010-04-17 15:26:15 +00008577SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8578 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008579 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8580 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008581 SDValue FIST = Vals.first, StackSlot = Vals.second;
8582 assert(FIST.getNode() && "Unexpected failure");
8583
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008584 if (StackSlot.getNode())
8585 // Load the result.
8586 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8587 FIST, StackSlot, MachinePointerInfo(),
8588 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008589
8590 // The node is the result.
8591 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008592}
8593
Craig Topperb84b4232013-01-21 06:13:28 +00008594static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Michael Liao9d796db2012-10-10 16:32:15 +00008595 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008596 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008597 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008598 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008599
8600 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8601
8602 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8603 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8604 In, DAG.getUNDEF(SVT)));
8605}
8606
Craig Topper43620672012-09-08 07:31:51 +00008607SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008608 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008609 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008610 MVT VT = Op.getValueType().getSimpleVT();
8611 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008612 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8613 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008614 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008615 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008616 }
Craig Topper43620672012-09-08 07:31:51 +00008617 Constant *C;
8618 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008619 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8620 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008621 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008622 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8623 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008624 C = ConstantVector::getSplat(NumElts, C);
8625 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8626 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008627 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008628 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008629 false, false, false, Alignment);
8630 if (VT.isVector()) {
8631 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8632 return DAG.getNode(ISD::BITCAST, dl, VT,
8633 DAG.getNode(ISD::AND, dl, ANDVT,
8634 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8635 Op.getOperand(0)),
8636 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8637 }
Dale Johannesenace16102009-02-03 19:33:06 +00008638 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008639}
8640
Dan Gohmand858e902010-04-17 15:26:15 +00008641SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008642 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008643 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008644 MVT VT = Op.getValueType().getSimpleVT();
8645 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008646 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8647 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008648 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008649 NumElts = VT.getVectorNumElements();
8650 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008651 Constant *C;
8652 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008653 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8654 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008655 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008656 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8657 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008658 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008659 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8660 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008661 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008662 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008663 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008664 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008665 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008666 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008667 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008668 DAG.getNode(ISD::BITCAST, dl, XORVT,
8669 Op.getOperand(0)),
8670 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008671 }
Craig Topper69947b92012-04-23 06:57:04 +00008672
8673 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008674}
8675
Dan Gohmand858e902010-04-17 15:26:15 +00008676SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008677 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008678 SDValue Op0 = Op.getOperand(0);
8679 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008680 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008681 MVT VT = Op.getValueType().getSimpleVT();
8682 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008683
8684 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008685 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008686 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008687 SrcVT = VT;
8688 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008689 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008690 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008691 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008692 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008693 }
8694
8695 // At this point the operands and the result should have the same
8696 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008697
Evan Cheng68c47cb2007-01-05 07:55:56 +00008698 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008699 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008700 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008701 const fltSemantics &Sem = APFloat::IEEEdouble;
8702 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8703 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008704 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008705 const fltSemantics &Sem = APFloat::IEEEsingle;
8706 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
8707 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8708 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8709 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008710 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008711 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008712 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008713 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008714 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008715 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008716 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008717
8718 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008719 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008720 // Op0 is MVT::f32, Op1 is MVT::f64.
8721 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8722 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8723 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008724 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008725 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008726 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008727 }
8728
Evan Cheng73d6cf12007-01-05 21:37:56 +00008729 // Clear first operand sign bit.
8730 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008731 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008732 const fltSemantics &Sem = APFloat::IEEEdouble;
8733 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8734 APInt(64, ~(1ULL << 63)))));
8735 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008736 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008737 const fltSemantics &Sem = APFloat::IEEEsingle;
8738 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8739 APInt(32, ~(1U << 31)))));
8740 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8741 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8742 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008743 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008744 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008745 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008746 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008747 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008748 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008749 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008750
8751 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008752 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008753}
8754
Craig Topper55b24052012-09-11 06:15:32 +00008755static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008756 SDValue N0 = Op.getOperand(0);
8757 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008758 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008759
8760 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8761 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8762 DAG.getConstant(1, VT));
8763 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8764}
8765
Michael Liaof966e4e2012-09-13 20:24:54 +00008766// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8767//
Craig Topperb99bafe2013-01-21 06:21:54 +00008768SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
8769 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00008770 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8771
8772 if (!Subtarget->hasSSE41())
8773 return SDValue();
8774
8775 if (!Op->hasOneUse())
8776 return SDValue();
8777
8778 SDNode *N = Op.getNode();
8779 DebugLoc DL = N->getDebugLoc();
8780
8781 SmallVector<SDValue, 8> Opnds;
8782 DenseMap<SDValue, unsigned> VecInMap;
8783 EVT VT = MVT::Other;
8784
8785 // Recognize a special case where a vector is casted into wide integer to
8786 // test all 0s.
8787 Opnds.push_back(N->getOperand(0));
8788 Opnds.push_back(N->getOperand(1));
8789
8790 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8791 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8792 // BFS traverse all OR'd operands.
8793 if (I->getOpcode() == ISD::OR) {
8794 Opnds.push_back(I->getOperand(0));
8795 Opnds.push_back(I->getOperand(1));
8796 // Re-evaluate the number of nodes to be traversed.
8797 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8798 continue;
8799 }
8800
8801 // Quit if a non-EXTRACT_VECTOR_ELT
8802 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8803 return SDValue();
8804
8805 // Quit if without a constant index.
8806 SDValue Idx = I->getOperand(1);
8807 if (!isa<ConstantSDNode>(Idx))
8808 return SDValue();
8809
8810 SDValue ExtractedFromVec = I->getOperand(0);
8811 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8812 if (M == VecInMap.end()) {
8813 VT = ExtractedFromVec.getValueType();
8814 // Quit if not 128/256-bit vector.
8815 if (!VT.is128BitVector() && !VT.is256BitVector())
8816 return SDValue();
8817 // Quit if not the same type.
8818 if (VecInMap.begin() != VecInMap.end() &&
8819 VT != VecInMap.begin()->first.getValueType())
8820 return SDValue();
8821 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8822 }
8823 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8824 }
8825
8826 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008827 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008828
8829 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8830 SmallVector<SDValue, 8> VecIns;
8831
8832 for (DenseMap<SDValue, unsigned>::const_iterator
8833 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8834 // Quit if not all elements are used.
8835 if (I->second != FullMask)
8836 return SDValue();
8837 VecIns.push_back(I->first);
8838 }
8839
8840 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8841
8842 // Cast all vectors into TestVT for PTEST.
8843 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8844 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8845
8846 // If more than one full vectors are evaluated, OR them first before PTEST.
8847 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8848 // Each iteration will OR 2 nodes and append the result until there is only
8849 // 1 node left, i.e. the final OR'd value of all vectors.
8850 SDValue LHS = VecIns[Slot];
8851 SDValue RHS = VecIns[Slot + 1];
8852 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8853 }
8854
8855 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8856 VecIns.back(), VecIns.back());
8857}
8858
Dan Gohman076aee32009-03-04 19:44:21 +00008859/// Emit nodes that will be selected as "test Op0,Op0", or something
8860/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008861SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008862 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008863 DebugLoc dl = Op.getDebugLoc();
8864
Dan Gohman31125812009-03-07 01:58:32 +00008865 // CF and OF aren't always set the way we want. Determine which
8866 // of these we need.
8867 bool NeedCF = false;
8868 bool NeedOF = false;
8869 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008870 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008871 case X86::COND_A: case X86::COND_AE:
8872 case X86::COND_B: case X86::COND_BE:
8873 NeedCF = true;
8874 break;
8875 case X86::COND_G: case X86::COND_GE:
8876 case X86::COND_L: case X86::COND_LE:
8877 case X86::COND_O: case X86::COND_NO:
8878 NeedOF = true;
8879 break;
Dan Gohman31125812009-03-07 01:58:32 +00008880 }
8881
Dan Gohman076aee32009-03-04 19:44:21 +00008882 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008883 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8884 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008885 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8886 // Emit a CMP with 0, which is the TEST pattern.
8887 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8888 DAG.getConstant(0, Op.getValueType()));
8889
8890 unsigned Opcode = 0;
8891 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008892
8893 // Truncate operations may prevent the merge of the SETCC instruction
8894 // and the arithmetic intruction before it. Attempt to truncate the operands
8895 // of the arithmetic instruction and use a reduced bit-width instruction.
8896 bool NeedTruncation = false;
8897 SDValue ArithOp = Op;
8898 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8899 SDValue Arith = Op->getOperand(0);
8900 // Both the trunc and the arithmetic op need to have one user each.
8901 if (Arith->hasOneUse())
8902 switch (Arith.getOpcode()) {
8903 default: break;
8904 case ISD::ADD:
8905 case ISD::SUB:
8906 case ISD::AND:
8907 case ISD::OR:
8908 case ISD::XOR: {
8909 NeedTruncation = true;
8910 ArithOp = Arith;
8911 }
8912 }
8913 }
8914
8915 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8916 // which may be the result of a CAST. We use the variable 'Op', which is the
8917 // non-casted variable when we check for possible users.
8918 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008919 case ISD::ADD:
8920 // Due to an isel shortcoming, be conservative if this add is likely to be
8921 // selected as part of a load-modify-store instruction. When the root node
8922 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8923 // uses of other nodes in the match, such as the ADD in this case. This
8924 // leads to the ADD being left around and reselected, with the result being
8925 // two adds in the output. Alas, even if none our users are stores, that
8926 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8927 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8928 // climbing the DAG back to the root, and it doesn't seem to be worth the
8929 // effort.
8930 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008931 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8932 if (UI->getOpcode() != ISD::CopyToReg &&
8933 UI->getOpcode() != ISD::SETCC &&
8934 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008935 goto default_case;
8936
8937 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008938 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008939 // An add of one will be selected as an INC.
8940 if (C->getAPIntValue() == 1) {
8941 Opcode = X86ISD::INC;
8942 NumOperands = 1;
8943 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008944 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008945
8946 // An add of negative one (subtract of one) will be selected as a DEC.
8947 if (C->getAPIntValue().isAllOnesValue()) {
8948 Opcode = X86ISD::DEC;
8949 NumOperands = 1;
8950 break;
8951 }
Dan Gohman076aee32009-03-04 19:44:21 +00008952 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008953
8954 // Otherwise use a regular EFLAGS-setting add.
8955 Opcode = X86ISD::ADD;
8956 NumOperands = 2;
8957 break;
8958 case ISD::AND: {
8959 // If the primary and result isn't used, don't bother using X86ISD::AND,
8960 // because a TEST instruction will be better.
8961 bool NonFlagUse = false;
8962 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8963 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8964 SDNode *User = *UI;
8965 unsigned UOpNo = UI.getOperandNo();
8966 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8967 // Look pass truncate.
8968 UOpNo = User->use_begin().getOperandNo();
8969 User = *User->use_begin();
8970 }
8971
8972 if (User->getOpcode() != ISD::BRCOND &&
8973 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008974 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008975 NonFlagUse = true;
8976 break;
8977 }
Dan Gohman076aee32009-03-04 19:44:21 +00008978 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008979
8980 if (!NonFlagUse)
8981 break;
8982 }
8983 // FALL THROUGH
8984 case ISD::SUB:
8985 case ISD::OR:
8986 case ISD::XOR:
8987 // Due to the ISEL shortcoming noted above, be conservative if this op is
8988 // likely to be selected as part of a load-modify-store instruction.
8989 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8990 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8991 if (UI->getOpcode() == ISD::STORE)
8992 goto default_case;
8993
8994 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008995 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008996 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008997 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008998 case ISD::XOR: Opcode = X86ISD::XOR; break;
8999 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00009000 case ISD::OR: {
9001 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
9002 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
9003 if (EFLAGS.getNode())
9004 return EFLAGS;
9005 }
9006 Opcode = X86ISD::OR;
9007 break;
9008 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009009 }
9010
9011 NumOperands = 2;
9012 break;
9013 case X86ISD::ADD:
9014 case X86ISD::SUB:
9015 case X86ISD::INC:
9016 case X86ISD::DEC:
9017 case X86ISD::OR:
9018 case X86ISD::XOR:
9019 case X86ISD::AND:
9020 return SDValue(Op.getNode(), 1);
9021 default:
9022 default_case:
9023 break;
Dan Gohman076aee32009-03-04 19:44:21 +00009024 }
9025
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009026 // If we found that truncation is beneficial, perform the truncation and
9027 // update 'Op'.
9028 if (NeedTruncation) {
9029 EVT VT = Op.getValueType();
9030 SDValue WideVal = Op->getOperand(0);
9031 EVT WideVT = WideVal.getValueType();
9032 unsigned ConvertedOp = 0;
9033 // Use a target machine opcode to prevent further DAGCombine
9034 // optimizations that may separate the arithmetic operations
9035 // from the setcc node.
9036 switch (WideVal.getOpcode()) {
9037 default: break;
9038 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
9039 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
9040 case ISD::AND: ConvertedOp = X86ISD::AND; break;
9041 case ISD::OR: ConvertedOp = X86ISD::OR; break;
9042 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
9043 }
9044
9045 if (ConvertedOp) {
9046 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9047 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9048 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9049 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9050 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9051 }
9052 }
9053 }
9054
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009055 if (Opcode == 0)
9056 // Emit a CMP with 0, which is the TEST pattern.
9057 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9058 DAG.getConstant(0, Op.getValueType()));
9059
9060 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9061 SmallVector<SDValue, 4> Ops;
9062 for (unsigned i = 0; i != NumOperands; ++i)
9063 Ops.push_back(Op.getOperand(i));
9064
9065 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9066 DAG.ReplaceAllUsesWith(Op, New);
9067 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009068}
9069
9070/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9071/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009072SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009073 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009074 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9075 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009076 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009077
9078 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009079 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9080 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9081 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9082 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9083 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9084 Op0, Op1);
9085 return SDValue(Sub.getNode(), 1);
9086 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009087 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009088}
9089
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009090/// Convert a comparison if required by the subtarget.
9091SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9092 SelectionDAG &DAG) const {
9093 // If the subtarget does not support the FUCOMI instruction, floating-point
9094 // comparisons have to be converted.
9095 if (Subtarget->hasCMov() ||
9096 Cmp.getOpcode() != X86ISD::CMP ||
9097 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9098 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9099 return Cmp;
9100
9101 // The instruction selector will select an FUCOM instruction instead of
9102 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9103 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9104 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9105 DebugLoc dl = Cmp.getDebugLoc();
9106 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9107 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9108 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9109 DAG.getConstant(8, MVT::i8));
9110 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9111 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9112}
9113
Evan Cheng4e544802012-12-05 00:10:38 +00009114static bool isAllOnes(SDValue V) {
9115 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9116 return C && C->isAllOnesValue();
9117}
9118
Evan Chengd40d03e2010-01-06 19:38:29 +00009119/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9120/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009121SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9122 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009123 SDValue Op0 = And.getOperand(0);
9124 SDValue Op1 = And.getOperand(1);
9125 if (Op0.getOpcode() == ISD::TRUNCATE)
9126 Op0 = Op0.getOperand(0);
9127 if (Op1.getOpcode() == ISD::TRUNCATE)
9128 Op1 = Op1.getOperand(0);
9129
Evan Chengd40d03e2010-01-06 19:38:29 +00009130 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009131 if (Op1.getOpcode() == ISD::SHL)
9132 std::swap(Op0, Op1);
9133 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009134 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9135 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009136 // If we looked past a truncate, check that it's only truncating away
9137 // known zeros.
9138 unsigned BitWidth = Op0.getValueSizeInBits();
9139 unsigned AndBitWidth = And.getValueSizeInBits();
9140 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009141 APInt Zeros, Ones;
9142 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009143 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9144 return SDValue();
9145 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009146 LHS = Op1;
9147 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009148 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009149 } else if (Op1.getOpcode() == ISD::Constant) {
9150 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009151 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009152 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009153
9154 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009155 LHS = AndLHS.getOperand(0);
9156 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009157 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009158
9159 // Use BT if the immediate can't be encoded in a TEST instruction.
9160 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9161 LHS = AndLHS;
9162 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9163 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009164 }
Evan Cheng0488db92007-09-25 01:57:46 +00009165
Evan Chengd40d03e2010-01-06 19:38:29 +00009166 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009167 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9168 // the condition code later.
9169 bool Invert = false;
9170 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9171 Invert = true;
9172 LHS = LHS.getOperand(0);
9173 }
9174
Evan Chenge5b51ac2010-04-17 06:13:15 +00009175 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009176 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009177 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009178 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009179 // Also promote i16 to i32 for performance / code size reason.
9180 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009181 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009182 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009183
Evan Chengd40d03e2010-01-06 19:38:29 +00009184 // If the operand types disagree, extend the shift amount to match. Since
9185 // BT ignores high bits (like shifts) we can use anyextend.
9186 if (LHS.getValueType() != RHS.getValueType())
9187 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009188
Evan Chengd40d03e2010-01-06 19:38:29 +00009189 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009190 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9191 // Flip the condition if the LHS was a not instruction
9192 if (Invert)
9193 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009194 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9195 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009196 }
9197
Evan Cheng54de3ea2010-01-05 06:52:31 +00009198 return SDValue();
9199}
9200
Craig Topper89af15e2011-09-18 08:03:58 +00009201// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009202// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009203static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009204 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009205
Craig Topper7a9a28b2012-08-12 02:23:29 +00009206 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009207 "Unsupported value type for operation");
9208
Craig Topper66ddd152012-04-27 22:54:43 +00009209 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009210 DebugLoc dl = Op.getDebugLoc();
9211 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009212
9213 // Extract the LHS vectors
9214 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009215 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9216 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009217
9218 // Extract the RHS vectors
9219 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009220 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9221 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009222
9223 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009224 MVT EltVT = VT.getVectorElementType();
9225 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009226 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9227 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9228 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9229}
9230
Craig Topper26827f32013-01-20 09:02:22 +00009231static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9232 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009233 SDValue Cond;
9234 SDValue Op0 = Op.getOperand(0);
9235 SDValue Op1 = Op.getOperand(1);
9236 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009237 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009238 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009239 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009240 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009241
9242 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009243#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009244 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009245 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9246#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009247
Craig Topper523908d2012-08-13 02:34:03 +00009248 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009249 bool Swap = false;
9250
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009251 // SSE Condition code mapping:
9252 // 0 - EQ
9253 // 1 - LT
9254 // 2 - LE
9255 // 3 - UNORD
9256 // 4 - NEQ
9257 // 5 - NLT
9258 // 6 - NLE
9259 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009260 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009261 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009262 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009263 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009264 case ISD::SETOGT:
9265 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009266 case ISD::SETLT:
9267 case ISD::SETOLT: SSECC = 1; break;
9268 case ISD::SETOGE:
9269 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009270 case ISD::SETLE:
9271 case ISD::SETOLE: SSECC = 2; break;
9272 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009273 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009274 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009275 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009276 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009277 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009278 case ISD::SETUGT: SSECC = 6; break;
9279 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009280 case ISD::SETUEQ:
9281 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009282 }
9283 if (Swap)
9284 std::swap(Op0, Op1);
9285
Nate Begemanfb8ead02008-07-25 19:05:58 +00009286 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009287 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009288 unsigned CC0, CC1;
9289 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009290 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009291 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9292 } else {
9293 assert(SetCCOpcode == ISD::SETONE);
9294 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009295 }
Craig Topper523908d2012-08-13 02:34:03 +00009296
9297 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9298 DAG.getConstant(CC0, MVT::i8));
9299 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9300 DAG.getConstant(CC1, MVT::i8));
9301 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009302 }
9303 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009304 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9305 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009306 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009307
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009308 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009309 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009310 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009311
Nate Begeman30a0de92008-07-17 16:51:19 +00009312 // We are handling one of the integer comparisons here. Since SSE only has
9313 // GT and EQ comparisons for integer, swapping operands and multiple
9314 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009315 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009316 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009317
Nate Begeman30a0de92008-07-17 16:51:19 +00009318 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009319 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009320 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009321 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009322 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009323 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009324 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009325 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009326 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009327 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009328 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009329 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009330 }
9331 if (Swap)
9332 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009333
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009334 // Check that the operation in question is available (most are plain SSE2,
9335 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009336 if (VT == MVT::v2i64) {
9337 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9338 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009339 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9340 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009341 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009342 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9343
9344 // First cast everything to the right type,
9345 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9346 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9347
9348 // Do the compare.
9349 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9350
9351 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009352 const int Mask[] = { 1, 0, 3, 2 };
9353 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9354 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009355
9356 if (Invert)
9357 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9358
9359 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9360 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009361 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009362
Nate Begeman30a0de92008-07-17 16:51:19 +00009363 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9364 // bits of the inputs before performing those operations.
9365 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009366 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009367 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9368 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009369 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009370 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9371 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009372 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9373 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009374 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009375
Dale Johannesenace16102009-02-03 19:33:06 +00009376 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009377
9378 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009379 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009380 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009381
Nate Begeman30a0de92008-07-17 16:51:19 +00009382 return Result;
9383}
Evan Cheng0488db92007-09-25 01:57:46 +00009384
Craig Topper26827f32013-01-20 09:02:22 +00009385SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9386
9387 MVT VT = Op.getValueType().getSimpleVT();
9388
9389 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9390
9391 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9392 SDValue Op0 = Op.getOperand(0);
9393 SDValue Op1 = Op.getOperand(1);
9394 DebugLoc dl = Op.getDebugLoc();
9395 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9396
9397 // Optimize to BT if possible.
9398 // Lower (X & (1 << N)) == 0 to BT(X, N).
9399 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9400 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9401 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9402 Op1.getOpcode() == ISD::Constant &&
9403 cast<ConstantSDNode>(Op1)->isNullValue() &&
9404 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9405 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9406 if (NewSetCC.getNode())
9407 return NewSetCC;
9408 }
9409
9410 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9411 // these.
9412 if (Op1.getOpcode() == ISD::Constant &&
9413 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9414 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9415 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9416
9417 // If the input is a setcc, then reuse the input setcc or use a new one with
9418 // the inverted condition.
9419 if (Op0.getOpcode() == X86ISD::SETCC) {
9420 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9421 bool Invert = (CC == ISD::SETNE) ^
9422 cast<ConstantSDNode>(Op1)->isNullValue();
9423 if (!Invert) return Op0;
9424
9425 CCode = X86::GetOppositeBranchCondition(CCode);
9426 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9427 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9428 }
9429 }
9430
9431 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9432 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9433 if (X86CC == X86::COND_INVALID)
9434 return SDValue();
9435
9436 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9437 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9438 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9439 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9440}
9441
Evan Cheng370e5342008-12-03 08:38:43 +00009442// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009443static bool isX86LogicalCmp(SDValue Op) {
9444 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009445 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9446 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009447 return true;
9448 if (Op.getResNo() == 1 &&
9449 (Opc == X86ISD::ADD ||
9450 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009451 Opc == X86ISD::ADC ||
9452 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009453 Opc == X86ISD::SMUL ||
9454 Opc == X86ISD::UMUL ||
9455 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009456 Opc == X86ISD::DEC ||
9457 Opc == X86ISD::OR ||
9458 Opc == X86ISD::XOR ||
9459 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009460 return true;
9461
Chris Lattner9637d5b2010-12-05 07:49:54 +00009462 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9463 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009464
Dan Gohman076aee32009-03-04 19:44:21 +00009465 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009466}
9467
Chris Lattnera2b56002010-12-05 01:23:24 +00009468static bool isZero(SDValue V) {
9469 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9470 return C && C->isNullValue();
9471}
9472
Evan Chengb64dd5f2012-08-07 22:21:00 +00009473static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9474 if (V.getOpcode() != ISD::TRUNCATE)
9475 return false;
9476
9477 SDValue VOp0 = V.getOperand(0);
9478 unsigned InBits = VOp0.getValueSizeInBits();
9479 unsigned Bits = V.getValueSizeInBits();
9480 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9481}
9482
Dan Gohmand858e902010-04-17 15:26:15 +00009483SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009484 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009485 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009486 SDValue Op1 = Op.getOperand(1);
9487 SDValue Op2 = Op.getOperand(2);
9488 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009489 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009490
Dan Gohman1a492952009-10-20 16:22:37 +00009491 if (Cond.getOpcode() == ISD::SETCC) {
9492 SDValue NewCond = LowerSETCC(Cond, DAG);
9493 if (NewCond.getNode())
9494 Cond = NewCond;
9495 }
Evan Cheng734503b2006-09-11 02:19:56 +00009496
Chris Lattnera2b56002010-12-05 01:23:24 +00009497 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009498 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009499 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009500 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009501 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009502 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9503 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009504 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009505
Chris Lattnera2b56002010-12-05 01:23:24 +00009506 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009507
9508 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009509 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9510 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009511
9512 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009513 // Apply further optimizations for special cases
9514 // (select (x != 0), -1, 0) -> neg & sbb
9515 // (select (x == 0), 0, -1) -> neg & sbb
9516 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009517 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009518 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9519 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009520 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9521 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009522 CmpOp0);
9523 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9524 DAG.getConstant(X86::COND_B, MVT::i8),
9525 SDValue(Neg.getNode(), 1));
9526 return Res;
9527 }
9528
Chris Lattnera2b56002010-12-05 01:23:24 +00009529 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9530 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009531 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009532
Chris Lattner96908b12010-12-05 02:00:51 +00009533 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009534 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9535 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009536
Chris Lattner96908b12010-12-05 02:00:51 +00009537 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9538 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009539
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009540 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009541 if (N2C == 0 || !N2C->isNullValue())
9542 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9543 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009544 }
9545 }
9546
Chris Lattnera2b56002010-12-05 01:23:24 +00009547 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009548 if (Cond.getOpcode() == ISD::AND &&
9549 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9550 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009551 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009552 Cond = Cond.getOperand(0);
9553 }
9554
Evan Cheng3f41d662007-10-08 22:16:29 +00009555 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9556 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009557 unsigned CondOpcode = Cond.getOpcode();
9558 if (CondOpcode == X86ISD::SETCC ||
9559 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009560 CC = Cond.getOperand(0);
9561
Dan Gohman475871a2008-07-27 21:46:04 +00009562 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009563 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009564 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009565
Evan Cheng3f41d662007-10-08 22:16:29 +00009566 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009567 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009568 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009569 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009570
Chris Lattnerd1980a52009-03-12 06:52:53 +00009571 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9572 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009573 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009574 addTest = false;
9575 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009576 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9577 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9578 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9579 Cond.getOperand(0).getValueType() != MVT::i8)) {
9580 SDValue LHS = Cond.getOperand(0);
9581 SDValue RHS = Cond.getOperand(1);
9582 unsigned X86Opcode;
9583 unsigned X86Cond;
9584 SDVTList VTs;
9585 switch (CondOpcode) {
9586 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9587 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9588 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9589 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9590 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9591 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9592 default: llvm_unreachable("unexpected overflowing operator");
9593 }
9594 if (CondOpcode == ISD::UMULO)
9595 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9596 MVT::i32);
9597 else
9598 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9599
9600 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9601
9602 if (CondOpcode == ISD::UMULO)
9603 Cond = X86Op.getValue(2);
9604 else
9605 Cond = X86Op.getValue(1);
9606
9607 CC = DAG.getConstant(X86Cond, MVT::i8);
9608 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009609 }
9610
9611 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009612 // Look pass the truncate if the high bits are known zero.
9613 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9614 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009615
9616 // We know the result of AND is compared against zero. Try to match
9617 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009618 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009619 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009620 if (NewSetCC.getNode()) {
9621 CC = NewSetCC.getOperand(0);
9622 Cond = NewSetCC.getOperand(1);
9623 addTest = false;
9624 }
9625 }
9626 }
9627
9628 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009629 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009630 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009631 }
9632
Benjamin Kramere915ff32010-12-22 23:09:28 +00009633 // a < b ? -1 : 0 -> RES = ~setcc_carry
9634 // a < b ? 0 : -1 -> RES = setcc_carry
9635 // a >= b ? -1 : 0 -> RES = setcc_carry
9636 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009637 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009638 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009639 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9640
9641 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9642 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9643 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9644 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9645 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9646 return DAG.getNOT(DL, Res, Res.getValueType());
9647 return Res;
9648 }
9649 }
9650
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009651 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9652 // widen the cmov and push the truncate through. This avoids introducing a new
9653 // branch during isel and doesn't add any extensions.
9654 if (Op.getValueType() == MVT::i8 &&
9655 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9656 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9657 if (T1.getValueType() == T2.getValueType() &&
9658 // Blacklist CopyFromReg to avoid partial register stalls.
9659 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9660 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009661 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009662 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9663 }
9664 }
9665
Evan Cheng0488db92007-09-25 01:57:46 +00009666 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9667 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009668 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009669 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009670 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009671}
9672
Nadav Rotem1a330af2012-12-27 22:47:16 +00009673SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9674 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00009675 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009676 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00009677 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009678 DebugLoc dl = Op->getDebugLoc();
9679
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009680 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9681 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9682 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009683
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009684 if (Subtarget->hasInt256())
9685 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009686
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009687 // Optimize vectors in AVX mode
9688 // Sign extend v8i16 to v8i32 and
9689 // v4i32 to v4i64
9690 //
9691 // Divide input vector into two parts
9692 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9693 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9694 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009695
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009696 unsigned NumElems = InVT.getVectorNumElements();
9697 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009698
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009699 SmallVector<int,8> ShufMask1(NumElems, -1);
9700 for (unsigned i = 0; i != NumElems/2; ++i)
9701 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009702
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009703 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009704
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009705 SmallVector<int,8> ShufMask2(NumElems, -1);
9706 for (unsigned i = 0; i != NumElems/2; ++i)
9707 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009708
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009709 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009710
Craig Toppera080daf2013-01-20 21:50:27 +00009711 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009712 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009713
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009714 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9715 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009716
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009717 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009718}
9719
Evan Cheng370e5342008-12-03 08:38:43 +00009720// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9721// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9722// from the AND / OR.
9723static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9724 Opc = Op.getOpcode();
9725 if (Opc != ISD::OR && Opc != ISD::AND)
9726 return false;
9727 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9728 Op.getOperand(0).hasOneUse() &&
9729 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9730 Op.getOperand(1).hasOneUse());
9731}
9732
Evan Cheng961d6d42009-02-02 08:19:07 +00009733// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9734// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009735static bool isXor1OfSetCC(SDValue Op) {
9736 if (Op.getOpcode() != ISD::XOR)
9737 return false;
9738 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9739 if (N1C && N1C->getAPIntValue() == 1) {
9740 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9741 Op.getOperand(0).hasOneUse();
9742 }
9743 return false;
9744}
9745
Dan Gohmand858e902010-04-17 15:26:15 +00009746SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009747 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009748 SDValue Chain = Op.getOperand(0);
9749 SDValue Cond = Op.getOperand(1);
9750 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009751 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009752 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009753 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009754
Dan Gohman1a492952009-10-20 16:22:37 +00009755 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009756 // Check for setcc([su]{add,sub,mul}o == 0).
9757 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9758 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9759 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9760 Cond.getOperand(0).getResNo() == 1 &&
9761 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9762 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9763 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9764 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9765 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9766 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9767 Inverted = true;
9768 Cond = Cond.getOperand(0);
9769 } else {
9770 SDValue NewCond = LowerSETCC(Cond, DAG);
9771 if (NewCond.getNode())
9772 Cond = NewCond;
9773 }
Dan Gohman1a492952009-10-20 16:22:37 +00009774 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009775#if 0
9776 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009777 else if (Cond.getOpcode() == X86ISD::ADD ||
9778 Cond.getOpcode() == X86ISD::SUB ||
9779 Cond.getOpcode() == X86ISD::SMUL ||
9780 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009781 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009782#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009783
Evan Chengad9c0a32009-12-15 00:53:42 +00009784 // Look pass (and (setcc_carry (cmp ...)), 1).
9785 if (Cond.getOpcode() == ISD::AND &&
9786 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9787 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009788 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009789 Cond = Cond.getOperand(0);
9790 }
9791
Evan Cheng3f41d662007-10-08 22:16:29 +00009792 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9793 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009794 unsigned CondOpcode = Cond.getOpcode();
9795 if (CondOpcode == X86ISD::SETCC ||
9796 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009797 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009798
Dan Gohman475871a2008-07-27 21:46:04 +00009799 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009800 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009801 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009802 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009803 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009804 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009805 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009806 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009807 default: break;
9808 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009809 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009810 // These can only come from an arithmetic instruction with overflow,
9811 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009812 Cond = Cond.getNode()->getOperand(1);
9813 addTest = false;
9814 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009815 }
Evan Cheng0488db92007-09-25 01:57:46 +00009816 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009817 }
9818 CondOpcode = Cond.getOpcode();
9819 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9820 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9821 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9822 Cond.getOperand(0).getValueType() != MVT::i8)) {
9823 SDValue LHS = Cond.getOperand(0);
9824 SDValue RHS = Cond.getOperand(1);
9825 unsigned X86Opcode;
9826 unsigned X86Cond;
9827 SDVTList VTs;
9828 switch (CondOpcode) {
9829 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9830 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9831 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9832 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9833 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9834 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9835 default: llvm_unreachable("unexpected overflowing operator");
9836 }
9837 if (Inverted)
9838 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9839 if (CondOpcode == ISD::UMULO)
9840 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9841 MVT::i32);
9842 else
9843 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9844
9845 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9846
9847 if (CondOpcode == ISD::UMULO)
9848 Cond = X86Op.getValue(2);
9849 else
9850 Cond = X86Op.getValue(1);
9851
9852 CC = DAG.getConstant(X86Cond, MVT::i8);
9853 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009854 } else {
9855 unsigned CondOpc;
9856 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9857 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009858 if (CondOpc == ISD::OR) {
9859 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9860 // two branches instead of an explicit OR instruction with a
9861 // separate test.
9862 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009863 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009864 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009865 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009866 Chain, Dest, CC, Cmp);
9867 CC = Cond.getOperand(1).getOperand(0);
9868 Cond = Cmp;
9869 addTest = false;
9870 }
9871 } else { // ISD::AND
9872 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9873 // two branches instead of an explicit AND instruction with a
9874 // separate test. However, we only do this if this block doesn't
9875 // have a fall-through edge, because this requires an explicit
9876 // jmp when the condition is false.
9877 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009878 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009879 Op.getNode()->hasOneUse()) {
9880 X86::CondCode CCode =
9881 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9882 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009883 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009884 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009885 // Look for an unconditional branch following this conditional branch.
9886 // We need this because we need to reverse the successors in order
9887 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009888 if (User->getOpcode() == ISD::BR) {
9889 SDValue FalseBB = User->getOperand(1);
9890 SDNode *NewBR =
9891 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009892 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009893 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009894 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009895
Dale Johannesene4d209d2009-02-03 20:21:25 +00009896 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009897 Chain, Dest, CC, Cmp);
9898 X86::CondCode CCode =
9899 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9900 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009901 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009902 Cond = Cmp;
9903 addTest = false;
9904 }
9905 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009906 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009907 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9908 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9909 // It should be transformed during dag combiner except when the condition
9910 // is set by a arithmetics with overflow node.
9911 X86::CondCode CCode =
9912 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9913 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009914 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009915 Cond = Cond.getOperand(0).getOperand(1);
9916 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009917 } else if (Cond.getOpcode() == ISD::SETCC &&
9918 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9919 // For FCMP_OEQ, we can emit
9920 // two branches instead of an explicit AND instruction with a
9921 // separate test. However, we only do this if this block doesn't
9922 // have a fall-through edge, because this requires an explicit
9923 // jmp when the condition is false.
9924 if (Op.getNode()->hasOneUse()) {
9925 SDNode *User = *Op.getNode()->use_begin();
9926 // Look for an unconditional branch following this conditional branch.
9927 // We need this because we need to reverse the successors in order
9928 // to implement FCMP_OEQ.
9929 if (User->getOpcode() == ISD::BR) {
9930 SDValue FalseBB = User->getOperand(1);
9931 SDNode *NewBR =
9932 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9933 assert(NewBR == User);
9934 (void)NewBR;
9935 Dest = FalseBB;
9936
9937 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9938 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009939 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009940 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9941 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9942 Chain, Dest, CC, Cmp);
9943 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9944 Cond = Cmp;
9945 addTest = false;
9946 }
9947 }
9948 } else if (Cond.getOpcode() == ISD::SETCC &&
9949 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9950 // For FCMP_UNE, we can emit
9951 // two branches instead of an explicit AND instruction with a
9952 // separate test. However, we only do this if this block doesn't
9953 // have a fall-through edge, because this requires an explicit
9954 // jmp when the condition is false.
9955 if (Op.getNode()->hasOneUse()) {
9956 SDNode *User = *Op.getNode()->use_begin();
9957 // Look for an unconditional branch following this conditional branch.
9958 // We need this because we need to reverse the successors in order
9959 // to implement FCMP_UNE.
9960 if (User->getOpcode() == ISD::BR) {
9961 SDValue FalseBB = User->getOperand(1);
9962 SDNode *NewBR =
9963 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9964 assert(NewBR == User);
9965 (void)NewBR;
9966
9967 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9968 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009969 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009970 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9971 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9972 Chain, Dest, CC, Cmp);
9973 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9974 Cond = Cmp;
9975 addTest = false;
9976 Dest = FalseBB;
9977 }
9978 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009979 }
Evan Cheng0488db92007-09-25 01:57:46 +00009980 }
9981
9982 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009983 // Look pass the truncate if the high bits are known zero.
9984 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9985 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009986
9987 // We know the result of AND is compared against zero. Try to match
9988 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009989 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009990 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9991 if (NewSetCC.getNode()) {
9992 CC = NewSetCC.getOperand(0);
9993 Cond = NewSetCC.getOperand(1);
9994 addTest = false;
9995 }
9996 }
9997 }
9998
9999 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010000 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +000010001 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +000010002 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010003 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010004 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +000010005 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +000010006}
10007
Anton Korobeynikove060b532007-04-17 19:34:00 +000010008// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
10009// Calls to _alloca is needed to probe the stack when allocating more than 4k
10010// bytes in one go. Touching the stack at 4K increments is necessary to ensure
10011// that the guard pages used by the OS virtual memory manager are allocated in
10012// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +000010013SDValue
10014X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010015 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010016 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010017 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010018 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +000010019 "are being used");
10020 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010021 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010022
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010023 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +000010024 SDValue Chain = Op.getOperand(0);
10025 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010026 // FIXME: Ensure alignment here
10027
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010028 bool Is64Bit = Subtarget->is64Bit();
10029 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010030
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010031 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010032 MachineFunction &MF = DAG.getMachineFunction();
10033 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010034
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010035 if (Is64Bit) {
10036 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +000010037 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010038 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010039
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010040 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +000010041 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010042 if (I->hasNestAttr())
10043 report_fatal_error("Cannot use segmented stacks with functions that "
10044 "have nested arguments.");
10045 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010046
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010047 const TargetRegisterClass *AddrRegClass =
10048 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10049 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10050 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10051 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10052 DAG.getRegister(Vreg, SPTy));
10053 SDValue Ops1[2] = { Value, Chain };
10054 return DAG.getMergeValues(Ops1, 2, dl);
10055 } else {
10056 SDValue Flag;
10057 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010058
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010059 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10060 Flag = Chain.getValue(1);
10061 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010062
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010063 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10064 Flag = Chain.getValue(1);
10065
Michael Liaoc5c970e2012-10-31 04:14:09 +000010066 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10067 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010068
10069 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10070 return DAG.getMergeValues(Ops1, 2, dl);
10071 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010072}
10073
Dan Gohmand858e902010-04-17 15:26:15 +000010074SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010075 MachineFunction &MF = DAG.getMachineFunction();
10076 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10077
Dan Gohman69de1932008-02-06 22:27:42 +000010078 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010079 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010080
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010081 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010082 // vastart just stores the address of the VarArgsFrameIndex slot into the
10083 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010084 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10085 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010086 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10087 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010088 }
10089
10090 // __va_list_tag:
10091 // gp_offset (0 - 6 * 8)
10092 // fp_offset (48 - 48 + 8 * 16)
10093 // overflow_arg_area (point to parameters coming in memory).
10094 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010095 SmallVector<SDValue, 8> MemOps;
10096 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010097 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010098 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010099 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10100 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010101 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010102 MemOps.push_back(Store);
10103
10104 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010105 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010106 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010107 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010108 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10109 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010110 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010111 MemOps.push_back(Store);
10112
10113 // Store ptr to overflow_arg_area
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));
Dan Gohman1e93df62010-04-17 14:41:14 +000010116 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10117 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010118 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10119 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010120 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010121 MemOps.push_back(Store);
10122
10123 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010124 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010125 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010126 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10127 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010128 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10129 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010130 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010131 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010132 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010133}
10134
Dan Gohmand858e902010-04-17 15:26:15 +000010135SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010136 assert(Subtarget->is64Bit() &&
10137 "LowerVAARG only handles 64-bit va_arg!");
10138 assert((Subtarget->isTargetLinux() ||
10139 Subtarget->isTargetDarwin()) &&
10140 "Unhandled target in LowerVAARG");
10141 assert(Op.getNode()->getNumOperands() == 4);
10142 SDValue Chain = Op.getOperand(0);
10143 SDValue SrcPtr = Op.getOperand(1);
10144 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10145 unsigned Align = Op.getConstantOperandVal(3);
10146 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010147
Dan Gohman320afb82010-10-12 18:00:49 +000010148 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010149 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010150 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010151 uint8_t ArgMode;
10152
10153 // Decide which area this value should be read from.
10154 // TODO: Implement the AMD64 ABI in its entirety. This simple
10155 // selection mechanism works only for the basic types.
10156 if (ArgVT == MVT::f80) {
10157 llvm_unreachable("va_arg for f80 not yet implemented");
10158 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10159 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10160 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10161 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10162 } else {
10163 llvm_unreachable("Unhandled argument type in LowerVAARG");
10164 }
10165
10166 if (ArgMode == 2) {
10167 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010168 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010169 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010170 .getFunction()->getAttributes()
10171 .hasAttribute(AttributeSet::FunctionIndex,
10172 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010173 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010174 }
10175
10176 // Insert VAARG_64 node into the DAG
10177 // VAARG_64 returns two values: Variable Argument Address, Chain
10178 SmallVector<SDValue, 11> InstOps;
10179 InstOps.push_back(Chain);
10180 InstOps.push_back(SrcPtr);
10181 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10182 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10183 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10184 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10185 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10186 VTs, &InstOps[0], InstOps.size(),
10187 MVT::i64,
10188 MachinePointerInfo(SV),
10189 /*Align=*/0,
10190 /*Volatile=*/false,
10191 /*ReadMem=*/true,
10192 /*WriteMem=*/true);
10193 Chain = VAARG.getValue(1);
10194
10195 // Load the next argument and return it
10196 return DAG.getLoad(ArgVT, dl,
10197 Chain,
10198 VAARG,
10199 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010200 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010201}
10202
Craig Topper55b24052012-09-11 06:15:32 +000010203static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10204 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010205 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010206 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010207 SDValue Chain = Op.getOperand(0);
10208 SDValue DstPtr = Op.getOperand(1);
10209 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010210 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10211 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010212 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010213
Chris Lattnere72f2022010-09-21 05:40:29 +000010214 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010215 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010216 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010217 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010218}
10219
Craig Topperff3139f2013-02-19 07:43:59 +000010220// getTargetVShiftNode - Handle vector element shifts where the shift amount
Craig Topper80e46362012-01-23 06:16:53 +000010221// may or may not be a constant. Takes immediate version of shift as input.
10222static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10223 SDValue SrcOp, SDValue ShAmt,
10224 SelectionDAG &DAG) {
10225 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10226
10227 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010228 // Constant may be a TargetConstant. Use a regular constant.
10229 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010230 switch (Opc) {
10231 default: llvm_unreachable("Unknown target vector shift node");
10232 case X86ISD::VSHLI:
10233 case X86ISD::VSRLI:
10234 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010235 return DAG.getNode(Opc, dl, VT, SrcOp,
10236 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010237 }
10238 }
10239
10240 // Change opcode to non-immediate version
10241 switch (Opc) {
10242 default: llvm_unreachable("Unknown target vector shift node");
10243 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10244 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10245 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10246 }
10247
10248 // Need to build a vector containing shift amount
10249 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10250 SDValue ShOps[4];
10251 ShOps[0] = ShAmt;
10252 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010253 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010254 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010255
10256 // The return type has to be a 128-bit type with the same element
10257 // type as the input type.
10258 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10259 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10260
10261 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010262 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10263}
10264
Craig Topper55b24052012-09-11 06:15:32 +000010265static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010266 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010267 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010268 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010269 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010270 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010271 case Intrinsic::x86_sse_comieq_ss:
10272 case Intrinsic::x86_sse_comilt_ss:
10273 case Intrinsic::x86_sse_comile_ss:
10274 case Intrinsic::x86_sse_comigt_ss:
10275 case Intrinsic::x86_sse_comige_ss:
10276 case Intrinsic::x86_sse_comineq_ss:
10277 case Intrinsic::x86_sse_ucomieq_ss:
10278 case Intrinsic::x86_sse_ucomilt_ss:
10279 case Intrinsic::x86_sse_ucomile_ss:
10280 case Intrinsic::x86_sse_ucomigt_ss:
10281 case Intrinsic::x86_sse_ucomige_ss:
10282 case Intrinsic::x86_sse_ucomineq_ss:
10283 case Intrinsic::x86_sse2_comieq_sd:
10284 case Intrinsic::x86_sse2_comilt_sd:
10285 case Intrinsic::x86_sse2_comile_sd:
10286 case Intrinsic::x86_sse2_comigt_sd:
10287 case Intrinsic::x86_sse2_comige_sd:
10288 case Intrinsic::x86_sse2_comineq_sd:
10289 case Intrinsic::x86_sse2_ucomieq_sd:
10290 case Intrinsic::x86_sse2_ucomilt_sd:
10291 case Intrinsic::x86_sse2_ucomile_sd:
10292 case Intrinsic::x86_sse2_ucomigt_sd:
10293 case Intrinsic::x86_sse2_ucomige_sd:
10294 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010295 unsigned Opc;
10296 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010297 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010298 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010299 case Intrinsic::x86_sse_comieq_ss:
10300 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010301 Opc = X86ISD::COMI;
10302 CC = ISD::SETEQ;
10303 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010304 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010305 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010306 Opc = X86ISD::COMI;
10307 CC = ISD::SETLT;
10308 break;
10309 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010310 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010311 Opc = X86ISD::COMI;
10312 CC = ISD::SETLE;
10313 break;
10314 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010315 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010316 Opc = X86ISD::COMI;
10317 CC = ISD::SETGT;
10318 break;
10319 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010320 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010321 Opc = X86ISD::COMI;
10322 CC = ISD::SETGE;
10323 break;
10324 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010325 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010326 Opc = X86ISD::COMI;
10327 CC = ISD::SETNE;
10328 break;
10329 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010330 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010331 Opc = X86ISD::UCOMI;
10332 CC = ISD::SETEQ;
10333 break;
10334 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010335 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010336 Opc = X86ISD::UCOMI;
10337 CC = ISD::SETLT;
10338 break;
10339 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010340 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010341 Opc = X86ISD::UCOMI;
10342 CC = ISD::SETLE;
10343 break;
10344 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010345 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010346 Opc = X86ISD::UCOMI;
10347 CC = ISD::SETGT;
10348 break;
10349 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010350 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010351 Opc = X86ISD::UCOMI;
10352 CC = ISD::SETGE;
10353 break;
10354 case Intrinsic::x86_sse_ucomineq_ss:
10355 case Intrinsic::x86_sse2_ucomineq_sd:
10356 Opc = X86ISD::UCOMI;
10357 CC = ISD::SETNE;
10358 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010359 }
Evan Cheng734503b2006-09-11 02:19:56 +000010360
Dan Gohman475871a2008-07-27 21:46:04 +000010361 SDValue LHS = Op.getOperand(1);
10362 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010363 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010364 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010365 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10366 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10367 DAG.getConstant(X86CC, MVT::i8), Cond);
10368 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010369 }
Craig Topper6d688152012-08-14 07:43:25 +000010370
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010371 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010372 case Intrinsic::x86_sse2_pmulu_dq:
10373 case Intrinsic::x86_avx2_pmulu_dq:
10374 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10375 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010376
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010377 // SSE2/AVX2 sub with unsigned saturation intrinsics
10378 case Intrinsic::x86_sse2_psubus_b:
10379 case Intrinsic::x86_sse2_psubus_w:
10380 case Intrinsic::x86_avx2_psubus_b:
10381 case Intrinsic::x86_avx2_psubus_w:
10382 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10383 Op.getOperand(1), Op.getOperand(2));
10384
Craig Topper6d688152012-08-14 07:43:25 +000010385 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010386 case Intrinsic::x86_sse3_hadd_ps:
10387 case Intrinsic::x86_sse3_hadd_pd:
10388 case Intrinsic::x86_avx_hadd_ps_256:
10389 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010390 case Intrinsic::x86_sse3_hsub_ps:
10391 case Intrinsic::x86_sse3_hsub_pd:
10392 case Intrinsic::x86_avx_hsub_ps_256:
10393 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010394 case Intrinsic::x86_ssse3_phadd_w_128:
10395 case Intrinsic::x86_ssse3_phadd_d_128:
10396 case Intrinsic::x86_avx2_phadd_w:
10397 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010398 case Intrinsic::x86_ssse3_phsub_w_128:
10399 case Intrinsic::x86_ssse3_phsub_d_128:
10400 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010401 case Intrinsic::x86_avx2_phsub_d: {
10402 unsigned Opcode;
10403 switch (IntNo) {
10404 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10405 case Intrinsic::x86_sse3_hadd_ps:
10406 case Intrinsic::x86_sse3_hadd_pd:
10407 case Intrinsic::x86_avx_hadd_ps_256:
10408 case Intrinsic::x86_avx_hadd_pd_256:
10409 Opcode = X86ISD::FHADD;
10410 break;
10411 case Intrinsic::x86_sse3_hsub_ps:
10412 case Intrinsic::x86_sse3_hsub_pd:
10413 case Intrinsic::x86_avx_hsub_ps_256:
10414 case Intrinsic::x86_avx_hsub_pd_256:
10415 Opcode = X86ISD::FHSUB;
10416 break;
10417 case Intrinsic::x86_ssse3_phadd_w_128:
10418 case Intrinsic::x86_ssse3_phadd_d_128:
10419 case Intrinsic::x86_avx2_phadd_w:
10420 case Intrinsic::x86_avx2_phadd_d:
10421 Opcode = X86ISD::HADD;
10422 break;
10423 case Intrinsic::x86_ssse3_phsub_w_128:
10424 case Intrinsic::x86_ssse3_phsub_d_128:
10425 case Intrinsic::x86_avx2_phsub_w:
10426 case Intrinsic::x86_avx2_phsub_d:
10427 Opcode = X86ISD::HSUB;
10428 break;
10429 }
10430 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010431 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010432 }
10433
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010434 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10435 case Intrinsic::x86_sse2_pmaxu_b:
10436 case Intrinsic::x86_sse41_pmaxuw:
10437 case Intrinsic::x86_sse41_pmaxud:
10438 case Intrinsic::x86_avx2_pmaxu_b:
10439 case Intrinsic::x86_avx2_pmaxu_w:
10440 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010441 case Intrinsic::x86_sse2_pminu_b:
10442 case Intrinsic::x86_sse41_pminuw:
10443 case Intrinsic::x86_sse41_pminud:
10444 case Intrinsic::x86_avx2_pminu_b:
10445 case Intrinsic::x86_avx2_pminu_w:
10446 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010447 case Intrinsic::x86_sse41_pmaxsb:
10448 case Intrinsic::x86_sse2_pmaxs_w:
10449 case Intrinsic::x86_sse41_pmaxsd:
10450 case Intrinsic::x86_avx2_pmaxs_b:
10451 case Intrinsic::x86_avx2_pmaxs_w:
10452 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010453 case Intrinsic::x86_sse41_pminsb:
10454 case Intrinsic::x86_sse2_pmins_w:
10455 case Intrinsic::x86_sse41_pminsd:
10456 case Intrinsic::x86_avx2_pmins_b:
10457 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010458 case Intrinsic::x86_avx2_pmins_d: {
10459 unsigned Opcode;
10460 switch (IntNo) {
10461 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10462 case Intrinsic::x86_sse2_pmaxu_b:
10463 case Intrinsic::x86_sse41_pmaxuw:
10464 case Intrinsic::x86_sse41_pmaxud:
10465 case Intrinsic::x86_avx2_pmaxu_b:
10466 case Intrinsic::x86_avx2_pmaxu_w:
10467 case Intrinsic::x86_avx2_pmaxu_d:
10468 Opcode = X86ISD::UMAX;
10469 break;
10470 case Intrinsic::x86_sse2_pminu_b:
10471 case Intrinsic::x86_sse41_pminuw:
10472 case Intrinsic::x86_sse41_pminud:
10473 case Intrinsic::x86_avx2_pminu_b:
10474 case Intrinsic::x86_avx2_pminu_w:
10475 case Intrinsic::x86_avx2_pminu_d:
10476 Opcode = X86ISD::UMIN;
10477 break;
10478 case Intrinsic::x86_sse41_pmaxsb:
10479 case Intrinsic::x86_sse2_pmaxs_w:
10480 case Intrinsic::x86_sse41_pmaxsd:
10481 case Intrinsic::x86_avx2_pmaxs_b:
10482 case Intrinsic::x86_avx2_pmaxs_w:
10483 case Intrinsic::x86_avx2_pmaxs_d:
10484 Opcode = X86ISD::SMAX;
10485 break;
10486 case Intrinsic::x86_sse41_pminsb:
10487 case Intrinsic::x86_sse2_pmins_w:
10488 case Intrinsic::x86_sse41_pminsd:
10489 case Intrinsic::x86_avx2_pmins_b:
10490 case Intrinsic::x86_avx2_pmins_w:
10491 case Intrinsic::x86_avx2_pmins_d:
10492 Opcode = X86ISD::SMIN;
10493 break;
10494 }
10495 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010496 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010497 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010498
Craig Topper6d183e42012-12-29 16:44:25 +000010499 // SSE/SSE2/AVX floating point max/min intrinsics.
10500 case Intrinsic::x86_sse_max_ps:
10501 case Intrinsic::x86_sse2_max_pd:
10502 case Intrinsic::x86_avx_max_ps_256:
10503 case Intrinsic::x86_avx_max_pd_256:
10504 case Intrinsic::x86_sse_min_ps:
10505 case Intrinsic::x86_sse2_min_pd:
10506 case Intrinsic::x86_avx_min_ps_256:
10507 case Intrinsic::x86_avx_min_pd_256: {
10508 unsigned Opcode;
10509 switch (IntNo) {
10510 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10511 case Intrinsic::x86_sse_max_ps:
10512 case Intrinsic::x86_sse2_max_pd:
10513 case Intrinsic::x86_avx_max_ps_256:
10514 case Intrinsic::x86_avx_max_pd_256:
10515 Opcode = X86ISD::FMAX;
10516 break;
10517 case Intrinsic::x86_sse_min_ps:
10518 case Intrinsic::x86_sse2_min_pd:
10519 case Intrinsic::x86_avx_min_ps_256:
10520 case Intrinsic::x86_avx_min_pd_256:
10521 Opcode = X86ISD::FMIN;
10522 break;
10523 }
10524 return DAG.getNode(Opcode, dl, Op.getValueType(),
10525 Op.getOperand(1), Op.getOperand(2));
10526 }
10527
Craig Topper6d688152012-08-14 07:43:25 +000010528 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010529 case Intrinsic::x86_avx2_psllv_d:
10530 case Intrinsic::x86_avx2_psllv_q:
10531 case Intrinsic::x86_avx2_psllv_d_256:
10532 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010533 case Intrinsic::x86_avx2_psrlv_d:
10534 case Intrinsic::x86_avx2_psrlv_q:
10535 case Intrinsic::x86_avx2_psrlv_d_256:
10536 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010537 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010538 case Intrinsic::x86_avx2_psrav_d_256: {
10539 unsigned Opcode;
10540 switch (IntNo) {
10541 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10542 case Intrinsic::x86_avx2_psllv_d:
10543 case Intrinsic::x86_avx2_psllv_q:
10544 case Intrinsic::x86_avx2_psllv_d_256:
10545 case Intrinsic::x86_avx2_psllv_q_256:
10546 Opcode = ISD::SHL;
10547 break;
10548 case Intrinsic::x86_avx2_psrlv_d:
10549 case Intrinsic::x86_avx2_psrlv_q:
10550 case Intrinsic::x86_avx2_psrlv_d_256:
10551 case Intrinsic::x86_avx2_psrlv_q_256:
10552 Opcode = ISD::SRL;
10553 break;
10554 case Intrinsic::x86_avx2_psrav_d:
10555 case Intrinsic::x86_avx2_psrav_d_256:
10556 Opcode = ISD::SRA;
10557 break;
10558 }
10559 return DAG.getNode(Opcode, dl, Op.getValueType(),
10560 Op.getOperand(1), Op.getOperand(2));
10561 }
10562
Craig Topper969ba282012-01-25 06:43:11 +000010563 case Intrinsic::x86_ssse3_pshuf_b_128:
10564 case Intrinsic::x86_avx2_pshuf_b:
10565 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10566 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010567
Craig Topper969ba282012-01-25 06:43:11 +000010568 case Intrinsic::x86_ssse3_psign_b_128:
10569 case Intrinsic::x86_ssse3_psign_w_128:
10570 case Intrinsic::x86_ssse3_psign_d_128:
10571 case Intrinsic::x86_avx2_psign_b:
10572 case Intrinsic::x86_avx2_psign_w:
10573 case Intrinsic::x86_avx2_psign_d:
10574 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10575 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010576
Craig Toppere566cd02012-01-26 07:18:03 +000010577 case Intrinsic::x86_sse41_insertps:
10578 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10579 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010580
Craig Toppere566cd02012-01-26 07:18:03 +000010581 case Intrinsic::x86_avx_vperm2f128_ps_256:
10582 case Intrinsic::x86_avx_vperm2f128_pd_256:
10583 case Intrinsic::x86_avx_vperm2f128_si_256:
10584 case Intrinsic::x86_avx2_vperm2i128:
10585 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10586 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010587
Craig Topperffa6c402012-04-16 07:13:00 +000010588 case Intrinsic::x86_avx2_permd:
10589 case Intrinsic::x86_avx2_permps:
10590 // Operands intentionally swapped. Mask is last operand to intrinsic,
10591 // but second operand for node/intruction.
10592 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10593 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010594
Craig Topper22d8f0d2012-12-29 18:18:20 +000010595 case Intrinsic::x86_sse_sqrt_ps:
10596 case Intrinsic::x86_sse2_sqrt_pd:
10597 case Intrinsic::x86_avx_sqrt_ps_256:
10598 case Intrinsic::x86_avx_sqrt_pd_256:
10599 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10600
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010601 // ptest and testp intrinsics. The intrinsic these come from are designed to
10602 // return an integer value, not just an instruction so lower it to the ptest
10603 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010604 case Intrinsic::x86_sse41_ptestz:
10605 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010606 case Intrinsic::x86_sse41_ptestnzc:
10607 case Intrinsic::x86_avx_ptestz_256:
10608 case Intrinsic::x86_avx_ptestc_256:
10609 case Intrinsic::x86_avx_ptestnzc_256:
10610 case Intrinsic::x86_avx_vtestz_ps:
10611 case Intrinsic::x86_avx_vtestc_ps:
10612 case Intrinsic::x86_avx_vtestnzc_ps:
10613 case Intrinsic::x86_avx_vtestz_pd:
10614 case Intrinsic::x86_avx_vtestc_pd:
10615 case Intrinsic::x86_avx_vtestnzc_pd:
10616 case Intrinsic::x86_avx_vtestz_ps_256:
10617 case Intrinsic::x86_avx_vtestc_ps_256:
10618 case Intrinsic::x86_avx_vtestnzc_ps_256:
10619 case Intrinsic::x86_avx_vtestz_pd_256:
10620 case Intrinsic::x86_avx_vtestc_pd_256:
10621 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10622 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010623 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010624 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010625 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010626 case Intrinsic::x86_avx_vtestz_ps:
10627 case Intrinsic::x86_avx_vtestz_pd:
10628 case Intrinsic::x86_avx_vtestz_ps_256:
10629 case Intrinsic::x86_avx_vtestz_pd_256:
10630 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010631 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010632 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010633 // ZF = 1
10634 X86CC = X86::COND_E;
10635 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010636 case Intrinsic::x86_avx_vtestc_ps:
10637 case Intrinsic::x86_avx_vtestc_pd:
10638 case Intrinsic::x86_avx_vtestc_ps_256:
10639 case Intrinsic::x86_avx_vtestc_pd_256:
10640 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010641 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010642 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010643 // CF = 1
10644 X86CC = X86::COND_B;
10645 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010646 case Intrinsic::x86_avx_vtestnzc_ps:
10647 case Intrinsic::x86_avx_vtestnzc_pd:
10648 case Intrinsic::x86_avx_vtestnzc_ps_256:
10649 case Intrinsic::x86_avx_vtestnzc_pd_256:
10650 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010651 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010652 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010653 // ZF and CF = 0
10654 X86CC = X86::COND_A;
10655 break;
10656 }
Eric Christopherfd179292009-08-27 18:07:15 +000010657
Eric Christopher71c67532009-07-29 00:28:05 +000010658 SDValue LHS = Op.getOperand(1);
10659 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010660 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10661 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010662 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10663 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10664 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010665 }
Evan Cheng5759f972008-05-04 09:15:50 +000010666
Craig Topper80e46362012-01-23 06:16:53 +000010667 // SSE/AVX shift intrinsics
10668 case Intrinsic::x86_sse2_psll_w:
10669 case Intrinsic::x86_sse2_psll_d:
10670 case Intrinsic::x86_sse2_psll_q:
10671 case Intrinsic::x86_avx2_psll_w:
10672 case Intrinsic::x86_avx2_psll_d:
10673 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010674 case Intrinsic::x86_sse2_psrl_w:
10675 case Intrinsic::x86_sse2_psrl_d:
10676 case Intrinsic::x86_sse2_psrl_q:
10677 case Intrinsic::x86_avx2_psrl_w:
10678 case Intrinsic::x86_avx2_psrl_d:
10679 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010680 case Intrinsic::x86_sse2_psra_w:
10681 case Intrinsic::x86_sse2_psra_d:
10682 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010683 case Intrinsic::x86_avx2_psra_d: {
10684 unsigned Opcode;
10685 switch (IntNo) {
10686 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10687 case Intrinsic::x86_sse2_psll_w:
10688 case Intrinsic::x86_sse2_psll_d:
10689 case Intrinsic::x86_sse2_psll_q:
10690 case Intrinsic::x86_avx2_psll_w:
10691 case Intrinsic::x86_avx2_psll_d:
10692 case Intrinsic::x86_avx2_psll_q:
10693 Opcode = X86ISD::VSHL;
10694 break;
10695 case Intrinsic::x86_sse2_psrl_w:
10696 case Intrinsic::x86_sse2_psrl_d:
10697 case Intrinsic::x86_sse2_psrl_q:
10698 case Intrinsic::x86_avx2_psrl_w:
10699 case Intrinsic::x86_avx2_psrl_d:
10700 case Intrinsic::x86_avx2_psrl_q:
10701 Opcode = X86ISD::VSRL;
10702 break;
10703 case Intrinsic::x86_sse2_psra_w:
10704 case Intrinsic::x86_sse2_psra_d:
10705 case Intrinsic::x86_avx2_psra_w:
10706 case Intrinsic::x86_avx2_psra_d:
10707 Opcode = X86ISD::VSRA;
10708 break;
10709 }
10710 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010711 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010712 }
10713
10714 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010715 case Intrinsic::x86_sse2_pslli_w:
10716 case Intrinsic::x86_sse2_pslli_d:
10717 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010718 case Intrinsic::x86_avx2_pslli_w:
10719 case Intrinsic::x86_avx2_pslli_d:
10720 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010721 case Intrinsic::x86_sse2_psrli_w:
10722 case Intrinsic::x86_sse2_psrli_d:
10723 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010724 case Intrinsic::x86_avx2_psrli_w:
10725 case Intrinsic::x86_avx2_psrli_d:
10726 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010727 case Intrinsic::x86_sse2_psrai_w:
10728 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010729 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010730 case Intrinsic::x86_avx2_psrai_d: {
10731 unsigned Opcode;
10732 switch (IntNo) {
10733 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10734 case Intrinsic::x86_sse2_pslli_w:
10735 case Intrinsic::x86_sse2_pslli_d:
10736 case Intrinsic::x86_sse2_pslli_q:
10737 case Intrinsic::x86_avx2_pslli_w:
10738 case Intrinsic::x86_avx2_pslli_d:
10739 case Intrinsic::x86_avx2_pslli_q:
10740 Opcode = X86ISD::VSHLI;
10741 break;
10742 case Intrinsic::x86_sse2_psrli_w:
10743 case Intrinsic::x86_sse2_psrli_d:
10744 case Intrinsic::x86_sse2_psrli_q:
10745 case Intrinsic::x86_avx2_psrli_w:
10746 case Intrinsic::x86_avx2_psrli_d:
10747 case Intrinsic::x86_avx2_psrli_q:
10748 Opcode = X86ISD::VSRLI;
10749 break;
10750 case Intrinsic::x86_sse2_psrai_w:
10751 case Intrinsic::x86_sse2_psrai_d:
10752 case Intrinsic::x86_avx2_psrai_w:
10753 case Intrinsic::x86_avx2_psrai_d:
10754 Opcode = X86ISD::VSRAI;
10755 break;
10756 }
10757 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010758 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010759 }
10760
Craig Topper4feb6472012-08-06 06:22:36 +000010761 case Intrinsic::x86_sse42_pcmpistria128:
10762 case Intrinsic::x86_sse42_pcmpestria128:
10763 case Intrinsic::x86_sse42_pcmpistric128:
10764 case Intrinsic::x86_sse42_pcmpestric128:
10765 case Intrinsic::x86_sse42_pcmpistrio128:
10766 case Intrinsic::x86_sse42_pcmpestrio128:
10767 case Intrinsic::x86_sse42_pcmpistris128:
10768 case Intrinsic::x86_sse42_pcmpestris128:
10769 case Intrinsic::x86_sse42_pcmpistriz128:
10770 case Intrinsic::x86_sse42_pcmpestriz128: {
10771 unsigned Opcode;
10772 unsigned X86CC;
10773 switch (IntNo) {
10774 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10775 case Intrinsic::x86_sse42_pcmpistria128:
10776 Opcode = X86ISD::PCMPISTRI;
10777 X86CC = X86::COND_A;
10778 break;
10779 case Intrinsic::x86_sse42_pcmpestria128:
10780 Opcode = X86ISD::PCMPESTRI;
10781 X86CC = X86::COND_A;
10782 break;
10783 case Intrinsic::x86_sse42_pcmpistric128:
10784 Opcode = X86ISD::PCMPISTRI;
10785 X86CC = X86::COND_B;
10786 break;
10787 case Intrinsic::x86_sse42_pcmpestric128:
10788 Opcode = X86ISD::PCMPESTRI;
10789 X86CC = X86::COND_B;
10790 break;
10791 case Intrinsic::x86_sse42_pcmpistrio128:
10792 Opcode = X86ISD::PCMPISTRI;
10793 X86CC = X86::COND_O;
10794 break;
10795 case Intrinsic::x86_sse42_pcmpestrio128:
10796 Opcode = X86ISD::PCMPESTRI;
10797 X86CC = X86::COND_O;
10798 break;
10799 case Intrinsic::x86_sse42_pcmpistris128:
10800 Opcode = X86ISD::PCMPISTRI;
10801 X86CC = X86::COND_S;
10802 break;
10803 case Intrinsic::x86_sse42_pcmpestris128:
10804 Opcode = X86ISD::PCMPESTRI;
10805 X86CC = X86::COND_S;
10806 break;
10807 case Intrinsic::x86_sse42_pcmpistriz128:
10808 Opcode = X86ISD::PCMPISTRI;
10809 X86CC = X86::COND_E;
10810 break;
10811 case Intrinsic::x86_sse42_pcmpestriz128:
10812 Opcode = X86ISD::PCMPESTRI;
10813 X86CC = X86::COND_E;
10814 break;
10815 }
10816 SmallVector<SDValue, 5> NewOps;
10817 NewOps.append(Op->op_begin()+1, Op->op_end());
10818 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10819 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10820 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10821 DAG.getConstant(X86CC, MVT::i8),
10822 SDValue(PCMP.getNode(), 1));
10823 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10824 }
Craig Topper6d688152012-08-14 07:43:25 +000010825
Craig Topper4feb6472012-08-06 06:22:36 +000010826 case Intrinsic::x86_sse42_pcmpistri128:
10827 case Intrinsic::x86_sse42_pcmpestri128: {
10828 unsigned Opcode;
10829 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10830 Opcode = X86ISD::PCMPISTRI;
10831 else
10832 Opcode = X86ISD::PCMPESTRI;
10833
10834 SmallVector<SDValue, 5> NewOps;
10835 NewOps.append(Op->op_begin()+1, Op->op_end());
10836 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10837 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10838 }
Craig Topper0e292372012-08-24 04:03:22 +000010839 case Intrinsic::x86_fma_vfmadd_ps:
10840 case Intrinsic::x86_fma_vfmadd_pd:
10841 case Intrinsic::x86_fma_vfmsub_ps:
10842 case Intrinsic::x86_fma_vfmsub_pd:
10843 case Intrinsic::x86_fma_vfnmadd_ps:
10844 case Intrinsic::x86_fma_vfnmadd_pd:
10845 case Intrinsic::x86_fma_vfnmsub_ps:
10846 case Intrinsic::x86_fma_vfnmsub_pd:
10847 case Intrinsic::x86_fma_vfmaddsub_ps:
10848 case Intrinsic::x86_fma_vfmaddsub_pd:
10849 case Intrinsic::x86_fma_vfmsubadd_ps:
10850 case Intrinsic::x86_fma_vfmsubadd_pd:
10851 case Intrinsic::x86_fma_vfmadd_ps_256:
10852 case Intrinsic::x86_fma_vfmadd_pd_256:
10853 case Intrinsic::x86_fma_vfmsub_ps_256:
10854 case Intrinsic::x86_fma_vfmsub_pd_256:
10855 case Intrinsic::x86_fma_vfnmadd_ps_256:
10856 case Intrinsic::x86_fma_vfnmadd_pd_256:
10857 case Intrinsic::x86_fma_vfnmsub_ps_256:
10858 case Intrinsic::x86_fma_vfnmsub_pd_256:
10859 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10860 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10861 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10862 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010863 unsigned Opc;
10864 switch (IntNo) {
10865 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10866 case Intrinsic::x86_fma_vfmadd_ps:
10867 case Intrinsic::x86_fma_vfmadd_pd:
10868 case Intrinsic::x86_fma_vfmadd_ps_256:
10869 case Intrinsic::x86_fma_vfmadd_pd_256:
10870 Opc = X86ISD::FMADD;
10871 break;
10872 case Intrinsic::x86_fma_vfmsub_ps:
10873 case Intrinsic::x86_fma_vfmsub_pd:
10874 case Intrinsic::x86_fma_vfmsub_ps_256:
10875 case Intrinsic::x86_fma_vfmsub_pd_256:
10876 Opc = X86ISD::FMSUB;
10877 break;
10878 case Intrinsic::x86_fma_vfnmadd_ps:
10879 case Intrinsic::x86_fma_vfnmadd_pd:
10880 case Intrinsic::x86_fma_vfnmadd_ps_256:
10881 case Intrinsic::x86_fma_vfnmadd_pd_256:
10882 Opc = X86ISD::FNMADD;
10883 break;
10884 case Intrinsic::x86_fma_vfnmsub_ps:
10885 case Intrinsic::x86_fma_vfnmsub_pd:
10886 case Intrinsic::x86_fma_vfnmsub_ps_256:
10887 case Intrinsic::x86_fma_vfnmsub_pd_256:
10888 Opc = X86ISD::FNMSUB;
10889 break;
10890 case Intrinsic::x86_fma_vfmaddsub_ps:
10891 case Intrinsic::x86_fma_vfmaddsub_pd:
10892 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10893 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10894 Opc = X86ISD::FMADDSUB;
10895 break;
10896 case Intrinsic::x86_fma_vfmsubadd_ps:
10897 case Intrinsic::x86_fma_vfmsubadd_pd:
10898 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10899 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10900 Opc = X86ISD::FMSUBADD;
10901 break;
10902 }
10903
10904 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10905 Op.getOperand(2), Op.getOperand(3));
10906 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010907 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010908}
Evan Cheng72261582005-12-20 06:22:03 +000010909
Craig Topper55b24052012-09-11 06:15:32 +000010910static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010911 DebugLoc dl = Op.getDebugLoc();
10912 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10913 switch (IntNo) {
10914 default: return SDValue(); // Don't custom lower most intrinsics.
10915
Michael Liaoc26392a2013-03-28 23:41:26 +000010916 // RDRAND/RDSEED intrinsics.
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010917 case Intrinsic::x86_rdrand_16:
10918 case Intrinsic::x86_rdrand_32:
Michael Liaoc26392a2013-03-28 23:41:26 +000010919 case Intrinsic::x86_rdrand_64:
10920 case Intrinsic::x86_rdseed_16:
10921 case Intrinsic::x86_rdseed_32:
10922 case Intrinsic::x86_rdseed_64: {
10923 unsigned Opcode = (IntNo == Intrinsic::x86_rdseed_16 ||
10924 IntNo == Intrinsic::x86_rdseed_32 ||
10925 IntNo == Intrinsic::x86_rdseed_64) ? X86ISD::RDSEED :
10926 X86ISD::RDRAND;
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010927 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010928 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
Michael Liaoc26392a2013-03-28 23:41:26 +000010929 SDValue Result = DAG.getNode(Opcode, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010930
Michael Liaoc26392a2013-03-28 23:41:26 +000010931 // If the value returned by RDRAND/RDSEED was valid (CF=1), return 1.
10932 // Otherwise return the value from Rand, which is always 0, casted to i32.
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010933 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10934 DAG.getConstant(1, Op->getValueType(1)),
10935 DAG.getConstant(X86::COND_B, MVT::i32),
10936 SDValue(Result.getNode(), 1) };
10937 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10938 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10939 Ops, 4);
10940
10941 // Return { result, isValid, chain }.
10942 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010943 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010944 }
Michael Liaof8fd8832013-03-26 22:47:01 +000010945
10946 // XTEST intrinsics.
10947 case Intrinsic::x86_xtest: {
10948 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Other);
10949 SDValue InTrans = DAG.getNode(X86ISD::XTEST, dl, VTs, Op.getOperand(0));
10950 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10951 DAG.getConstant(X86::COND_NE, MVT::i8),
10952 InTrans);
10953 SDValue Ret = DAG.getNode(ISD::ZERO_EXTEND, dl, Op->getValueType(0), SetCC);
10954 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(),
10955 Ret, SDValue(InTrans.getNode(), 1));
10956 }
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010957 }
10958}
10959
Dan Gohmand858e902010-04-17 15:26:15 +000010960SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10961 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010962 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10963 MFI->setReturnAddressIsTaken(true);
10964
Bill Wendling64e87322009-01-16 19:25:27 +000010965 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010966 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010967 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010968
10969 if (Depth > 0) {
10970 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10971 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010972 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10973 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10974 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010975 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010976 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010977 }
10978
10979 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010980 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010981 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010982 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010983}
10984
Dan Gohmand858e902010-04-17 15:26:15 +000010985SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010986 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10987 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010988
Owen Andersone50ed302009-08-10 22:56:29 +000010989 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010990 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010991 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10992 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010993 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010994 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010995 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10996 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010997 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010998 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010999}
11000
Dan Gohman475871a2008-07-27 21:46:04 +000011001SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000011002 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011003 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011004}
11005
Dan Gohmand858e902010-04-17 15:26:15 +000011006SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011007 SDValue Chain = Op.getOperand(0);
11008 SDValue Offset = Op.getOperand(1);
11009 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011010 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011011
Dan Gohmand8816272010-08-11 18:14:00 +000011012 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
11013 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
11014 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000011015 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011016
Dan Gohmand8816272010-08-11 18:14:00 +000011017 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011018 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011019 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000011020 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
11021 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000011022 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011023
Dale Johannesene4d209d2009-02-03 20:21:25 +000011024 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011025 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000011026 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011027}
11028
Michael Liao6c0e04c2012-10-15 22:39:43 +000011029SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
11030 SelectionDAG &DAG) const {
11031 DebugLoc DL = Op.getDebugLoc();
11032 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
11033 DAG.getVTList(MVT::i32, MVT::Other),
11034 Op.getOperand(0), Op.getOperand(1));
11035}
11036
11037SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
11038 SelectionDAG &DAG) const {
11039 DebugLoc DL = Op.getDebugLoc();
11040 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
11041 Op.getOperand(0), Op.getOperand(1));
11042}
11043
Craig Topper55b24052012-09-11 06:15:32 +000011044static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000011045 return Op.getOperand(0);
11046}
11047
11048SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
11049 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011050 SDValue Root = Op.getOperand(0);
11051 SDValue Trmp = Op.getOperand(1); // trampoline
11052 SDValue FPtr = Op.getOperand(2); // nested function
11053 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011054 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011055
Dan Gohman69de1932008-02-06 22:27:42 +000011056 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000011057 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011058
11059 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000011060 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000011061
11062 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000011063 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
11064 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011065
Michael Liao7abf67a2012-10-04 19:50:43 +000011066 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11067 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011068
11069 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11070
11071 // Load the pointer to the nested function into R11.
11072 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011073 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011074 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011075 Addr, MachinePointerInfo(TrmpAddr),
11076 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011077
Owen Anderson825b72b2009-08-11 20:47:22 +000011078 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11079 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011080 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11081 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011082 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011083
11084 // Load the 'nest' parameter value into R10.
11085 // R10 is specified in X86CallingConv.td
11086 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011087 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11088 DAG.getConstant(10, MVT::i64));
11089 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011090 Addr, MachinePointerInfo(TrmpAddr, 10),
11091 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011092
Owen Anderson825b72b2009-08-11 20:47:22 +000011093 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11094 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011095 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11096 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011097 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011098
11099 // Jump to the nested function.
11100 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011101 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11102 DAG.getConstant(20, MVT::i64));
11103 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011104 Addr, MachinePointerInfo(TrmpAddr, 20),
11105 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011106
11107 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011108 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11109 DAG.getConstant(22, MVT::i64));
11110 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011111 MachinePointerInfo(TrmpAddr, 22),
11112 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011113
Duncan Sands4a544a72011-09-06 13:37:06 +000011114 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011115 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011116 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011117 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011118 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011119 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011120
11121 switch (CC) {
11122 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011123 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011124 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011125 case CallingConv::X86_StdCall: {
11126 // Pass 'nest' parameter in ECX.
11127 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011128 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011129
11130 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011131 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011132 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011133
Chris Lattner58d74912008-03-12 17:45:29 +000011134 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011135 unsigned InRegCount = 0;
11136 unsigned Idx = 1;
11137
11138 for (FunctionType::param_iterator I = FTy->param_begin(),
11139 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011140 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011141 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011142 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011143
11144 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011145 report_fatal_error("Nest register in use - reduce number of inreg"
11146 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011147 }
11148 }
11149 break;
11150 }
11151 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011152 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011153 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011154 // Pass 'nest' parameter in EAX.
11155 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011156 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011157 break;
11158 }
11159
Dan Gohman475871a2008-07-27 21:46:04 +000011160 SDValue OutChains[4];
11161 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011162
Owen Anderson825b72b2009-08-11 20:47:22 +000011163 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11164 DAG.getConstant(10, MVT::i32));
11165 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011166
Chris Lattnera62fe662010-02-05 19:20:30 +000011167 // This is storing the opcode for MOV32ri.
11168 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011169 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011170 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011171 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011172 Trmp, MachinePointerInfo(TrmpAddr),
11173 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011174
Owen Anderson825b72b2009-08-11 20:47:22 +000011175 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11176 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011177 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11178 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011179 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011180
Chris Lattnera62fe662010-02-05 19:20:30 +000011181 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011182 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11183 DAG.getConstant(5, MVT::i32));
11184 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011185 MachinePointerInfo(TrmpAddr, 5),
11186 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011187
Owen Anderson825b72b2009-08-11 20:47:22 +000011188 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11189 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011190 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11191 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011192 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011193
Duncan Sands4a544a72011-09-06 13:37:06 +000011194 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011195 }
11196}
11197
Dan Gohmand858e902010-04-17 15:26:15 +000011198SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11199 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011200 /*
11201 The rounding mode is in bits 11:10 of FPSR, and has the following
11202 settings:
11203 00 Round to nearest
11204 01 Round to -inf
11205 10 Round to +inf
11206 11 Round to 0
11207
11208 FLT_ROUNDS, on the other hand, expects the following:
11209 -1 Undefined
11210 0 Round to 0
11211 1 Round to nearest
11212 2 Round to +inf
11213 3 Round to -inf
11214
11215 To perform the conversion, we do:
11216 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11217 */
11218
11219 MachineFunction &MF = DAG.getMachineFunction();
11220 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011221 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011222 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011223 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011224 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011225
11226 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011227 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011228 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011229
Chris Lattner2156b792010-09-22 01:11:26 +000011230 MachineMemOperand *MMO =
11231 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11232 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011233
Chris Lattner2156b792010-09-22 01:11:26 +000011234 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11235 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11236 DAG.getVTList(MVT::Other),
11237 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011238
11239 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011240 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011241 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011242
11243 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011244 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011245 DAG.getNode(ISD::SRL, DL, MVT::i16,
11246 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011247 CWD, DAG.getConstant(0x800, MVT::i16)),
11248 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011249 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011250 DAG.getNode(ISD::SRL, DL, MVT::i16,
11251 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011252 CWD, DAG.getConstant(0x400, MVT::i16)),
11253 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011254
Dan Gohman475871a2008-07-27 21:46:04 +000011255 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011256 DAG.getNode(ISD::AND, DL, MVT::i16,
11257 DAG.getNode(ISD::ADD, DL, MVT::i16,
11258 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011259 DAG.getConstant(1, MVT::i16)),
11260 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011261
Duncan Sands83ec4b62008-06-06 12:08:01 +000011262 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011263 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011264}
11265
Craig Topper55b24052012-09-11 06:15:32 +000011266static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011267 EVT VT = Op.getValueType();
11268 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011269 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011270 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011271
11272 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011273 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011274 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011275 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011276 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011277 }
Evan Cheng18efe262007-12-14 02:13:44 +000011278
Evan Cheng152804e2007-12-14 08:30:15 +000011279 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011280 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011281 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011282
11283 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011284 SDValue Ops[] = {
11285 Op,
11286 DAG.getConstant(NumBits+NumBits-1, OpVT),
11287 DAG.getConstant(X86::COND_E, MVT::i8),
11288 Op.getValue(1)
11289 };
11290 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011291
11292 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011293 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011294
Owen Anderson825b72b2009-08-11 20:47:22 +000011295 if (VT == MVT::i8)
11296 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011297 return Op;
11298}
11299
Craig Topper55b24052012-09-11 06:15:32 +000011300static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011301 EVT VT = Op.getValueType();
11302 EVT OpVT = VT;
11303 unsigned NumBits = VT.getSizeInBits();
11304 DebugLoc dl = Op.getDebugLoc();
11305
11306 Op = Op.getOperand(0);
11307 if (VT == MVT::i8) {
11308 // Zero extend to i32 since there is not an i8 bsr.
11309 OpVT = MVT::i32;
11310 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11311 }
11312
11313 // Issue a bsr (scan bits in reverse).
11314 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11315 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11316
11317 // And xor with NumBits-1.
11318 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11319
11320 if (VT == MVT::i8)
11321 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11322 return Op;
11323}
11324
Craig Topper55b24052012-09-11 06:15:32 +000011325static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011326 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011327 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011328 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011329 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011330
11331 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011332 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011333 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011334
11335 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011336 SDValue Ops[] = {
11337 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011338 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011339 DAG.getConstant(X86::COND_E, MVT::i8),
11340 Op.getValue(1)
11341 };
Chandler Carruth77821022011-12-24 12:12:34 +000011342 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011343}
11344
Craig Topper13894fa2011-08-24 06:14:18 +000011345// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11346// ones, and then concatenate the result back.
11347static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011348 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011349
Craig Topper7a9a28b2012-08-12 02:23:29 +000011350 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011351 "Unsupported value type for operation");
11352
Craig Topper66ddd152012-04-27 22:54:43 +000011353 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011354 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011355
11356 // Extract the LHS vectors
11357 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011358 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11359 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011360
11361 // Extract the RHS vectors
11362 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011363 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11364 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011365
11366 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11367 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11368
11369 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11370 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11371 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11372}
11373
Craig Topper55b24052012-09-11 06:15:32 +000011374static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011375 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011376 Op.getValueType().isInteger() &&
11377 "Only handle AVX 256-bit vector integer operation");
11378 return Lower256IntArith(Op, DAG);
11379}
11380
Craig Topper55b24052012-09-11 06:15:32 +000011381static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011382 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011383 Op.getValueType().isInteger() &&
11384 "Only handle AVX 256-bit vector integer operation");
11385 return Lower256IntArith(Op, DAG);
11386}
11387
Craig Topper55b24052012-09-11 06:15:32 +000011388static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11389 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011390 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011391 EVT VT = Op.getValueType();
11392
11393 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011394 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011395 return Lower256IntArith(Op, DAG);
11396
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011397 SDValue A = Op.getOperand(0);
11398 SDValue B = Op.getOperand(1);
11399
11400 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11401 if (VT == MVT::v4i32) {
11402 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11403 "Should not custom lower when pmuldq is available!");
11404
11405 // Extract the odd parts.
11406 const int UnpackMask[] = { 1, -1, 3, -1 };
11407 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11408 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11409
11410 // Multiply the even parts.
11411 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11412 // Now multiply odd parts.
11413 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11414
11415 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11416 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11417
11418 // Merge the two vectors back together with a shuffle. This expands into 2
11419 // shuffles.
11420 const int ShufMask[] = { 0, 4, 2, 6 };
11421 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11422 }
11423
Craig Topper5b209e82012-02-05 03:14:49 +000011424 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11425 "Only know how to lower V2I64/V4I64 multiply");
11426
Craig Topper5b209e82012-02-05 03:14:49 +000011427 // Ahi = psrlqi(a, 32);
11428 // Bhi = psrlqi(b, 32);
11429 //
11430 // AloBlo = pmuludq(a, b);
11431 // AloBhi = pmuludq(a, Bhi);
11432 // AhiBlo = pmuludq(Ahi, b);
11433
11434 // AloBhi = psllqi(AloBhi, 32);
11435 // AhiBlo = psllqi(AhiBlo, 32);
11436 // return AloBlo + AloBhi + AhiBlo;
11437
Craig Topper5b209e82012-02-05 03:14:49 +000011438 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011439
Craig Topper5b209e82012-02-05 03:14:49 +000011440 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11441 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011442
Craig Topper5b209e82012-02-05 03:14:49 +000011443 // Bit cast to 32-bit vectors for MULUDQ
11444 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11445 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11446 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11447 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11448 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011449
Craig Topper5b209e82012-02-05 03:14:49 +000011450 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11451 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11452 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011453
Craig Topper5b209e82012-02-05 03:14:49 +000011454 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11455 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011456
Dale Johannesene4d209d2009-02-03 20:21:25 +000011457 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011458 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011459}
11460
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011461SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11462 EVT VT = Op.getValueType();
11463 EVT EltTy = VT.getVectorElementType();
11464 unsigned NumElts = VT.getVectorNumElements();
11465 SDValue N0 = Op.getOperand(0);
11466 DebugLoc dl = Op.getDebugLoc();
11467
11468 // Lower sdiv X, pow2-const.
11469 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11470 if (!C)
11471 return SDValue();
11472
11473 APInt SplatValue, SplatUndef;
11474 unsigned MinSplatBits;
11475 bool HasAnyUndefs;
11476 if (!C->isConstantSplat(SplatValue, SplatUndef, MinSplatBits, HasAnyUndefs))
11477 return SDValue();
11478
11479 if ((SplatValue != 0) &&
11480 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11481 unsigned lg2 = SplatValue.countTrailingZeros();
11482 // Splat the sign bit.
11483 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11484 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11485 // Add (N0 < 0) ? abs2 - 1 : 0;
11486 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11487 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11488 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11489 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11490 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11491
11492 // If we're dividing by a positive value, we're done. Otherwise, we must
11493 // negate the result.
11494 if (SplatValue.isNonNegative())
11495 return SRA;
11496
11497 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11498 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11499 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11500 }
11501 return SDValue();
11502}
11503
Michael Liao4b7ab122013-03-20 02:20:36 +000011504static SDValue LowerScalarImmediateShift(SDValue Op, SelectionDAG &DAG,
11505 const X86Subtarget *Subtarget) {
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011506 EVT VT = Op.getValueType();
11507 DebugLoc dl = Op.getDebugLoc();
11508 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011509 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011510
Nadav Rotem43012222011-05-11 08:12:09 +000011511 // Optimize shl/srl/sra with constant shift amount.
11512 if (isSplatVector(Amt.getNode())) {
11513 SDValue SclrAmt = Amt->getOperand(0);
11514 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11515 uint64_t ShiftAmt = C->getZExtValue();
11516
Craig Toppered2e13d2012-01-22 19:15:14 +000011517 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011518 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011519 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11520 if (Op.getOpcode() == ISD::SHL)
11521 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11522 DAG.getConstant(ShiftAmt, MVT::i32));
11523 if (Op.getOpcode() == ISD::SRL)
11524 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11525 DAG.getConstant(ShiftAmt, MVT::i32));
11526 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11527 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11528 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011529 }
11530
Craig Toppered2e13d2012-01-22 19:15:14 +000011531 if (VT == MVT::v16i8) {
11532 if (Op.getOpcode() == ISD::SHL) {
11533 // Make a large shift.
11534 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11535 DAG.getConstant(ShiftAmt, MVT::i32));
11536 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11537 // Zero out the rightmost bits.
11538 SmallVector<SDValue, 16> V(16,
11539 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11540 MVT::i8));
11541 return DAG.getNode(ISD::AND, dl, VT, SHL,
11542 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011543 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011544 if (Op.getOpcode() == ISD::SRL) {
11545 // Make a large shift.
11546 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11547 DAG.getConstant(ShiftAmt, MVT::i32));
11548 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11549 // Zero out the leftmost bits.
11550 SmallVector<SDValue, 16> V(16,
11551 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11552 MVT::i8));
11553 return DAG.getNode(ISD::AND, dl, VT, SRL,
11554 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11555 }
11556 if (Op.getOpcode() == ISD::SRA) {
11557 if (ShiftAmt == 7) {
11558 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011559 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011560 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011561 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011562
Craig Toppered2e13d2012-01-22 19:15:14 +000011563 // R s>> a === ((R u>> a) ^ m) - m
11564 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11565 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11566 MVT::i8));
11567 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11568 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11569 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11570 return Res;
11571 }
Craig Topper731dfd02012-04-23 03:42:40 +000011572 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011573 }
Craig Topper46154eb2011-11-11 07:39:23 +000011574
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011575 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011576 if (Op.getOpcode() == ISD::SHL) {
11577 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011578 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11579 DAG.getConstant(ShiftAmt, MVT::i32));
11580 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011581 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011582 SmallVector<SDValue, 32> V(32,
11583 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11584 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011585 return DAG.getNode(ISD::AND, dl, VT, SHL,
11586 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011587 }
Craig Topper0d86d462011-11-20 00:12:05 +000011588 if (Op.getOpcode() == ISD::SRL) {
11589 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011590 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11591 DAG.getConstant(ShiftAmt, MVT::i32));
11592 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011593 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011594 SmallVector<SDValue, 32> V(32,
11595 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11596 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011597 return DAG.getNode(ISD::AND, dl, VT, SRL,
11598 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11599 }
11600 if (Op.getOpcode() == ISD::SRA) {
11601 if (ShiftAmt == 7) {
11602 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011603 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011604 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011605 }
11606
11607 // R s>> a === ((R u>> a) ^ m) - m
11608 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11609 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11610 MVT::i8));
11611 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11612 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11613 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11614 return Res;
11615 }
Craig Topper731dfd02012-04-23 03:42:40 +000011616 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011617 }
Nadav Rotem43012222011-05-11 08:12:09 +000011618 }
11619 }
11620
Michael Liao42317cc2013-03-20 02:33:21 +000011621 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11622 if (!Subtarget->is64Bit() &&
11623 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11624 Amt.getOpcode() == ISD::BITCAST &&
11625 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11626 Amt = Amt.getOperand(0);
11627 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11628 VT.getVectorNumElements();
11629 unsigned RatioInLog2 = Log2_32_Ceil(Ratio);
11630 uint64_t ShiftAmt = 0;
11631 for (unsigned i = 0; i != Ratio; ++i) {
11632 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Amt.getOperand(i));
11633 if (C == 0)
11634 return SDValue();
11635 // 6 == Log2(64)
11636 ShiftAmt |= C->getZExtValue() << (i * (1 << (6 - RatioInLog2)));
11637 }
11638 // Check remaining shift amounts.
11639 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11640 uint64_t ShAmt = 0;
11641 for (unsigned j = 0; j != Ratio; ++j) {
11642 ConstantSDNode *C =
11643 dyn_cast<ConstantSDNode>(Amt.getOperand(i + j));
11644 if (C == 0)
11645 return SDValue();
11646 // 6 == Log2(64)
11647 ShAmt |= C->getZExtValue() << (j * (1 << (6 - RatioInLog2)));
11648 }
11649 if (ShAmt != ShiftAmt)
11650 return SDValue();
11651 }
11652 switch (Op.getOpcode()) {
11653 default:
11654 llvm_unreachable("Unknown shift opcode!");
11655 case ISD::SHL:
11656 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11657 DAG.getConstant(ShiftAmt, MVT::i32));
11658 case ISD::SRL:
11659 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11660 DAG.getConstant(ShiftAmt, MVT::i32));
11661 case ISD::SRA:
11662 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11663 DAG.getConstant(ShiftAmt, MVT::i32));
11664 }
11665 }
11666
11667 return SDValue();
11668}
11669
11670static SDValue LowerScalarVariableShift(SDValue Op, SelectionDAG &DAG,
11671 const X86Subtarget* Subtarget) {
11672 EVT VT = Op.getValueType();
11673 DebugLoc dl = Op.getDebugLoc();
11674 SDValue R = Op.getOperand(0);
11675 SDValue Amt = Op.getOperand(1);
11676
11677 if ((VT == MVT::v2i64 && Op.getOpcode() != ISD::SRA) ||
11678 VT == MVT::v4i32 || VT == MVT::v8i16 ||
11679 (Subtarget->hasInt256() &&
11680 ((VT == MVT::v4i64 && Op.getOpcode() != ISD::SRA) ||
11681 VT == MVT::v8i32 || VT == MVT::v16i16))) {
11682 SDValue BaseShAmt;
11683 EVT EltVT = VT.getVectorElementType();
11684
11685 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11686 unsigned NumElts = VT.getVectorNumElements();
11687 unsigned i, j;
11688 for (i = 0; i != NumElts; ++i) {
11689 if (Amt.getOperand(i).getOpcode() == ISD::UNDEF)
11690 continue;
11691 break;
11692 }
11693 for (j = i; j != NumElts; ++j) {
11694 SDValue Arg = Amt.getOperand(j);
11695 if (Arg.getOpcode() == ISD::UNDEF) continue;
11696 if (Arg != Amt.getOperand(i))
11697 break;
11698 }
11699 if (i != NumElts && j == NumElts)
11700 BaseShAmt = Amt.getOperand(i);
11701 } else {
11702 if (Amt.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11703 Amt = Amt.getOperand(0);
11704 if (Amt.getOpcode() == ISD::VECTOR_SHUFFLE &&
11705 cast<ShuffleVectorSDNode>(Amt)->isSplat()) {
11706 SDValue InVec = Amt.getOperand(0);
11707 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11708 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11709 unsigned i = 0;
11710 for (; i != NumElts; ++i) {
11711 SDValue Arg = InVec.getOperand(i);
11712 if (Arg.getOpcode() == ISD::UNDEF) continue;
11713 BaseShAmt = Arg;
11714 break;
11715 }
11716 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11717 if (ConstantSDNode *C =
11718 dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
11719 unsigned SplatIdx =
11720 cast<ShuffleVectorSDNode>(Amt)->getSplatIndex();
11721 if (C->getZExtValue() == SplatIdx)
11722 BaseShAmt = InVec.getOperand(1);
11723 }
11724 }
11725 if (BaseShAmt.getNode() == 0)
11726 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Amt,
11727 DAG.getIntPtrConstant(0));
11728 }
11729 }
11730
11731 if (BaseShAmt.getNode()) {
11732 if (EltVT.bitsGT(MVT::i32))
11733 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
11734 else if (EltVT.bitsLT(MVT::i32))
11735 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, BaseShAmt);
11736
11737 switch (Op.getOpcode()) {
11738 default:
11739 llvm_unreachable("Unknown shift opcode!");
11740 case ISD::SHL:
11741 switch (VT.getSimpleVT().SimpleTy) {
11742 default: return SDValue();
11743 case MVT::v2i64:
11744 case MVT::v4i32:
11745 case MVT::v8i16:
11746 case MVT::v4i64:
11747 case MVT::v8i32:
11748 case MVT::v16i16:
11749 return getTargetVShiftNode(X86ISD::VSHLI, dl, VT, R, BaseShAmt, DAG);
11750 }
11751 case ISD::SRA:
11752 switch (VT.getSimpleVT().SimpleTy) {
11753 default: return SDValue();
11754 case MVT::v4i32:
11755 case MVT::v8i16:
11756 case MVT::v8i32:
11757 case MVT::v16i16:
11758 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, R, BaseShAmt, DAG);
11759 }
11760 case ISD::SRL:
11761 switch (VT.getSimpleVT().SimpleTy) {
11762 default: return SDValue();
11763 case MVT::v2i64:
11764 case MVT::v4i32:
11765 case MVT::v8i16:
11766 case MVT::v4i64:
11767 case MVT::v8i32:
11768 case MVT::v16i16:
11769 return getTargetVShiftNode(X86ISD::VSRLI, dl, VT, R, BaseShAmt, DAG);
11770 }
11771 }
11772 }
11773 }
11774
11775 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11776 if (!Subtarget->is64Bit() &&
11777 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11778 Amt.getOpcode() == ISD::BITCAST &&
11779 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11780 Amt = Amt.getOperand(0);
11781 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11782 VT.getVectorNumElements();
11783 std::vector<SDValue> Vals(Ratio);
11784 for (unsigned i = 0; i != Ratio; ++i)
11785 Vals[i] = Amt.getOperand(i);
11786 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11787 for (unsigned j = 0; j != Ratio; ++j)
11788 if (Vals[j] != Amt.getOperand(i + j))
11789 return SDValue();
11790 }
11791 switch (Op.getOpcode()) {
11792 default:
11793 llvm_unreachable("Unknown shift opcode!");
11794 case ISD::SHL:
11795 return DAG.getNode(X86ISD::VSHL, dl, VT, R, Op.getOperand(1));
11796 case ISD::SRL:
11797 return DAG.getNode(X86ISD::VSRL, dl, VT, R, Op.getOperand(1));
11798 case ISD::SRA:
11799 return DAG.getNode(X86ISD::VSRA, dl, VT, R, Op.getOperand(1));
11800 }
11801 }
11802
Michael Liao4b7ab122013-03-20 02:20:36 +000011803 return SDValue();
11804}
11805
11806SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11807
11808 EVT VT = Op.getValueType();
11809 DebugLoc dl = Op.getDebugLoc();
11810 SDValue R = Op.getOperand(0);
11811 SDValue Amt = Op.getOperand(1);
11812 SDValue V;
11813
11814 if (!Subtarget->hasSSE2())
11815 return SDValue();
11816
11817 V = LowerScalarImmediateShift(Op, DAG, Subtarget);
11818 if (V.getNode())
11819 return V;
11820
Michael Liao42317cc2013-03-20 02:33:21 +000011821 V = LowerScalarVariableShift(Op, DAG, Subtarget);
11822 if (V.getNode())
11823 return V;
11824
Michael Liao5c5f1902013-03-20 02:28:20 +000011825 // AVX2 has VPSLLV/VPSRAV/VPSRLV.
11826 if (Subtarget->hasInt256()) {
11827 if (Op.getOpcode() == ISD::SRL &&
11828 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
11829 VT == MVT::v4i64 || VT == MVT::v8i32))
11830 return Op;
11831 if (Op.getOpcode() == ISD::SHL &&
11832 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
11833 VT == MVT::v4i64 || VT == MVT::v8i32))
11834 return Op;
11835 if (Op.getOpcode() == ISD::SRA && (VT == MVT::v4i32 || VT == MVT::v8i32))
11836 return Op;
11837 }
11838
Nadav Rotem43012222011-05-11 08:12:09 +000011839 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011840 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011841 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));
Nate Begeman51409212010-07-28 00:21:48 +000011842
Benjamin Kramer9fa92512013-02-04 15:19:25 +000011843 Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011844 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011845 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11846 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11847 }
Nadav Rotem43012222011-05-11 08:12:09 +000011848 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011849 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011850
Nate Begeman51409212010-07-28 00:21:48 +000011851 // a = a << 5;
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011852 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT));
Craig Toppered2e13d2012-01-22 19:15:14 +000011853 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011854
Lang Hames8b99c1e2011-12-17 01:08:46 +000011855 // Turn 'a' into a mask suitable for VSELECT
11856 SDValue VSelM = DAG.getConstant(0x80, VT);
11857 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011858 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011859
Lang Hames8b99c1e2011-12-17 01:08:46 +000011860 SDValue CM1 = DAG.getConstant(0x0f, VT);
11861 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011862
Lang Hames8b99c1e2011-12-17 01:08:46 +000011863 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11864 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011865 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11866 DAG.getConstant(4, MVT::i32), DAG);
11867 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011868 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11869
Nate Begeman51409212010-07-28 00:21:48 +000011870 // a += a
11871 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011872 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011873 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011874
Lang Hames8b99c1e2011-12-17 01:08:46 +000011875 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11876 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011877 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11878 DAG.getConstant(2, MVT::i32), DAG);
11879 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011880 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11881
Nate Begeman51409212010-07-28 00:21:48 +000011882 // a += a
11883 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011884 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011885 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011886
Lang Hames8b99c1e2011-12-17 01:08:46 +000011887 // return VSELECT(r, r+r, a);
11888 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011889 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011890 return R;
11891 }
Craig Topper46154eb2011-11-11 07:39:23 +000011892
11893 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011894 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011895 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011896 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11897 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11898
11899 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011900 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11901 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011902
11903 // Recreate the shift amount vectors
11904 SDValue Amt1, Amt2;
11905 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11906 // Constant shift amount
11907 SmallVector<SDValue, 4> Amt1Csts;
11908 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011909 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011910 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011911 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011912 Amt2Csts.push_back(Amt->getOperand(i));
11913
11914 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11915 &Amt1Csts[0], NumElems/2);
11916 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11917 &Amt2Csts[0], NumElems/2);
11918 } else {
11919 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011920 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11921 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011922 }
11923
11924 // Issue new vector shifts for the smaller types
11925 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11926 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11927
11928 // Concatenate the result back
11929 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11930 }
11931
Nate Begeman51409212010-07-28 00:21:48 +000011932 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011933}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011934
Craig Topper55b24052012-09-11 06:15:32 +000011935static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011936 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11937 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011938 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11939 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011940 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011941 SDValue LHS = N->getOperand(0);
11942 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011943 unsigned BaseOp = 0;
11944 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011945 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011946 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011947 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011948 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011949 // A subtract of one will be selected as a INC. Note that INC doesn't
11950 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011951 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11952 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011953 BaseOp = X86ISD::INC;
11954 Cond = X86::COND_O;
11955 break;
11956 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011957 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011958 Cond = X86::COND_O;
11959 break;
11960 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011961 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011962 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011963 break;
11964 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011965 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11966 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011967 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11968 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011969 BaseOp = X86ISD::DEC;
11970 Cond = X86::COND_O;
11971 break;
11972 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011973 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011974 Cond = X86::COND_O;
11975 break;
11976 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011977 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011978 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011979 break;
11980 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011981 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011982 Cond = X86::COND_O;
11983 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011984 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11985 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11986 MVT::i32);
11987 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011988
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011989 SDValue SetCC =
11990 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11991 DAG.getConstant(X86::COND_O, MVT::i32),
11992 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011993
Dan Gohman6e5fda22011-07-22 18:45:15 +000011994 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011995 }
Bill Wendling74c37652008-12-09 22:08:41 +000011996 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011997
Bill Wendling61edeb52008-12-02 01:06:39 +000011998 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011999 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012000 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000012001
Bill Wendling61edeb52008-12-02 01:06:39 +000012002 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000012003 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
12004 DAG.getConstant(Cond, MVT::i32),
12005 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000012006
Dan Gohman6e5fda22011-07-22 18:45:15 +000012007 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000012008}
12009
Chad Rosier30450e82011-12-22 22:35:21 +000012010SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
12011 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012012 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000012013 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
12014 EVT VT = Op.getValueType();
12015
Craig Toppered2e13d2012-01-22 19:15:14 +000012016 if (!Subtarget->hasSSE2() || !VT.isVector())
12017 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012018
Craig Toppered2e13d2012-01-22 19:15:14 +000012019 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
12020 ExtraVT.getScalarType().getSizeInBits();
12021 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
12022
12023 switch (VT.getSimpleVT().SimpleTy) {
12024 default: return SDValue();
12025 case MVT::v8i32:
12026 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012027 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012028 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012029 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000012030 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000012031 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000012032
Craig Toppered2e13d2012-01-22 19:15:14 +000012033 // Extract the LHS vectors
12034 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000012035 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
12036 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000012037
Craig Toppered2e13d2012-01-22 19:15:14 +000012038 MVT EltVT = VT.getVectorElementType().getSimpleVT();
12039 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000012040
Craig Toppered2e13d2012-01-22 19:15:14 +000012041 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000012042 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000012043 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
12044 ExtraNumElems/2);
12045 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000012046
Craig Toppered2e13d2012-01-22 19:15:14 +000012047 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
12048 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000012049
Dmitri Gribenko2de05722012-09-10 21:26:47 +000012050 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000012051 }
12052 // fall through
12053 case MVT::v4i32:
12054 case MVT::v8i16: {
Nadav Rotemb05130e2013-03-19 18:38:27 +000012055 // (sext (vzext x)) -> (vsext x)
12056 SDValue Op0 = Op.getOperand(0);
12057 SDValue Op00 = Op0.getOperand(0);
12058 SDValue Tmp1;
12059 // Hopefully, this VECTOR_SHUFFLE is just a VZEXT.
12060 if (Op0.getOpcode() == ISD::BITCAST &&
12061 Op00.getOpcode() == ISD::VECTOR_SHUFFLE)
12062 Tmp1 = LowerVectorIntExtend(Op00, DAG);
12063 if (Tmp1.getNode()) {
12064 SDValue Tmp1Op0 = Tmp1.getOperand(0);
12065 assert(Tmp1Op0.getOpcode() == X86ISD::VZEXT &&
12066 "This optimization is invalid without a VZEXT.");
12067 return DAG.getNode(X86ISD::VSEXT, dl, VT, Tmp1Op0.getOperand(0));
12068 }
12069
12070 // If the above didn't work, then just use Shift-Left + Shift-Right.
12071 Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT, Op0, ShAmt, DAG);
Craig Toppered2e13d2012-01-22 19:15:14 +000012072 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012073 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012074 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012075}
12076
Craig Topper55b24052012-09-11 06:15:32 +000012077static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
12078 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000012079 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012080
Eric Christopher77ed1352011-07-08 00:04:56 +000012081 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
12082 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000012083 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000012084 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000012085 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000012086 SDValue Ops[] = {
12087 DAG.getRegister(X86::ESP, MVT::i32), // Base
12088 DAG.getTargetConstant(1, MVT::i8), // Scale
12089 DAG.getRegister(0, MVT::i32), // Index
12090 DAG.getTargetConstant(0, MVT::i32), // Disp
12091 DAG.getRegister(0, MVT::i32), // Segment.
12092 Zero,
12093 Chain
12094 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000012095 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000012096 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
12097 array_lengthof(Ops));
12098 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000012099 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012100
Eric Christopher9a9d2752010-07-22 02:48:34 +000012101 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000012102 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000012103 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000012104
Chris Lattner132929a2010-08-14 17:26:09 +000012105 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
12106 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
12107 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
12108 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012109
Chris Lattner132929a2010-08-14 17:26:09 +000012110 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
12111 if (!Op1 && !Op2 && !Op3 && Op4)
12112 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000012113
Chris Lattner132929a2010-08-14 17:26:09 +000012114 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
12115 if (Op1 && !Op2 && !Op3 && !Op4)
12116 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000012117
12118 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000012119 // (MFENCE)>;
12120 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000012121}
12122
Craig Topper55b24052012-09-11 06:15:32 +000012123static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
12124 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000012125 DebugLoc dl = Op.getDebugLoc();
12126 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
12127 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
12128 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
12129 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
12130
12131 // The only fence that needs an instruction is a sequentially-consistent
12132 // cross-thread fence.
12133 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
12134 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
12135 // no-sse2). There isn't any reason to disable it if the target processor
12136 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000012137 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000012138 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
12139
12140 SDValue Chain = Op.getOperand(0);
12141 SDValue Zero = DAG.getConstant(0, MVT::i32);
12142 SDValue Ops[] = {
12143 DAG.getRegister(X86::ESP, MVT::i32), // Base
12144 DAG.getTargetConstant(1, MVT::i8), // Scale
12145 DAG.getRegister(0, MVT::i32), // Index
12146 DAG.getTargetConstant(0, MVT::i32), // Disp
12147 DAG.getRegister(0, MVT::i32), // Segment.
12148 Zero,
12149 Chain
12150 };
12151 SDNode *Res =
12152 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
12153 array_lengthof(Ops));
12154 return SDValue(Res, 0);
12155 }
12156
12157 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
12158 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
12159}
12160
Craig Topper55b24052012-09-11 06:15:32 +000012161static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
12162 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000012163 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012164 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000012165 unsigned Reg = 0;
12166 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000012167 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000012168 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000012169 case MVT::i8: Reg = X86::AL; size = 1; break;
12170 case MVT::i16: Reg = X86::AX; size = 2; break;
12171 case MVT::i32: Reg = X86::EAX; size = 4; break;
12172 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000012173 assert(Subtarget->is64Bit() && "Node not type legal!");
12174 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000012175 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000012176 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012177 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000012178 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000012179 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012180 Op.getOperand(1),
12181 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000012182 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012183 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012184 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012185 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
12186 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
12187 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000012188 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012189 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000012190 return cpOut;
12191}
12192
Craig Topper55b24052012-09-11 06:15:32 +000012193static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
12194 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000012195 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012196 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012197 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000012198 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000012199 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012200 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
12201 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000012202 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000012203 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
12204 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000012205 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000012206 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000012207 rdx.getValue(1)
12208 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000012209 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012210}
12211
Craig Topper55b24052012-09-11 06:15:32 +000012212SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000012213 EVT SrcVT = Op.getOperand(0).getValueType();
12214 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000012215 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000012216 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012217 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000012218 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012219 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000012220 // i64 <=> MMX conversions are Legal.
12221 if (SrcVT==MVT::i64 && DstVT.isVector())
12222 return Op;
12223 if (DstVT==MVT::i64 && SrcVT.isVector())
12224 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000012225 // MMX <=> MMX conversions are Legal.
12226 if (SrcVT.isVector() && DstVT.isVector())
12227 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000012228 // All other conversions need to be expanded.
12229 return SDValue();
12230}
Chris Lattner5b856542010-12-20 00:59:46 +000012231
Craig Topper55b24052012-09-11 06:15:32 +000012232static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012233 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000012234 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012235 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012236 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000012237 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000012238 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012239 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012240 Node->getOperand(0),
12241 Node->getOperand(1), negOp,
12242 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000012243 cast<AtomicSDNode>(Node)->getAlignment(),
12244 cast<AtomicSDNode>(Node)->getOrdering(),
12245 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000012246}
12247
Eli Friedman327236c2011-08-24 20:50:09 +000012248static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
12249 SDNode *Node = Op.getNode();
12250 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012251 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000012252
12253 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012254 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
12255 // FIXME: On 32-bit, store -> fist or movq would be more efficient
12256 // (The only way to get a 16-byte store is cmpxchg16b)
12257 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
12258 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
12259 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000012260 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
12261 cast<AtomicSDNode>(Node)->getMemoryVT(),
12262 Node->getOperand(0),
12263 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012264 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000012265 cast<AtomicSDNode>(Node)->getOrdering(),
12266 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000012267 return Swap.getValue(1);
12268 }
12269 // Other atomic stores have a simple pattern.
12270 return Op;
12271}
12272
Chris Lattner5b856542010-12-20 00:59:46 +000012273static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
12274 EVT VT = Op.getNode()->getValueType(0);
12275
12276 // Let legalize expand this if it isn't a legal type yet.
12277 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
12278 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012279
Chris Lattner5b856542010-12-20 00:59:46 +000012280 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012281
Chris Lattner5b856542010-12-20 00:59:46 +000012282 unsigned Opc;
12283 bool ExtraOp = false;
12284 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012285 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012286 case ISD::ADDC: Opc = X86ISD::ADD; break;
12287 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12288 case ISD::SUBC: Opc = X86ISD::SUB; break;
12289 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12290 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012291
Chris Lattner5b856542010-12-20 00:59:46 +000012292 if (!ExtraOp)
12293 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12294 Op.getOperand(1));
12295 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12296 Op.getOperand(1), Op.getOperand(2));
12297}
12298
Evan Cheng8688a582013-01-29 02:32:37 +000012299SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga66f40a2013-01-30 22:56:35 +000012300 assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
Eric Christophere187e252013-01-31 00:50:48 +000012301
Evan Cheng8688a582013-01-29 02:32:37 +000012302 // For MacOSX, we want to call an alternative entry point: __sincos_stret,
12303 // which returns the values in two XMM registers.
12304 DebugLoc dl = Op.getDebugLoc();
12305 SDValue Arg = Op.getOperand(0);
12306 EVT ArgVT = Arg.getValueType();
12307 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Eric Christophere187e252013-01-31 00:50:48 +000012308
Evan Cheng8688a582013-01-29 02:32:37 +000012309 ArgListTy Args;
12310 ArgListEntry Entry;
Eric Christophere187e252013-01-31 00:50:48 +000012311
Evan Cheng8688a582013-01-29 02:32:37 +000012312 Entry.Node = Arg;
12313 Entry.Ty = ArgTy;
12314 Entry.isSExt = false;
12315 Entry.isZExt = false;
12316 Args.push_back(Entry);
Evan Chenga66f40a2013-01-30 22:56:35 +000012317
12318 // Only optimize x86_64 for now. i386 is a bit messy. For f32,
12319 // the small struct {f32, f32} is returned in (eax, edx). For f64,
12320 // the results are returned via SRet in memory.
Evan Cheng8688a582013-01-29 02:32:37 +000012321 const char *LibcallName = (ArgVT == MVT::f64)
12322 ? "__sincos_stret" : "__sincosf_stret";
12323 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
Evan Chenga66f40a2013-01-30 22:56:35 +000012324
Evan Cheng8688a582013-01-29 02:32:37 +000012325 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
12326 TargetLowering::
Evan Chenga66f40a2013-01-30 22:56:35 +000012327 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy,
12328 false, false, false, false, 0,
12329 CallingConv::C, /*isTaillCall=*/false,
12330 /*doesNotRet=*/false, /*isReturnValueUsed*/true,
12331 Callee, Args, DAG, dl);
Evan Cheng8688a582013-01-29 02:32:37 +000012332 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Evan Cheng8688a582013-01-29 02:32:37 +000012333 return CallResult.first;
Evan Cheng8688a582013-01-29 02:32:37 +000012334}
12335
Evan Cheng0db9fe62006-04-25 20:13:52 +000012336/// LowerOperation - Provide custom lowering hooks for some operations.
12337///
Dan Gohmand858e902010-04-17 15:26:15 +000012338SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012339 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012340 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012341 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012342 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
12343 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12344 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012345 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012346 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012347 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012348 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012349 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12350 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12351 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012352 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12353 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012354 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12355 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12356 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012357 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012358 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012359 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012360 case ISD::SHL_PARTS:
12361 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012362 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012363 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012364 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012365 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012366 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12367 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12368 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012369 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012370 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012371 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012372 case ISD::FABS: return LowerFABS(Op, DAG);
12373 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012374 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012375 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012376 case ISD::SETCC: return LowerSETCC(Op, DAG);
12377 case ISD::SELECT: return LowerSELECT(Op, DAG);
12378 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012379 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012380 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012381 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012382 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012383 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012384 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012385 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12386 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012387 case ISD::FRAME_TO_ARGS_OFFSET:
12388 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012389 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012390 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012391 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12392 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012393 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12394 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012395 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012396 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012397 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012398 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012399 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012400 case ISD::SRA:
12401 case ISD::SRL:
12402 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012403 case ISD::SADDO:
12404 case ISD::UADDO:
12405 case ISD::SSUBO:
12406 case ISD::USUBO:
12407 case ISD::SMULO:
12408 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012409 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012410 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012411 case ISD::ADDC:
12412 case ISD::ADDE:
12413 case ISD::SUBC:
12414 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012415 case ISD::ADD: return LowerADD(Op, DAG);
12416 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012417 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng8688a582013-01-29 02:32:37 +000012418 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012419 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012420}
12421
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012422static void ReplaceATOMIC_LOAD(SDNode *Node,
12423 SmallVectorImpl<SDValue> &Results,
12424 SelectionDAG &DAG) {
12425 DebugLoc dl = Node->getDebugLoc();
12426 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12427
12428 // Convert wide load -> cmpxchg8b/cmpxchg16b
12429 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12430 // (The only way to get a 16-byte load is cmpxchg16b)
12431 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012432 SDValue Zero = DAG.getConstant(0, VT);
12433 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012434 Node->getOperand(0),
12435 Node->getOperand(1), Zero, Zero,
12436 cast<AtomicSDNode>(Node)->getMemOperand(),
12437 cast<AtomicSDNode>(Node)->getOrdering(),
12438 cast<AtomicSDNode>(Node)->getSynchScope());
12439 Results.push_back(Swap.getValue(0));
12440 Results.push_back(Swap.getValue(1));
12441}
12442
Craig Topperc0878702012-08-17 06:55:11 +000012443static void
Duncan Sands1607f052008-12-01 11:39:25 +000012444ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012445 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012446 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012447 assert (Node->getValueType(0) == MVT::i64 &&
12448 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012449
12450 SDValue Chain = Node->getOperand(0);
12451 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012452 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012453 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012454 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012455 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012456 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012457 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012458 SDValue Result =
12459 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12460 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012461 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012462 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012463 Results.push_back(Result.getValue(2));
12464}
12465
Duncan Sands126d9072008-07-04 11:47:58 +000012466/// ReplaceNodeResults - Replace a node with an illegal result type
12467/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012468void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12469 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012470 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012471 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012472 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012473 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012474 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012475 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012476 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012477 case ISD::ADDC:
12478 case ISD::ADDE:
12479 case ISD::SUBC:
12480 case ISD::SUBE:
12481 // We don't want to expand or promote these.
12482 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012483 case ISD::FP_TO_SINT:
12484 case ISD::FP_TO_UINT: {
12485 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12486
12487 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12488 return;
12489
Eli Friedman948e95a2009-05-23 09:59:16 +000012490 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012491 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012492 SDValue FIST = Vals.first, StackSlot = Vals.second;
12493 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012494 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012495 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012496 if (StackSlot.getNode() != 0)
12497 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12498 MachinePointerInfo(),
12499 false, false, false, 0));
12500 else
12501 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012502 }
12503 return;
12504 }
Michael Liao991b6a22012-10-24 04:09:32 +000012505 case ISD::UINT_TO_FP: {
Michael Liao6f8c6852013-03-14 06:57:42 +000012506 assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
12507 if (N->getOperand(0).getValueType() != MVT::v2i32 ||
Michael Liao991b6a22012-10-24 04:09:32 +000012508 N->getValueType(0) != MVT::v2f32)
12509 return;
12510 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12511 N->getOperand(0));
12512 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12513 MVT::f64);
12514 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12515 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12516 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12517 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12518 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12519 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12520 return;
12521 }
Michael Liao44c2d612012-10-10 16:53:28 +000012522 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012523 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12524 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012525 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12526 Results.push_back(V);
12527 return;
12528 }
Duncan Sands1607f052008-12-01 11:39:25 +000012529 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012530 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012531 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012532 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012533 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012534 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012535 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012536 eax.getValue(2));
12537 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12538 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012539 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012540 Results.push_back(edx.getValue(1));
12541 return;
12542 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012543 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012544 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012545 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012546 bool Regs64bit = T == MVT::i128;
12547 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012548 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012549 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12550 DAG.getConstant(0, HalfT));
12551 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12552 DAG.getConstant(1, HalfT));
12553 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12554 Regs64bit ? X86::RAX : X86::EAX,
12555 cpInL, SDValue());
12556 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12557 Regs64bit ? X86::RDX : X86::EDX,
12558 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012559 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012560 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12561 DAG.getConstant(0, HalfT));
12562 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12563 DAG.getConstant(1, HalfT));
12564 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12565 Regs64bit ? X86::RBX : X86::EBX,
12566 swapInL, cpInH.getValue(1));
12567 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012568 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012569 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012570 SDValue Ops[] = { swapInH.getValue(0),
12571 N->getOperand(1),
12572 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012573 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012574 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012575 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12576 X86ISD::LCMPXCHG8_DAG;
12577 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012578 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012579 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12580 Regs64bit ? X86::RAX : X86::EAX,
12581 HalfT, Result.getValue(1));
12582 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12583 Regs64bit ? X86::RDX : X86::EDX,
12584 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012585 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012586 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012587 Results.push_back(cpOutH.getValue(1));
12588 return;
12589 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012590 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012591 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012592 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012593 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012594 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012595 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012596 case ISD::ATOMIC_LOAD_MAX:
12597 case ISD::ATOMIC_LOAD_MIN:
12598 case ISD::ATOMIC_LOAD_UMAX:
12599 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012600 case ISD::ATOMIC_SWAP: {
12601 unsigned Opc;
12602 switch (N->getOpcode()) {
12603 default: llvm_unreachable("Unexpected opcode");
12604 case ISD::ATOMIC_LOAD_ADD:
12605 Opc = X86ISD::ATOMADD64_DAG;
12606 break;
12607 case ISD::ATOMIC_LOAD_AND:
12608 Opc = X86ISD::ATOMAND64_DAG;
12609 break;
12610 case ISD::ATOMIC_LOAD_NAND:
12611 Opc = X86ISD::ATOMNAND64_DAG;
12612 break;
12613 case ISD::ATOMIC_LOAD_OR:
12614 Opc = X86ISD::ATOMOR64_DAG;
12615 break;
12616 case ISD::ATOMIC_LOAD_SUB:
12617 Opc = X86ISD::ATOMSUB64_DAG;
12618 break;
12619 case ISD::ATOMIC_LOAD_XOR:
12620 Opc = X86ISD::ATOMXOR64_DAG;
12621 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012622 case ISD::ATOMIC_LOAD_MAX:
12623 Opc = X86ISD::ATOMMAX64_DAG;
12624 break;
12625 case ISD::ATOMIC_LOAD_MIN:
12626 Opc = X86ISD::ATOMMIN64_DAG;
12627 break;
12628 case ISD::ATOMIC_LOAD_UMAX:
12629 Opc = X86ISD::ATOMUMAX64_DAG;
12630 break;
12631 case ISD::ATOMIC_LOAD_UMIN:
12632 Opc = X86ISD::ATOMUMIN64_DAG;
12633 break;
Craig Topperc0878702012-08-17 06:55:11 +000012634 case ISD::ATOMIC_SWAP:
12635 Opc = X86ISD::ATOMSWAP64_DAG;
12636 break;
12637 }
12638 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012639 return;
Craig Topperc0878702012-08-17 06:55:11 +000012640 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012641 case ISD::ATOMIC_LOAD:
12642 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012643 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012644}
12645
Evan Cheng72261582005-12-20 06:22:03 +000012646const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12647 switch (Opcode) {
12648 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012649 case X86ISD::BSF: return "X86ISD::BSF";
12650 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012651 case X86ISD::SHLD: return "X86ISD::SHLD";
12652 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012653 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012654 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012655 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012656 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012657 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012658 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012659 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12660 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12661 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012662 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012663 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012664 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012665 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012666 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012667 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012668 case X86ISD::COMI: return "X86ISD::COMI";
12669 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012670 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012671 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012672 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12673 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012674 case X86ISD::CMOV: return "X86ISD::CMOV";
12675 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012676 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012677 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12678 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012679 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012680 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012681 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012682 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012683 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012684 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12685 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012686 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012687 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012688 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012689 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012690 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012691 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012692 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012693 case X86ISD::HADD: return "X86ISD::HADD";
12694 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012695 case X86ISD::FHADD: return "X86ISD::FHADD";
12696 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012697 case X86ISD::UMAX: return "X86ISD::UMAX";
12698 case X86ISD::UMIN: return "X86ISD::UMIN";
12699 case X86ISD::SMAX: return "X86ISD::SMAX";
12700 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012701 case X86ISD::FMAX: return "X86ISD::FMAX";
12702 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012703 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12704 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012705 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12706 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012707 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012708 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012709 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012710 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12711 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012712 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012713 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012714 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012715 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012716 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12717 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012718 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12719 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12720 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12721 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12722 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12723 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012724 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012725 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012726 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012727 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12728 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012729 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012730 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012731 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12732 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012733 case X86ISD::VSHL: return "X86ISD::VSHL";
12734 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012735 case X86ISD::VSRA: return "X86ISD::VSRA";
12736 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12737 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12738 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012739 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012740 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12741 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012742 case X86ISD::ADD: return "X86ISD::ADD";
12743 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012744 case X86ISD::ADC: return "X86ISD::ADC";
12745 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012746 case X86ISD::SMUL: return "X86ISD::SMUL";
12747 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012748 case X86ISD::INC: return "X86ISD::INC";
12749 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012750 case X86ISD::OR: return "X86ISD::OR";
12751 case X86ISD::XOR: return "X86ISD::XOR";
12752 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012753 case X86ISD::BLSI: return "X86ISD::BLSI";
12754 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12755 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012756 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012757 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012758 case X86ISD::TESTP: return "X86ISD::TESTP";
Craig Topper4aee1bb2013-01-28 06:48:25 +000012759 case X86ISD::PALIGNR: return "X86ISD::PALIGNR";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012760 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12761 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012762 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012763 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012764 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012765 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012766 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012767 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12768 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012769 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12770 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12771 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012772 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12773 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012774 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12775 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012776 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012777 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012778 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012779 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12780 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012781 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012782 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012783 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012784 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012785 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012786 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012787 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012788 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012789 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Michael Liaoc26392a2013-03-28 23:41:26 +000012790 case X86ISD::RDSEED: return "X86ISD::RDSEED";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012791 case X86ISD::FMADD: return "X86ISD::FMADD";
12792 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12793 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12794 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12795 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12796 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012797 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12798 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Michael Liaof8fd8832013-03-26 22:47:01 +000012799 case X86ISD::XTEST: return "X86ISD::XTEST";
Evan Cheng72261582005-12-20 06:22:03 +000012800 }
12801}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012802
Chris Lattnerc9addb72007-03-30 23:15:24 +000012803// isLegalAddressingMode - Return true if the addressing mode represented
12804// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012805bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012806 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012807 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012808 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012809 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012810
Chris Lattnerc9addb72007-03-30 23:15:24 +000012811 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012812 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012813 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012814
Chris Lattnerc9addb72007-03-30 23:15:24 +000012815 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012816 unsigned GVFlags =
12817 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012818
Chris Lattnerdfed4132009-07-10 07:38:24 +000012819 // If a reference to this global requires an extra load, we can't fold it.
12820 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012821 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012822
Chris Lattnerdfed4132009-07-10 07:38:24 +000012823 // If BaseGV requires a register for the PIC base, we cannot also have a
12824 // BaseReg specified.
12825 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012826 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012827
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012828 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012829 if ((M != CodeModel::Small || R != Reloc::Static) &&
12830 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012831 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012832 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012833
Chris Lattnerc9addb72007-03-30 23:15:24 +000012834 switch (AM.Scale) {
12835 case 0:
12836 case 1:
12837 case 2:
12838 case 4:
12839 case 8:
12840 // These scales always work.
12841 break;
12842 case 3:
12843 case 5:
12844 case 9:
12845 // These scales are formed with basereg+scalereg. Only accept if there is
12846 // no basereg yet.
12847 if (AM.HasBaseReg)
12848 return false;
12849 break;
12850 default: // Other stuff never works.
12851 return false;
12852 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012853
Chris Lattnerc9addb72007-03-30 23:15:24 +000012854 return true;
12855}
12856
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012857bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012858 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012859 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012860 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12861 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012862 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012863}
12864
Evan Cheng70e10d32012-07-17 06:53:39 +000012865bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012866 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012867}
12868
12869bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012870 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012871 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012872}
12873
Owen Andersone50ed302009-08-10 22:56:29 +000012874bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012875 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012876 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012877 unsigned NumBits1 = VT1.getSizeInBits();
12878 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012879 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012880}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012881
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012882bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012883 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012884 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012885}
12886
Owen Andersone50ed302009-08-10 22:56:29 +000012887bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012888 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012889 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012890}
12891
Evan Cheng2766a472012-12-06 19:13:27 +000012892bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12893 EVT VT1 = Val.getValueType();
12894 if (isZExtFree(VT1, VT2))
12895 return true;
12896
12897 if (Val.getOpcode() != ISD::LOAD)
12898 return false;
12899
12900 if (!VT1.isSimple() || !VT1.isInteger() ||
12901 !VT2.isSimple() || !VT2.isInteger())
12902 return false;
12903
12904 switch (VT1.getSimpleVT().SimpleTy) {
12905 default: break;
12906 case MVT::i8:
12907 case MVT::i16:
12908 case MVT::i32:
12909 // X86 has 8, 16, and 32-bit zero-extending loads.
12910 return true;
12911 }
12912
12913 return false;
12914}
12915
Owen Andersone50ed302009-08-10 22:56:29 +000012916bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012917 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012918 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012919}
12920
Evan Cheng60c07e12006-07-05 22:17:51 +000012921/// isShuffleMaskLegal - Targets can use this to indicate that they only
12922/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12923/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12924/// are assumed to be legal.
12925bool
Eric Christopherfd179292009-08-27 18:07:15 +000012926X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012927 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012928 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012929 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012930 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012931
Nate Begemana09008b2009-10-19 02:17:23 +000012932 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012933 return (VT.getVectorNumElements() == 2 ||
12934 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12935 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012936 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012937 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012938 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12939 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012940 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012941 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12942 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12943 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12944 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012945}
12946
Dan Gohman7d8143f2008-04-09 20:09:42 +000012947bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012948X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012949 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012950 unsigned NumElts = VT.getVectorNumElements();
12951 // FIXME: This collection of masks seems suspect.
12952 if (NumElts == 2)
12953 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012954 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012955 return (isMOVLMask(Mask, VT) ||
12956 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012957 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12958 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012959 }
12960 return false;
12961}
12962
12963//===----------------------------------------------------------------------===//
12964// X86 Scheduler Hooks
12965//===----------------------------------------------------------------------===//
12966
Michael Liaobe02a902012-11-08 07:28:54 +000012967/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012968static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12969 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012970 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012971
12972 const BasicBlock *BB = MBB->getBasicBlock();
12973 MachineFunction::iterator I = MBB;
12974 ++I;
12975
12976 // For the v = xbegin(), we generate
12977 //
12978 // thisMBB:
12979 // xbegin sinkMBB
12980 //
12981 // mainMBB:
12982 // eax = -1
12983 //
12984 // sinkMBB:
12985 // v = eax
12986
12987 MachineBasicBlock *thisMBB = MBB;
12988 MachineFunction *MF = MBB->getParent();
12989 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12990 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12991 MF->insert(I, mainMBB);
12992 MF->insert(I, sinkMBB);
12993
12994 // Transfer the remainder of BB and its successor edges to sinkMBB.
12995 sinkMBB->splice(sinkMBB->begin(), MBB,
12996 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12997 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12998
12999 // thisMBB:
13000 // xbegin sinkMBB
13001 // # fallthrough to mainMBB
13002 // # abortion to sinkMBB
13003 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
13004 thisMBB->addSuccessor(mainMBB);
13005 thisMBB->addSuccessor(sinkMBB);
13006
13007 // mainMBB:
13008 // EAX = -1
13009 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
13010 mainMBB->addSuccessor(sinkMBB);
13011
13012 // sinkMBB:
13013 // EAX is live into the sinkMBB
13014 sinkMBB->addLiveIn(X86::EAX);
13015 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13016 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13017 .addReg(X86::EAX);
13018
13019 MI->eraseFromParent();
13020 return sinkMBB;
13021}
13022
Michael Liaob118a072012-09-20 03:06:15 +000013023// Get CMPXCHG opcode for the specified data type.
13024static unsigned getCmpXChgOpcode(EVT VT) {
13025 switch (VT.getSimpleVT().SimpleTy) {
13026 case MVT::i8: return X86::LCMPXCHG8;
13027 case MVT::i16: return X86::LCMPXCHG16;
13028 case MVT::i32: return X86::LCMPXCHG32;
13029 case MVT::i64: return X86::LCMPXCHG64;
13030 default:
13031 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000013032 }
Michael Liaob118a072012-09-20 03:06:15 +000013033 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000013034}
13035
Michael Liaob118a072012-09-20 03:06:15 +000013036// Get LOAD opcode for the specified data type.
13037static unsigned getLoadOpcode(EVT VT) {
13038 switch (VT.getSimpleVT().SimpleTy) {
13039 case MVT::i8: return X86::MOV8rm;
13040 case MVT::i16: return X86::MOV16rm;
13041 case MVT::i32: return X86::MOV32rm;
13042 case MVT::i64: return X86::MOV64rm;
13043 default:
13044 break;
13045 }
13046 llvm_unreachable("Invalid operand size!");
13047}
13048
13049// Get opcode of the non-atomic one from the specified atomic instruction.
13050static unsigned getNonAtomicOpcode(unsigned Opc) {
13051 switch (Opc) {
13052 case X86::ATOMAND8: return X86::AND8rr;
13053 case X86::ATOMAND16: return X86::AND16rr;
13054 case X86::ATOMAND32: return X86::AND32rr;
13055 case X86::ATOMAND64: return X86::AND64rr;
13056 case X86::ATOMOR8: return X86::OR8rr;
13057 case X86::ATOMOR16: return X86::OR16rr;
13058 case X86::ATOMOR32: return X86::OR32rr;
13059 case X86::ATOMOR64: return X86::OR64rr;
13060 case X86::ATOMXOR8: return X86::XOR8rr;
13061 case X86::ATOMXOR16: return X86::XOR16rr;
13062 case X86::ATOMXOR32: return X86::XOR32rr;
13063 case X86::ATOMXOR64: return X86::XOR64rr;
13064 }
13065 llvm_unreachable("Unhandled atomic-load-op opcode!");
13066}
13067
13068// Get opcode of the non-atomic one from the specified atomic instruction with
13069// extra opcode.
13070static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
13071 unsigned &ExtraOpc) {
13072 switch (Opc) {
13073 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
13074 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
13075 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
13076 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013077 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013078 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
13079 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
13080 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013081 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013082 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
13083 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
13084 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013085 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013086 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
13087 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
13088 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013089 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013090 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
13091 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
13092 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
13093 }
13094 llvm_unreachable("Unhandled atomic-load-op opcode!");
13095}
13096
13097// Get opcode of the non-atomic one from the specified atomic instruction for
13098// 64-bit data type on 32-bit target.
13099static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
13100 switch (Opc) {
13101 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
13102 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
13103 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
13104 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
13105 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
13106 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013107 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
13108 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
13109 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
13110 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000013111 }
13112 llvm_unreachable("Unhandled atomic-load-op opcode!");
13113}
13114
13115// Get opcode of the non-atomic one from the specified atomic instruction for
13116// 64-bit data type on 32-bit target with extra opcode.
13117static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
13118 unsigned &HiOpc,
13119 unsigned &ExtraOpc) {
13120 switch (Opc) {
13121 case X86::ATOMNAND6432:
13122 ExtraOpc = X86::NOT32r;
13123 HiOpc = X86::AND32rr;
13124 return X86::AND32rr;
13125 }
13126 llvm_unreachable("Unhandled atomic-load-op opcode!");
13127}
13128
13129// Get pseudo CMOV opcode from the specified data type.
13130static unsigned getPseudoCMOVOpc(EVT VT) {
13131 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000013132 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000013133 case MVT::i16: return X86::CMOV_GR16;
13134 case MVT::i32: return X86::CMOV_GR32;
13135 default:
13136 break;
13137 }
13138 llvm_unreachable("Unknown CMOV opcode!");
13139}
13140
13141// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
13142// They will be translated into a spin-loop or compare-exchange loop from
13143//
13144// ...
13145// dst = atomic-fetch-op MI.addr, MI.val
13146// ...
13147//
13148// to
13149//
13150// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013151// t1 = LOAD MI.addr
Michael Liaob118a072012-09-20 03:06:15 +000013152// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013153// t4 = phi(t1, t3 / loop)
13154// t2 = OP MI.val, t4
13155// EAX = t4
13156// LCMPXCHG [MI.addr], t2, [EAX is implicitly used & defined]
13157// t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013158// JNE loop
13159// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013160// dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013161// ...
Mon P Wang63307c32008-05-05 19:05:59 +000013162MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000013163X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
13164 MachineBasicBlock *MBB) const {
13165 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13166 DebugLoc DL = MI->getDebugLoc();
13167
13168 MachineFunction *MF = MBB->getParent();
13169 MachineRegisterInfo &MRI = MF->getRegInfo();
13170
13171 const BasicBlock *BB = MBB->getBasicBlock();
13172 MachineFunction::iterator I = MBB;
13173 ++I;
13174
Michael Liao13d08bf2013-01-22 21:47:38 +000013175 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000013176 "Unexpected number of operands");
13177
13178 assert(MI->hasOneMemOperand() &&
13179 "Expected atomic-load-op to have one memoperand");
13180
13181 // Memory Reference
13182 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13183 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13184
13185 unsigned DstReg, SrcReg;
13186 unsigned MemOpndSlot;
13187
13188 unsigned CurOp = 0;
13189
13190 DstReg = MI->getOperand(CurOp++).getReg();
13191 MemOpndSlot = CurOp;
13192 CurOp += X86::AddrNumOperands;
13193 SrcReg = MI->getOperand(CurOp++).getReg();
13194
13195 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000013196 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaoc537f792013-03-06 00:17:04 +000013197 unsigned t1 = MRI.createVirtualRegister(RC);
13198 unsigned t2 = MRI.createVirtualRegister(RC);
13199 unsigned t3 = MRI.createVirtualRegister(RC);
13200 unsigned t4 = MRI.createVirtualRegister(RC);
13201 unsigned PhyReg = getX86SubSuperRegister(X86::EAX, VT);
Michael Liaob118a072012-09-20 03:06:15 +000013202
13203 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
13204 unsigned LOADOpc = getLoadOpcode(VT);
13205
13206 // For the atomic load-arith operator, we generate
13207 //
13208 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013209 // t1 = LOAD [MI.addr]
Michael Liaob118a072012-09-20 03:06:15 +000013210 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013211 // t4 = phi(t1 / thisMBB, t3 / mainMBB)
Michael Liaob118a072012-09-20 03:06:15 +000013212 // t1 = OP MI.val, EAX
Michael Liaoc537f792013-03-06 00:17:04 +000013213 // EAX = t4
Michael Liaob118a072012-09-20 03:06:15 +000013214 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013215 // t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013216 // JNE mainMBB
13217 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013218 // dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013219
13220 MachineBasicBlock *thisMBB = MBB;
13221 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13222 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13223 MF->insert(I, mainMBB);
13224 MF->insert(I, sinkMBB);
13225
13226 MachineInstrBuilder MIB;
13227
13228 // Transfer the remainder of BB and its successor edges to sinkMBB.
13229 sinkMBB->splice(sinkMBB->begin(), MBB,
13230 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13231 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13232
13233 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013234 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1);
13235 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13236 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13237 if (NewMO.isReg())
13238 NewMO.setIsKill(false);
13239 MIB.addOperand(NewMO);
13240 }
13241 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13242 unsigned flags = (*MMOI)->getFlags();
13243 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13244 MachineMemOperand *MMO =
13245 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13246 (*MMOI)->getSize(),
13247 (*MMOI)->getBaseAlignment(),
13248 (*MMOI)->getTBAAInfo(),
13249 (*MMOI)->getRanges());
13250 MIB.addMemOperand(MMO);
13251 }
Michael Liaob118a072012-09-20 03:06:15 +000013252
13253 thisMBB->addSuccessor(mainMBB);
13254
13255 // mainMBB:
13256 MachineBasicBlock *origMainMBB = mainMBB;
Michael Liaob118a072012-09-20 03:06:15 +000013257
Michael Liaoc537f792013-03-06 00:17:04 +000013258 // Add a PHI.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013259 MachineInstr *Phi = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4)
13260 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
Michael Liaob118a072012-09-20 03:06:15 +000013261
Michael Liaob118a072012-09-20 03:06:15 +000013262 unsigned Opc = MI->getOpcode();
13263 switch (Opc) {
13264 default:
13265 llvm_unreachable("Unhandled atomic-load-op opcode!");
13266 case X86::ATOMAND8:
13267 case X86::ATOMAND16:
13268 case X86::ATOMAND32:
13269 case X86::ATOMAND64:
13270 case X86::ATOMOR8:
13271 case X86::ATOMOR16:
13272 case X86::ATOMOR32:
13273 case X86::ATOMOR64:
13274 case X86::ATOMXOR8:
13275 case X86::ATOMXOR16:
13276 case X86::ATOMXOR32:
13277 case X86::ATOMXOR64: {
13278 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
Michael Liaoc537f792013-03-06 00:17:04 +000013279 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t2).addReg(SrcReg)
13280 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013281 break;
13282 }
13283 case X86::ATOMNAND8:
13284 case X86::ATOMNAND16:
13285 case X86::ATOMNAND32:
13286 case X86::ATOMNAND64: {
Michael Liaoc537f792013-03-06 00:17:04 +000013287 unsigned Tmp = MRI.createVirtualRegister(RC);
Michael Liaob118a072012-09-20 03:06:15 +000013288 unsigned NOTOpc;
13289 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013290 BuildMI(mainMBB, DL, TII->get(ANDOpc), Tmp).addReg(SrcReg)
13291 .addReg(t4);
13292 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2).addReg(Tmp);
Michael Liaob118a072012-09-20 03:06:15 +000013293 break;
13294 }
Michael Liao08382492012-09-21 03:00:17 +000013295 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013296 case X86::ATOMMAX16:
13297 case X86::ATOMMAX32:
13298 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013299 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013300 case X86::ATOMMIN16:
13301 case X86::ATOMMIN32:
13302 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000013303 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013304 case X86::ATOMUMAX16:
13305 case X86::ATOMUMAX32:
13306 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013307 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013308 case X86::ATOMUMIN16:
13309 case X86::ATOMUMIN32:
13310 case X86::ATOMUMIN64: {
13311 unsigned CMPOpc;
13312 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
13313
13314 BuildMI(mainMBB, DL, TII->get(CMPOpc))
13315 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013316 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013317
13318 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000013319 if (VT != MVT::i8) {
13320 // Native support
Michael Liaoc537f792013-03-06 00:17:04 +000013321 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
Michael Liaofe87c302012-09-21 03:18:52 +000013322 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013323 .addReg(t4);
Michael Liaofe87c302012-09-21 03:18:52 +000013324 } else {
13325 // Promote i8 to i32 to use CMOV32
Michael Liaoc537f792013-03-06 00:17:04 +000013326 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13327 const TargetRegisterClass *RC32 =
13328 TRI->getSubClassWithSubReg(getRegClassFor(MVT::i32), X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013329 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
13330 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
Michael Liaoc537f792013-03-06 00:17:04 +000013331 unsigned Tmp = MRI.createVirtualRegister(RC32);
Michael Liaofe87c302012-09-21 03:18:52 +000013332
13333 unsigned Undef = MRI.createVirtualRegister(RC32);
13334 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
13335
13336 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
13337 .addReg(Undef)
13338 .addReg(SrcReg)
13339 .addImm(X86::sub_8bit);
13340 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
13341 .addReg(Undef)
Michael Liaoc537f792013-03-06 00:17:04 +000013342 .addReg(t4)
Michael Liaofe87c302012-09-21 03:18:52 +000013343 .addImm(X86::sub_8bit);
13344
Michael Liaoc537f792013-03-06 00:17:04 +000013345 BuildMI(mainMBB, DL, TII->get(CMOVOpc), Tmp)
Michael Liaofe87c302012-09-21 03:18:52 +000013346 .addReg(SrcReg32)
13347 .addReg(AccReg32);
13348
Michael Liaoc537f792013-03-06 00:17:04 +000013349 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t2)
13350 .addReg(Tmp, 0, X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013351 }
Michael Liaob118a072012-09-20 03:06:15 +000013352 } else {
13353 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013354 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013355 "Invalid atomic-load-op transformation!");
13356 unsigned SelOpc = getPseudoCMOVOpc(VT);
13357 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13358 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
Michael Liaoc537f792013-03-06 00:17:04 +000013359 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t2)
13360 .addReg(SrcReg).addReg(t4)
Michael Liaob118a072012-09-20 03:06:15 +000013361 .addImm(CC);
13362 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013363 // Replace the original PHI node as mainMBB is changed after CMOV
13364 // lowering.
13365 BuildMI(*origMainMBB, Phi, DL, TII->get(X86::PHI), t4)
13366 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
13367 Phi->eraseFromParent();
Michael Liaob118a072012-09-20 03:06:15 +000013368 }
13369 break;
13370 }
13371 }
13372
Michael Liaoc537f792013-03-06 00:17:04 +000013373 // Copy PhyReg back from virtual register.
13374 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), PhyReg)
13375 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013376
13377 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013378 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13379 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13380 if (NewMO.isReg())
13381 NewMO.setIsKill(false);
13382 MIB.addOperand(NewMO);
13383 }
13384 MIB.addReg(t2);
Michael Liaob118a072012-09-20 03:06:15 +000013385 MIB.setMemRefs(MMOBegin, MMOEnd);
13386
Michael Liaoc537f792013-03-06 00:17:04 +000013387 // Copy PhyReg back to virtual register.
13388 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3)
13389 .addReg(PhyReg);
13390
Michael Liaob118a072012-09-20 03:06:15 +000013391 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13392
13393 mainMBB->addSuccessor(origMainMBB);
13394 mainMBB->addSuccessor(sinkMBB);
13395
13396 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013397 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13398 TII->get(TargetOpcode::COPY), DstReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013399 .addReg(t3);
Michael Liaob118a072012-09-20 03:06:15 +000013400
13401 MI->eraseFromParent();
13402 return sinkMBB;
13403}
13404
13405// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13406// instructions. They will be translated into a spin-loop or compare-exchange
13407// loop from
13408//
13409// ...
13410// dst = atomic-fetch-op MI.addr, MI.val
13411// ...
13412//
13413// to
13414//
13415// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013416// t1L = LOAD [MI.addr + 0]
13417// t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013418// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013419// t4L = phi(t1L, t3L / loop)
13420// t4H = phi(t1H, t3H / loop)
13421// t2L = OP MI.val.lo, t4L
13422// t2H = OP MI.val.hi, t4H
13423// EAX = t4L
13424// EDX = t4H
13425// EBX = t2L
13426// ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013427// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013428// t3L = EAX
13429// t3H = EDX
Michael Liaob118a072012-09-20 03:06:15 +000013430// JNE loop
13431// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013432// dstL = t3L
13433// dstH = t3H
Michael Liaob118a072012-09-20 03:06:15 +000013434// ...
13435MachineBasicBlock *
13436X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13437 MachineBasicBlock *MBB) const {
13438 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13439 DebugLoc DL = MI->getDebugLoc();
13440
13441 MachineFunction *MF = MBB->getParent();
13442 MachineRegisterInfo &MRI = MF->getRegInfo();
13443
13444 const BasicBlock *BB = MBB->getBasicBlock();
13445 MachineFunction::iterator I = MBB;
13446 ++I;
13447
Michael Liao13d08bf2013-01-22 21:47:38 +000013448 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013449 "Unexpected number of operands");
13450
13451 assert(MI->hasOneMemOperand() &&
13452 "Expected atomic-load-op32 to have one memoperand");
13453
13454 // Memory Reference
13455 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13456 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13457
13458 unsigned DstLoReg, DstHiReg;
13459 unsigned SrcLoReg, SrcHiReg;
13460 unsigned MemOpndSlot;
13461
13462 unsigned CurOp = 0;
13463
13464 DstLoReg = MI->getOperand(CurOp++).getReg();
13465 DstHiReg = MI->getOperand(CurOp++).getReg();
13466 MemOpndSlot = CurOp;
13467 CurOp += X86::AddrNumOperands;
13468 SrcLoReg = MI->getOperand(CurOp++).getReg();
13469 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013470
Craig Topperc9099502012-04-20 06:31:50 +000013471 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013472 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013473
Michael Liaoc537f792013-03-06 00:17:04 +000013474 unsigned t1L = MRI.createVirtualRegister(RC);
13475 unsigned t1H = MRI.createVirtualRegister(RC);
13476 unsigned t2L = MRI.createVirtualRegister(RC);
13477 unsigned t2H = MRI.createVirtualRegister(RC);
13478 unsigned t3L = MRI.createVirtualRegister(RC);
13479 unsigned t3H = MRI.createVirtualRegister(RC);
13480 unsigned t4L = MRI.createVirtualRegister(RC);
13481 unsigned t4H = MRI.createVirtualRegister(RC);
13482
Michael Liaob118a072012-09-20 03:06:15 +000013483 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13484 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013485
Michael Liaob118a072012-09-20 03:06:15 +000013486 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013487 //
Michael Liaob118a072012-09-20 03:06:15 +000013488 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013489 // t1L = LOAD [MI.addr + 0]
13490 // t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013491 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013492 // t4L = phi(t1L / thisMBB, t3L / mainMBB)
13493 // t4H = phi(t1H / thisMBB, t3H / mainMBB)
13494 // t2L = OP MI.val.lo, t4L
13495 // t2H = OP MI.val.hi, t4H
13496 // EBX = t2L
13497 // ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013498 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013499 // t3L = EAX
13500 // t3H = EDX
13501 // JNE loop
Michael Liaob118a072012-09-20 03:06:15 +000013502 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013503 // dstL = t3L
13504 // dstH = t3H
Scott Michelfdc40a02009-02-17 22:15:04 +000013505
Mon P Wang63307c32008-05-05 19:05:59 +000013506 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013507 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13508 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13509 MF->insert(I, mainMBB);
13510 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013511
Michael Liaob118a072012-09-20 03:06:15 +000013512 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013513
Michael Liaob118a072012-09-20 03:06:15 +000013514 // Transfer the remainder of BB and its successor edges to sinkMBB.
13515 sinkMBB->splice(sinkMBB->begin(), MBB,
13516 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13517 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013518
Michael Liaob118a072012-09-20 03:06:15 +000013519 // thisMBB:
13520 // Lo
Michael Liaoc537f792013-03-06 00:17:04 +000013521 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1L);
Michael Liaob118a072012-09-20 03:06:15 +000013522 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Michael Liaoc537f792013-03-06 00:17:04 +000013523 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13524 if (NewMO.isReg())
13525 NewMO.setIsKill(false);
13526 MIB.addOperand(NewMO);
Michael Liaob118a072012-09-20 03:06:15 +000013527 }
Michael Liaoc537f792013-03-06 00:17:04 +000013528 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13529 unsigned flags = (*MMOI)->getFlags();
13530 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13531 MachineMemOperand *MMO =
13532 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13533 (*MMOI)->getSize(),
13534 (*MMOI)->getBaseAlignment(),
13535 (*MMOI)->getTBAAInfo(),
13536 (*MMOI)->getRanges());
13537 MIB.addMemOperand(MMO);
13538 };
13539 MachineInstr *LowMI = MIB;
13540
13541 // Hi
13542 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1H);
13543 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13544 if (i == X86::AddrDisp) {
13545 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
13546 } else {
13547 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13548 if (NewMO.isReg())
13549 NewMO.setIsKill(false);
13550 MIB.addOperand(NewMO);
13551 }
13552 }
13553 MIB.setMemRefs(LowMI->memoperands_begin(), LowMI->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000013554
Michael Liaob118a072012-09-20 03:06:15 +000013555 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013556
Michael Liaob118a072012-09-20 03:06:15 +000013557 // mainMBB:
13558 MachineBasicBlock *origMainMBB = mainMBB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013559
Michael Liaoc537f792013-03-06 00:17:04 +000013560 // Add PHIs.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013561 MachineInstr *PhiL = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4L)
13562 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13563 MachineInstr *PhiH = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4H)
13564 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013565
Michael Liaob118a072012-09-20 03:06:15 +000013566 unsigned Opc = MI->getOpcode();
13567 switch (Opc) {
13568 default:
13569 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13570 case X86::ATOMAND6432:
13571 case X86::ATOMOR6432:
13572 case X86::ATOMXOR6432:
13573 case X86::ATOMADD6432:
13574 case X86::ATOMSUB6432: {
13575 unsigned HiOpc;
13576 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013577 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(t4L)
13578 .addReg(SrcLoReg);
13579 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(t4H)
13580 .addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013581 break;
13582 }
13583 case X86::ATOMNAND6432: {
13584 unsigned HiOpc, NOTOpc;
13585 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013586 unsigned TmpL = MRI.createVirtualRegister(RC);
13587 unsigned TmpH = MRI.createVirtualRegister(RC);
13588 BuildMI(mainMBB, DL, TII->get(LoOpc), TmpL).addReg(SrcLoReg)
13589 .addReg(t4L);
13590 BuildMI(mainMBB, DL, TII->get(HiOpc), TmpH).addReg(SrcHiReg)
13591 .addReg(t4H);
13592 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2L).addReg(TmpL);
13593 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2H).addReg(TmpH);
Michael Liaob118a072012-09-20 03:06:15 +000013594 break;
13595 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013596 case X86::ATOMMAX6432:
13597 case X86::ATOMMIN6432:
13598 case X86::ATOMUMAX6432:
13599 case X86::ATOMUMIN6432: {
13600 unsigned HiOpc;
13601 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13602 unsigned cL = MRI.createVirtualRegister(RC8);
13603 unsigned cH = MRI.createVirtualRegister(RC8);
13604 unsigned cL32 = MRI.createVirtualRegister(RC);
13605 unsigned cH32 = MRI.createVirtualRegister(RC);
13606 unsigned cc = MRI.createVirtualRegister(RC);
13607 // cl := cmp src_lo, lo
13608 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013609 .addReg(SrcLoReg).addReg(t4L);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013610 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13611 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13612 // ch := cmp src_hi, hi
13613 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013614 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013615 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13616 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13617 // cc := if (src_hi == hi) ? cl : ch;
13618 if (Subtarget->hasCMov()) {
13619 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13620 .addReg(cH32).addReg(cL32);
13621 } else {
13622 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13623 .addReg(cH32).addReg(cL32)
13624 .addImm(X86::COND_E);
13625 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13626 }
13627 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13628 if (Subtarget->hasCMov()) {
Michael Liaoc537f792013-03-06 00:17:04 +000013629 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2L)
13630 .addReg(SrcLoReg).addReg(t4L);
13631 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2H)
13632 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013633 } else {
Michael Liaoc537f792013-03-06 00:17:04 +000013634 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2L)
13635 .addReg(SrcLoReg).addReg(t4L)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013636 .addImm(X86::COND_NE);
13637 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013638 // As the lowered CMOV won't clobber EFLAGS, we could reuse it for the
13639 // 2nd CMOV lowering.
13640 mainMBB->addLiveIn(X86::EFLAGS);
Michael Liaoc537f792013-03-06 00:17:04 +000013641 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2H)
13642 .addReg(SrcHiReg).addReg(t4H)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013643 .addImm(X86::COND_NE);
13644 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013645 // Replace the original PHI node as mainMBB is changed after CMOV
13646 // lowering.
13647 BuildMI(*origMainMBB, PhiL, DL, TII->get(X86::PHI), t4L)
13648 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13649 BuildMI(*origMainMBB, PhiH, DL, TII->get(X86::PHI), t4H)
13650 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
13651 PhiL->eraseFromParent();
13652 PhiH->eraseFromParent();
Michael Liaoe5e8f762012-09-25 18:08:13 +000013653 }
13654 break;
13655 }
Michael Liaob118a072012-09-20 03:06:15 +000013656 case X86::ATOMSWAP6432: {
13657 unsigned HiOpc;
13658 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013659 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg);
13660 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013661 break;
13662 }
13663 }
Mon P Wang63307c32008-05-05 19:05:59 +000013664
Michael Liaob118a072012-09-20 03:06:15 +000013665 // Copy EDX:EAX back from HiReg:LoReg
Michael Liaoc537f792013-03-06 00:17:04 +000013666 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(t4L);
13667 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(t4H);
Michael Liaob118a072012-09-20 03:06:15 +000013668 // Copy ECX:EBX from t1H:t1L
Michael Liaoc537f792013-03-06 00:17:04 +000013669 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t2L);
13670 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t2H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013671
Michael Liaob118a072012-09-20 03:06:15 +000013672 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013673 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13674 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13675 if (NewMO.isReg())
13676 NewMO.setIsKill(false);
13677 MIB.addOperand(NewMO);
13678 }
Michael Liaob118a072012-09-20 03:06:15 +000013679 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013680
Michael Liaoc537f792013-03-06 00:17:04 +000013681 // Copy EDX:EAX back to t3H:t3L
13682 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3L).addReg(X86::EAX);
13683 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3H).addReg(X86::EDX);
13684
Michael Liaob118a072012-09-20 03:06:15 +000013685 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013686
Michael Liaob118a072012-09-20 03:06:15 +000013687 mainMBB->addSuccessor(origMainMBB);
13688 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013689
Michael Liaob118a072012-09-20 03:06:15 +000013690 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013691 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13692 TII->get(TargetOpcode::COPY), DstLoReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013693 .addReg(t3L);
Michael Liaob118a072012-09-20 03:06:15 +000013694 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13695 TII->get(TargetOpcode::COPY), DstHiReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013696 .addReg(t3H);
Mon P Wang63307c32008-05-05 19:05:59 +000013697
Michael Liaob118a072012-09-20 03:06:15 +000013698 MI->eraseFromParent();
13699 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013700}
13701
Eric Christopherf83a5de2009-08-27 18:08:16 +000013702// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013703// or XMM0_V32I8 in AVX all of this code can be replaced with that
13704// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013705static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13706 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013707 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013708 switch (MI->getOpcode()) {
13709 default: llvm_unreachable("illegal opcode!");
13710 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13711 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13712 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13713 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13714 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13715 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13716 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13717 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013718 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013719
Craig Topper8aae8dd2012-11-10 08:57:41 +000013720 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013721 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013722
Craig Topper52ea2452012-11-10 09:25:36 +000013723 unsigned NumArgs = MI->getNumOperands();
13724 for (unsigned i = 1; i < NumArgs; ++i) {
13725 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013726 if (!(Op.isReg() && Op.isImplicit()))
13727 MIB.addOperand(Op);
13728 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013729 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013730 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13731
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013732 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013733 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013734 .addReg(X86::XMM0);
13735
Dan Gohman14152b42010-07-06 20:24:04 +000013736 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013737 return BB;
13738}
13739
Craig Topper9c7ae012012-11-10 01:23:36 +000013740// FIXME: Custom handling because TableGen doesn't support multiple implicit
13741// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013742static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13743 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013744 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013745 switch (MI->getOpcode()) {
13746 default: llvm_unreachable("illegal opcode!");
13747 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13748 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13749 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13750 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13751 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13752 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13753 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13754 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013755 }
13756
Craig Topper8aae8dd2012-11-10 08:57:41 +000013757 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013758 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013759
Craig Topper52ea2452012-11-10 09:25:36 +000013760 unsigned NumArgs = MI->getNumOperands(); // remove the results
13761 for (unsigned i = 1; i < NumArgs; ++i) {
13762 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013763 if (!(Op.isReg() && Op.isImplicit()))
13764 MIB.addOperand(Op);
13765 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013766 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013767 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13768
13769 BuildMI(*BB, MI, dl,
13770 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13771 .addReg(X86::ECX);
13772
13773 MI->eraseFromParent();
13774 return BB;
13775}
13776
Craig Topper2da36912012-11-11 22:45:02 +000013777static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13778 const TargetInstrInfo *TII,
13779 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013780 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013781
Eric Christopher228232b2010-11-30 07:20:12 +000013782 // Address into RAX/EAX, other two args into ECX, EDX.
13783 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13784 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13785 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13786 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013787 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013788
Eric Christopher228232b2010-11-30 07:20:12 +000013789 unsigned ValOps = X86::AddrNumOperands;
13790 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13791 .addReg(MI->getOperand(ValOps).getReg());
13792 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13793 .addReg(MI->getOperand(ValOps+1).getReg());
13794
13795 // The instruction doesn't actually take any operands though.
13796 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013797
Eric Christopher228232b2010-11-30 07:20:12 +000013798 MI->eraseFromParent(); // The pseudo is gone now.
13799 return BB;
13800}
13801
13802MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013803X86TargetLowering::EmitVAARG64WithCustomInserter(
13804 MachineInstr *MI,
13805 MachineBasicBlock *MBB) const {
13806 // Emit va_arg instruction on X86-64.
13807
13808 // Operands to this pseudo-instruction:
13809 // 0 ) Output : destination address (reg)
13810 // 1-5) Input : va_list address (addr, i64mem)
13811 // 6 ) ArgSize : Size (in bytes) of vararg type
13812 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13813 // 8 ) Align : Alignment of type
13814 // 9 ) EFLAGS (implicit-def)
13815
13816 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13817 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13818
13819 unsigned DestReg = MI->getOperand(0).getReg();
13820 MachineOperand &Base = MI->getOperand(1);
13821 MachineOperand &Scale = MI->getOperand(2);
13822 MachineOperand &Index = MI->getOperand(3);
13823 MachineOperand &Disp = MI->getOperand(4);
13824 MachineOperand &Segment = MI->getOperand(5);
13825 unsigned ArgSize = MI->getOperand(6).getImm();
13826 unsigned ArgMode = MI->getOperand(7).getImm();
13827 unsigned Align = MI->getOperand(8).getImm();
13828
13829 // Memory Reference
13830 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13831 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13832 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13833
13834 // Machine Information
13835 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13836 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13837 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13838 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13839 DebugLoc DL = MI->getDebugLoc();
13840
13841 // struct va_list {
13842 // i32 gp_offset
13843 // i32 fp_offset
13844 // i64 overflow_area (address)
13845 // i64 reg_save_area (address)
13846 // }
13847 // sizeof(va_list) = 24
13848 // alignment(va_list) = 8
13849
13850 unsigned TotalNumIntRegs = 6;
13851 unsigned TotalNumXMMRegs = 8;
13852 bool UseGPOffset = (ArgMode == 1);
13853 bool UseFPOffset = (ArgMode == 2);
13854 unsigned MaxOffset = TotalNumIntRegs * 8 +
13855 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13856
13857 /* Align ArgSize to a multiple of 8 */
13858 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13859 bool NeedsAlign = (Align > 8);
13860
13861 MachineBasicBlock *thisMBB = MBB;
13862 MachineBasicBlock *overflowMBB;
13863 MachineBasicBlock *offsetMBB;
13864 MachineBasicBlock *endMBB;
13865
13866 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13867 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13868 unsigned OffsetReg = 0;
13869
13870 if (!UseGPOffset && !UseFPOffset) {
13871 // If we only pull from the overflow region, we don't create a branch.
13872 // We don't need to alter control flow.
13873 OffsetDestReg = 0; // unused
13874 OverflowDestReg = DestReg;
13875
13876 offsetMBB = NULL;
13877 overflowMBB = thisMBB;
13878 endMBB = thisMBB;
13879 } else {
13880 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13881 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13882 // If not, pull from overflow_area. (branch to overflowMBB)
13883 //
13884 // thisMBB
13885 // | .
13886 // | .
13887 // offsetMBB overflowMBB
13888 // | .
13889 // | .
13890 // endMBB
13891
13892 // Registers for the PHI in endMBB
13893 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13894 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13895
13896 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13897 MachineFunction *MF = MBB->getParent();
13898 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13899 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13900 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13901
13902 MachineFunction::iterator MBBIter = MBB;
13903 ++MBBIter;
13904
13905 // Insert the new basic blocks
13906 MF->insert(MBBIter, offsetMBB);
13907 MF->insert(MBBIter, overflowMBB);
13908 MF->insert(MBBIter, endMBB);
13909
13910 // Transfer the remainder of MBB and its successor edges to endMBB.
13911 endMBB->splice(endMBB->begin(), thisMBB,
13912 llvm::next(MachineBasicBlock::iterator(MI)),
13913 thisMBB->end());
13914 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13915
13916 // Make offsetMBB and overflowMBB successors of thisMBB
13917 thisMBB->addSuccessor(offsetMBB);
13918 thisMBB->addSuccessor(overflowMBB);
13919
13920 // endMBB is a successor of both offsetMBB and overflowMBB
13921 offsetMBB->addSuccessor(endMBB);
13922 overflowMBB->addSuccessor(endMBB);
13923
13924 // Load the offset value into a register
13925 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13926 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13927 .addOperand(Base)
13928 .addOperand(Scale)
13929 .addOperand(Index)
13930 .addDisp(Disp, UseFPOffset ? 4 : 0)
13931 .addOperand(Segment)
13932 .setMemRefs(MMOBegin, MMOEnd);
13933
13934 // Check if there is enough room left to pull this argument.
13935 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13936 .addReg(OffsetReg)
13937 .addImm(MaxOffset + 8 - ArgSizeA8);
13938
13939 // Branch to "overflowMBB" if offset >= max
13940 // Fall through to "offsetMBB" otherwise
13941 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13942 .addMBB(overflowMBB);
13943 }
13944
13945 // In offsetMBB, emit code to use the reg_save_area.
13946 if (offsetMBB) {
13947 assert(OffsetReg != 0);
13948
13949 // Read the reg_save_area address.
13950 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13951 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13952 .addOperand(Base)
13953 .addOperand(Scale)
13954 .addOperand(Index)
13955 .addDisp(Disp, 16)
13956 .addOperand(Segment)
13957 .setMemRefs(MMOBegin, MMOEnd);
13958
13959 // Zero-extend the offset
13960 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13961 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13962 .addImm(0)
13963 .addReg(OffsetReg)
13964 .addImm(X86::sub_32bit);
13965
13966 // Add the offset to the reg_save_area to get the final address.
13967 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13968 .addReg(OffsetReg64)
13969 .addReg(RegSaveReg);
13970
13971 // Compute the offset for the next argument
13972 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13973 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13974 .addReg(OffsetReg)
13975 .addImm(UseFPOffset ? 16 : 8);
13976
13977 // Store it back into the va_list.
13978 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13979 .addOperand(Base)
13980 .addOperand(Scale)
13981 .addOperand(Index)
13982 .addDisp(Disp, UseFPOffset ? 4 : 0)
13983 .addOperand(Segment)
13984 .addReg(NextOffsetReg)
13985 .setMemRefs(MMOBegin, MMOEnd);
13986
13987 // Jump to endMBB
13988 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13989 .addMBB(endMBB);
13990 }
13991
13992 //
13993 // Emit code to use overflow area
13994 //
13995
13996 // Load the overflow_area address into a register.
13997 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13998 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13999 .addOperand(Base)
14000 .addOperand(Scale)
14001 .addOperand(Index)
14002 .addDisp(Disp, 8)
14003 .addOperand(Segment)
14004 .setMemRefs(MMOBegin, MMOEnd);
14005
14006 // If we need to align it, do so. Otherwise, just copy the address
14007 // to OverflowDestReg.
14008 if (NeedsAlign) {
14009 // Align the overflow address
14010 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
14011 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
14012
14013 // aligned_addr = (addr + (align-1)) & ~(align-1)
14014 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
14015 .addReg(OverflowAddrReg)
14016 .addImm(Align-1);
14017
14018 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
14019 .addReg(TmpReg)
14020 .addImm(~(uint64_t)(Align-1));
14021 } else {
14022 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
14023 .addReg(OverflowAddrReg);
14024 }
14025
14026 // Compute the next overflow address after this argument.
14027 // (the overflow address should be kept 8-byte aligned)
14028 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
14029 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
14030 .addReg(OverflowDestReg)
14031 .addImm(ArgSizeA8);
14032
14033 // Store the new overflow address.
14034 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
14035 .addOperand(Base)
14036 .addOperand(Scale)
14037 .addOperand(Index)
14038 .addDisp(Disp, 8)
14039 .addOperand(Segment)
14040 .addReg(NextAddrReg)
14041 .setMemRefs(MMOBegin, MMOEnd);
14042
14043 // If we branched, emit the PHI to the front of endMBB.
14044 if (offsetMBB) {
14045 BuildMI(*endMBB, endMBB->begin(), DL,
14046 TII->get(X86::PHI), DestReg)
14047 .addReg(OffsetDestReg).addMBB(offsetMBB)
14048 .addReg(OverflowDestReg).addMBB(overflowMBB);
14049 }
14050
14051 // Erase the pseudo instruction
14052 MI->eraseFromParent();
14053
14054 return endMBB;
14055}
14056
14057MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000014058X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
14059 MachineInstr *MI,
14060 MachineBasicBlock *MBB) const {
14061 // Emit code to save XMM registers to the stack. The ABI says that the
14062 // number of registers to save is given in %al, so it's theoretically
14063 // possible to do an indirect jump trick to avoid saving all of them,
14064 // however this code takes a simpler approach and just executes all
14065 // of the stores if %al is non-zero. It's less code, and it's probably
14066 // easier on the hardware branch predictor, and stores aren't all that
14067 // expensive anyway.
14068
14069 // Create the new basic blocks. One block contains all the XMM stores,
14070 // and one block is the final destination regardless of whether any
14071 // stores were performed.
14072 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
14073 MachineFunction *F = MBB->getParent();
14074 MachineFunction::iterator MBBIter = MBB;
14075 ++MBBIter;
14076 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
14077 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
14078 F->insert(MBBIter, XMMSaveMBB);
14079 F->insert(MBBIter, EndMBB);
14080
Dan Gohman14152b42010-07-06 20:24:04 +000014081 // Transfer the remainder of MBB and its successor edges to EndMBB.
14082 EndMBB->splice(EndMBB->begin(), MBB,
14083 llvm::next(MachineBasicBlock::iterator(MI)),
14084 MBB->end());
14085 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
14086
Dan Gohmand6708ea2009-08-15 01:38:56 +000014087 // The original block will now fall through to the XMM save block.
14088 MBB->addSuccessor(XMMSaveMBB);
14089 // The XMMSaveMBB will fall through to the end block.
14090 XMMSaveMBB->addSuccessor(EndMBB);
14091
14092 // Now add the instructions.
14093 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14094 DebugLoc DL = MI->getDebugLoc();
14095
14096 unsigned CountReg = MI->getOperand(0).getReg();
14097 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
14098 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
14099
14100 if (!Subtarget->isTargetWin64()) {
14101 // If %al is 0, branch around the XMM save block.
14102 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000014103 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014104 MBB->addSuccessor(EndMBB);
14105 }
14106
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014107 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000014108 // In the XMM save block, save all the XMM argument registers.
14109 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
14110 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000014111 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000014112 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000014113 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000014114 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000014115 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000014116 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000014117 .addFrameIndex(RegSaveFrameIndex)
14118 .addImm(/*Scale=*/1)
14119 .addReg(/*IndexReg=*/0)
14120 .addImm(/*Disp=*/Offset)
14121 .addReg(/*Segment=*/0)
14122 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000014123 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014124 }
14125
Dan Gohman14152b42010-07-06 20:24:04 +000014126 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000014127
14128 return EndMBB;
14129}
Mon P Wang63307c32008-05-05 19:05:59 +000014130
Lang Hames6e3f7e42012-02-03 01:13:49 +000014131// The EFLAGS operand of SelectItr might be missing a kill marker
14132// because there were multiple uses of EFLAGS, and ISel didn't know
14133// which to mark. Figure out whether SelectItr should have had a
14134// kill marker, and set it if it should. Returns the correct kill
14135// marker value.
14136static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
14137 MachineBasicBlock* BB,
14138 const TargetRegisterInfo* TRI) {
14139 // Scan forward through BB for a use/def of EFLAGS.
14140 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
14141 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000014142 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014143 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000014144 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014145 if (mi.definesRegister(X86::EFLAGS))
14146 break; // Should have kill-flag - update below.
14147 }
14148
14149 // If we hit the end of the block, check whether EFLAGS is live into a
14150 // successor.
14151 if (miI == BB->end()) {
14152 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
14153 sEnd = BB->succ_end();
14154 sItr != sEnd; ++sItr) {
14155 MachineBasicBlock* succ = *sItr;
14156 if (succ->isLiveIn(X86::EFLAGS))
14157 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000014158 }
14159 }
14160
Lang Hames6e3f7e42012-02-03 01:13:49 +000014161 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
14162 // out. SelectMI should have a kill flag on EFLAGS.
14163 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000014164 return true;
14165}
14166
Evan Cheng60c07e12006-07-05 22:17:51 +000014167MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000014168X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014169 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000014170 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14171 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000014172
Chris Lattner52600972009-09-02 05:57:00 +000014173 // To "insert" a SELECT_CC instruction, we actually have to insert the
14174 // diamond control-flow pattern. The incoming instruction knows the
14175 // destination vreg to set, the condition code register to branch on, the
14176 // true/false values to select between, and a branch opcode to use.
14177 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14178 MachineFunction::iterator It = BB;
14179 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000014180
Chris Lattner52600972009-09-02 05:57:00 +000014181 // thisMBB:
14182 // ...
14183 // TrueVal = ...
14184 // cmpTY ccX, r1, r2
14185 // bCC copy1MBB
14186 // fallthrough --> copy0MBB
14187 MachineBasicBlock *thisMBB = BB;
14188 MachineFunction *F = BB->getParent();
14189 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
14190 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000014191 F->insert(It, copy0MBB);
14192 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000014193
Bill Wendling730c07e2010-06-25 20:48:10 +000014194 // If the EFLAGS register isn't dead in the terminator, then claim that it's
14195 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000014196 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
14197 if (!MI->killsRegister(X86::EFLAGS) &&
14198 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
14199 copy0MBB->addLiveIn(X86::EFLAGS);
14200 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000014201 }
14202
Dan Gohman14152b42010-07-06 20:24:04 +000014203 // Transfer the remainder of BB and its successor edges to sinkMBB.
14204 sinkMBB->splice(sinkMBB->begin(), BB,
14205 llvm::next(MachineBasicBlock::iterator(MI)),
14206 BB->end());
14207 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
14208
14209 // Add the true and fallthrough blocks as its successors.
14210 BB->addSuccessor(copy0MBB);
14211 BB->addSuccessor(sinkMBB);
14212
14213 // Create the conditional branch instruction.
14214 unsigned Opc =
14215 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
14216 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
14217
Chris Lattner52600972009-09-02 05:57:00 +000014218 // copy0MBB:
14219 // %FalseValue = ...
14220 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000014221 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000014222
Chris Lattner52600972009-09-02 05:57:00 +000014223 // sinkMBB:
14224 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
14225 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000014226 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14227 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000014228 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
14229 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
14230
Dan Gohman14152b42010-07-06 20:24:04 +000014231 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000014232 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000014233}
14234
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014235MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014236X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
14237 bool Is64Bit) const {
14238 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14239 DebugLoc DL = MI->getDebugLoc();
14240 MachineFunction *MF = BB->getParent();
14241 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14242
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014243 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014244
14245 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
14246 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
14247
14248 // BB:
14249 // ... [Till the alloca]
14250 // If stacklet is not large enough, jump to mallocMBB
14251 //
14252 // bumpMBB:
14253 // Allocate by subtracting from RSP
14254 // Jump to continueMBB
14255 //
14256 // mallocMBB:
14257 // Allocate by call to runtime
14258 //
14259 // continueMBB:
14260 // ...
14261 // [rest of original BB]
14262 //
14263
14264 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14265 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14266 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14267
14268 MachineRegisterInfo &MRI = MF->getRegInfo();
14269 const TargetRegisterClass *AddrRegClass =
14270 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
14271
14272 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14273 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14274 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000014275 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014276 sizeVReg = MI->getOperand(1).getReg(),
14277 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
14278
14279 MachineFunction::iterator MBBIter = BB;
14280 ++MBBIter;
14281
14282 MF->insert(MBBIter, bumpMBB);
14283 MF->insert(MBBIter, mallocMBB);
14284 MF->insert(MBBIter, continueMBB);
14285
14286 continueMBB->splice(continueMBB->begin(), BB, llvm::next
14287 (MachineBasicBlock::iterator(MI)), BB->end());
14288 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
14289
14290 // Add code to the main basic block to check if the stack limit has been hit,
14291 // and if so, jump to mallocMBB otherwise to bumpMBB.
14292 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000014293 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014294 .addReg(tmpSPVReg).addReg(sizeVReg);
14295 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000014296 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014297 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014298 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
14299
14300 // bumpMBB simply decreases the stack pointer, since we know the current
14301 // stacklet has enough space.
14302 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014303 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014304 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014305 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014306 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14307
14308 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014309 const uint32_t *RegMask =
14310 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014311 if (Is64Bit) {
14312 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
14313 .addReg(sizeVReg);
14314 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014315 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014316 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014317 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014318 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014319 } else {
14320 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
14321 .addImm(12);
14322 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
14323 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014324 .addExternalSymbol("__morestack_allocate_stack_space")
14325 .addRegMask(RegMask)
14326 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014327 }
14328
14329 if (!Is64Bit)
14330 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
14331 .addImm(16);
14332
14333 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
14334 .addReg(Is64Bit ? X86::RAX : X86::EAX);
14335 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14336
14337 // Set up the CFG correctly.
14338 BB->addSuccessor(bumpMBB);
14339 BB->addSuccessor(mallocMBB);
14340 mallocMBB->addSuccessor(continueMBB);
14341 bumpMBB->addSuccessor(continueMBB);
14342
14343 // Take care of the PHI nodes.
14344 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
14345 MI->getOperand(0).getReg())
14346 .addReg(mallocPtrVReg).addMBB(mallocMBB)
14347 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
14348
14349 // Delete the original pseudo instruction.
14350 MI->eraseFromParent();
14351
14352 // And we're done.
14353 return continueMBB;
14354}
14355
14356MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014357X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014358 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014359 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14360 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014361
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014362 assert(!Subtarget->isTargetEnvMacho());
14363
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014364 // The lowering is pretty easy: we're just emitting the call to _alloca. The
14365 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014366
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014367 if (Subtarget->isTargetWin64()) {
14368 if (Subtarget->isTargetCygMing()) {
14369 // ___chkstk(Mingw64):
14370 // Clobbers R10, R11, RAX and EFLAGS.
14371 // Updates RSP.
14372 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14373 .addExternalSymbol("___chkstk")
14374 .addReg(X86::RAX, RegState::Implicit)
14375 .addReg(X86::RSP, RegState::Implicit)
14376 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
14377 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
14378 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14379 } else {
14380 // __chkstk(MSVCRT): does not update stack pointer.
14381 // Clobbers R10, R11 and EFLAGS.
14382 // FIXME: RAX(allocated size) might be reused and not killed.
14383 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14384 .addExternalSymbol("__chkstk")
14385 .addReg(X86::RAX, RegState::Implicit)
14386 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14387 // RAX has the offset to subtracted from RSP.
14388 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
14389 .addReg(X86::RSP)
14390 .addReg(X86::RAX);
14391 }
14392 } else {
14393 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014394 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
14395
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014396 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
14397 .addExternalSymbol(StackProbeSymbol)
14398 .addReg(X86::EAX, RegState::Implicit)
14399 .addReg(X86::ESP, RegState::Implicit)
14400 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
14401 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
14402 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14403 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014404
Dan Gohman14152b42010-07-06 20:24:04 +000014405 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014406 return BB;
14407}
Chris Lattner52600972009-09-02 05:57:00 +000014408
14409MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000014410X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
14411 MachineBasicBlock *BB) const {
14412 // This is pretty easy. We're taking the value that we received from
14413 // our load from the relocation, sticking it in either RDI (x86-64)
14414 // or EAX and doing an indirect call. The return value will then
14415 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000014416 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000014417 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000014418 DebugLoc DL = MI->getDebugLoc();
14419 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000014420
14421 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014422 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014423
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014424 // Get a register mask for the lowered call.
14425 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14426 // proper register mask.
14427 const uint32_t *RegMask =
14428 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014429 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014430 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14431 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014432 .addReg(X86::RIP)
14433 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014434 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014435 MI->getOperand(3).getTargetFlags())
14436 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014437 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014438 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014439 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014440 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014441 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14442 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014443 .addReg(0)
14444 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014445 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014446 MI->getOperand(3).getTargetFlags())
14447 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014448 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014449 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014450 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014451 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014452 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14453 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014454 .addReg(TII->getGlobalBaseReg(F))
14455 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014456 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014457 MI->getOperand(3).getTargetFlags())
14458 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014459 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014460 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014461 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014462 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014463
Dan Gohman14152b42010-07-06 20:24:04 +000014464 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014465 return BB;
14466}
14467
14468MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014469X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14470 MachineBasicBlock *MBB) const {
14471 DebugLoc DL = MI->getDebugLoc();
14472 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14473
14474 MachineFunction *MF = MBB->getParent();
14475 MachineRegisterInfo &MRI = MF->getRegInfo();
14476
14477 const BasicBlock *BB = MBB->getBasicBlock();
14478 MachineFunction::iterator I = MBB;
14479 ++I;
14480
14481 // Memory Reference
14482 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14483 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14484
14485 unsigned DstReg;
14486 unsigned MemOpndSlot = 0;
14487
14488 unsigned CurOp = 0;
14489
14490 DstReg = MI->getOperand(CurOp++).getReg();
14491 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14492 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14493 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14494 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14495
14496 MemOpndSlot = CurOp;
14497
14498 MVT PVT = getPointerTy();
14499 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14500 "Invalid Pointer Size!");
14501
14502 // For v = setjmp(buf), we generate
14503 //
14504 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014505 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014506 // SjLjSetup restoreMBB
14507 //
14508 // mainMBB:
14509 // v_main = 0
14510 //
14511 // sinkMBB:
14512 // v = phi(main, restore)
14513 //
14514 // restoreMBB:
14515 // v_restore = 1
14516
14517 MachineBasicBlock *thisMBB = MBB;
14518 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14519 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14520 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14521 MF->insert(I, mainMBB);
14522 MF->insert(I, sinkMBB);
14523 MF->push_back(restoreMBB);
14524
14525 MachineInstrBuilder MIB;
14526
14527 // Transfer the remainder of BB and its successor edges to sinkMBB.
14528 sinkMBB->splice(sinkMBB->begin(), MBB,
14529 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14530 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14531
14532 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014533 unsigned PtrStoreOpc = 0;
14534 unsigned LabelReg = 0;
14535 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14536 Reloc::Model RM = getTargetMachine().getRelocationModel();
14537 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14538 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014539
Michael Liao281ae5a2012-10-17 02:22:27 +000014540 // Prepare IP either in reg or imm.
14541 if (!UseImmLabel) {
14542 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14543 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14544 LabelReg = MRI.createVirtualRegister(PtrRC);
14545 if (Subtarget->is64Bit()) {
14546 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14547 .addReg(X86::RIP)
14548 .addImm(0)
14549 .addReg(0)
14550 .addMBB(restoreMBB)
14551 .addReg(0);
14552 } else {
14553 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14554 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14555 .addReg(XII->getGlobalBaseReg(MF))
14556 .addImm(0)
14557 .addReg(0)
14558 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14559 .addReg(0);
14560 }
14561 } else
14562 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014563 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014564 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014565 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14566 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014567 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014568 else
14569 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14570 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014571 if (!UseImmLabel)
14572 MIB.addReg(LabelReg);
14573 else
14574 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014575 MIB.setMemRefs(MMOBegin, MMOEnd);
14576 // Setup
14577 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14578 .addMBB(restoreMBB);
14579 MIB.addRegMask(RegInfo->getNoPreservedMask());
14580 thisMBB->addSuccessor(mainMBB);
14581 thisMBB->addSuccessor(restoreMBB);
14582
14583 // mainMBB:
14584 // EAX = 0
14585 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14586 mainMBB->addSuccessor(sinkMBB);
14587
14588 // sinkMBB:
14589 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14590 TII->get(X86::PHI), DstReg)
14591 .addReg(mainDstReg).addMBB(mainMBB)
14592 .addReg(restoreDstReg).addMBB(restoreMBB);
14593
14594 // restoreMBB:
14595 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14596 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14597 restoreMBB->addSuccessor(sinkMBB);
14598
14599 MI->eraseFromParent();
14600 return sinkMBB;
14601}
14602
14603MachineBasicBlock *
14604X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14605 MachineBasicBlock *MBB) const {
14606 DebugLoc DL = MI->getDebugLoc();
14607 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14608
14609 MachineFunction *MF = MBB->getParent();
14610 MachineRegisterInfo &MRI = MF->getRegInfo();
14611
14612 // Memory Reference
14613 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14614 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14615
14616 MVT PVT = getPointerTy();
14617 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14618 "Invalid Pointer Size!");
14619
14620 const TargetRegisterClass *RC =
14621 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14622 unsigned Tmp = MRI.createVirtualRegister(RC);
14623 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14624 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14625 unsigned SP = RegInfo->getStackRegister();
14626
14627 MachineInstrBuilder MIB;
14628
Michael Liao281ae5a2012-10-17 02:22:27 +000014629 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14630 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014631
14632 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14633 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14634
14635 // Reload FP
14636 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14637 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14638 MIB.addOperand(MI->getOperand(i));
14639 MIB.setMemRefs(MMOBegin, MMOEnd);
14640 // Reload IP
14641 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14642 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14643 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014644 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014645 else
14646 MIB.addOperand(MI->getOperand(i));
14647 }
14648 MIB.setMemRefs(MMOBegin, MMOEnd);
14649 // Reload SP
14650 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14651 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14652 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014653 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014654 else
14655 MIB.addOperand(MI->getOperand(i));
14656 }
14657 MIB.setMemRefs(MMOBegin, MMOEnd);
14658 // Jump
14659 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14660
14661 MI->eraseFromParent();
14662 return MBB;
14663}
14664
14665MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014666X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014667 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014668 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014669 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014670 case X86::TAILJMPd64:
14671 case X86::TAILJMPr64:
14672 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014673 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014674 case X86::TCRETURNdi64:
14675 case X86::TCRETURNri64:
14676 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014677 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014678 case X86::WIN_ALLOCA:
14679 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014680 case X86::SEG_ALLOCA_32:
14681 return EmitLoweredSegAlloca(MI, BB, false);
14682 case X86::SEG_ALLOCA_64:
14683 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014684 case X86::TLSCall_32:
14685 case X86::TLSCall_64:
14686 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014687 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014688 case X86::CMOV_FR32:
14689 case X86::CMOV_FR64:
14690 case X86::CMOV_V4F32:
14691 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014692 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014693 case X86::CMOV_V8F32:
14694 case X86::CMOV_V4F64:
14695 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014696 case X86::CMOV_GR16:
14697 case X86::CMOV_GR32:
14698 case X86::CMOV_RFP32:
14699 case X86::CMOV_RFP64:
14700 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014701 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014702
Dale Johannesen849f2142007-07-03 00:53:03 +000014703 case X86::FP32_TO_INT16_IN_MEM:
14704 case X86::FP32_TO_INT32_IN_MEM:
14705 case X86::FP32_TO_INT64_IN_MEM:
14706 case X86::FP64_TO_INT16_IN_MEM:
14707 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014708 case X86::FP64_TO_INT64_IN_MEM:
14709 case X86::FP80_TO_INT16_IN_MEM:
14710 case X86::FP80_TO_INT32_IN_MEM:
14711 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014712 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14713 DebugLoc DL = MI->getDebugLoc();
14714
Evan Cheng60c07e12006-07-05 22:17:51 +000014715 // Change the floating point control register to use "round towards zero"
14716 // mode when truncating to an integer value.
14717 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014718 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014719 addFrameReference(BuildMI(*BB, MI, DL,
14720 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014721
14722 // Load the old value of the high byte of the control word...
14723 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014724 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014725 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014726 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014727
14728 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014729 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014730 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014731
14732 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014733 addFrameReference(BuildMI(*BB, MI, DL,
14734 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014735
14736 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014737 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014738 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014739
14740 // Get the X86 opcode to use.
14741 unsigned Opc;
14742 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014743 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014744 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14745 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14746 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14747 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14748 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14749 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014750 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14751 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14752 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014753 }
14754
14755 X86AddressMode AM;
14756 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014757 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014758 AM.BaseType = X86AddressMode::RegBase;
14759 AM.Base.Reg = Op.getReg();
14760 } else {
14761 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014762 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014763 }
14764 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014765 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014766 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014767 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014768 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014769 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014770 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014771 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014772 AM.GV = Op.getGlobal();
14773 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014774 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014775 }
Dan Gohman14152b42010-07-06 20:24:04 +000014776 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014777 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014778
14779 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014780 addFrameReference(BuildMI(*BB, MI, DL,
14781 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014782
Dan Gohman14152b42010-07-06 20:24:04 +000014783 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014784 return BB;
14785 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014786 // String/text processing lowering.
14787 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014788 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014789 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014790 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014791 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014792 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014793 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014794 case X86::VPCMPESTRM128MEM:
14795 assert(Subtarget->hasSSE42() &&
14796 "Target must have SSE4.2 or AVX features enabled");
14797 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014798
14799 // String/text processing lowering.
14800 case X86::PCMPISTRIREG:
14801 case X86::VPCMPISTRIREG:
14802 case X86::PCMPISTRIMEM:
14803 case X86::VPCMPISTRIMEM:
14804 case X86::PCMPESTRIREG:
14805 case X86::VPCMPESTRIREG:
14806 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014807 case X86::VPCMPESTRIMEM:
14808 assert(Subtarget->hasSSE42() &&
14809 "Target must have SSE4.2 or AVX features enabled");
14810 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014811
Craig Topper8aae8dd2012-11-10 08:57:41 +000014812 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014813 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014814 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014815
Michael Liaobe02a902012-11-08 07:28:54 +000014816 // xbegin
14817 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014818 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014819
Craig Topper8aae8dd2012-11-10 08:57:41 +000014820 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014821 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014822 case X86::ATOMAND16:
14823 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014824 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014825 // Fall through
14826 case X86::ATOMOR8:
14827 case X86::ATOMOR16:
14828 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014829 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014830 // Fall through
14831 case X86::ATOMXOR16:
14832 case X86::ATOMXOR8:
14833 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014834 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014835 // Fall through
14836 case X86::ATOMNAND8:
14837 case X86::ATOMNAND16:
14838 case X86::ATOMNAND32:
14839 case X86::ATOMNAND64:
14840 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014841 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014842 case X86::ATOMMAX16:
14843 case X86::ATOMMAX32:
14844 case X86::ATOMMAX64:
14845 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014846 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014847 case X86::ATOMMIN16:
14848 case X86::ATOMMIN32:
14849 case X86::ATOMMIN64:
14850 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014851 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014852 case X86::ATOMUMAX16:
14853 case X86::ATOMUMAX32:
14854 case X86::ATOMUMAX64:
14855 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014856 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014857 case X86::ATOMUMIN16:
14858 case X86::ATOMUMIN32:
14859 case X86::ATOMUMIN64:
14860 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014861
14862 // This group does 64-bit operations on a 32-bit host.
14863 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014864 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014865 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014866 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014867 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014868 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014869 case X86::ATOMMAX6432:
14870 case X86::ATOMMIN6432:
14871 case X86::ATOMUMAX6432:
14872 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014873 case X86::ATOMSWAP6432:
14874 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014875
Dan Gohmand6708ea2009-08-15 01:38:56 +000014876 case X86::VASTART_SAVE_XMM_REGS:
14877 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014878
14879 case X86::VAARG_64:
14880 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014881
14882 case X86::EH_SjLj_SetJmp32:
14883 case X86::EH_SjLj_SetJmp64:
14884 return emitEHSjLjSetJmp(MI, BB);
14885
14886 case X86::EH_SjLj_LongJmp32:
14887 case X86::EH_SjLj_LongJmp64:
14888 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014889 }
14890}
14891
14892//===----------------------------------------------------------------------===//
14893// X86 Optimization Hooks
14894//===----------------------------------------------------------------------===//
14895
Dan Gohman475871a2008-07-27 21:46:04 +000014896void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014897 APInt &KnownZero,
14898 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014899 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014900 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014901 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014902 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014903 assert((Opc >= ISD::BUILTIN_OP_END ||
14904 Opc == ISD::INTRINSIC_WO_CHAIN ||
14905 Opc == ISD::INTRINSIC_W_CHAIN ||
14906 Opc == ISD::INTRINSIC_VOID) &&
14907 "Should use MaskedValueIsZero if you don't know whether Op"
14908 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014909
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014910 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014911 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014912 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014913 case X86ISD::ADD:
14914 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014915 case X86ISD::ADC:
14916 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014917 case X86ISD::SMUL:
14918 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014919 case X86ISD::INC:
14920 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014921 case X86ISD::OR:
14922 case X86ISD::XOR:
14923 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014924 // These nodes' second result is a boolean.
14925 if (Op.getResNo() == 0)
14926 break;
14927 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014928 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014929 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014930 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014931 case ISD::INTRINSIC_WO_CHAIN: {
14932 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14933 unsigned NumLoBits = 0;
14934 switch (IntId) {
14935 default: break;
14936 case Intrinsic::x86_sse_movmsk_ps:
14937 case Intrinsic::x86_avx_movmsk_ps_256:
14938 case Intrinsic::x86_sse2_movmsk_pd:
14939 case Intrinsic::x86_avx_movmsk_pd_256:
14940 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014941 case Intrinsic::x86_sse2_pmovmskb_128:
14942 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014943 // High bits of movmskp{s|d}, pmovmskb are known zero.
14944 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014945 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014946 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14947 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14948 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14949 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14950 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14951 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014952 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014953 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014954 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014955 break;
14956 }
14957 }
14958 break;
14959 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014960 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014961}
Chris Lattner259e97c2006-01-31 19:43:35 +000014962
Owen Andersonbc146b02010-09-21 20:42:50 +000014963unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14964 unsigned Depth) const {
14965 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14966 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14967 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014968
Owen Andersonbc146b02010-09-21 20:42:50 +000014969 // Fallback case.
14970 return 1;
14971}
14972
Evan Cheng206ee9d2006-07-07 08:33:52 +000014973/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014974/// node is a GlobalAddress + offset.
14975bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014976 const GlobalValue* &GA,
14977 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014978 if (N->getOpcode() == X86ISD::Wrapper) {
14979 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014980 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014981 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014982 return true;
14983 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014984 }
Evan Chengad4196b2008-05-12 19:56:52 +000014985 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014986}
14987
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014988/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14989/// same as extracting the high 128-bit part of 256-bit vector and then
14990/// inserting the result into the low part of a new 256-bit vector
14991static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14992 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014993 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014994
14995 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014996 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014997 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14998 SVOp->getMaskElt(j) >= 0)
14999 return false;
15000
15001 return true;
15002}
15003
15004/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
15005/// same as extracting the low 128-bit part of 256-bit vector and then
15006/// inserting the result into the high part of a new 256-bit vector
15007static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
15008 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015009 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015010
15011 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000015012 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015013 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
15014 SVOp->getMaskElt(j) >= 0)
15015 return false;
15016
15017 return true;
15018}
15019
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015020/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
15021static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000015022 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015023 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015024 DebugLoc dl = N->getDebugLoc();
15025 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
15026 SDValue V1 = SVOp->getOperand(0);
15027 SDValue V2 = SVOp->getOperand(1);
15028 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015029 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015030
15031 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
15032 V2.getOpcode() == ISD::CONCAT_VECTORS) {
15033 //
15034 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000015035 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015036 // V UNDEF BUILD_VECTOR UNDEF
15037 // \ / \ /
15038 // CONCAT_VECTOR CONCAT_VECTOR
15039 // \ /
15040 // \ /
15041 // RESULT: V + zero extended
15042 //
15043 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
15044 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
15045 V1.getOperand(1).getOpcode() != ISD::UNDEF)
15046 return SDValue();
15047
15048 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
15049 return SDValue();
15050
15051 // To match the shuffle mask, the first half of the mask should
15052 // be exactly the first vector, and all the rest a splat with the
15053 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000015054 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015055 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
15056 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
15057 return SDValue();
15058
Chad Rosier3d1161e2012-01-03 21:05:52 +000015059 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
15060 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000015061 if (Ld->hasNUsesOfValue(1, 0)) {
15062 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
15063 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
15064 SDValue ResNode =
15065 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
15066 Ld->getMemoryVT(),
15067 Ld->getPointerInfo(),
15068 Ld->getAlignment(),
15069 false/*isVolatile*/, true/*ReadMem*/,
15070 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000015071
15072 // Make sure the newly-created LOAD is in the same position as Ld in
15073 // terms of dependency. We create a TokenFactor for Ld and ResNode,
15074 // and update uses of Ld's output chain to use the TokenFactor.
15075 if (Ld->hasAnyUseOfValue(1)) {
15076 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
15077 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
15078 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
15079 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
15080 SDValue(ResNode.getNode(), 1));
15081 }
15082
Chad Rosier42726832012-05-07 18:47:44 +000015083 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
15084 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000015085 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000015086
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015087 // Emit a zeroed vector and insert the desired subvector on its
15088 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015089 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000015090 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015091 return DCI.CombineTo(N, InsV);
15092 }
15093
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015094 //===--------------------------------------------------------------------===//
15095 // Combine some shuffles into subvector extracts and inserts:
15096 //
15097
15098 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
15099 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015100 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
15101 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015102 return DCI.CombineTo(N, InsV);
15103 }
15104
15105 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
15106 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015107 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
15108 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015109 return DCI.CombineTo(N, InsV);
15110 }
15111
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015112 return SDValue();
15113}
15114
15115/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000015116static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015117 TargetLowering::DAGCombinerInfo &DCI,
15118 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000015119 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000015120 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000015121
Mon P Wanga0fd0d52010-12-19 23:55:53 +000015122 // Don't create instructions with illegal types after legalize types has run.
15123 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15124 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
15125 return SDValue();
15126
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015127 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015128 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015129 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015130 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015131
15132 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015133 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015134 return SDValue();
15135
15136 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
15137 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
15138 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000015139 SmallVector<SDValue, 16> Elts;
15140 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000015141 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000015142
Nate Begemanfdea31a2010-03-24 20:49:50 +000015143 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000015144}
Evan Chengd880b972008-05-09 21:53:03 +000015145
Nadav Roteme12bf182013-01-04 17:35:21 +000015146/// PerformTruncateCombine - Converts truncate operation to
15147/// a sequence of vector shuffle operations.
15148/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000015149static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
15150 TargetLowering::DAGCombinerInfo &DCI,
15151 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000015152 return SDValue();
15153}
15154
Craig Topper89f4e662012-03-20 07:17:59 +000015155/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
15156/// specific shuffle of a load can be folded into a single element load.
15157/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
15158/// shuffles have been customed lowered so we need to handle those here.
15159static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
15160 TargetLowering::DAGCombinerInfo &DCI) {
15161 if (DCI.isBeforeLegalizeOps())
15162 return SDValue();
15163
15164 SDValue InVec = N->getOperand(0);
15165 SDValue EltNo = N->getOperand(1);
15166
15167 if (!isa<ConstantSDNode>(EltNo))
15168 return SDValue();
15169
15170 EVT VT = InVec.getValueType();
15171
15172 bool HasShuffleIntoBitcast = false;
15173 if (InVec.getOpcode() == ISD::BITCAST) {
15174 // Don't duplicate a load with other uses.
15175 if (!InVec.hasOneUse())
15176 return SDValue();
15177 EVT BCVT = InVec.getOperand(0).getValueType();
15178 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
15179 return SDValue();
15180 InVec = InVec.getOperand(0);
15181 HasShuffleIntoBitcast = true;
15182 }
15183
15184 if (!isTargetShuffle(InVec.getOpcode()))
15185 return SDValue();
15186
15187 // Don't duplicate a load with other uses.
15188 if (!InVec.hasOneUse())
15189 return SDValue();
15190
15191 SmallVector<int, 16> ShuffleMask;
15192 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000015193 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
15194 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000015195 return SDValue();
15196
15197 // Select the input vector, guarding against out of range extract vector.
15198 unsigned NumElems = VT.getVectorNumElements();
15199 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
15200 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
15201 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
15202 : InVec.getOperand(1);
15203
15204 // If inputs to shuffle are the same for both ops, then allow 2 uses
15205 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
15206
15207 if (LdNode.getOpcode() == ISD::BITCAST) {
15208 // Don't duplicate a load with other uses.
15209 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
15210 return SDValue();
15211
15212 AllowedUses = 1; // only allow 1 load use if we have a bitcast
15213 LdNode = LdNode.getOperand(0);
15214 }
15215
15216 if (!ISD::isNormalLoad(LdNode.getNode()))
15217 return SDValue();
15218
15219 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
15220
15221 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
15222 return SDValue();
15223
15224 if (HasShuffleIntoBitcast) {
15225 // If there's a bitcast before the shuffle, check if the load type and
15226 // alignment is valid.
15227 unsigned Align = LN0->getAlignment();
15228 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000015229 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000015230 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
15231
15232 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
15233 return SDValue();
15234 }
15235
15236 // All checks match so transform back to vector_shuffle so that DAG combiner
15237 // can finish the job
15238 DebugLoc dl = N->getDebugLoc();
15239
15240 // Create shuffle node taking into account the case that its a unary shuffle
15241 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
15242 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
15243 InVec.getOperand(0), Shuffle,
15244 &ShuffleMask[0]);
15245 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
15246 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
15247 EltNo);
15248}
15249
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000015250/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
15251/// generation and convert it from being a bunch of shuffles and extracts
15252/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015253static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000015254 TargetLowering::DAGCombinerInfo &DCI) {
15255 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
15256 if (NewOp.getNode())
15257 return NewOp;
15258
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015259 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000015260 // Detect whether we are trying to convert from mmx to i32 and the bitcast
15261 // from mmx to v2i32 has a single usage.
15262 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
15263 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
15264 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
15265 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
15266 N->getValueType(0),
15267 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015268
15269 // Only operate on vectors of 4 elements, where the alternative shuffling
15270 // gets to be more expensive.
15271 if (InputVector.getValueType() != MVT::v4i32)
15272 return SDValue();
15273
15274 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
15275 // single use which is a sign-extend or zero-extend, and all elements are
15276 // used.
15277 SmallVector<SDNode *, 4> Uses;
15278 unsigned ExtractedElements = 0;
15279 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
15280 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
15281 if (UI.getUse().getResNo() != InputVector.getResNo())
15282 return SDValue();
15283
15284 SDNode *Extract = *UI;
15285 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
15286 return SDValue();
15287
15288 if (Extract->getValueType(0) != MVT::i32)
15289 return SDValue();
15290 if (!Extract->hasOneUse())
15291 return SDValue();
15292 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
15293 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
15294 return SDValue();
15295 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
15296 return SDValue();
15297
15298 // Record which element was extracted.
15299 ExtractedElements |=
15300 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
15301
15302 Uses.push_back(Extract);
15303 }
15304
15305 // If not all the elements were used, this may not be worthwhile.
15306 if (ExtractedElements != 15)
15307 return SDValue();
15308
15309 // Ok, we've now decided to do the transformation.
15310 DebugLoc dl = InputVector.getDebugLoc();
15311
15312 // Store the value to a temporary stack slot.
15313 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000015314 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
15315 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015316
15317 // Replace each use (extract) with a load of the appropriate element.
15318 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
15319 UE = Uses.end(); UI != UE; ++UI) {
15320 SDNode *Extract = *UI;
15321
Nadav Rotem86694292011-05-17 08:31:57 +000015322 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015323 SDValue Idx = Extract->getOperand(1);
15324 unsigned EltSize =
15325 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
15326 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000015327 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015328 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
15329
Nadav Rotem86694292011-05-17 08:31:57 +000015330 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015331 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015332
15333 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000015334 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000015335 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015336 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015337
15338 // Replace the exact with the load.
15339 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
15340 }
15341
15342 // The replacement was made in place; don't return anything.
15343 return SDValue();
15344}
15345
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015346/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
15347static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
15348 SDValue RHS, SelectionDAG &DAG,
15349 const X86Subtarget *Subtarget) {
15350 if (!VT.isVector())
15351 return 0;
15352
15353 switch (VT.getSimpleVT().SimpleTy) {
15354 default: return 0;
15355 case MVT::v32i8:
15356 case MVT::v16i16:
15357 case MVT::v8i32:
15358 if (!Subtarget->hasAVX2())
15359 return 0;
15360 case MVT::v16i8:
15361 case MVT::v8i16:
15362 case MVT::v4i32:
15363 if (!Subtarget->hasSSE2())
15364 return 0;
15365 }
15366
15367 // SSE2 has only a small subset of the operations.
15368 bool hasUnsigned = Subtarget->hasSSE41() ||
15369 (Subtarget->hasSSE2() && VT == MVT::v16i8);
15370 bool hasSigned = Subtarget->hasSSE41() ||
15371 (Subtarget->hasSSE2() && VT == MVT::v8i16);
15372
15373 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15374
15375 // Check for x CC y ? x : y.
15376 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15377 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15378 switch (CC) {
15379 default: break;
15380 case ISD::SETULT:
15381 case ISD::SETULE:
15382 return hasUnsigned ? X86ISD::UMIN : 0;
15383 case ISD::SETUGT:
15384 case ISD::SETUGE:
15385 return hasUnsigned ? X86ISD::UMAX : 0;
15386 case ISD::SETLT:
15387 case ISD::SETLE:
15388 return hasSigned ? X86ISD::SMIN : 0;
15389 case ISD::SETGT:
15390 case ISD::SETGE:
15391 return hasSigned ? X86ISD::SMAX : 0;
15392 }
15393 // Check for x CC y ? y : x -- a min/max with reversed arms.
15394 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15395 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
15396 switch (CC) {
15397 default: break;
15398 case ISD::SETULT:
15399 case ISD::SETULE:
15400 return hasUnsigned ? X86ISD::UMAX : 0;
15401 case ISD::SETUGT:
15402 case ISD::SETUGE:
15403 return hasUnsigned ? X86ISD::UMIN : 0;
15404 case ISD::SETLT:
15405 case ISD::SETLE:
15406 return hasSigned ? X86ISD::SMAX : 0;
15407 case ISD::SETGT:
15408 case ISD::SETGE:
15409 return hasSigned ? X86ISD::SMIN : 0;
15410 }
15411 }
15412
15413 return 0;
15414}
15415
Duncan Sands6bcd2192011-09-17 16:49:39 +000015416/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
15417/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015418static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000015419 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000015420 const X86Subtarget *Subtarget) {
15421 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000015422 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015423 // Get the LHS/RHS of the select.
15424 SDValue LHS = N->getOperand(1);
15425 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015426 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015427
Dan Gohman670e5392009-09-21 18:03:22 +000015428 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015429 // instructions match the semantics of the common C idiom x<y?x:y but not
15430 // x<=y?x:y, because of how they handle negative zero (which can be
15431 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015432 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15433 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015434 (Subtarget->hasSSE2() ||
15435 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015436 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015437
Chris Lattner47b4ce82009-03-11 05:48:52 +000015438 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015439 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015440 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15441 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015442 switch (CC) {
15443 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015444 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015445 // Converting this to a min would handle NaNs incorrectly, and swapping
15446 // the operands would cause it to handle comparisons between positive
15447 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015448 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015449 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015450 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15451 break;
15452 std::swap(LHS, RHS);
15453 }
Dan Gohman670e5392009-09-21 18:03:22 +000015454 Opcode = X86ISD::FMIN;
15455 break;
15456 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015457 // Converting this to a min would handle comparisons between positive
15458 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015459 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015460 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15461 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015462 Opcode = X86ISD::FMIN;
15463 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015464 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015465 // Converting this to a min would handle both negative zeros and NaNs
15466 // incorrectly, but we can swap the operands to fix both.
15467 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015468 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015469 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015470 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015471 Opcode = X86ISD::FMIN;
15472 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015473
Dan Gohman670e5392009-09-21 18:03:22 +000015474 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015475 // Converting this to a max would handle comparisons between positive
15476 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015477 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015478 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015479 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015480 Opcode = X86ISD::FMAX;
15481 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015482 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015483 // Converting this to a max would handle NaNs incorrectly, and swapping
15484 // the operands would cause it to handle comparisons between positive
15485 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015486 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015487 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015488 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15489 break;
15490 std::swap(LHS, RHS);
15491 }
Dan Gohman670e5392009-09-21 18:03:22 +000015492 Opcode = X86ISD::FMAX;
15493 break;
15494 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015495 // Converting this to a max would handle both negative zeros and NaNs
15496 // incorrectly, but we can swap the operands to fix both.
15497 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015498 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015499 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015500 case ISD::SETGE:
15501 Opcode = X86ISD::FMAX;
15502 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015503 }
Dan Gohman670e5392009-09-21 18:03:22 +000015504 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015505 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15506 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015507 switch (CC) {
15508 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015509 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015510 // Converting this to a min would handle comparisons between positive
15511 // and negative zero incorrectly, and swapping the operands would
15512 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015513 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015514 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015515 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015516 break;
15517 std::swap(LHS, RHS);
15518 }
Dan Gohman670e5392009-09-21 18:03:22 +000015519 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015520 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015521 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015522 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015523 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015524 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15525 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015526 Opcode = X86ISD::FMIN;
15527 break;
15528 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015529 // Converting this to a min would handle both negative zeros and NaNs
15530 // incorrectly, but we can swap the operands to fix both.
15531 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015532 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015533 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015534 case ISD::SETGE:
15535 Opcode = X86ISD::FMIN;
15536 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015537
Dan Gohman670e5392009-09-21 18:03:22 +000015538 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015539 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015540 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015541 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015542 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015543 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015544 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015545 // Converting this to a max would handle comparisons between positive
15546 // and negative zero incorrectly, and swapping the operands would
15547 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015548 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015549 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015550 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015551 break;
15552 std::swap(LHS, RHS);
15553 }
Dan Gohman670e5392009-09-21 18:03:22 +000015554 Opcode = X86ISD::FMAX;
15555 break;
15556 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015557 // Converting this to a max would handle both negative zeros and NaNs
15558 // incorrectly, but we can swap the operands to fix both.
15559 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015560 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015561 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015562 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015563 Opcode = X86ISD::FMAX;
15564 break;
15565 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015566 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015567
Chris Lattner47b4ce82009-03-11 05:48:52 +000015568 if (Opcode)
15569 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015570 }
Eric Christopherfd179292009-08-27 18:07:15 +000015571
Chris Lattnerd1980a52009-03-12 06:52:53 +000015572 // If this is a select between two integer constants, try to do some
15573 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015574 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15575 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015576 // Don't do this for crazy integer types.
15577 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15578 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015579 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015580 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015581
Chris Lattnercee56e72009-03-13 05:53:31 +000015582 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015583 // Efficiently invertible.
15584 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15585 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15586 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15587 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015588 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015589 }
Eric Christopherfd179292009-08-27 18:07:15 +000015590
Chris Lattnerd1980a52009-03-12 06:52:53 +000015591 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015592 if (FalseC->getAPIntValue() == 0 &&
15593 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015594 if (NeedsCondInvert) // Invert the condition if needed.
15595 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15596 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015597
Chris Lattnerd1980a52009-03-12 06:52:53 +000015598 // Zero extend the condition if needed.
15599 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015600
Chris Lattnercee56e72009-03-13 05:53:31 +000015601 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015602 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015603 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015604 }
Eric Christopherfd179292009-08-27 18:07:15 +000015605
Chris Lattner97a29a52009-03-13 05:22:11 +000015606 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015607 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015608 if (NeedsCondInvert) // Invert the condition if needed.
15609 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15610 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015611
Chris Lattner97a29a52009-03-13 05:22:11 +000015612 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015613 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15614 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015615 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015616 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015617 }
Eric Christopherfd179292009-08-27 18:07:15 +000015618
Chris Lattnercee56e72009-03-13 05:53:31 +000015619 // Optimize cases that will turn into an LEA instruction. This requires
15620 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015621 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015622 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015623 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015624
Chris Lattnercee56e72009-03-13 05:53:31 +000015625 bool isFastMultiplier = false;
15626 if (Diff < 10) {
15627 switch ((unsigned char)Diff) {
15628 default: break;
15629 case 1: // result = add base, cond
15630 case 2: // result = lea base( , cond*2)
15631 case 3: // result = lea base(cond, cond*2)
15632 case 4: // result = lea base( , cond*4)
15633 case 5: // result = lea base(cond, cond*4)
15634 case 8: // result = lea base( , cond*8)
15635 case 9: // result = lea base(cond, cond*8)
15636 isFastMultiplier = true;
15637 break;
15638 }
15639 }
Eric Christopherfd179292009-08-27 18:07:15 +000015640
Chris Lattnercee56e72009-03-13 05:53:31 +000015641 if (isFastMultiplier) {
15642 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15643 if (NeedsCondInvert) // Invert the condition if needed.
15644 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15645 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015646
Chris Lattnercee56e72009-03-13 05:53:31 +000015647 // Zero extend the condition if needed.
15648 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15649 Cond);
15650 // Scale the condition by the difference.
15651 if (Diff != 1)
15652 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15653 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015654
Chris Lattnercee56e72009-03-13 05:53:31 +000015655 // Add the base if non-zero.
15656 if (FalseC->getAPIntValue() != 0)
15657 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15658 SDValue(FalseC, 0));
15659 return Cond;
15660 }
Eric Christopherfd179292009-08-27 18:07:15 +000015661 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015662 }
15663 }
Eric Christopherfd179292009-08-27 18:07:15 +000015664
Evan Cheng56f582d2012-01-04 01:41:39 +000015665 // Canonicalize max and min:
15666 // (x > y) ? x : y -> (x >= y) ? x : y
15667 // (x < y) ? x : y -> (x <= y) ? x : y
15668 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15669 // the need for an extra compare
15670 // against zero. e.g.
15671 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15672 // subl %esi, %edi
15673 // testl %edi, %edi
15674 // movl $0, %eax
15675 // cmovgl %edi, %eax
15676 // =>
15677 // xorl %eax, %eax
15678 // subl %esi, $edi
15679 // cmovsl %eax, %edi
15680 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15681 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15682 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15683 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15684 switch (CC) {
15685 default: break;
15686 case ISD::SETLT:
15687 case ISD::SETGT: {
15688 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15689 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15690 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15691 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15692 }
15693 }
15694 }
15695
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015696 // Match VSELECTs into subs with unsigned saturation.
15697 if (!DCI.isBeforeLegalize() &&
15698 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15699 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15700 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15701 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15702 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15703
15704 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15705 // left side invert the predicate to simplify logic below.
15706 SDValue Other;
15707 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15708 Other = RHS;
15709 CC = ISD::getSetCCInverse(CC, true);
15710 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15711 Other = LHS;
15712 }
15713
15714 if (Other.getNode() && Other->getNumOperands() == 2 &&
15715 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15716 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15717 SDValue CondRHS = Cond->getOperand(1);
15718
15719 // Look for a general sub with unsigned saturation first.
15720 // x >= y ? x-y : 0 --> subus x, y
15721 // x > y ? x-y : 0 --> subus x, y
15722 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15723 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15724 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15725
15726 // If the RHS is a constant we have to reverse the const canonicalization.
15727 // x > C-1 ? x+-C : 0 --> subus x, C
15728 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15729 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15730 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015731 if (CondRHS.getConstantOperandVal(0) == -A-1)
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015732 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015733 DAG.getConstant(-A, VT));
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015734 }
15735
15736 // Another special case: If C was a sign bit, the sub has been
15737 // canonicalized into a xor.
15738 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15739 // it's safe to decanonicalize the xor?
15740 // x s< 0 ? x^C : 0 --> subus x, C
15741 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15742 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15743 isSplatVector(OpRHS.getNode())) {
15744 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15745 if (A.isSignBit())
15746 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15747 }
15748 }
15749 }
15750
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015751 // Try to match a min/max vector operation.
15752 if (!DCI.isBeforeLegalize() &&
15753 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15754 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15755 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15756
Nadav Rotemcc616562012-01-15 19:27:55 +000015757 // If we know that this node is legal then we know that it is going to be
15758 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15759 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15760 // to simplify previous instructions.
15761 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15762 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015763 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015764 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015765
15766 // Don't optimize vector selects that map to mask-registers.
15767 if (BitWidth == 1)
15768 return SDValue();
15769
Nadav Rotemcc616562012-01-15 19:27:55 +000015770 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15771 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15772
15773 APInt KnownZero, KnownOne;
15774 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15775 DCI.isBeforeLegalizeOps());
15776 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15777 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15778 DCI.CommitTargetLoweringOpt(TLO);
15779 }
15780
Dan Gohman475871a2008-07-27 21:46:04 +000015781 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015782}
15783
Michael Liao2a33cec2012-08-10 19:58:13 +000015784// Check whether a boolean test is testing a boolean value generated by
15785// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15786// code.
15787//
15788// Simplify the following patterns:
15789// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15790// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15791// to (Op EFLAGS Cond)
15792//
15793// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15794// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15795// to (Op EFLAGS !Cond)
15796//
15797// where Op could be BRCOND or CMOV.
15798//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015799static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015800 // Quit if not CMP and SUB with its value result used.
15801 if (Cmp.getOpcode() != X86ISD::CMP &&
15802 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15803 return SDValue();
15804
15805 // Quit if not used as a boolean value.
15806 if (CC != X86::COND_E && CC != X86::COND_NE)
15807 return SDValue();
15808
15809 // Check CMP operands. One of them should be 0 or 1 and the other should be
15810 // an SetCC or extended from it.
15811 SDValue Op1 = Cmp.getOperand(0);
15812 SDValue Op2 = Cmp.getOperand(1);
15813
15814 SDValue SetCC;
15815 const ConstantSDNode* C = 0;
15816 bool needOppositeCond = (CC == X86::COND_E);
15817
15818 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15819 SetCC = Op2;
15820 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15821 SetCC = Op1;
15822 else // Quit if all operands are not constants.
15823 return SDValue();
15824
15825 if (C->getZExtValue() == 1)
15826 needOppositeCond = !needOppositeCond;
15827 else if (C->getZExtValue() != 0)
15828 // Quit if the constant is neither 0 or 1.
15829 return SDValue();
15830
Michael Liao258d9b72013-03-28 23:38:52 +000015831 // Skip 'zext' or 'trunc' node.
15832 if (SetCC.getOpcode() == ISD::ZERO_EXTEND ||
15833 SetCC.getOpcode() == ISD::TRUNCATE)
Michael Liao2a33cec2012-08-10 19:58:13 +000015834 SetCC = SetCC.getOperand(0);
15835
Michael Liao7fdc66b2012-09-10 16:36:16 +000015836 switch (SetCC.getOpcode()) {
15837 case X86ISD::SETCC:
15838 // Set the condition code or opposite one if necessary.
15839 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15840 if (needOppositeCond)
15841 CC = X86::GetOppositeBranchCondition(CC);
15842 return SetCC.getOperand(1);
15843 case X86ISD::CMOV: {
15844 // Check whether false/true value has canonical one, i.e. 0 or 1.
15845 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15846 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15847 // Quit if true value is not a constant.
15848 if (!TVal)
15849 return SDValue();
15850 // Quit if false value is not a constant.
15851 if (!FVal) {
Michael Liao7fdc66b2012-09-10 16:36:16 +000015852 SDValue Op = SetCC.getOperand(0);
Michael Liao258d9b72013-03-28 23:38:52 +000015853 // Skip 'zext' or 'trunc' node.
15854 if (Op.getOpcode() == ISD::ZERO_EXTEND ||
15855 Op.getOpcode() == ISD::TRUNCATE)
15856 Op = Op.getOperand(0);
Michael Liaoc26392a2013-03-28 23:41:26 +000015857 // A special case for rdrand/rdseed, where 0 is set if false cond is
15858 // found.
15859 if ((Op.getOpcode() != X86ISD::RDRAND &&
15860 Op.getOpcode() != X86ISD::RDSEED) || Op.getResNo() != 0)
Michael Liao7fdc66b2012-09-10 16:36:16 +000015861 return SDValue();
15862 }
15863 // Quit if false value is not the constant 0 or 1.
15864 bool FValIsFalse = true;
15865 if (FVal && FVal->getZExtValue() != 0) {
15866 if (FVal->getZExtValue() != 1)
15867 return SDValue();
15868 // If FVal is 1, opposite cond is needed.
15869 needOppositeCond = !needOppositeCond;
15870 FValIsFalse = false;
15871 }
15872 // Quit if TVal is not the constant opposite of FVal.
15873 if (FValIsFalse && TVal->getZExtValue() != 1)
15874 return SDValue();
15875 if (!FValIsFalse && TVal->getZExtValue() != 0)
15876 return SDValue();
15877 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15878 if (needOppositeCond)
15879 CC = X86::GetOppositeBranchCondition(CC);
15880 return SetCC.getOperand(3);
15881 }
15882 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015883
Michael Liao7fdc66b2012-09-10 16:36:16 +000015884 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015885}
15886
Chris Lattnerd1980a52009-03-12 06:52:53 +000015887/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15888static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015889 TargetLowering::DAGCombinerInfo &DCI,
15890 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015891 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015892
Chris Lattnerd1980a52009-03-12 06:52:53 +000015893 // If the flag operand isn't dead, don't touch this CMOV.
15894 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15895 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015896
Evan Chengb5a55d92011-05-24 01:48:22 +000015897 SDValue FalseOp = N->getOperand(0);
15898 SDValue TrueOp = N->getOperand(1);
15899 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15900 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015901
Evan Chengb5a55d92011-05-24 01:48:22 +000015902 if (CC == X86::COND_E || CC == X86::COND_NE) {
15903 switch (Cond.getOpcode()) {
15904 default: break;
15905 case X86ISD::BSR:
15906 case X86ISD::BSF:
15907 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15908 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15909 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15910 }
15911 }
15912
Michael Liao2a33cec2012-08-10 19:58:13 +000015913 SDValue Flags;
15914
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015915 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015916 if (Flags.getNode() &&
15917 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015918 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015919 SDValue Ops[] = { FalseOp, TrueOp,
15920 DAG.getConstant(CC, MVT::i8), Flags };
15921 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15922 Ops, array_lengthof(Ops));
15923 }
15924
Chris Lattnerd1980a52009-03-12 06:52:53 +000015925 // If this is a select between two integer constants, try to do some
15926 // optimizations. Note that the operands are ordered the opposite of SELECT
15927 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015928 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15929 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015930 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15931 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015932 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15933 CC = X86::GetOppositeBranchCondition(CC);
15934 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015935 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015936 }
Eric Christopherfd179292009-08-27 18:07:15 +000015937
Chris Lattnerd1980a52009-03-12 06:52:53 +000015938 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015939 // This is efficient for any integer data type (including i8/i16) and
15940 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015941 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015942 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15943 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015944
Chris Lattnerd1980a52009-03-12 06:52:53 +000015945 // Zero extend the condition if needed.
15946 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015947
Chris Lattnerd1980a52009-03-12 06:52:53 +000015948 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15949 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015950 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015951 if (N->getNumValues() == 2) // Dead flag value?
15952 return DCI.CombineTo(N, Cond, SDValue());
15953 return Cond;
15954 }
Eric Christopherfd179292009-08-27 18:07:15 +000015955
Chris Lattnercee56e72009-03-13 05:53:31 +000015956 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15957 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015958 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015959 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15960 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015961
Chris Lattner97a29a52009-03-13 05:22:11 +000015962 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015963 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15964 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015965 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15966 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015967
Chris Lattner97a29a52009-03-13 05:22:11 +000015968 if (N->getNumValues() == 2) // Dead flag value?
15969 return DCI.CombineTo(N, Cond, SDValue());
15970 return Cond;
15971 }
Eric Christopherfd179292009-08-27 18:07:15 +000015972
Chris Lattnercee56e72009-03-13 05:53:31 +000015973 // Optimize cases that will turn into an LEA instruction. This requires
15974 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015975 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015976 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015977 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015978
Chris Lattnercee56e72009-03-13 05:53:31 +000015979 bool isFastMultiplier = false;
15980 if (Diff < 10) {
15981 switch ((unsigned char)Diff) {
15982 default: break;
15983 case 1: // result = add base, cond
15984 case 2: // result = lea base( , cond*2)
15985 case 3: // result = lea base(cond, cond*2)
15986 case 4: // result = lea base( , cond*4)
15987 case 5: // result = lea base(cond, cond*4)
15988 case 8: // result = lea base( , cond*8)
15989 case 9: // result = lea base(cond, cond*8)
15990 isFastMultiplier = true;
15991 break;
15992 }
15993 }
Eric Christopherfd179292009-08-27 18:07:15 +000015994
Chris Lattnercee56e72009-03-13 05:53:31 +000015995 if (isFastMultiplier) {
15996 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015997 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15998 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015999 // Zero extend the condition if needed.
16000 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
16001 Cond);
16002 // Scale the condition by the difference.
16003 if (Diff != 1)
16004 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
16005 DAG.getConstant(Diff, Cond.getValueType()));
16006
16007 // Add the base if non-zero.
16008 if (FalseC->getAPIntValue() != 0)
16009 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
16010 SDValue(FalseC, 0));
16011 if (N->getNumValues() == 2) // Dead flag value?
16012 return DCI.CombineTo(N, Cond, SDValue());
16013 return Cond;
16014 }
Eric Christopherfd179292009-08-27 18:07:15 +000016015 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000016016 }
16017 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016018
16019 // Handle these cases:
16020 // (select (x != c), e, c) -> select (x != c), e, x),
16021 // (select (x == c), c, e) -> select (x == c), x, e)
16022 // where the c is an integer constant, and the "select" is the combination
16023 // of CMOV and CMP.
16024 //
16025 // The rationale for this change is that the conditional-move from a constant
16026 // needs two instructions, however, conditional-move from a register needs
16027 // only one instruction.
16028 //
16029 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
16030 // some instruction-combining opportunities. This opt needs to be
16031 // postponed as late as possible.
16032 //
16033 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
16034 // the DCI.xxxx conditions are provided to postpone the optimization as
16035 // late as possible.
16036
16037 ConstantSDNode *CmpAgainst = 0;
16038 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
16039 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016040 !isa<ConstantSDNode>(Cond.getOperand(0))) {
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016041
16042 if (CC == X86::COND_NE &&
16043 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
16044 CC = X86::GetOppositeBranchCondition(CC);
16045 std::swap(TrueOp, FalseOp);
16046 }
16047
16048 if (CC == X86::COND_E &&
16049 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
16050 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
16051 DAG.getConstant(CC, MVT::i8), Cond };
16052 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
16053 array_lengthof(Ops));
16054 }
16055 }
16056 }
16057
Chris Lattnerd1980a52009-03-12 06:52:53 +000016058 return SDValue();
16059}
16060
Evan Cheng0b0cd912009-03-28 05:57:29 +000016061/// PerformMulCombine - Optimize a single multiply with constant into two
16062/// in order to implement it with two cheaper instructions, e.g.
16063/// LEA + SHL, LEA + LEA.
16064static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
16065 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000016066 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
16067 return SDValue();
16068
Owen Andersone50ed302009-08-10 22:56:29 +000016069 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000016070 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000016071 return SDValue();
16072
16073 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
16074 if (!C)
16075 return SDValue();
16076 uint64_t MulAmt = C->getZExtValue();
16077 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
16078 return SDValue();
16079
16080 uint64_t MulAmt1 = 0;
16081 uint64_t MulAmt2 = 0;
16082 if ((MulAmt % 9) == 0) {
16083 MulAmt1 = 9;
16084 MulAmt2 = MulAmt / 9;
16085 } else if ((MulAmt % 5) == 0) {
16086 MulAmt1 = 5;
16087 MulAmt2 = MulAmt / 5;
16088 } else if ((MulAmt % 3) == 0) {
16089 MulAmt1 = 3;
16090 MulAmt2 = MulAmt / 3;
16091 }
16092 if (MulAmt2 &&
16093 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
16094 DebugLoc DL = N->getDebugLoc();
16095
16096 if (isPowerOf2_64(MulAmt2) &&
16097 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
16098 // If second multiplifer is pow2, issue it first. We want the multiply by
16099 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
16100 // is an add.
16101 std::swap(MulAmt1, MulAmt2);
16102
16103 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000016104 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016105 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000016106 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000016107 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016108 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000016109 DAG.getConstant(MulAmt1, VT));
16110
Eric Christopherfd179292009-08-27 18:07:15 +000016111 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016112 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000016113 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000016114 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016115 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000016116 DAG.getConstant(MulAmt2, VT));
16117
16118 // Do not add new nodes to DAG combiner worklist.
16119 DCI.CombineTo(N, NewMul, false);
16120 }
16121 return SDValue();
16122}
16123
Evan Chengad9c0a32009-12-15 00:53:42 +000016124static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
16125 SDValue N0 = N->getOperand(0);
16126 SDValue N1 = N->getOperand(1);
16127 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
16128 EVT VT = N0.getValueType();
16129
16130 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
16131 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016132 if (VT.isInteger() && !VT.isVector() &&
16133 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000016134 N0.getOperand(1).getOpcode() == ISD::Constant) {
16135 SDValue N00 = N0.getOperand(0);
16136 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
16137 ((N00.getOpcode() == ISD::ANY_EXTEND ||
16138 N00.getOpcode() == ISD::ZERO_EXTEND) &&
16139 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
16140 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
16141 APInt ShAmt = N1C->getAPIntValue();
16142 Mask = Mask.shl(ShAmt);
16143 if (Mask != 0)
16144 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
16145 N00, DAG.getConstant(Mask, VT));
16146 }
16147 }
16148
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016149 // Hardware support for vector shifts is sparse which makes us scalarize the
16150 // vector operations in many cases. Also, on sandybridge ADD is faster than
16151 // shl.
16152 // (shl V, 1) -> add V,V
16153 if (isSplatVector(N1.getNode())) {
16154 assert(N0.getValueType().isVector() && "Invalid vector shift type");
16155 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
16156 // We shift all of the values by one. In many cases we do not have
16157 // hardware support for this operation. This is better expressed as an ADD
16158 // of two values.
16159 if (N1C && (1 == N1C->getZExtValue())) {
16160 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
16161 }
16162 }
16163
Evan Chengad9c0a32009-12-15 00:53:42 +000016164 return SDValue();
16165}
Evan Cheng0b0cd912009-03-28 05:57:29 +000016166
Nate Begeman740ab032009-01-26 00:52:55 +000016167/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
16168/// when possible.
16169static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000016170 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000016171 const X86Subtarget *Subtarget) {
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016172 if (N->getOpcode() == ISD::SHL) {
16173 SDValue V = PerformSHLCombine(N, DAG);
16174 if (V.getNode()) return V;
16175 }
Evan Chengad9c0a32009-12-15 00:53:42 +000016176
Michael Liao42317cc2013-03-20 02:33:21 +000016177 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000016178}
16179
Stuart Hastings865f0932011-06-03 23:53:54 +000016180// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
16181// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
16182// and friends. Likewise for OR -> CMPNEQSS.
16183static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
16184 TargetLowering::DAGCombinerInfo &DCI,
16185 const X86Subtarget *Subtarget) {
16186 unsigned opcode;
16187
16188 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
16189 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000016190 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000016191 SDValue N0 = N->getOperand(0);
16192 SDValue N1 = N->getOperand(1);
16193 SDValue CMP0 = N0->getOperand(1);
16194 SDValue CMP1 = N1->getOperand(1);
16195 DebugLoc DL = N->getDebugLoc();
16196
16197 // The SETCCs should both refer to the same CMP.
16198 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
16199 return SDValue();
16200
16201 SDValue CMP00 = CMP0->getOperand(0);
16202 SDValue CMP01 = CMP0->getOperand(1);
16203 EVT VT = CMP00.getValueType();
16204
16205 if (VT == MVT::f32 || VT == MVT::f64) {
16206 bool ExpectingFlags = false;
16207 // Check for any users that want flags:
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016208 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Stuart Hastings865f0932011-06-03 23:53:54 +000016209 !ExpectingFlags && UI != UE; ++UI)
16210 switch (UI->getOpcode()) {
16211 default:
16212 case ISD::BR_CC:
16213 case ISD::BRCOND:
16214 case ISD::SELECT:
16215 ExpectingFlags = true;
16216 break;
16217 case ISD::CopyToReg:
16218 case ISD::SIGN_EXTEND:
16219 case ISD::ZERO_EXTEND:
16220 case ISD::ANY_EXTEND:
16221 break;
16222 }
16223
16224 if (!ExpectingFlags) {
16225 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
16226 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
16227
16228 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
16229 X86::CondCode tmp = cc0;
16230 cc0 = cc1;
16231 cc1 = tmp;
16232 }
16233
16234 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
16235 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
16236 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
16237 X86ISD::NodeType NTOperator = is64BitFP ?
16238 X86ISD::FSETCCsd : X86ISD::FSETCCss;
16239 // FIXME: need symbolic constants for these magic numbers.
16240 // See X86ATTInstPrinter.cpp:printSSECC().
16241 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
16242 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
16243 DAG.getConstant(x86cc, MVT::i8));
16244 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
16245 OnesOrZeroesF);
16246 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
16247 DAG.getConstant(1, MVT::i32));
16248 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
16249 return OneBitOfTruth;
16250 }
16251 }
16252 }
16253 }
16254 return SDValue();
16255}
16256
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016257/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
16258/// so it can be folded inside ANDNP.
16259static bool CanFoldXORWithAllOnes(const SDNode *N) {
16260 EVT VT = N->getValueType(0);
16261
16262 // Match direct AllOnes for 128 and 256-bit vectors
16263 if (ISD::isBuildVectorAllOnes(N))
16264 return true;
16265
16266 // Look through a bit convert.
16267 if (N->getOpcode() == ISD::BITCAST)
16268 N = N->getOperand(0).getNode();
16269
16270 // Sometimes the operand may come from a insert_subvector building a 256-bit
16271 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000016272 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000016273 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
16274 SDValue V1 = N->getOperand(0);
16275 SDValue V2 = N->getOperand(1);
16276
16277 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
16278 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
16279 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
16280 ISD::isBuildVectorAllOnes(V2.getNode()))
16281 return true;
16282 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016283
16284 return false;
16285}
16286
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016287// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
16288// register. In most cases we actually compare or select YMM-sized registers
16289// and mixing the two types creates horrible code. This method optimizes
16290// some of the transition sequences.
16291static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
16292 TargetLowering::DAGCombinerInfo &DCI,
16293 const X86Subtarget *Subtarget) {
16294 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016295 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016296 return SDValue();
16297
16298 assert((N->getOpcode() == ISD::ANY_EXTEND ||
16299 N->getOpcode() == ISD::ZERO_EXTEND ||
16300 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
16301
16302 SDValue Narrow = N->getOperand(0);
16303 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016304 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016305 return SDValue();
16306
16307 if (Narrow->getOpcode() != ISD::XOR &&
16308 Narrow->getOpcode() != ISD::AND &&
16309 Narrow->getOpcode() != ISD::OR)
16310 return SDValue();
16311
16312 SDValue N0 = Narrow->getOperand(0);
16313 SDValue N1 = Narrow->getOperand(1);
16314 DebugLoc DL = Narrow->getDebugLoc();
16315
16316 // The Left side has to be a trunc.
16317 if (N0.getOpcode() != ISD::TRUNCATE)
16318 return SDValue();
16319
16320 // The type of the truncated inputs.
16321 EVT WideVT = N0->getOperand(0)->getValueType(0);
16322 if (WideVT != VT)
16323 return SDValue();
16324
16325 // The right side has to be a 'trunc' or a constant vector.
16326 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16327 bool RHSConst = (isSplatVector(N1.getNode()) &&
16328 isa<ConstantSDNode>(N1->getOperand(0)));
16329 if (!RHSTrunc && !RHSConst)
16330 return SDValue();
16331
16332 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16333
16334 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16335 return SDValue();
16336
16337 // Set N0 and N1 to hold the inputs to the new wide operation.
16338 N0 = N0->getOperand(0);
16339 if (RHSConst) {
16340 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16341 N1->getOperand(0));
16342 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16343 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16344 } else if (RHSTrunc) {
16345 N1 = N1->getOperand(0);
16346 }
16347
16348 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016349 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016350 unsigned Opcode = N->getOpcode();
16351 switch (Opcode) {
16352 case ISD::ANY_EXTEND:
16353 return Op;
16354 case ISD::ZERO_EXTEND: {
16355 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16356 APInt Mask = APInt::getAllOnesValue(InBits);
16357 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16358 return DAG.getNode(ISD::AND, DL, VT,
16359 Op, DAG.getConstant(Mask, VT));
16360 }
16361 case ISD::SIGN_EXTEND:
16362 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16363 Op, DAG.getValueType(NarrowVT));
16364 default:
16365 llvm_unreachable("Unexpected opcode");
16366 }
16367}
16368
Nate Begemanb65c1752010-12-17 22:55:37 +000016369static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16370 TargetLowering::DAGCombinerInfo &DCI,
16371 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016372 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016373 if (DCI.isBeforeLegalizeOps())
16374 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016375
Stuart Hastings865f0932011-06-03 23:53:54 +000016376 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16377 if (R.getNode())
16378 return R;
16379
Craig Topperb926afc2012-12-17 05:12:30 +000016380 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016381 // BLSI is X & (-X)
16382 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016383 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16384 SDValue N0 = N->getOperand(0);
16385 SDValue N1 = N->getOperand(1);
16386 DebugLoc DL = N->getDebugLoc();
16387
Craig Topperb4c94572011-10-21 06:55:01 +000016388 // Check LHS for neg
16389 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16390 isZero(N0.getOperand(0)))
16391 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16392
16393 // Check RHS for neg
16394 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16395 isZero(N1.getOperand(0)))
16396 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16397
16398 // Check LHS for X-1
16399 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16400 isAllOnes(N0.getOperand(1)))
16401 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16402
16403 // Check RHS for X-1
16404 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16405 isAllOnes(N1.getOperand(1)))
16406 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16407
Craig Topper54a11172011-10-14 07:06:56 +000016408 return SDValue();
16409 }
16410
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016411 // Want to form ANDNP nodes:
16412 // 1) In the hopes of then easily combining them with OR and AND nodes
16413 // to form PBLEND/PSIGN.
16414 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016415 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016416 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016417
Nate Begemanb65c1752010-12-17 22:55:37 +000016418 SDValue N0 = N->getOperand(0);
16419 SDValue N1 = N->getOperand(1);
16420 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016421
Nate Begemanb65c1752010-12-17 22:55:37 +000016422 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016423 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016424 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16425 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016426 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016427
16428 // Check RHS for vnot
16429 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016430 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16431 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016432 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016433
Nate Begemanb65c1752010-12-17 22:55:37 +000016434 return SDValue();
16435}
16436
Evan Cheng760d1942010-01-04 21:22:48 +000016437static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016438 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016439 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016440 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016441 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016442 return SDValue();
16443
Stuart Hastings865f0932011-06-03 23:53:54 +000016444 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16445 if (R.getNode())
16446 return R;
16447
Evan Cheng760d1942010-01-04 21:22:48 +000016448 SDValue N0 = N->getOperand(0);
16449 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016450
Nate Begemanb65c1752010-12-17 22:55:37 +000016451 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016452 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016453 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016454 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016455 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016456
Craig Topper1666cb62011-11-19 07:07:26 +000016457 // Canonicalize pandn to RHS
16458 if (N0.getOpcode() == X86ISD::ANDNP)
16459 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016460 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016461 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16462 SDValue Mask = N1.getOperand(0);
16463 SDValue X = N1.getOperand(1);
16464 SDValue Y;
16465 if (N0.getOperand(0) == Mask)
16466 Y = N0.getOperand(1);
16467 if (N0.getOperand(1) == Mask)
16468 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016469
Craig Topper1666cb62011-11-19 07:07:26 +000016470 // Check to see if the mask appeared in both the AND and ANDNP and
16471 if (!Y.getNode())
16472 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016473
Craig Topper1666cb62011-11-19 07:07:26 +000016474 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016475 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016476 if (Mask.getOpcode() == ISD::BITCAST)
16477 Mask = Mask.getOperand(0);
16478 if (X.getOpcode() == ISD::BITCAST)
16479 X = X.getOperand(0);
16480 if (Y.getOpcode() == ISD::BITCAST)
16481 Y = Y.getOperand(0);
16482
Craig Topper1666cb62011-11-19 07:07:26 +000016483 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016484
Craig Toppered2e13d2012-01-22 19:15:14 +000016485 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016486 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16487 // there is no psrai.b
Craig Topper1666cb62011-11-19 07:07:26 +000016488 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
Michael Liao42317cc2013-03-20 02:33:21 +000016489 unsigned SraAmt = ~0;
16490 if (Mask.getOpcode() == ISD::SRA) {
16491 SDValue Amt = Mask.getOperand(1);
16492 if (isSplatVector(Amt.getNode())) {
16493 SDValue SclrAmt = Amt->getOperand(0);
16494 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt))
16495 SraAmt = C->getZExtValue();
16496 }
16497 } else if (Mask.getOpcode() == X86ISD::VSRAI) {
16498 SDValue SraC = Mask.getOperand(1);
16499 SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16500 }
Craig Topper1666cb62011-11-19 07:07:26 +000016501 if ((SraAmt + 1) != EltBits)
16502 return SDValue();
16503
16504 DebugLoc DL = N->getDebugLoc();
16505
16506 // Now we know we at least have a plendvb with the mask val. See if
16507 // we can form a psignb/w/d.
16508 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016509 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16510 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016511 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16512 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16513 "Unsupported VT for PSIGN");
Nadav Rotemf8db4472013-02-24 07:09:35 +000016514 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000016515 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016516 }
16517 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016518 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016519 return SDValue();
16520
16521 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16522
16523 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16524 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16525 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016526 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016527 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016528 }
16529 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016530
Craig Topper1666cb62011-11-19 07:07:26 +000016531 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16532 return SDValue();
16533
Nate Begemanb65c1752010-12-17 22:55:37 +000016534 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016535 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16536 std::swap(N0, N1);
16537 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16538 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016539 if (!N0.hasOneUse() || !N1.hasOneUse())
16540 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016541
16542 SDValue ShAmt0 = N0.getOperand(1);
16543 if (ShAmt0.getValueType() != MVT::i8)
16544 return SDValue();
16545 SDValue ShAmt1 = N1.getOperand(1);
16546 if (ShAmt1.getValueType() != MVT::i8)
16547 return SDValue();
16548 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16549 ShAmt0 = ShAmt0.getOperand(0);
16550 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16551 ShAmt1 = ShAmt1.getOperand(0);
16552
16553 DebugLoc DL = N->getDebugLoc();
16554 unsigned Opc = X86ISD::SHLD;
16555 SDValue Op0 = N0.getOperand(0);
16556 SDValue Op1 = N1.getOperand(0);
16557 if (ShAmt0.getOpcode() == ISD::SUB) {
16558 Opc = X86ISD::SHRD;
16559 std::swap(Op0, Op1);
16560 std::swap(ShAmt0, ShAmt1);
16561 }
16562
Evan Cheng8b1190a2010-04-28 01:18:01 +000016563 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016564 if (ShAmt1.getOpcode() == ISD::SUB) {
16565 SDValue Sum = ShAmt1.getOperand(0);
16566 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016567 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16568 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16569 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16570 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016571 return DAG.getNode(Opc, DL, VT,
16572 Op0, Op1,
16573 DAG.getNode(ISD::TRUNCATE, DL,
16574 MVT::i8, ShAmt0));
16575 }
16576 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16577 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16578 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016579 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016580 return DAG.getNode(Opc, DL, VT,
16581 N0.getOperand(0), N1.getOperand(0),
16582 DAG.getNode(ISD::TRUNCATE, DL,
16583 MVT::i8, ShAmt0));
16584 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016585
Evan Cheng760d1942010-01-04 21:22:48 +000016586 return SDValue();
16587}
16588
Manman Ren92363622012-06-07 22:39:10 +000016589// Generate NEG and CMOV for integer abs.
16590static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16591 EVT VT = N->getValueType(0);
16592
16593 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16594 // 8-bit integer abs to NEG and CMOV.
16595 if (VT.isInteger() && VT.getSizeInBits() == 8)
16596 return SDValue();
16597
16598 SDValue N0 = N->getOperand(0);
16599 SDValue N1 = N->getOperand(1);
16600 DebugLoc DL = N->getDebugLoc();
16601
16602 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16603 // and change it to SUB and CMOV.
16604 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16605 N0.getOpcode() == ISD::ADD &&
16606 N0.getOperand(1) == N1 &&
16607 N1.getOpcode() == ISD::SRA &&
16608 N1.getOperand(0) == N0.getOperand(0))
16609 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16610 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16611 // Generate SUB & CMOV.
16612 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16613 DAG.getConstant(0, VT), N0.getOperand(0));
16614
16615 SDValue Ops[] = { N0.getOperand(0), Neg,
16616 DAG.getConstant(X86::COND_GE, MVT::i8),
16617 SDValue(Neg.getNode(), 1) };
16618 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16619 Ops, array_lengthof(Ops));
16620 }
16621 return SDValue();
16622}
16623
Craig Topper3738ccd2011-12-27 06:27:23 +000016624// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016625static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16626 TargetLowering::DAGCombinerInfo &DCI,
16627 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016628 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016629 if (DCI.isBeforeLegalizeOps())
16630 return SDValue();
16631
Manman Ren45d53b82012-06-08 18:58:26 +000016632 if (Subtarget->hasCMov()) {
16633 SDValue RV = performIntegerAbsCombine(N, DAG);
16634 if (RV.getNode())
16635 return RV;
16636 }
Manman Ren92363622012-06-07 22:39:10 +000016637
16638 // Try forming BMI if it is available.
16639 if (!Subtarget->hasBMI())
16640 return SDValue();
16641
Craig Topperb4c94572011-10-21 06:55:01 +000016642 if (VT != MVT::i32 && VT != MVT::i64)
16643 return SDValue();
16644
Craig Topper3738ccd2011-12-27 06:27:23 +000016645 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16646
Craig Topperb4c94572011-10-21 06:55:01 +000016647 // Create BLSMSK instructions by finding X ^ (X-1)
16648 SDValue N0 = N->getOperand(0);
16649 SDValue N1 = N->getOperand(1);
16650 DebugLoc DL = N->getDebugLoc();
16651
16652 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16653 isAllOnes(N0.getOperand(1)))
16654 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16655
16656 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16657 isAllOnes(N1.getOperand(1)))
16658 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16659
16660 return SDValue();
16661}
16662
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016663/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16664static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016665 TargetLowering::DAGCombinerInfo &DCI,
16666 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016667 LoadSDNode *Ld = cast<LoadSDNode>(N);
16668 EVT RegVT = Ld->getValueType(0);
16669 EVT MemVT = Ld->getMemoryVT();
16670 DebugLoc dl = Ld->getDebugLoc();
16671 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016672 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016673
Michael Liaod4584c92013-03-25 23:50:10 +000016674 // On Sandybridge unaligned 256bit loads are inefficient.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016675 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016676 unsigned Alignment = Ld->getAlignment();
Michael Liaod4584c92013-03-25 23:50:10 +000016677 bool IsAligned = Alignment == 0 || Alignment >= MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016678 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016679 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016680 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016681 if (NumElems < 2)
16682 return SDValue();
16683
Nadav Rotem48177ac2013-01-18 23:10:30 +000016684 SDValue Ptr = Ld->getBasePtr();
16685 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16686
16687 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16688 NumElems/2);
16689 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16690 Ld->getPointerInfo(), Ld->isVolatile(),
16691 Ld->isNonTemporal(), Ld->isInvariant(),
16692 Alignment);
16693 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16694 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16695 Ld->getPointerInfo(), Ld->isVolatile(),
16696 Ld->isNonTemporal(), Ld->isInvariant(),
Michael Liaod4584c92013-03-25 23:50:10 +000016697 std::min(16U, Alignment));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016698 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16699 Load1.getValue(1),
16700 Load2.getValue(1));
16701
16702 SDValue NewVec = DAG.getUNDEF(RegVT);
16703 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16704 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16705 return DCI.CombineTo(N, NewVec, TF, true);
16706 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016707
Nadav Rotemca6f2962011-09-18 19:00:23 +000016708 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016709 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16710 // expansion is still better than scalar code.
16711 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16712 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016713 // TODO: It is possible to support ZExt by zeroing the undef values
16714 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016715 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16716 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016717 assert(MemVT != RegVT && "Cannot extend to the same type");
16718 assert(MemVT.isVector() && "Must load a vector from memory");
16719
16720 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016721 unsigned MemSz = MemVT.getSizeInBits();
16722 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016723
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016724 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16725 return SDValue();
16726
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016727 // All sizes must be a power of two.
16728 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16729 return SDValue();
16730
16731 // Attempt to load the original value using scalar loads.
16732 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016733 MVT SclrLoadTy = MVT::i8;
16734 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16735 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16736 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016737 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016738 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016739 }
16740 }
16741
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016742 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16743 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16744 (64 <= MemSz))
16745 SclrLoadTy = MVT::f64;
16746
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016747 // Calculate the number of scalar loads that we need to perform
16748 // in order to load our vector from memory.
16749 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016750 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16751 return SDValue();
16752
16753 unsigned loadRegZize = RegSz;
16754 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16755 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016756
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016757 // Represent our vector as a sequence of elements which are the
16758 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016759 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016760 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016761
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016762 // Represent the data using the same element type that is stored in
16763 // memory. In practice, we ''widen'' MemVT.
Eric Christophere187e252013-01-31 00:50:48 +000016764 EVT WideVecVT =
16765 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016766 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016767
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016768 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16769 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016770
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016771 // We can't shuffle using an illegal type.
16772 if (!TLI.isTypeLegal(WideVecVT))
16773 return SDValue();
16774
16775 SmallVector<SDValue, 8> Chains;
16776 SDValue Ptr = Ld->getBasePtr();
16777 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16778 TLI.getPointerTy());
16779 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16780
16781 for (unsigned i = 0; i < NumLoads; ++i) {
16782 // Perform a single load.
16783 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16784 Ptr, Ld->getPointerInfo(),
16785 Ld->isVolatile(), Ld->isNonTemporal(),
16786 Ld->isInvariant(), Ld->getAlignment());
16787 Chains.push_back(ScalarLoad.getValue(1));
16788 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16789 // another round of DAGCombining.
16790 if (i == 0)
16791 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16792 else
16793 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16794 ScalarLoad, DAG.getIntPtrConstant(i));
16795
16796 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16797 }
16798
16799 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16800 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016801
16802 // Bitcast the loaded value to a vector of the original element type, in
16803 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016804 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016805 unsigned SizeRatio = RegSz/MemSz;
16806
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016807 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016808 // If we have SSE4.1 we can directly emit a VSEXT node.
16809 if (Subtarget->hasSSE41()) {
16810 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16811 return DCI.CombineTo(N, Sext, TF, true);
16812 }
16813
16814 // Otherwise we'll shuffle the small elements in the high bits of the
16815 // larger type and perform an arithmetic shift. If the shift is not legal
16816 // it's better to scalarize.
16817 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16818 return SDValue();
16819
16820 // Redistribute the loaded elements into the different locations.
16821 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16822 for (unsigned i = 0; i != NumElems; ++i)
16823 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16824
16825 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16826 DAG.getUNDEF(WideVecVT),
16827 &ShuffleVec[0]);
16828
16829 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16830
16831 // Build the arithmetic shift.
16832 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16833 MemVT.getVectorElementType().getSizeInBits();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016834 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff,
16835 DAG.getConstant(Amt, RegVT));
Benjamin Kramer17347912012-12-22 11:34:28 +000016836
16837 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016838 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016839
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016840 // Redistribute the loaded elements into the different locations.
16841 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016842 for (unsigned i = 0; i != NumElems; ++i)
16843 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016844
16845 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016846 DAG.getUNDEF(WideVecVT),
16847 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016848
16849 // Bitcast to the requested type.
16850 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16851 // Replace the original load with the new sequence
16852 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016853 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016854 }
16855
16856 return SDValue();
16857}
16858
Chris Lattner149a4e52008-02-22 02:09:43 +000016859/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016860static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016861 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016862 StoreSDNode *St = cast<StoreSDNode>(N);
16863 EVT VT = St->getValue().getValueType();
16864 EVT StVT = St->getMemoryVT();
16865 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016866 SDValue StoredVal = St->getOperand(1);
16867 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16868
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016869 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016870 // On Sandy Bridge, 256-bit memory operations are executed by two
16871 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16872 // memory operation.
Michael Liaod4584c92013-03-25 23:50:10 +000016873 unsigned Alignment = St->getAlignment();
16874 bool IsAligned = Alignment == 0 || Alignment >= VT.getSizeInBits()/8;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016875 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016876 StVT == VT && !IsAligned) {
16877 unsigned NumElems = VT.getVectorNumElements();
16878 if (NumElems < 2)
16879 return SDValue();
16880
16881 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
16882 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016883
16884 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16885 SDValue Ptr0 = St->getBasePtr();
16886 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16887
16888 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16889 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016890 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016891 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16892 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016893 St->isNonTemporal(),
Michael Liaod4584c92013-03-25 23:50:10 +000016894 std::min(16U, Alignment));
Nadav Rotem5e742a32011-08-11 16:41:21 +000016895 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16896 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016897
16898 // Optimize trunc store (of multiple scalars) to shuffle and store.
16899 // First, pack all of the elements in one place. Next, store to memory
16900 // in fewer chunks.
16901 if (St->isTruncatingStore() && VT.isVector()) {
16902 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16903 unsigned NumElems = VT.getVectorNumElements();
16904 assert(StVT != VT && "Cannot truncate to the same type");
16905 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16906 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16907
16908 // From, To sizes and ElemCount must be pow of two
16909 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016910 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016911 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016912 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016913
Nadav Rotem614061b2011-08-10 19:30:14 +000016914 unsigned SizeRatio = FromSz / ToSz;
16915
16916 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16917
16918 // Create a type on which we perform the shuffle
16919 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16920 StVT.getScalarType(), NumElems*SizeRatio);
16921
16922 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16923
16924 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16925 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016926 for (unsigned i = 0; i != NumElems; ++i)
16927 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016928
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016929 // Can't shuffle using an illegal type.
16930 if (!TLI.isTypeLegal(WideVecVT))
16931 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016932
16933 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016934 DAG.getUNDEF(WideVecVT),
16935 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016936 // At this point all of the data is stored at the bottom of the
16937 // register. We now need to save it to mem.
16938
16939 // Find the largest store unit
16940 MVT StoreType = MVT::i8;
16941 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16942 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16943 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016944 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016945 StoreType = Tp;
16946 }
16947
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016948 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16949 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16950 (64 <= NumElems * ToSz))
16951 StoreType = MVT::f64;
16952
Nadav Rotem614061b2011-08-10 19:30:14 +000016953 // Bitcast the original vector into a vector of store-size units
16954 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016955 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016956 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16957 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16958 SmallVector<SDValue, 8> Chains;
16959 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16960 TLI.getPointerTy());
16961 SDValue Ptr = St->getBasePtr();
16962
16963 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016964 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016965 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16966 StoreType, ShuffWide,
16967 DAG.getIntPtrConstant(i));
16968 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16969 St->getPointerInfo(), St->isVolatile(),
16970 St->isNonTemporal(), St->getAlignment());
16971 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16972 Chains.push_back(Ch);
16973 }
16974
16975 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16976 Chains.size());
16977 }
16978
Chris Lattner149a4e52008-02-22 02:09:43 +000016979 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16980 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016981 // A preferable solution to the general problem is to figure out the right
16982 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016983
16984 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016985 if (VT.getSizeInBits() != 64)
16986 return SDValue();
16987
Devang Patel578efa92009-06-05 21:57:13 +000016988 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000016989 bool NoImplicitFloatOps = F->getAttributes().
16990 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016991 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016992 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016993 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016994 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016995 isa<LoadSDNode>(St->getValue()) &&
16996 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16997 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016998 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016999 LoadSDNode *Ld = 0;
17000 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000017001 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000017002 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017003 // Must be a store of a load. We currently handle two cases: the load
17004 // is a direct child, and it's under an intervening TokenFactor. It is
17005 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000017006 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000017007 Ld = cast<LoadSDNode>(St->getChain());
17008 else if (St->getValue().hasOneUse() &&
17009 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000017010 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000017011 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000017012 TokenFactorIndex = i;
17013 Ld = cast<LoadSDNode>(St->getValue());
17014 } else
17015 Ops.push_back(ChainVal->getOperand(i));
17016 }
17017 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000017018
Evan Cheng536e6672009-03-12 05:59:15 +000017019 if (!Ld || !ISD::isNormalLoad(Ld))
17020 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017021
Evan Cheng536e6672009-03-12 05:59:15 +000017022 // If this is not the MMX case, i.e. we are just turning i64 load/store
17023 // into f64 load/store, avoid the transformation if there are multiple
17024 // uses of the loaded value.
17025 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
17026 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017027
Evan Cheng536e6672009-03-12 05:59:15 +000017028 DebugLoc LdDL = Ld->getDebugLoc();
17029 DebugLoc StDL = N->getDebugLoc();
17030 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
17031 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
17032 // pair instead.
17033 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017034 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000017035 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
17036 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017037 Ld->isNonTemporal(), Ld->isInvariant(),
17038 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017039 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000017040 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000017041 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000017042 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000017043 Ops.size());
17044 }
Evan Cheng536e6672009-03-12 05:59:15 +000017045 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000017046 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017047 St->isVolatile(), St->isNonTemporal(),
17048 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000017049 }
Evan Cheng536e6672009-03-12 05:59:15 +000017050
17051 // Otherwise, lower to two pairs of 32-bit loads / stores.
17052 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017053 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
17054 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017055
Owen Anderson825b72b2009-08-11 20:47:22 +000017056 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017057 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017058 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017059 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000017060 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017061 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000017062 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017063 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000017064 MinAlign(Ld->getAlignment(), 4));
17065
17066 SDValue NewChain = LoLd.getValue(1);
17067 if (TokenFactorIndex != -1) {
17068 Ops.push_back(LoLd);
17069 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000017070 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000017071 Ops.size());
17072 }
17073
17074 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017075 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
17076 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017077
17078 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017079 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017080 St->isVolatile(), St->isNonTemporal(),
17081 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017082 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017083 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000017084 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000017085 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000017086 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000017087 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000017088 }
Dan Gohman475871a2008-07-27 21:46:04 +000017089 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000017090}
17091
Duncan Sands17470be2011-09-22 20:15:48 +000017092/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
17093/// and return the operands for the horizontal operation in LHS and RHS. A
17094/// horizontal operation performs the binary operation on successive elements
17095/// of its first operand, then on successive elements of its second operand,
17096/// returning the resulting values in a vector. For example, if
17097/// A = < float a0, float a1, float a2, float a3 >
17098/// and
17099/// B = < float b0, float b1, float b2, float b3 >
17100/// then the result of doing a horizontal operation on A and B is
17101/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
17102/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
17103/// A horizontal-op B, for some already available A and B, and if so then LHS is
17104/// set to A, RHS to B, and the routine returns 'true'.
17105/// Note that the binary operation should have the property that if one of the
17106/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017107static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000017108 // Look for the following pattern: if
17109 // A = < float a0, float a1, float a2, float a3 >
17110 // B = < float b0, float b1, float b2, float b3 >
17111 // and
17112 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
17113 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
17114 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
17115 // which is A horizontal-op B.
17116
17117 // At least one of the operands should be a vector shuffle.
17118 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
17119 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
17120 return false;
17121
17122 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000017123
17124 assert((VT.is128BitVector() || VT.is256BitVector()) &&
17125 "Unsupported vector type for horizontal add/sub");
17126
17127 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
17128 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000017129 unsigned NumElts = VT.getVectorNumElements();
17130 unsigned NumLanes = VT.getSizeInBits()/128;
17131 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000017132 assert((NumLaneElts % 2 == 0) &&
17133 "Vector type should have an even number of elements in each lane");
17134 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000017135
17136 // View LHS in the form
17137 // LHS = VECTOR_SHUFFLE A, B, LMask
17138 // If LHS is not a shuffle then pretend it is the shuffle
17139 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
17140 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
17141 // type VT.
17142 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000017143 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017144 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17145 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
17146 A = LHS.getOperand(0);
17147 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
17148 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017149 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
17150 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017151 } else {
17152 if (LHS.getOpcode() != ISD::UNDEF)
17153 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017154 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017155 LMask[i] = i;
17156 }
17157
17158 // Likewise, view RHS in the form
17159 // RHS = VECTOR_SHUFFLE C, D, RMask
17160 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000017161 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017162 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17163 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
17164 C = RHS.getOperand(0);
17165 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
17166 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017167 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
17168 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017169 } else {
17170 if (RHS.getOpcode() != ISD::UNDEF)
17171 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017172 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017173 RMask[i] = i;
17174 }
17175
17176 // Check that the shuffles are both shuffling the same vectors.
17177 if (!(A == C && B == D) && !(A == D && B == C))
17178 return false;
17179
17180 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
17181 if (!A.getNode() && !B.getNode())
17182 return false;
17183
17184 // If A and B occur in reverse order in RHS, then "swap" them (which means
17185 // rewriting the mask).
17186 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000017187 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017188
17189 // At this point LHS and RHS are equivalent to
17190 // LHS = VECTOR_SHUFFLE A, B, LMask
17191 // RHS = VECTOR_SHUFFLE A, B, RMask
17192 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000017193 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000017194 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000017195
Craig Topperf8363302011-12-02 08:18:41 +000017196 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017197 if (LIdx < 0 || RIdx < 0 ||
17198 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
17199 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000017200 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000017201
Craig Topperf8363302011-12-02 08:18:41 +000017202 // Check that successive elements are being operated on. If not, this is
17203 // not a horizontal operation.
17204 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
17205 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000017206 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000017207 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000017208 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000017209 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000017210 }
17211
17212 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
17213 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
17214 return true;
17215}
17216
17217/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
17218static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
17219 const X86Subtarget *Subtarget) {
17220 EVT VT = N->getValueType(0);
17221 SDValue LHS = N->getOperand(0);
17222 SDValue RHS = N->getOperand(1);
17223
17224 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017225 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017226 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017227 isHorizontalBinOp(LHS, RHS, true))
17228 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
17229 return SDValue();
17230}
17231
17232/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
17233static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
17234 const X86Subtarget *Subtarget) {
17235 EVT VT = N->getValueType(0);
17236 SDValue LHS = N->getOperand(0);
17237 SDValue RHS = N->getOperand(1);
17238
17239 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017240 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017241 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017242 isHorizontalBinOp(LHS, RHS, false))
17243 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
17244 return SDValue();
17245}
17246
Chris Lattner6cf73262008-01-25 06:14:17 +000017247/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
17248/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017249static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000017250 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
17251 // F[X]OR(0.0, x) -> x
17252 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000017253 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17254 if (C->getValueAPF().isPosZero())
17255 return N->getOperand(1);
17256 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17257 if (C->getValueAPF().isPosZero())
17258 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000017259 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017260}
17261
Nadav Rotemd60cb112012-08-19 13:06:16 +000017262/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
17263/// X86ISD::FMAX nodes.
17264static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
17265 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
17266
17267 // Only perform optimizations if UnsafeMath is used.
17268 if (!DAG.getTarget().Options.UnsafeFPMath)
17269 return SDValue();
17270
17271 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000017272 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000017273 unsigned NewOp = 0;
17274 switch (N->getOpcode()) {
17275 default: llvm_unreachable("unknown opcode");
17276 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
17277 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
17278 }
17279
17280 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
17281 N->getOperand(0), N->getOperand(1));
17282}
17283
Chris Lattneraf723b92008-01-25 05:46:26 +000017284/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017285static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000017286 // FAND(0.0, x) -> 0.0
17287 // FAND(x, 0.0) -> 0.0
17288 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17289 if (C->getValueAPF().isPosZero())
17290 return N->getOperand(0);
17291 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17292 if (C->getValueAPF().isPosZero())
17293 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000017294 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017295}
17296
Dan Gohmane5af2d32009-01-29 01:59:02 +000017297static SDValue PerformBTCombine(SDNode *N,
17298 SelectionDAG &DAG,
17299 TargetLowering::DAGCombinerInfo &DCI) {
17300 // BT ignores high bits in the bit index operand.
17301 SDValue Op1 = N->getOperand(1);
17302 if (Op1.hasOneUse()) {
17303 unsigned BitWidth = Op1.getValueSizeInBits();
17304 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
17305 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017306 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
17307 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000017308 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000017309 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17310 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17311 DCI.CommitTargetLoweringOpt(TLO);
17312 }
17313 return SDValue();
17314}
Chris Lattner83e6c992006-10-04 06:57:07 +000017315
Eli Friedman7a5e5552009-06-07 06:52:44 +000017316static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17317 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017318 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017319 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017320 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017321 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017322 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017323 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017324 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017325 }
17326 return SDValue();
17327}
17328
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017329static SDValue PerformSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG,
17330 const X86Subtarget *Subtarget) {
17331 EVT VT = N->getValueType(0);
17332 if (!VT.isVector())
17333 return SDValue();
17334
17335 SDValue N0 = N->getOperand(0);
17336 SDValue N1 = N->getOperand(1);
17337 EVT ExtraVT = cast<VTSDNode>(N1)->getVT();
17338 DebugLoc dl = N->getDebugLoc();
17339
17340 // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the
17341 // both SSE and AVX2 since there is no sign-extended shift right
17342 // operation on a vector with 64-bit elements.
17343 //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) ->
17344 // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT)))
17345 if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND ||
17346 N0.getOpcode() == ISD::SIGN_EXTEND)) {
17347 SDValue N00 = N0.getOperand(0);
17348
17349 // EXTLOAD has a better solution on AVX2,
17350 // it may be replaced with X86ISD::VSEXT node.
17351 if (N00.getOpcode() == ISD::LOAD && Subtarget->hasInt256())
17352 if (!ISD::isNormalLoad(N00.getNode()))
17353 return SDValue();
17354
17355 if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) {
17356 SDValue Tmp = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32,
17357 N00, N1);
17358 return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp);
17359 }
17360 }
17361 return SDValue();
17362}
17363
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017364static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17365 TargetLowering::DAGCombinerInfo &DCI,
17366 const X86Subtarget *Subtarget) {
17367 if (!DCI.isBeforeLegalizeOps())
17368 return SDValue();
17369
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017370 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017371 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017372
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017373 EVT VT = N->getValueType(0);
17374 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017375 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17376 if (R.getNode())
17377 return R;
17378 }
17379
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017380 return SDValue();
17381}
17382
Michael Liaof6c24ee2012-08-10 14:39:24 +000017383static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017384 const X86Subtarget* Subtarget) {
17385 DebugLoc dl = N->getDebugLoc();
17386 EVT VT = N->getValueType(0);
17387
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017388 // Let legalize expand this if it isn't a legal type yet.
17389 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17390 return SDValue();
17391
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017392 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017393 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17394 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017395 return SDValue();
17396
17397 SDValue A = N->getOperand(0);
17398 SDValue B = N->getOperand(1);
17399 SDValue C = N->getOperand(2);
17400
17401 bool NegA = (A.getOpcode() == ISD::FNEG);
17402 bool NegB = (B.getOpcode() == ISD::FNEG);
17403 bool NegC = (C.getOpcode() == ISD::FNEG);
17404
Michael Liaof6c24ee2012-08-10 14:39:24 +000017405 // Negative multiplication when NegA xor NegB
17406 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017407 if (NegA)
17408 A = A.getOperand(0);
17409 if (NegB)
17410 B = B.getOperand(0);
17411 if (NegC)
17412 C = C.getOperand(0);
17413
17414 unsigned Opcode;
17415 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017416 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017417 else
Craig Topperbf404372012-08-31 15:40:30 +000017418 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17419
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017420 return DAG.getNode(Opcode, dl, VT, A, B, C);
17421}
17422
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017423static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017424 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017425 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017426 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17427 // (and (i32 x86isd::setcc_carry), 1)
17428 // This eliminates the zext. This transformation is necessary because
17429 // ISD::SETCC is always legalized to i8.
17430 DebugLoc dl = N->getDebugLoc();
17431 SDValue N0 = N->getOperand(0);
17432 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017433
Evan Cheng2e489c42009-12-16 00:53:11 +000017434 if (N0.getOpcode() == ISD::AND &&
17435 N0.hasOneUse() &&
17436 N0.getOperand(0).hasOneUse()) {
17437 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017438 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17439 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17440 if (!C || C->getZExtValue() != 1)
17441 return SDValue();
17442 return DAG.getNode(ISD::AND, dl, VT,
17443 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17444 N00.getOperand(0), N00.getOperand(1)),
17445 DAG.getConstant(1, VT));
17446 }
17447 }
17448
Craig Topper5a529e42013-01-18 06:44:29 +000017449 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017450 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17451 if (R.getNode())
17452 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017453 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017454
Evan Cheng2e489c42009-12-16 00:53:11 +000017455 return SDValue();
17456}
17457
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017458// Optimize x == -y --> x+y == 0
17459// x != -y --> x+y != 0
17460static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17461 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17462 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017463 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017464
17465 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17466 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17467 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17468 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17469 LHS.getValueType(), RHS, LHS.getOperand(1));
17470 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17471 addV, DAG.getConstant(0, addV.getValueType()), CC);
17472 }
17473 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17474 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17475 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17476 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17477 RHS.getValueType(), LHS, RHS.getOperand(1));
17478 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17479 addV, DAG.getConstant(0, addV.getValueType()), CC);
17480 }
17481 return SDValue();
17482}
17483
Eric Christophere187e252013-01-31 00:50:48 +000017484// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17485// as "sbb reg,reg", since it can be extended without zext and produces
Shuxin Yanga5526a92012-10-31 23:11:48 +000017486// an all-ones bit which is more useful than 0/1 in some cases.
17487static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17488 return DAG.getNode(ISD::AND, DL, MVT::i8,
17489 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17490 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17491 DAG.getConstant(1, MVT::i8));
17492}
17493
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017494// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017495static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17496 TargetLowering::DAGCombinerInfo &DCI,
17497 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017498 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017499 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17500 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017501
Shuxin Yanga5526a92012-10-31 23:11:48 +000017502 if (CC == X86::COND_A) {
Eric Christophere187e252013-01-31 00:50:48 +000017503 // Try to convert COND_A into COND_B in an attempt to facilitate
Shuxin Yanga5526a92012-10-31 23:11:48 +000017504 // materializing "setb reg".
17505 //
17506 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17507 // cannot take an immediate as its first operand.
17508 //
Eric Christophere187e252013-01-31 00:50:48 +000017509 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
Shuxin Yanga5526a92012-10-31 23:11:48 +000017510 EFLAGS.getValueType().isInteger() &&
17511 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17512 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17513 EFLAGS.getNode()->getVTList(),
17514 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17515 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17516 return MaterializeSETB(DL, NewEFLAGS, DAG);
17517 }
17518 }
17519
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017520 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17521 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17522 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017523 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017524 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017525
Michael Liao2a33cec2012-08-10 19:58:13 +000017526 SDValue Flags;
17527
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017528 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17529 if (Flags.getNode()) {
17530 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17531 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17532 }
17533
Michael Liao2a33cec2012-08-10 19:58:13 +000017534 return SDValue();
17535}
17536
17537// Optimize branch condition evaluation.
17538//
17539static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17540 TargetLowering::DAGCombinerInfo &DCI,
17541 const X86Subtarget *Subtarget) {
17542 DebugLoc DL = N->getDebugLoc();
17543 SDValue Chain = N->getOperand(0);
17544 SDValue Dest = N->getOperand(1);
17545 SDValue EFLAGS = N->getOperand(3);
17546 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17547
17548 SDValue Flags;
17549
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017550 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17551 if (Flags.getNode()) {
17552 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17553 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17554 Flags);
17555 }
17556
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017557 return SDValue();
17558}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017559
Benjamin Kramer1396c402011-06-18 11:09:41 +000017560static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17561 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017562 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017563 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017564
17565 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017566 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017567 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017568 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017569 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17570 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17571 }
17572
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017573 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17574 // a 32-bit target where SSE doesn't support i64->FP operations.
17575 if (Op0.getOpcode() == ISD::LOAD) {
17576 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17577 EVT VT = Ld->getValueType(0);
17578 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17579 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17580 !XTLI->getSubtarget()->is64Bit() &&
17581 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017582 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17583 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017584 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17585 return FILDChain;
17586 }
17587 }
17588 return SDValue();
17589}
17590
Chris Lattner23a01992010-12-20 01:37:09 +000017591// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17592static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17593 X86TargetLowering::DAGCombinerInfo &DCI) {
17594 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17595 // the result is either zero or one (depending on the input carry bit).
17596 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17597 if (X86::isZeroNode(N->getOperand(0)) &&
17598 X86::isZeroNode(N->getOperand(1)) &&
17599 // We don't have a good way to replace an EFLAGS use, so only do this when
17600 // dead right now.
17601 SDValue(N, 1).use_empty()) {
17602 DebugLoc DL = N->getDebugLoc();
17603 EVT VT = N->getValueType(0);
17604 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17605 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17606 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17607 DAG.getConstant(X86::COND_B,MVT::i8),
17608 N->getOperand(2)),
17609 DAG.getConstant(1, VT));
17610 return DCI.CombineTo(N, Res1, CarryOut);
17611 }
17612
17613 return SDValue();
17614}
17615
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017616// fold (add Y, (sete X, 0)) -> adc 0, Y
17617// (add Y, (setne X, 0)) -> sbb -1, Y
17618// (sub (sete X, 0), Y) -> sbb 0, Y
17619// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017620static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017621 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017622
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017623 // Look through ZExts.
17624 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17625 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17626 return SDValue();
17627
17628 SDValue SetCC = Ext.getOperand(0);
17629 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17630 return SDValue();
17631
17632 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17633 if (CC != X86::COND_E && CC != X86::COND_NE)
17634 return SDValue();
17635
17636 SDValue Cmp = SetCC.getOperand(1);
17637 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017638 !X86::isZeroNode(Cmp.getOperand(1)) ||
17639 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017640 return SDValue();
17641
17642 SDValue CmpOp0 = Cmp.getOperand(0);
17643 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17644 DAG.getConstant(1, CmpOp0.getValueType()));
17645
17646 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17647 if (CC == X86::COND_NE)
17648 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17649 DL, OtherVal.getValueType(), OtherVal,
17650 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17651 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17652 DL, OtherVal.getValueType(), OtherVal,
17653 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17654}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017655
Craig Topper54f952a2011-11-19 09:02:40 +000017656/// PerformADDCombine - Do target-specific dag combines on integer adds.
17657static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17658 const X86Subtarget *Subtarget) {
17659 EVT VT = N->getValueType(0);
17660 SDValue Op0 = N->getOperand(0);
17661 SDValue Op1 = N->getOperand(1);
17662
17663 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017664 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017665 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017666 isHorizontalBinOp(Op0, Op1, true))
17667 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17668
17669 return OptimizeConditionalInDecrement(N, DAG);
17670}
17671
17672static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17673 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017674 SDValue Op0 = N->getOperand(0);
17675 SDValue Op1 = N->getOperand(1);
17676
17677 // X86 can't encode an immediate LHS of a sub. See if we can push the
17678 // negation into a preceding instruction.
17679 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017680 // If the RHS of the sub is a XOR with one use and a constant, invert the
17681 // immediate. Then add one to the LHS of the sub so we can turn
17682 // X-Y -> X+~Y+1, saving one register.
17683 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17684 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017685 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017686 EVT VT = Op0.getValueType();
17687 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17688 Op1.getOperand(0),
17689 DAG.getConstant(~XorC, VT));
17690 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017691 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017692 }
17693 }
17694
Craig Topper54f952a2011-11-19 09:02:40 +000017695 // Try to synthesize horizontal adds from adds of shuffles.
17696 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017697 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017698 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017699 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017700 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17701
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017702 return OptimizeConditionalInDecrement(N, DAG);
17703}
17704
Michael Liaod9d09602012-10-23 17:34:00 +000017705/// performVZEXTCombine - Performs build vector combines
17706static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17707 TargetLowering::DAGCombinerInfo &DCI,
17708 const X86Subtarget *Subtarget) {
17709 // (vzext (bitcast (vzext (x)) -> (vzext x)
17710 SDValue In = N->getOperand(0);
17711 while (In.getOpcode() == ISD::BITCAST)
17712 In = In.getOperand(0);
17713
17714 if (In.getOpcode() != X86ISD::VZEXT)
17715 return SDValue();
17716
Nadav Rotemb39a5522013-02-14 18:20:48 +000017717 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0),
17718 In.getOperand(0));
Michael Liaod9d09602012-10-23 17:34:00 +000017719}
17720
Dan Gohman475871a2008-07-27 21:46:04 +000017721SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017722 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017723 SelectionDAG &DAG = DCI.DAG;
17724 switch (N->getOpcode()) {
17725 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017726 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017727 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017728 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017729 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017730 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017731 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17732 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017733 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017734 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017735 case ISD::SHL:
17736 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017737 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017738 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017739 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017740 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017741 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017742 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017743 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017744 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17745 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017746 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017747 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017748 case X86ISD::FMIN:
17749 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017750 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017751 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017752 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017753 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017754 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017755 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017756 case ISD::SIGN_EXTEND_INREG: return PerformSIGN_EXTEND_INREGCombine(N, DAG, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017757 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017758 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017759 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017760 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017761 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017762 case X86ISD::SHUFP: // Handle all target specific shuffles
Craig Topper4aee1bb2013-01-28 06:48:25 +000017763 case X86ISD::PALIGNR:
Craig Topper34671b82011-12-06 08:21:25 +000017764 case X86ISD::UNPCKH:
17765 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017766 case X86ISD::MOVHLPS:
17767 case X86ISD::MOVLHPS:
17768 case X86ISD::PSHUFD:
17769 case X86ISD::PSHUFHW:
17770 case X86ISD::PSHUFLW:
17771 case X86ISD::MOVSS:
17772 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017773 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017774 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017775 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017776 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017777 }
17778
Dan Gohman475871a2008-07-27 21:46:04 +000017779 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017780}
17781
Evan Chenge5b51ac2010-04-17 06:13:15 +000017782/// isTypeDesirableForOp - Return true if the target has native support for
17783/// the specified value type and it is 'desirable' to use the type for the
17784/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17785/// instruction encodings are longer and some i16 instructions are slow.
17786bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17787 if (!isTypeLegal(VT))
17788 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017789 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017790 return true;
17791
17792 switch (Opc) {
17793 default:
17794 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017795 case ISD::LOAD:
17796 case ISD::SIGN_EXTEND:
17797 case ISD::ZERO_EXTEND:
17798 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017799 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017800 case ISD::SRL:
17801 case ISD::SUB:
17802 case ISD::ADD:
17803 case ISD::MUL:
17804 case ISD::AND:
17805 case ISD::OR:
17806 case ISD::XOR:
17807 return false;
17808 }
17809}
17810
17811/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017812/// beneficial for dag combiner to promote the specified node. If true, it
17813/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017814bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017815 EVT VT = Op.getValueType();
17816 if (VT != MVT::i16)
17817 return false;
17818
Evan Cheng4c26e932010-04-19 19:29:22 +000017819 bool Promote = false;
17820 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017821 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017822 default: break;
17823 case ISD::LOAD: {
17824 LoadSDNode *LD = cast<LoadSDNode>(Op);
17825 // If the non-extending load has a single use and it's not live out, then it
17826 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017827 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17828 Op.hasOneUse()*/) {
17829 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17830 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17831 // The only case where we'd want to promote LOAD (rather then it being
17832 // promoted as an operand is when it's only use is liveout.
17833 if (UI->getOpcode() != ISD::CopyToReg)
17834 return false;
17835 }
17836 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017837 Promote = true;
17838 break;
17839 }
17840 case ISD::SIGN_EXTEND:
17841 case ISD::ZERO_EXTEND:
17842 case ISD::ANY_EXTEND:
17843 Promote = true;
17844 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017845 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017846 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017847 SDValue N0 = Op.getOperand(0);
17848 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017849 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017850 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017851 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017852 break;
17853 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017854 case ISD::ADD:
17855 case ISD::MUL:
17856 case ISD::AND:
17857 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017858 case ISD::XOR:
17859 Commute = true;
17860 // fallthrough
17861 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017862 SDValue N0 = Op.getOperand(0);
17863 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017864 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017865 return false;
17866 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017867 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017868 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017869 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017870 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017871 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017872 }
17873 }
17874
17875 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017876 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017877}
17878
Evan Cheng60c07e12006-07-05 22:17:51 +000017879//===----------------------------------------------------------------------===//
17880// X86 Inline Assembly Support
17881//===----------------------------------------------------------------------===//
17882
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017883namespace {
17884 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017885 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017886 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017887
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017888 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017889 StringRef piece(*args[i]);
17890 if (!s.startswith(piece)) // Check if the piece matches.
17891 return false;
17892
17893 s = s.substr(piece.size());
17894 StringRef::size_type pos = s.find_first_not_of(" \t");
17895 if (pos == 0) // We matched a prefix.
17896 return false;
17897
17898 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017899 }
17900
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017901 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017902 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017903 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017904}
17905
Chris Lattnerb8105652009-07-20 17:51:36 +000017906bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17907 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017908
17909 std::string AsmStr = IA->getAsmString();
17910
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017911 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17912 if (!Ty || Ty->getBitWidth() % 16 != 0)
17913 return false;
17914
Chris Lattnerb8105652009-07-20 17:51:36 +000017915 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017916 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017917 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017918
17919 switch (AsmPieces.size()) {
17920 default: return false;
17921 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017922 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017923 // we will turn this bswap into something that will be lowered to logical
17924 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17925 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017926 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017927 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17928 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17929 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17930 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17931 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17932 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017933 // No need to check constraints, nothing other than the equivalent of
17934 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017935 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017936 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017937
Chris Lattnerb8105652009-07-20 17:51:36 +000017938 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017939 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017940 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017941 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17942 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017943 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017944 const std::string &ConstraintsStr = IA->getConstraintString();
17945 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000017946 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Dan Gohman0ef701e2010-03-04 19:58:08 +000017947 if (AsmPieces.size() == 4 &&
17948 AsmPieces[0] == "~{cc}" &&
17949 AsmPieces[1] == "~{dirflag}" &&
17950 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017951 AsmPieces[3] == "~{fpsr}")
17952 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017953 }
17954 break;
17955 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017956 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017957 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017958 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17959 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17960 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017961 AsmPieces.clear();
17962 const std::string &ConstraintsStr = IA->getConstraintString();
17963 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000017964 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017965 if (AsmPieces.size() == 4 &&
17966 AsmPieces[0] == "~{cc}" &&
17967 AsmPieces[1] == "~{dirflag}" &&
17968 AsmPieces[2] == "~{flags}" &&
17969 AsmPieces[3] == "~{fpsr}")
17970 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017971 }
Evan Cheng55d42002011-01-08 01:24:27 +000017972
17973 if (CI->getType()->isIntegerTy(64)) {
17974 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17975 if (Constraints.size() >= 2 &&
17976 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17977 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17978 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017979 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17980 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17981 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017982 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017983 }
17984 }
17985 break;
17986 }
17987 return false;
17988}
17989
Chris Lattnerf4dff842006-07-11 02:54:03 +000017990/// getConstraintType - Given a constraint letter, return the type of
17991/// constraint it is for this target.
17992X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017993X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17994 if (Constraint.size() == 1) {
17995 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017996 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017997 case 'q':
17998 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017999 case 'f':
18000 case 't':
18001 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000018002 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000018003 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000018004 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000018005 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000018006 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000018007 case 'a':
18008 case 'b':
18009 case 'c':
18010 case 'd':
18011 case 'S':
18012 case 'D':
18013 case 'A':
18014 return C_Register;
18015 case 'I':
18016 case 'J':
18017 case 'K':
18018 case 'L':
18019 case 'M':
18020 case 'N':
18021 case 'G':
18022 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000018023 case 'e':
18024 case 'Z':
18025 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000018026 default:
18027 break;
18028 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000018029 }
Chris Lattner4234f572007-03-25 02:14:49 +000018030 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000018031}
18032
John Thompson44ab89e2010-10-29 17:29:13 +000018033/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000018034/// This object must already have been set up with the operand type
18035/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000018036TargetLowering::ConstraintWeight
18037 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000018038 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000018039 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018040 Value *CallOperandVal = info.CallOperandVal;
18041 // If we don't have a value, we can't do a match,
18042 // but allow it at the lowest weight.
18043 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000018044 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000018045 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000018046 // Look at the constraint type.
18047 switch (*constraint) {
18048 default:
John Thompson44ab89e2010-10-29 17:29:13 +000018049 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
18050 case 'R':
18051 case 'q':
18052 case 'Q':
18053 case 'a':
18054 case 'b':
18055 case 'c':
18056 case 'd':
18057 case 'S':
18058 case 'D':
18059 case 'A':
18060 if (CallOperandVal->getType()->isIntegerTy())
18061 weight = CW_SpecificReg;
18062 break;
18063 case 'f':
18064 case 't':
18065 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018066 if (type->isFloatingPointTy())
18067 weight = CW_SpecificReg;
18068 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018069 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018070 if (type->isX86_MMXTy() && Subtarget->hasMMX())
18071 weight = CW_SpecificReg;
18072 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018073 case 'x':
18074 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000018075 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000018076 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000018077 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018078 break;
18079 case 'I':
18080 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
18081 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000018082 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018083 }
18084 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018085 case 'J':
18086 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18087 if (C->getZExtValue() <= 63)
18088 weight = CW_Constant;
18089 }
18090 break;
18091 case 'K':
18092 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18093 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
18094 weight = CW_Constant;
18095 }
18096 break;
18097 case 'L':
18098 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18099 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
18100 weight = CW_Constant;
18101 }
18102 break;
18103 case 'M':
18104 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18105 if (C->getZExtValue() <= 3)
18106 weight = CW_Constant;
18107 }
18108 break;
18109 case 'N':
18110 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18111 if (C->getZExtValue() <= 0xff)
18112 weight = CW_Constant;
18113 }
18114 break;
18115 case 'G':
18116 case 'C':
18117 if (dyn_cast<ConstantFP>(CallOperandVal)) {
18118 weight = CW_Constant;
18119 }
18120 break;
18121 case 'e':
18122 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18123 if ((C->getSExtValue() >= -0x80000000LL) &&
18124 (C->getSExtValue() <= 0x7fffffffLL))
18125 weight = CW_Constant;
18126 }
18127 break;
18128 case 'Z':
18129 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18130 if (C->getZExtValue() <= 0xffffffff)
18131 weight = CW_Constant;
18132 }
18133 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018134 }
18135 return weight;
18136}
18137
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018138/// LowerXConstraint - try to replace an X constraint, which matches anything,
18139/// with another that has more specific requirements based on the type of the
18140/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000018141const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000018142LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000018143 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
18144 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000018145 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000018146 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000018147 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000018148 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000018149 return "x";
18150 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018151
Chris Lattner5e764232008-04-26 23:02:14 +000018152 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018153}
18154
Chris Lattner48884cd2007-08-25 00:47:38 +000018155/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
18156/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000018157void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000018158 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000018159 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000018160 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000018161 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000018162
Eric Christopher100c8332011-06-02 23:16:42 +000018163 // Only support length 1 constraints for now.
18164 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000018165
Eric Christopher100c8332011-06-02 23:16:42 +000018166 char ConstraintLetter = Constraint[0];
18167 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018168 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000018169 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000018170 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018171 if (C->getZExtValue() <= 31) {
18172 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018173 break;
18174 }
Devang Patel84f7fd22007-03-17 00:13:28 +000018175 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018176 return;
Evan Cheng364091e2008-09-22 23:57:37 +000018177 case 'J':
18178 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000018179 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000018180 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18181 break;
18182 }
18183 }
18184 return;
18185 case 'K':
18186 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000018187 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000018188 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18189 break;
18190 }
18191 }
18192 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000018193 case 'N':
18194 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018195 if (C->getZExtValue() <= 255) {
18196 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018197 break;
18198 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000018199 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018200 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000018201 case 'e': {
18202 // 32-bit signed value
18203 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018204 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18205 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018206 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018207 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000018208 break;
18209 }
18210 // FIXME gcc accepts some relocatable values here too, but only in certain
18211 // memory models; it's complicated.
18212 }
18213 return;
18214 }
18215 case 'Z': {
18216 // 32-bit unsigned value
18217 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018218 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18219 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018220 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18221 break;
18222 }
18223 }
18224 // FIXME gcc accepts some relocatable values here too, but only in certain
18225 // memory models; it's complicated.
18226 return;
18227 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018228 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018229 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000018230 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018231 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018232 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000018233 break;
18234 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018235
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018236 // In any sort of PIC mode addresses need to be computed at runtime by
18237 // adding in a register or some sort of table lookup. These can't
18238 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000018239 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018240 return;
18241
Chris Lattnerdc43a882007-05-03 16:52:29 +000018242 // If we are in non-pic codegen mode, we allow the address of a global (with
18243 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000018244 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018245 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000018246
Chris Lattner49921962009-05-08 18:23:14 +000018247 // Match either (GA), (GA+C), (GA+C1+C2), etc.
18248 while (1) {
18249 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
18250 Offset += GA->getOffset();
18251 break;
18252 } else if (Op.getOpcode() == ISD::ADD) {
18253 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18254 Offset += C->getZExtValue();
18255 Op = Op.getOperand(0);
18256 continue;
18257 }
18258 } else if (Op.getOpcode() == ISD::SUB) {
18259 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18260 Offset += -C->getZExtValue();
18261 Op = Op.getOperand(0);
18262 continue;
18263 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018264 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018265
Chris Lattner49921962009-05-08 18:23:14 +000018266 // Otherwise, this isn't something we can handle, reject it.
18267 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018268 }
Eric Christopherfd179292009-08-27 18:07:15 +000018269
Dan Gohman46510a72010-04-15 01:51:59 +000018270 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018271 // If we require an extra load to get this address, as in PIC mode, we
18272 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000018273 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
18274 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018275 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000018276
Devang Patel0d881da2010-07-06 22:08:15 +000018277 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
18278 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000018279 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018280 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018281 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018282
Gabor Greifba36cb52008-08-28 21:40:38 +000018283 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000018284 Ops.push_back(Result);
18285 return;
18286 }
Dale Johannesen1784d162010-06-25 21:55:36 +000018287 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018288}
18289
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018290std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000018291X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000018292 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000018293 // First, see if this is a constraint that directly corresponds to an LLVM
18294 // register class.
18295 if (Constraint.size() == 1) {
18296 // GCC Constraint Letters
18297 switch (Constraint[0]) {
18298 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000018299 // TODO: Slight differences here in allocation order and leaving
18300 // RIP in the class. Do they matter any more here than they do
18301 // in the normal allocation?
18302 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
18303 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000018304 if (VT == MVT::i32 || VT == MVT::f32)
18305 return std::make_pair(0U, &X86::GR32RegClass);
18306 if (VT == MVT::i16)
18307 return std::make_pair(0U, &X86::GR16RegClass);
18308 if (VT == MVT::i8 || VT == MVT::i1)
18309 return std::make_pair(0U, &X86::GR8RegClass);
18310 if (VT == MVT::i64 || VT == MVT::f64)
18311 return std::make_pair(0U, &X86::GR64RegClass);
18312 break;
Eric Christopherd176af82011-06-29 17:23:50 +000018313 }
18314 // 32-bit fallthrough
18315 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000018316 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000018317 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
18318 if (VT == MVT::i16)
18319 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
18320 if (VT == MVT::i8 || VT == MVT::i1)
18321 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
18322 if (VT == MVT::i64)
18323 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000018324 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018325 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000018326 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018327 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018328 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018329 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018330 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000018331 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018332 return std::make_pair(0U, &X86::GR32RegClass);
18333 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018334 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018335 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018336 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018337 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018338 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018339 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018340 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
18341 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018342 case 'f': // FP Stack registers.
18343 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18344 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018345 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018346 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018347 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018348 return std::make_pair(0U, &X86::RFP64RegClass);
18349 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018350 case 'y': // MMX_REGS if MMX allowed.
18351 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018352 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018353 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018354 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018355 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018356 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018357 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018358
Owen Anderson825b72b2009-08-11 20:47:22 +000018359 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018360 default: break;
18361 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018362 case MVT::f32:
18363 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018364 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018365 case MVT::f64:
18366 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018367 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018368 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018369 case MVT::v16i8:
18370 case MVT::v8i16:
18371 case MVT::v4i32:
18372 case MVT::v2i64:
18373 case MVT::v4f32:
18374 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018375 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018376 // AVX types.
18377 case MVT::v32i8:
18378 case MVT::v16i16:
18379 case MVT::v8i32:
18380 case MVT::v4i64:
18381 case MVT::v8f32:
18382 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018383 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018384 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018385 break;
18386 }
18387 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018388
Chris Lattnerf76d1802006-07-31 23:26:50 +000018389 // Use the default implementation in TargetLowering to convert the register
18390 // constraint into a member of a register class.
18391 std::pair<unsigned, const TargetRegisterClass*> Res;
18392 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018393
18394 // Not found as a standard register?
18395 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018396 // Map st(0) -> st(7) -> ST0
18397 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18398 tolower(Constraint[1]) == 's' &&
18399 tolower(Constraint[2]) == 't' &&
18400 Constraint[3] == '(' &&
18401 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18402 Constraint[5] == ')' &&
18403 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018404
Chris Lattner56d77c72009-09-13 22:41:48 +000018405 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018406 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018407 return Res;
18408 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018409
Chris Lattner56d77c72009-09-13 22:41:48 +000018410 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018411 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018412 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018413 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018414 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018415 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018416
18417 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018418 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018419 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018420 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018421 return Res;
18422 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018423
Dale Johannesen330169f2008-11-13 21:52:36 +000018424 // 'A' means EAX + EDX.
18425 if (Constraint == "A") {
18426 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018427 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018428 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018429 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018430 return Res;
18431 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018432
Chris Lattnerf76d1802006-07-31 23:26:50 +000018433 // Otherwise, check to see if this is a register class of the wrong value
18434 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18435 // turn into {ax},{dx}.
18436 if (Res.second->hasType(VT))
18437 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018438
Chris Lattnerf76d1802006-07-31 23:26:50 +000018439 // All of the single-register GCC register classes map their values onto
18440 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18441 // really want an 8-bit or 32-bit register, map to the appropriate register
18442 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018443 if (Res.second == &X86::GR16RegClass) {
Eric Christopher23571f42013-02-13 06:01:05 +000018444 if (VT == MVT::i8 || VT == MVT::i1) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018445 unsigned DestReg = 0;
18446 switch (Res.first) {
18447 default: break;
18448 case X86::AX: DestReg = X86::AL; break;
18449 case X86::DX: DestReg = X86::DL; break;
18450 case X86::CX: DestReg = X86::CL; break;
18451 case X86::BX: DestReg = X86::BL; break;
18452 }
18453 if (DestReg) {
18454 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018455 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018456 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018457 } else if (VT == MVT::i32 || VT == MVT::f32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018458 unsigned DestReg = 0;
18459 switch (Res.first) {
18460 default: break;
18461 case X86::AX: DestReg = X86::EAX; break;
18462 case X86::DX: DestReg = X86::EDX; break;
18463 case X86::CX: DestReg = X86::ECX; break;
18464 case X86::BX: DestReg = X86::EBX; break;
18465 case X86::SI: DestReg = X86::ESI; break;
18466 case X86::DI: DestReg = X86::EDI; break;
18467 case X86::BP: DestReg = X86::EBP; break;
18468 case X86::SP: DestReg = X86::ESP; break;
18469 }
18470 if (DestReg) {
18471 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018472 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018473 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018474 } else if (VT == MVT::i64 || VT == MVT::f64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018475 unsigned DestReg = 0;
18476 switch (Res.first) {
18477 default: break;
18478 case X86::AX: DestReg = X86::RAX; break;
18479 case X86::DX: DestReg = X86::RDX; break;
18480 case X86::CX: DestReg = X86::RCX; break;
18481 case X86::BX: DestReg = X86::RBX; break;
18482 case X86::SI: DestReg = X86::RSI; break;
18483 case X86::DI: DestReg = X86::RDI; break;
18484 case X86::BP: DestReg = X86::RBP; break;
18485 case X86::SP: DestReg = X86::RSP; break;
18486 }
18487 if (DestReg) {
18488 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018489 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018490 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018491 }
Craig Topperc9099502012-04-20 06:31:50 +000018492 } else if (Res.second == &X86::FR32RegClass ||
18493 Res.second == &X86::FR64RegClass ||
18494 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018495 // Handle references to XMM physical registers that got mapped into the
18496 // wrong class. This can happen with constraints like {xmm0} where the
18497 // target independent register mapper will just pick the first match it can
18498 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018499
18500 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018501 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018502 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018503 Res.second = &X86::FR64RegClass;
18504 else if (X86::VR128RegClass.hasType(VT))
18505 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018506 else if (X86::VR256RegClass.hasType(VT))
18507 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018508 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018509
Chris Lattnerf76d1802006-07-31 23:26:50 +000018510 return Res;
18511}