blob: e0f87c03b7c976371d23d6d2ddd6d3c6c7d7ffde [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.
Jim Grosbach3450f802013-02-20 21:13:59 +00001346 BenefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001347
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001348 // Predictable cmov don't hurt on atom because it's in-order.
Jim Grosbach3450f802013-02-20 21:13:59 +00001349 PredictableSelectIsExpensive = !Subtarget->isAtom();
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001350
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001351 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001352}
1353
Duncan Sands28b77e92011-09-06 19:07:46 +00001354EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1355 if (!VT.isVector()) return MVT::i8;
1356 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001357}
1358
Evan Cheng29286502008-01-23 23:17:41 +00001359/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1360/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001361static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001362 if (MaxAlign == 16)
1363 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001364 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001365 if (VTy->getBitWidth() == 128)
1366 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001367 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001368 unsigned EltAlign = 0;
1369 getMaxByValAlign(ATy->getElementType(), EltAlign);
1370 if (EltAlign > MaxAlign)
1371 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001372 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001373 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1374 unsigned EltAlign = 0;
1375 getMaxByValAlign(STy->getElementType(i), EltAlign);
1376 if (EltAlign > MaxAlign)
1377 MaxAlign = EltAlign;
1378 if (MaxAlign == 16)
1379 break;
1380 }
1381 }
Evan Cheng29286502008-01-23 23:17:41 +00001382}
1383
1384/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1385/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001386/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1387/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001388unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001389 if (Subtarget->is64Bit()) {
1390 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001391 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001392 if (TyAlign > 8)
1393 return TyAlign;
1394 return 8;
1395 }
1396
Evan Cheng29286502008-01-23 23:17:41 +00001397 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001398 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001399 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001400 return Align;
1401}
Chris Lattner2b02a442007-02-25 08:29:00 +00001402
Evan Chengf0df0312008-05-15 08:39:06 +00001403/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001404/// and store operations as a result of memset, memcpy, and memmove
1405/// lowering. If DstAlign is zero that means it's safe to destination
1406/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1407/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00001408/// probably because the source does not need to be loaded. If 'IsMemset' is
1409/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1410/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1411/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001412/// It returns EVT::Other if the type should be determined using generic
1413/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001414EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001415X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1416 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00001417 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00001418 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001419 MachineFunction &MF) const {
Dan Gohman37f32ee2010-04-16 20:11:05 +00001420 const Function *F = MF.getFunction();
Evan Cheng946a3a92012-12-12 02:34:41 +00001421 if ((!IsMemset || ZeroMemset) &&
Bill Wendling831737d2012-12-30 10:32:01 +00001422 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1423 Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001424 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001425 (Subtarget->isUnalignedMemAccessFast() ||
1426 ((DstAlign == 0 || DstAlign >= 16) &&
Benjamin Kramer2dbe9292012-11-14 20:08:40 +00001427 (SrcAlign == 0 || SrcAlign >= 16)))) {
1428 if (Size >= 32) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001429 if (Subtarget->hasInt256())
Craig Topper562659f2012-01-13 08:32:21 +00001430 return MVT::v8i32;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00001431 if (Subtarget->hasFp256())
Craig Topper562659f2012-01-13 08:32:21 +00001432 return MVT::v8f32;
1433 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001434 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001435 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001436 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001437 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001438 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001439 !Subtarget->is64Bit() &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001440 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001441 // Do not use f64 to lower memcpy if source is string constant. It's
1442 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001443 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001444 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001445 }
Evan Chengf0df0312008-05-15 08:39:06 +00001446 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001447 return MVT::i64;
1448 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001449}
1450
Evan Cheng7d342672012-12-12 01:32:07 +00001451bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001452 if (VT == MVT::f32)
1453 return X86ScalarSSEf32;
1454 else if (VT == MVT::f64)
1455 return X86ScalarSSEf64;
Evan Cheng7d342672012-12-12 01:32:07 +00001456 return true;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00001457}
1458
Evan Cheng376642e2012-12-10 23:21:26 +00001459bool
1460X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
1461 if (Fast)
1462 *Fast = Subtarget->isUnalignedMemAccessFast();
1463 return true;
1464}
1465
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001466/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1467/// current function. The returned value is a member of the
1468/// MachineJumpTableInfo::JTEntryKind enum.
1469unsigned X86TargetLowering::getJumpTableEncoding() const {
1470 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1471 // symbol.
1472 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1473 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001474 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001475
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001476 // Otherwise, use the normal jump table encoding heuristics.
1477 return TargetLowering::getJumpTableEncoding();
1478}
1479
Chris Lattnerc64daab2010-01-26 05:02:42 +00001480const MCExpr *
1481X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1482 const MachineBasicBlock *MBB,
1483 unsigned uid,MCContext &Ctx) const{
1484 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1485 Subtarget->isPICStyleGOT());
1486 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1487 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001488 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1489 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001490}
1491
Evan Chengcc415862007-11-09 01:32:10 +00001492/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1493/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001494SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001495 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001496 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001497 // This doesn't have DebugLoc associated with it, but is not really the
1498 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001499 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001500 return Table;
1501}
1502
Chris Lattner589c6f62010-01-26 06:28:43 +00001503/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1504/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1505/// MCExpr.
1506const MCExpr *X86TargetLowering::
1507getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1508 MCContext &Ctx) const {
1509 // X86-64 uses RIP relative addressing based on the jump table label.
1510 if (Subtarget->isPICStyleRIPRel())
1511 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1512
1513 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001514 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001515}
1516
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001517// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001518std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglund03405572012-12-19 11:30:36 +00001519X86TargetLowering::findRepresentativeClass(MVT VT) const{
Evan Chengdee81012010-07-26 21:50:05 +00001520 const TargetRegisterClass *RRC = 0;
1521 uint8_t Cost = 1;
Patrik Hagglund03405572012-12-19 11:30:36 +00001522 switch (VT.SimpleTy) {
Evan Chengdee81012010-07-26 21:50:05 +00001523 default:
1524 return TargetLowering::findRepresentativeClass(VT);
1525 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001526 RRC = Subtarget->is64Bit() ?
1527 (const TargetRegisterClass*)&X86::GR64RegClass :
1528 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001529 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001530 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001531 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001532 break;
1533 case MVT::f32: case MVT::f64:
1534 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1535 case MVT::v4f32: case MVT::v2f64:
1536 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1537 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001538 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001539 break;
1540 }
1541 return std::make_pair(RRC, Cost);
1542}
1543
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001544bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1545 unsigned &Offset) const {
1546 if (!Subtarget->isTargetLinux())
1547 return false;
1548
1549 if (Subtarget->is64Bit()) {
1550 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1551 Offset = 0x28;
1552 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1553 AddressSpace = 256;
1554 else
1555 AddressSpace = 257;
1556 } else {
1557 // %gs:0x14 on i386
1558 Offset = 0x14;
1559 AddressSpace = 256;
1560 }
1561 return true;
1562}
1563
Chris Lattner2b02a442007-02-25 08:29:00 +00001564//===----------------------------------------------------------------------===//
1565// Return Value Calling Convention Implementation
1566//===----------------------------------------------------------------------===//
1567
Chris Lattner59ed56b2007-02-28 04:55:35 +00001568#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001569
Michael J. Spencerec38de22010-10-10 22:04:20 +00001570bool
Eric Christopher471e4222011-06-08 23:55:35 +00001571X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001572 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001573 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001574 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001575 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001576 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001577 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001578 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001579}
1580
Dan Gohman98ca4f22009-08-05 01:29:28 +00001581SDValue
1582X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001583 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001584 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001585 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001586 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001587 MachineFunction &MF = DAG.getMachineFunction();
1588 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001589
Chris Lattner9774c912007-02-27 05:28:59 +00001590 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001591 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001592 RVLocs, *DAG.getContext());
1593 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001594
Dan Gohman475871a2008-07-27 21:46:04 +00001595 SDValue Flag;
Dan Gohman475871a2008-07-27 21:46:04 +00001596 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001597 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1598 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001599 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1600 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001602 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001603 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1604 CCValAssign &VA = RVLocs[i];
1605 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001606 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001607 EVT ValVT = ValToCopy.getValueType();
1608
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001609 // Promote values to the appropriate types
1610 if (VA.getLocInfo() == CCValAssign::SExt)
1611 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1612 else if (VA.getLocInfo() == CCValAssign::ZExt)
1613 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1614 else if (VA.getLocInfo() == CCValAssign::AExt)
1615 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1616 else if (VA.getLocInfo() == CCValAssign::BCvt)
1617 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1618
Dale Johannesenc4510512010-09-24 19:05:48 +00001619 // If this is x86-64, and we disabled SSE, we can't return FP values,
1620 // or SSE or MMX vectors.
1621 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1622 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001623 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001624 report_fatal_error("SSE register return with SSE disabled");
1625 }
1626 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1627 // llvm-gcc has never done it right and no one has noticed, so this
1628 // should be OK for now.
1629 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001630 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001631 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001632
Chris Lattner447ff682008-03-11 03:23:40 +00001633 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1634 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001635 if (VA.getLocReg() == X86::ST0 ||
1636 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001637 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1638 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001639 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001640 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001641 RetOps.push_back(ValToCopy);
1642 // Don't emit a copytoreg.
1643 continue;
1644 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001645
Evan Cheng242b38b2009-02-23 09:03:22 +00001646 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1647 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001648 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001649 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001650 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001651 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001652 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1653 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001654 // If we don't have SSE2 available, convert to v4f32 so the generated
1655 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001656 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001657 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001658 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001659 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001660 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001661
Dale Johannesendd64c412009-02-04 00:33:20 +00001662 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001663 Flag = Chain.getValue(1);
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +00001664 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001665 }
Dan Gohman61a92132008-04-21 23:59:07 +00001666
Eli Benderskya5597f02013-01-25 22:07:43 +00001667 // The x86-64 ABIs require that for returning structs by value we copy
1668 // the sret argument into %rax/%eax (depending on ABI) for the return.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001669 // Win32 requires us to put the sret argument to %eax as well.
Eli Benderskya5597f02013-01-25 22:07:43 +00001670 // We saved the argument into a virtual register in the entry block,
1671 // so now we copy the value out and into %rax/%eax.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001672 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr() &&
1673 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Dan Gohman61a92132008-04-21 23:59:07 +00001674 MachineFunction &MF = DAG.getMachineFunction();
1675 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1676 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001677 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001678 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001679 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001680
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001681 unsigned RetValReg
1682 = (Subtarget->is64Bit() && !Subtarget->isTarget64BitILP32()) ?
1683 X86::RAX : X86::EAX;
Eli Benderskya5597f02013-01-25 22:07:43 +00001684 Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001685 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001686
Eli Benderskya5597f02013-01-25 22:07:43 +00001687 // RAX/EAX now acts like a return value.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00001688 RetOps.push_back(DAG.getRegister(RetValReg, getPointerTy()));
Dan Gohman61a92132008-04-21 23:59:07 +00001689 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001690
Chris Lattner447ff682008-03-11 03:23:40 +00001691 RetOps[0] = Chain; // Update chain.
1692
1693 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001694 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001695 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001696
1697 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001699}
1700
Evan Chengbf010eb2012-04-10 01:51:00 +00001701bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001702 if (N->getNumValues() != 1)
1703 return false;
1704 if (!N->hasNUsesOfValue(1, 0))
1705 return false;
1706
Evan Chengbf010eb2012-04-10 01:51:00 +00001707 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001708 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001709 if (Copy->getOpcode() == ISD::CopyToReg) {
1710 // If the copy has a glue operand, we conservatively assume it isn't safe to
1711 // perform a tail call.
1712 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1713 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001714 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001715 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001716 return false;
1717
Evan Cheng1bf891a2010-12-01 22:59:46 +00001718 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001719 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001720 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001721 if (UI->getOpcode() != X86ISD::RET_FLAG)
1722 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001723 HasRet = true;
1724 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001725
Evan Chengbf010eb2012-04-10 01:51:00 +00001726 if (!HasRet)
1727 return false;
1728
1729 Chain = TCChain;
1730 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001731}
1732
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001733MVT
1734X86TargetLowering::getTypeForExtArgOrReturn(MVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001735 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001736 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001737 // TODO: Is this also valid on 32-bit?
1738 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001739 ReturnMVT = MVT::i8;
1740 else
1741 ReturnMVT = MVT::i32;
1742
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001743 MVT MinVT = getRegisterType(ReturnMVT);
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001744 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001745}
1746
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747/// LowerCallResult - Lower the result values of a call into the
1748/// appropriate copies out of appropriate physical registers.
1749///
1750SDValue
1751X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001752 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001753 const SmallVectorImpl<ISD::InputArg> &Ins,
1754 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001755 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001756
Chris Lattnere32bbf62007-02-28 07:09:55 +00001757 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001758 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001759 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001760 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001761 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001762 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Chris Lattner3085e152007-02-25 08:59:22 +00001764 // Copy all of the result registers out of their specified physreg.
Jakub Staszakc20323a2012-12-29 15:57:26 +00001765 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001766 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001767 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001768
Torok Edwin3f142c32009-02-01 18:15:56 +00001769 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001770 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001771 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001772 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001773 }
1774
Evan Cheng79fb3b42009-02-20 20:43:02 +00001775 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001776
1777 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001778 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001779 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001780 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001781 // instead.
1782 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1783 // If we prefer to use the value in xmm registers, copy it out as f80 and
1784 // use a truncate to move it from fp stack reg to xmm reg.
1785 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001786 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001787 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1788 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001789 Val = Chain.getValue(0);
1790
1791 // Round the f80 to the right size, which also moves it to the appropriate
1792 // xmm register.
1793 if (CopyVT != VA.getValVT())
1794 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1795 // This truncation won't change the value.
1796 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001797 } else {
1798 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1799 CopyVT, InFlag).getValue(1);
1800 Val = Chain.getValue(0);
1801 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001802 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001803 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001804 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001805
Dan Gohman98ca4f22009-08-05 01:29:28 +00001806 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001807}
1808
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001809//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001810// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001811//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001812// StdCall calling convention seems to be standard for many Windows' API
1813// routines and around. It differs from C calling convention just a little:
1814// callee should clean up the stack, not caller. Symbols should be also
1815// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001816// For info on fast calling convention see Fast Calling Convention (tail call)
1817// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001818
Dan Gohman98ca4f22009-08-05 01:29:28 +00001819/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001820/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001821enum StructReturnType {
1822 NotStructReturn,
1823 RegStructReturn,
1824 StackStructReturn
1825};
1826static StructReturnType
1827callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001828 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001829 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001830
Rafael Espindola1cee7102012-07-25 13:41:10 +00001831 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1832 if (!Flags.isSRet())
1833 return NotStructReturn;
1834 if (Flags.isInReg())
1835 return RegStructReturn;
1836 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001837}
1838
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001839/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001840/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001841static StructReturnType
1842argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001843 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001844 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001845
Rafael Espindola1cee7102012-07-25 13:41:10 +00001846 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1847 if (!Flags.isSRet())
1848 return NotStructReturn;
1849 if (Flags.isInReg())
1850 return RegStructReturn;
1851 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001852}
1853
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001854/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1855/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001856/// the specific parameter attribute. The copy will be passed as a byval
1857/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001858static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001859CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001860 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1861 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001862 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001863
Dale Johannesendd64c412009-02-04 00:33:20 +00001864 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001865 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001866 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001867}
1868
Chris Lattner29689432010-03-11 00:22:57 +00001869/// IsTailCallConvention - Return true if the calling convention is one that
1870/// supports tail call optimization.
1871static bool IsTailCallConvention(CallingConv::ID CC) {
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001872 return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
1873 CC == CallingConv::HiPE);
Chris Lattner29689432010-03-11 00:22:57 +00001874}
1875
Evan Cheng485fafc2011-03-21 01:19:09 +00001876bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001877 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001878 return false;
1879
1880 CallSite CS(CI);
1881 CallingConv::ID CalleeCC = CS.getCallingConv();
1882 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1883 return false;
1884
1885 return true;
1886}
1887
Evan Cheng0c439eb2010-01-27 00:07:07 +00001888/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1889/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001890static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1891 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001892 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001893}
1894
Dan Gohman98ca4f22009-08-05 01:29:28 +00001895SDValue
1896X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001897 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001898 const SmallVectorImpl<ISD::InputArg> &Ins,
1899 DebugLoc dl, SelectionDAG &DAG,
1900 const CCValAssign &VA,
1901 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001902 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001903 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001904 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001905 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1906 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001907 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001908 EVT ValVT;
1909
1910 // If value is passed by pointer we have address passed instead of the value
1911 // itself.
1912 if (VA.getLocInfo() == CCValAssign::Indirect)
1913 ValVT = VA.getLocVT();
1914 else
1915 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001916
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001917 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001918 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001919 // In case of tail call optimization mark all arguments mutable. Since they
1920 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001921 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001922 unsigned Bytes = Flags.getByValSize();
1923 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1924 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001925 return DAG.getFrameIndex(FI, getPointerTy());
1926 } else {
1927 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001928 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001929 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1930 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001931 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001932 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001933 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001934}
1935
Dan Gohman475871a2008-07-27 21:46:04 +00001936SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001937X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001938 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001939 bool isVarArg,
1940 const SmallVectorImpl<ISD::InputArg> &Ins,
1941 DebugLoc dl,
1942 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001943 SmallVectorImpl<SDValue> &InVals)
1944 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001945 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001946 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001947
Gordon Henriksen86737662008-01-05 16:56:59 +00001948 const Function* Fn = MF.getFunction();
1949 if (Fn->hasExternalLinkage() &&
1950 Subtarget->isTargetCygMing() &&
1951 Fn->getName() == "main")
1952 FuncInfo->setForceFramePointer(true);
1953
Evan Cheng1bc78042006-04-26 01:20:17 +00001954 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001956 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001957 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001958
Chris Lattner29689432010-03-11 00:22:57 +00001959 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00001960 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001961
Chris Lattner638402b2007-02-28 07:00:42 +00001962 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001963 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001964 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001965 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001966
1967 // Allocate shadow area for Win64
1968 if (IsWin64) {
1969 CCInfo.AllocateStack(32, 8);
1970 }
1971
Duncan Sands45907662010-10-31 13:21:44 +00001972 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001973
Chris Lattnerf39f7712007-02-28 05:46:49 +00001974 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001975 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001976 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1977 CCValAssign &VA = ArgLocs[i];
1978 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1979 // places.
1980 assert(VA.getValNo() != LastVal &&
1981 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001982 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001983 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001984
Chris Lattnerf39f7712007-02-28 05:46:49 +00001985 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001986 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001987 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001988 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001989 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001990 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001991 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001992 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001993 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001994 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001995 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001996 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001997 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001998 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001999 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00002000 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00002001 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002002 else
Torok Edwinc23197a2009-07-14 16:55:14 +00002003 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002004
Devang Patel68e6bee2011-02-21 23:21:26 +00002005 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002006 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002007
Chris Lattnerf39f7712007-02-28 05:46:49 +00002008 // If this is an 8 or 16-bit value, it is really passed promoted to 32
2009 // bits. Insert an assert[sz]ext to capture this, then truncate to the
2010 // right size.
2011 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002012 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002013 DAG.getValueType(VA.getValVT()));
2014 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00002015 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00002016 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002017 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002018 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00002019
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002020 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002021 // Handle MMX values passed in XMM regs.
Jakub Staszakc20323a2012-12-29 15:57:26 +00002022 if (RegVT.isVector())
2023 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2024 else
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002025 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00002026 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00002027 } else {
2028 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002029 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00002030 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002031
2032 // If value is passed via pointer - do a load.
2033 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00002034 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00002035 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002036
Dan Gohman98ca4f22009-08-05 01:29:28 +00002037 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00002038 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002039
Eli Benderskya5597f02013-01-25 22:07:43 +00002040 // The x86-64 ABIs require that for returning structs by value we copy
2041 // the sret argument into %rax/%eax (depending on ABI) for the return.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00002042 // Win32 requires us to put the sret argument to %eax as well.
Eli Benderskya5597f02013-01-25 22:07:43 +00002043 // Save the argument into a virtual register so that we can access it
2044 // from the return points.
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +00002045 if (MF.getFunction()->hasStructRetAttr() &&
2046 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Dan Gohman61a92132008-04-21 23:59:07 +00002047 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2048 unsigned Reg = FuncInfo->getSRetReturnReg();
2049 if (!Reg) {
Eli Benderskya5597f02013-01-25 22:07:43 +00002050 MVT PtrTy = getPointerTy();
2051 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
Dan Gohman61a92132008-04-21 23:59:07 +00002052 FuncInfo->setSRetReturnReg(Reg);
2053 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002054 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002055 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00002056 }
2057
Chris Lattnerf39f7712007-02-28 05:46:49 +00002058 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00002059 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002060 if (FuncIsMadeTailCallSafe(CallConv,
2061 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00002062 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00002063
Evan Cheng1bc78042006-04-26 01:20:17 +00002064 // If the function takes variable number of arguments, make a frame index for
2065 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002066 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002067 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2068 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00002069 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00002070 }
2071 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002072 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
2073
2074 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00002075 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002076 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002078 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002079 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2080 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002081 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002082 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2083 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2084 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002085 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002086 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002087
2088 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002089 // The XMM registers which might contain var arg parameters are shadowed
2090 // in their paired GPR. So we only need to save the GPR to their home
2091 // slots.
2092 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002093 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002094 } else {
2095 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2096 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002097
Chad Rosier30450e82011-12-22 22:35:21 +00002098 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2099 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002100 }
2101 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2102 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002103
Bill Wendling831737d2012-12-30 10:32:01 +00002104 bool NoImplicitFloatOps = Fn->getAttributes().
2105 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002106 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002107 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002108 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2109 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002110 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002111 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002112 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002113 // Kernel mode asks for SSE to be disabled, so don't push them
2114 // on the stack.
2115 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002116
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002117 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002118 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002119 // Get to the caller-allocated home save location. Add 8 to account
2120 // for the return address.
2121 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002122 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002123 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002124 // Fixup to set vararg frame on shadow area (4 x i64).
2125 if (NumIntRegs < 4)
2126 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002127 } else {
2128 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002129 // registers, then we must store them to their spots on the stack so
2130 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002131 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2132 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2133 FuncInfo->setRegSaveFrameIndex(
2134 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002135 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002136 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002137
Gordon Henriksen86737662008-01-05 16:56:59 +00002138 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002139 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002140 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2141 getPointerTy());
2142 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002143 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002144 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2145 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002146 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002147 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002148 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002149 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002150 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002151 MachinePointerInfo::getFixedStack(
2152 FuncInfo->getRegSaveFrameIndex(), Offset),
2153 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002154 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002155 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002156 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002157
Dan Gohmanface41a2009-08-16 21:24:25 +00002158 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2159 // Now store the XMM (fp + vector) parameter registers.
2160 SmallVector<SDValue, 11> SaveXMMOps;
2161 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002162
Craig Topperc9099502012-04-20 06:31:50 +00002163 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002164 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2165 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002166
Dan Gohman1e93df62010-04-17 14:41:14 +00002167 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2168 FuncInfo->getRegSaveFrameIndex()));
2169 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2170 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002171
Dan Gohmanface41a2009-08-16 21:24:25 +00002172 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002173 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002174 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002175 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2176 SaveXMMOps.push_back(Val);
2177 }
2178 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2179 MVT::Other,
2180 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002181 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002182
2183 if (!MemOps.empty())
2184 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2185 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002186 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002188
Gordon Henriksen86737662008-01-05 16:56:59 +00002189 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002190 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2191 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002192 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002193 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002194 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002195 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002196 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002197 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002198 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002199 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002200
Gordon Henriksen86737662008-01-05 16:56:59 +00002201 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002202 // RegSaveFrameIndex is X86-64 only.
2203 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002204 if (CallConv == CallingConv::X86_FastCall ||
2205 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002206 // fastcc functions can't have varargs.
2207 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002208 }
Evan Cheng25caf632006-05-23 21:06:34 +00002209
Rafael Espindola76927d752011-08-30 19:39:58 +00002210 FuncInfo->setArgumentStackSize(StackSize);
2211
Dan Gohman98ca4f22009-08-05 01:29:28 +00002212 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002213}
2214
Dan Gohman475871a2008-07-27 21:46:04 +00002215SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002216X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2217 SDValue StackPtr, SDValue Arg,
2218 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002219 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002220 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002221 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002222 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002223 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002224 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002225 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002226
2227 return DAG.getStore(Chain, dl, Arg, PtrOff,
2228 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002229 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002230}
2231
Bill Wendling64e87322009-01-16 19:25:27 +00002232/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002233/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002234SDValue
2235X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002236 SDValue &OutRetAddr, SDValue Chain,
2237 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002238 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002239 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002240 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002241 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002242
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002243 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002244 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002245 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002246 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002247}
2248
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002249/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002250/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002251static SDValue
2252EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002253 SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT,
2254 unsigned SlotSize, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002255 // Store the return address to the appropriate stack slot.
2256 if (!FPDiff) return Chain;
2257 // Calculate the new stack slot for the return address.
Scott Michelfdc40a02009-02-17 22:15:04 +00002258 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002259 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002260 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002261 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002262 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002263 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002264 return Chain;
2265}
2266
Dan Gohman98ca4f22009-08-05 01:29:28 +00002267SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002268X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002269 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002270 SelectionDAG &DAG = CLI.DAG;
2271 DebugLoc &dl = CLI.DL;
2272 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2273 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2274 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2275 SDValue Chain = CLI.Chain;
2276 SDValue Callee = CLI.Callee;
2277 CallingConv::ID CallConv = CLI.CallConv;
2278 bool &isTailCall = CLI.IsTailCall;
2279 bool isVarArg = CLI.IsVarArg;
2280
Dan Gohman98ca4f22009-08-05 01:29:28 +00002281 MachineFunction &MF = DAG.getMachineFunction();
2282 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002283 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002284 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002285 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002286 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002287
Nick Lewycky22de16d2012-01-19 00:34:10 +00002288 if (MF.getTarget().Options.DisableTailCalls)
2289 isTailCall = false;
2290
Evan Cheng5f941932010-02-05 02:21:12 +00002291 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002292 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002293 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002294 isVarArg, SR != NotStructReturn,
Evan Chengb1cacc72012-09-25 05:32:34 +00002295 MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002296 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002297
2298 // Sibcalls are automatically detected tailcalls which do not require
2299 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002300 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002301 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002302
2303 if (isTailCall)
2304 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002305 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002306
Chris Lattner29689432010-03-11 00:22:57 +00002307 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
Duncan Sandsdc7f1742012-11-16 12:36:39 +00002308 "Var args not supported with calling convention fastcc, ghc or hipe");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002309
Chris Lattner638402b2007-02-28 07:00:42 +00002310 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002311 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002312 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002313 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002314
2315 // Allocate shadow area for Win64
2316 if (IsWin64) {
2317 CCInfo.AllocateStack(32, 8);
2318 }
2319
Duncan Sands45907662010-10-31 13:21:44 +00002320 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002321
Chris Lattner423c5f42007-02-28 05:31:48 +00002322 // Get a count of how many bytes are to be pushed on the stack.
2323 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002324 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002325 // This is a sibcall. The memory operands are available in caller's
2326 // own caller's stack.
2327 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002328 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2329 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002330 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002331
Gordon Henriksen86737662008-01-05 16:56:59 +00002332 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002333 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002334 // Lower arguments at fp - stackoffset + fpdiff.
Jakub Staszak96df4372012-10-29 22:02:26 +00002335 X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
2336 unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
2337
Gordon Henriksen86737662008-01-05 16:56:59 +00002338 FPDiff = NumBytesCallerPushed - NumBytes;
2339
2340 // Set the delta of movement of the returnaddr stackslot.
2341 // But only set if delta is greater than previous delta.
Jakub Staszak96df4372012-10-29 22:02:26 +00002342 if (FPDiff < X86Info->getTCReturnAddrDelta())
2343 X86Info->setTCReturnAddrDelta(FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00002344 }
2345
Evan Chengf22f9b32010-02-06 03:28:46 +00002346 if (!IsSibcall)
2347 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002348
Dan Gohman475871a2008-07-27 21:46:04 +00002349 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002350 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002351 if (isTailCall && FPDiff)
2352 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2353 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002354
Dan Gohman475871a2008-07-27 21:46:04 +00002355 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2356 SmallVector<SDValue, 8> MemOpChains;
2357 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002358
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002359 // Walk the register/memloc assignments, inserting copies/loads. In the case
2360 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002361 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2362 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002363 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002364 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002365 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002366 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002367
Chris Lattner423c5f42007-02-28 05:31:48 +00002368 // Promote the value if needed.
2369 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002370 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002371 case CCValAssign::Full: break;
2372 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002373 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002374 break;
2375 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002376 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002377 break;
2378 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002379 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002380 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002381 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002382 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2383 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002384 } else
2385 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2386 break;
2387 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002388 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002389 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002390 case CCValAssign::Indirect: {
2391 // Store the argument.
2392 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002393 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002394 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002395 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002396 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002397 Arg = SpillSlot;
2398 break;
2399 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002400 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002401
Chris Lattner423c5f42007-02-28 05:31:48 +00002402 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002403 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2404 if (isVarArg && IsWin64) {
2405 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2406 // shadow reg if callee is a varargs function.
2407 unsigned ShadowReg = 0;
2408 switch (VA.getLocReg()) {
2409 case X86::XMM0: ShadowReg = X86::RCX; break;
2410 case X86::XMM1: ShadowReg = X86::RDX; break;
2411 case X86::XMM2: ShadowReg = X86::R8; break;
2412 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002413 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002414 if (ShadowReg)
2415 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002416 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002417 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002418 assert(VA.isMemLoc());
2419 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002420 StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
2421 getPointerTy());
Evan Cheng5f941932010-02-05 02:21:12 +00002422 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2423 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002424 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002425 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002426
Evan Cheng32fe1032006-05-25 00:59:30 +00002427 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002428 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002429 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002430
Chris Lattner88e1fd52009-07-09 04:24:46 +00002431 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002432 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2433 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002434 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002435 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2436 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002437 } else {
2438 // If we are tail calling and generating PIC/GOT style code load the
2439 // address of the callee into ECX. The value in ecx is used as target of
2440 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2441 // for tail calls on PIC/GOT architectures. Normally we would just put the
2442 // address of GOT into ebx and then call target@PLT. But for tail calls
2443 // ebx would be restored (since ebx is callee saved) before jumping to the
2444 // target@PLT.
2445
2446 // Note: The actual moving to ECX is done further down.
2447 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2448 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2449 !G->getGlobal()->hasProtectedVisibility())
2450 Callee = LowerGlobalAddress(Callee, DAG);
2451 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002452 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002453 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002454 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002455
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002456 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002457 // From AMD64 ABI document:
2458 // For calls that may call functions that use varargs or stdargs
2459 // (prototype-less calls or calls to functions containing ellipsis (...) in
2460 // the declaration) %al is used as hidden argument to specify the number
2461 // of SSE registers used. The contents of %al do not need to match exactly
2462 // the number of registers, but must be an ubound on the number of SSE
2463 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002464
Gordon Henriksen86737662008-01-05 16:56:59 +00002465 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002466 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002467 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2468 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2469 };
2470 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002471 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002472 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002473
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002474 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2475 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002476 }
2477
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002478 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002479 if (isTailCall) {
2480 // Force all the incoming stack arguments to be loaded from the stack
2481 // before any new outgoing arguments are stored to the stack, because the
2482 // outgoing stack slots may alias the incoming argument stack slots, and
2483 // the alias isn't otherwise explicit. This is slightly more conservative
2484 // than necessary, because it means that each store effectively depends
2485 // on every argument instead of just those arguments it would clobber.
2486 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2487
Dan Gohman475871a2008-07-27 21:46:04 +00002488 SmallVector<SDValue, 8> MemOpChains2;
2489 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002490 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002491 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002492 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2493 CCValAssign &VA = ArgLocs[i];
2494 if (VA.isRegLoc())
2495 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002496 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002497 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002498 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002499 // Create frame index.
2500 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002501 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002502 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002503 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002504
Duncan Sands276dcbd2008-03-21 09:14:45 +00002505 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002506 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002507 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002508 if (StackPtr.getNode() == 0)
Michael Liaoc5c970e2012-10-31 04:14:09 +00002509 StackPtr = DAG.getCopyFromReg(Chain, dl,
2510 RegInfo->getStackRegister(),
Dale Johannesendd64c412009-02-04 00:33:20 +00002511 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002512 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002513
Dan Gohman98ca4f22009-08-05 01:29:28 +00002514 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2515 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002516 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002517 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002518 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002519 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002520 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002521 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002522 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002523 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002524 }
2525 }
2526
2527 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002528 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002529 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002530
2531 // Store the return address to the appropriate stack slot.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002532 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
2533 getPointerTy(), RegInfo->getSlotSize(),
Dale Johannesenace16102009-02-03 19:33:06 +00002534 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002535 }
2536
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002537 // Build a sequence of copy-to-reg nodes chained together with token chain
2538 // and flag operands which copy the outgoing args into registers.
2539 SDValue InFlag;
2540 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2541 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2542 RegsToPass[i].second, InFlag);
2543 InFlag = Chain.getValue(1);
2544 }
2545
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002546 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2547 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2548 // In the 64-bit large code model, we have to make all calls
2549 // through a register, since the call instruction's 32-bit
2550 // pc-relative offset may not be large enough to hold the whole
2551 // address.
2552 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002553 // If the callee is a GlobalAddress node (quite common, every direct call
2554 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2555 // it.
2556
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002557 // We should use extra load for direct calls to dllimported functions in
2558 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002559 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002560 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002561 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002562 bool ExtraLoad = false;
2563 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002564
Chris Lattner48a7d022009-07-09 05:02:21 +00002565 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2566 // external symbols most go through the PLT in PIC mode. If the symbol
2567 // has hidden or protected visibility, or if it is static or local, then
2568 // we don't need to use the PLT - we can directly call it.
2569 if (Subtarget->isTargetELF() &&
2570 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002571 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002572 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002573 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002574 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002575 (!Subtarget->getTargetTriple().isMacOSX() ||
2576 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002577 // PC-relative references to external symbols should go through $stub,
2578 // unless we're building with the leopard linker or later, which
2579 // automatically synthesizes these stubs.
2580 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002581 } else if (Subtarget->isPICStyleRIPRel() &&
2582 isa<Function>(GV) &&
Bill Wendling831737d2012-12-30 10:32:01 +00002583 cast<Function>(GV)->getAttributes().
2584 hasAttribute(AttributeSet::FunctionIndex,
2585 Attribute::NonLazyBind)) {
John McCall3a3465b2011-06-15 20:36:13 +00002586 // If the function is marked as non-lazy, generate an indirect call
2587 // which loads from the GOT directly. This avoids runtime overhead
2588 // at the cost of eager binding (and one extra byte of encoding).
2589 OpFlags = X86II::MO_GOTPCREL;
2590 WrapperKind = X86ISD::WrapperRIP;
2591 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002592 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002593
Devang Patel0d881da2010-07-06 22:08:15 +00002594 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002595 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002596
2597 // Add a wrapper if needed.
2598 if (WrapperKind != ISD::DELETED_NODE)
2599 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2600 // Add extra indirection if needed.
2601 if (ExtraLoad)
2602 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2603 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002604 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002605 }
Bill Wendling056292f2008-09-16 21:48:12 +00002606 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002607 unsigned char OpFlags = 0;
2608
Evan Cheng1bf891a2010-12-01 22:59:46 +00002609 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2610 // external symbols should go through the PLT.
2611 if (Subtarget->isTargetELF() &&
2612 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2613 OpFlags = X86II::MO_PLT;
2614 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002615 (!Subtarget->getTargetTriple().isMacOSX() ||
2616 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002617 // PC-relative references to external symbols should go through $stub,
2618 // unless we're building with the leopard linker or later, which
2619 // automatically synthesizes these stubs.
2620 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002621 }
Eric Christopherfd179292009-08-27 18:07:15 +00002622
Chris Lattner48a7d022009-07-09 05:02:21 +00002623 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2624 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002625 }
2626
Chris Lattnerd96d0722007-02-25 06:40:16 +00002627 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002628 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002629 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002630
Evan Chengf22f9b32010-02-06 03:28:46 +00002631 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002632 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2633 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002634 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002635 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002636
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002637 Ops.push_back(Chain);
2638 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002639
Dan Gohman98ca4f22009-08-05 01:29:28 +00002640 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002641 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002642
Gordon Henriksen86737662008-01-05 16:56:59 +00002643 // Add argument registers to the end of the list so that they are known live
2644 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002645 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2646 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2647 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002648
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002649 // Add a register mask operand representing the call-preserved registers.
2650 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2651 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2652 assert(Mask && "Missing call preserved mask for calling convention");
2653 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002654
Gabor Greifba36cb52008-08-28 21:40:38 +00002655 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002656 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002657
Dan Gohman98ca4f22009-08-05 01:29:28 +00002658 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002659 // We used to do:
2660 //// If this is the first return lowered for this function, add the regs
2661 //// to the liveout set for the function.
2662 // This isn't right, although it's probably harmless on x86; liveouts
2663 // should be computed from returns not tail calls. Consider a void
2664 // function making a tail call to a function returning int.
Jakub Staszak30fcfc32013-02-16 13:34:26 +00002665 return DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002666 }
2667
Dale Johannesenace16102009-02-03 19:33:06 +00002668 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002669 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002670
Chris Lattner2d297092006-05-23 18:50:38 +00002671 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002672 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002673 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2674 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002675 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002676 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002677 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002678 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002679 // pops the hidden struct pointer, so we have to push it back.
2680 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002681 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002682 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002683 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002684 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002685
Gordon Henriksenae636f82008-01-03 16:47:34 +00002686 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002687 if (!IsSibcall) {
2688 Chain = DAG.getCALLSEQ_END(Chain,
2689 DAG.getIntPtrConstant(NumBytes, true),
2690 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2691 true),
2692 InFlag);
2693 InFlag = Chain.getValue(1);
2694 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002695
Chris Lattner3085e152007-02-25 08:59:22 +00002696 // Handle result values, copying them out of physregs into vregs that we
2697 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002698 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2699 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002700}
2701
Evan Cheng25ab6902006-09-08 06:48:29 +00002702//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002703// Fast Calling Convention (tail call) implementation
2704//===----------------------------------------------------------------------===//
2705
2706// Like std call, callee cleans arguments, convention except that ECX is
2707// reserved for storing the tail called function address. Only 2 registers are
2708// free for argument passing (inreg). Tail call optimization is performed
2709// provided:
2710// * tailcallopt is enabled
2711// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002712// On X86_64 architecture with GOT-style position independent code only local
2713// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002714// To keep the stack aligned according to platform abi the function
2715// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2716// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002717// If a tail called function callee has more arguments than the caller the
2718// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002719// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002720// original REtADDR, but before the saved framepointer or the spilled registers
2721// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2722// stack layout:
2723// arg1
2724// arg2
2725// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002726// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002727// move area ]
2728// (possible EBP)
2729// ESI
2730// EDI
2731// local1 ..
2732
2733/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2734/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002735unsigned
2736X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2737 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002738 MachineFunction &MF = DAG.getMachineFunction();
2739 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002740 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002741 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002742 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002743 int64_t Offset = StackSize;
Michael Liaoaa3c2c02012-10-25 06:29:14 +00002744 unsigned SlotSize = RegInfo->getSlotSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002745 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2746 // Number smaller than 12 so just add the difference.
2747 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2748 } else {
2749 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002750 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002751 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002752 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002753 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002754}
2755
Evan Cheng5f941932010-02-05 02:21:12 +00002756/// MatchingStackOffset - Return true if the given stack call argument is
2757/// already available in the same position (relatively) of the caller's
2758/// incoming argument stack.
2759static
2760bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2761 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2762 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002763 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2764 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002765 if (Arg.getOpcode() == ISD::CopyFromReg) {
2766 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002767 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002768 return false;
2769 MachineInstr *Def = MRI->getVRegDef(VR);
2770 if (!Def)
2771 return false;
2772 if (!Flags.isByVal()) {
2773 if (!TII->isLoadFromStackSlot(Def, FI))
2774 return false;
2775 } else {
2776 unsigned Opcode = Def->getOpcode();
2777 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2778 Def->getOperand(1).isFI()) {
2779 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002780 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002781 } else
2782 return false;
2783 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002784 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2785 if (Flags.isByVal())
2786 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002787 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002788 // define @foo(%struct.X* %A) {
2789 // tail call @bar(%struct.X* byval %A)
2790 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002791 return false;
2792 SDValue Ptr = Ld->getBasePtr();
2793 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2794 if (!FINode)
2795 return false;
2796 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002797 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002798 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002799 FI = FINode->getIndex();
2800 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002801 } else
2802 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002803
Evan Cheng4cae1332010-03-05 08:38:04 +00002804 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002805 if (!MFI->isFixedObjectIndex(FI))
2806 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002807 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002808}
2809
Dan Gohman98ca4f22009-08-05 01:29:28 +00002810/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2811/// for tail call optimization. Targets which want to do tail call
2812/// optimization should implement this function.
2813bool
2814X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002815 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002816 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002817 bool isCalleeStructRet,
2818 bool isCallerStructRet,
Evan Chengb1cacc72012-09-25 05:32:34 +00002819 Type *RetTy,
Evan Chengb1712452010-01-27 06:25:16 +00002820 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002821 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002822 const SmallVectorImpl<ISD::InputArg> &Ins,
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002823 SelectionDAG &DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002824 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002825 CalleeCC != CallingConv::C)
2826 return false;
2827
Evan Cheng7096ae42010-01-29 06:45:59 +00002828 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002829 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002830 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengb1cacc72012-09-25 05:32:34 +00002831
2832 // If the function return type is x86_fp80 and the callee return type is not,
2833 // then the FP_EXTEND of the call result is not a nop. It's not safe to
2834 // perform a tailcall optimization here.
2835 if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
2836 return false;
2837
Evan Cheng13617962010-04-30 01:12:32 +00002838 CallingConv::ID CallerCC = CallerF->getCallingConv();
2839 bool CCMatch = CallerCC == CalleeCC;
2840
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002841 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002842 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002843 return true;
2844 return false;
2845 }
2846
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002847 // Look for obvious safe cases to perform tail call optimization that do not
2848 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002849
Evan Cheng2c12cb42010-03-26 16:26:03 +00002850 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2851 // emit a special epilogue.
2852 if (RegInfo->needsStackRealignment(MF))
2853 return false;
2854
Evan Chenga375d472010-03-15 18:54:48 +00002855 // Also avoid sibcall optimization if either caller or callee uses struct
2856 // return semantics.
2857 if (isCalleeStructRet || isCallerStructRet)
2858 return false;
2859
Chad Rosier2416da32011-06-24 21:15:36 +00002860 // An stdcall caller is expected to clean up its arguments; the callee
2861 // isn't going to do that.
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002862 if (!CCMatch && CallerCC == CallingConv::X86_StdCall)
Chad Rosier2416da32011-06-24 21:15:36 +00002863 return false;
2864
Chad Rosier871f6642011-05-18 19:59:50 +00002865 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002866 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002867 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002868
2869 // Optimizing for varargs on Win64 is unlikely to be safe without
2870 // additional testing.
2871 if (Subtarget->isTargetWin64())
2872 return false;
2873
Chad Rosier871f6642011-05-18 19:59:50 +00002874 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002875 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002876 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002877
Chad Rosier871f6642011-05-18 19:59:50 +00002878 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2879 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2880 if (!ArgLocs[i].isRegLoc())
2881 return false;
2882 }
2883
Chad Rosier30450e82011-12-22 22:35:21 +00002884 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2885 // stack. Therefore, if it's not used by the call it is not safe to optimize
2886 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002887 bool Unused = false;
2888 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2889 if (!Ins[i].Used) {
2890 Unused = true;
2891 break;
2892 }
2893 }
2894 if (Unused) {
2895 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002896 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002897 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002898 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002899 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002900 CCValAssign &VA = RVLocs[i];
2901 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2902 return false;
2903 }
2904 }
2905
Evan Cheng13617962010-04-30 01:12:32 +00002906 // If the calling conventions do not match, then we'd better make sure the
2907 // results are returned in the same way as what the caller expects.
2908 if (!CCMatch) {
2909 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002910 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002911 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002912 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2913
2914 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002915 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002916 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002917 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2918
2919 if (RVLocs1.size() != RVLocs2.size())
2920 return false;
2921 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2922 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2923 return false;
2924 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2925 return false;
2926 if (RVLocs1[i].isRegLoc()) {
2927 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2928 return false;
2929 } else {
2930 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2931 return false;
2932 }
2933 }
2934 }
2935
Evan Chenga6bff982010-01-30 01:22:00 +00002936 // If the callee takes no arguments then go on to check the results of the
2937 // call.
2938 if (!Outs.empty()) {
2939 // Check if stack adjustment is needed. For now, do not do this if any
2940 // argument is passed on the stack.
2941 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002942 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002943 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002944
2945 // Allocate shadow area for Win64
2946 if (Subtarget->isTargetWin64()) {
2947 CCInfo.AllocateStack(32, 8);
2948 }
2949
Duncan Sands45907662010-10-31 13:21:44 +00002950 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002951 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002952 MachineFunction &MF = DAG.getMachineFunction();
2953 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2954 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002955
2956 // Check if the arguments are already laid out in the right way as
2957 // the caller's fixed stack objects.
2958 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002959 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2960 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002961 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002962 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2963 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002964 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002965 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002966 if (VA.getLocInfo() == CCValAssign::Indirect)
2967 return false;
2968 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002969 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2970 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002971 return false;
2972 }
2973 }
2974 }
Evan Cheng9c044672010-05-29 01:35:22 +00002975
2976 // If the tailcall address may be in a register, then make sure it's
2977 // possible to register allocate for it. In 32-bit, the call address can
2978 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002979 // callee-saved registers are restored. These happen to be the same
2980 // registers used to pass 'inreg' arguments so watch out for those.
2981 if (!Subtarget->is64Bit() &&
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002982 ((!isa<GlobalAddressSDNode>(Callee) &&
2983 !isa<ExternalSymbolSDNode>(Callee)) ||
2984 getTargetMachine().getRelocationModel() == Reloc::PIC_)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002985 unsigned NumInRegs = 0;
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002986 // In PIC we need an extra register to formulate the address computation
2987 // for the callee.
2988 unsigned MaxInRegs =
2989 (getTargetMachine().getRelocationModel() == Reloc::PIC_) ? 2 : 3;
2990
Evan Cheng9c044672010-05-29 01:35:22 +00002991 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2992 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002993 if (!VA.isRegLoc())
2994 continue;
2995 unsigned Reg = VA.getLocReg();
2996 switch (Reg) {
2997 default: break;
2998 case X86::EAX: case X86::EDX: case X86::ECX:
Nick Lewycky48aaf5f2013-02-13 21:59:15 +00002999 if (++NumInRegs == MaxInRegs)
Evan Cheng9c044672010-05-29 01:35:22 +00003000 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00003001 break;
Evan Cheng9c044672010-05-29 01:35:22 +00003002 }
3003 }
3004 }
Evan Chenga6bff982010-01-30 01:22:00 +00003005 }
Evan Chengb1712452010-01-27 06:25:16 +00003006
Evan Cheng86809cc2010-02-03 03:28:02 +00003007 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003008}
3009
Dan Gohman3df24e62008-09-03 23:12:08 +00003010FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00003011X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3012 const TargetLibraryInfo *libInfo) const {
3013 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00003014}
3015
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003016//===----------------------------------------------------------------------===//
3017// Other Lowering Hooks
3018//===----------------------------------------------------------------------===//
3019
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00003020static bool MayFoldLoad(SDValue Op) {
3021 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3022}
3023
3024static bool MayFoldIntoStore(SDValue Op) {
3025 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3026}
3027
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003028static bool isTargetShuffle(unsigned Opcode) {
3029 switch(Opcode) {
3030 default: return false;
3031 case X86ISD::PSHUFD:
3032 case X86ISD::PSHUFHW:
3033 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00003034 case X86ISD::SHUFP:
Craig Topper4aee1bb2013-01-28 06:48:25 +00003035 case X86ISD::PALIGNR:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003036 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003037 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003038 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003039 case X86ISD::MOVLPS:
3040 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003041 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003042 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003043 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003044 case X86ISD::MOVSS:
3045 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003046 case X86ISD::UNPCKL:
3047 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00003048 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00003049 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00003050 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003051 return true;
3052 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003053}
3054
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003055static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003056 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003057 switch(Opc) {
3058 default: llvm_unreachable("Unknown x86 shuffle node");
3059 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00003060 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00003061 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003062 return DAG.getNode(Opc, dl, VT, V1);
3063 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00003064}
3065
3066static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003067 SDValue V1, unsigned TargetMask,
3068 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003069 switch(Opc) {
3070 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003071 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003072 case X86ISD::PSHUFHW:
3073 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00003074 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00003075 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003076 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
3077 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00003078}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00003079
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003080static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00003081 SDValue V1, SDValue V2, unsigned TargetMask,
3082 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003083 switch(Opc) {
3084 default: llvm_unreachable("Unknown x86 shuffle node");
Craig Topper4aee1bb2013-01-28 06:48:25 +00003085 case X86ISD::PALIGNR:
Craig Topperb3982da2011-12-31 23:50:21 +00003086 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00003087 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003088 return DAG.getNode(Opc, dl, VT, V1, V2,
3089 DAG.getConstant(TargetMask, MVT::i8));
3090 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003091}
3092
3093static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
3094 SDValue V1, SDValue V2, SelectionDAG &DAG) {
3095 switch(Opc) {
3096 default: llvm_unreachable("Unknown x86 shuffle node");
3097 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00003098 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00003099 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003100 case X86ISD::MOVLPS:
3101 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003102 case X86ISD::MOVSS:
3103 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00003104 case X86ISD::UNPCKL:
3105 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003106 return DAG.getNode(Opc, dl, VT, V1, V2);
3107 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00003108}
3109
Dan Gohmand858e902010-04-17 15:26:15 +00003110SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003111 MachineFunction &MF = DAG.getMachineFunction();
3112 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3113 int ReturnAddrIndex = FuncInfo->getRAIndex();
3114
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003115 if (ReturnAddrIndex == 0) {
3116 // Set up a frame object for the return address.
Michael Liaoaa3c2c02012-10-25 06:29:14 +00003117 unsigned SlotSize = RegInfo->getSlotSize();
David Greene3f2bf852009-11-12 20:49:22 +00003118 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003119 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003120 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003121 }
3122
Evan Cheng25ab6902006-09-08 06:48:29 +00003123 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003124}
3125
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003126bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3127 bool hasSymbolicDisplacement) {
3128 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003129 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003130 return false;
3131
3132 // If we don't have a symbolic displacement - we don't have any extra
3133 // restrictions.
3134 if (!hasSymbolicDisplacement)
3135 return true;
3136
3137 // FIXME: Some tweaks might be needed for medium code model.
3138 if (M != CodeModel::Small && M != CodeModel::Kernel)
3139 return false;
3140
3141 // For small code model we assume that latest object is 16MB before end of 31
3142 // bits boundary. We may also accept pretty large negative constants knowing
3143 // that all objects are in the positive half of address space.
3144 if (M == CodeModel::Small && Offset < 16*1024*1024)
3145 return true;
3146
3147 // For kernel code model we know that all object resist in the negative half
3148 // of 32bits address space. We may not accept negative offsets, since they may
3149 // be just off and we may accept pretty large positive ones.
3150 if (M == CodeModel::Kernel && Offset > 0)
3151 return true;
3152
3153 return false;
3154}
3155
Evan Chengef41ff62011-06-23 17:54:54 +00003156/// isCalleePop - Determines whether the callee is required to pop its
3157/// own arguments. Callee pop is necessary to support tail calls.
3158bool X86::isCalleePop(CallingConv::ID CallingConv,
3159 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3160 if (IsVarArg)
3161 return false;
3162
3163 switch (CallingConv) {
3164 default:
3165 return false;
3166 case CallingConv::X86_StdCall:
3167 return !is64Bit;
3168 case CallingConv::X86_FastCall:
3169 return !is64Bit;
3170 case CallingConv::X86_ThisCall:
3171 return !is64Bit;
3172 case CallingConv::Fast:
3173 return TailCallOpt;
3174 case CallingConv::GHC:
3175 return TailCallOpt;
Duncan Sandsdc7f1742012-11-16 12:36:39 +00003176 case CallingConv::HiPE:
3177 return TailCallOpt;
Evan Chengef41ff62011-06-23 17:54:54 +00003178 }
3179}
3180
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003181/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3182/// specific condition code, returning the condition code and the LHS/RHS of the
3183/// comparison to make.
3184static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3185 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003186 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003187 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3188 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3189 // X > -1 -> X == 0, jump !sign.
3190 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003191 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003192 }
3193 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003194 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003195 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003196 }
3197 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003198 // X < 1 -> X <= 0
3199 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003200 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003201 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003202 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003203
Evan Chengd9558e02006-01-06 00:43:03 +00003204 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003205 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003206 case ISD::SETEQ: return X86::COND_E;
3207 case ISD::SETGT: return X86::COND_G;
3208 case ISD::SETGE: return X86::COND_GE;
3209 case ISD::SETLT: return X86::COND_L;
3210 case ISD::SETLE: return X86::COND_LE;
3211 case ISD::SETNE: return X86::COND_NE;
3212 case ISD::SETULT: return X86::COND_B;
3213 case ISD::SETUGT: return X86::COND_A;
3214 case ISD::SETULE: return X86::COND_BE;
3215 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003216 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003217 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003218
Chris Lattner4c78e022008-12-23 23:42:27 +00003219 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003220
Chris Lattner4c78e022008-12-23 23:42:27 +00003221 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003222 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3223 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003224 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3225 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003226 }
3227
Chris Lattner4c78e022008-12-23 23:42:27 +00003228 switch (SetCCOpcode) {
3229 default: break;
3230 case ISD::SETOLT:
3231 case ISD::SETOLE:
3232 case ISD::SETUGT:
3233 case ISD::SETUGE:
3234 std::swap(LHS, RHS);
3235 break;
3236 }
3237
3238 // On a floating point condition, the flags are set as follows:
3239 // ZF PF CF op
3240 // 0 | 0 | 0 | X > Y
3241 // 0 | 0 | 1 | X < Y
3242 // 1 | 0 | 0 | X == Y
3243 // 1 | 1 | 1 | unordered
3244 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003245 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003246 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003247 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003248 case ISD::SETOLT: // flipped
3249 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003250 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003251 case ISD::SETOLE: // flipped
3252 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003253 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003254 case ISD::SETUGT: // flipped
3255 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003256 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003257 case ISD::SETUGE: // flipped
3258 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003259 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003260 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003261 case ISD::SETNE: return X86::COND_NE;
3262 case ISD::SETUO: return X86::COND_P;
3263 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003264 case ISD::SETOEQ:
3265 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003266 }
Evan Chengd9558e02006-01-06 00:43:03 +00003267}
3268
Evan Cheng4a460802006-01-11 00:33:36 +00003269/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3270/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003271/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003272static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003273 switch (X86CC) {
3274 default:
3275 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003276 case X86::COND_B:
3277 case X86::COND_BE:
3278 case X86::COND_E:
3279 case X86::COND_P:
3280 case X86::COND_A:
3281 case X86::COND_AE:
3282 case X86::COND_NE:
3283 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003284 return true;
3285 }
3286}
3287
Evan Chengeb2f9692009-10-27 19:56:55 +00003288/// isFPImmLegal - Returns true if the target can instruction select the
3289/// specified FP immediate natively. If false, the legalizer will
3290/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003291bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003292 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3293 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3294 return true;
3295 }
3296 return false;
3297}
3298
Nate Begeman9008ca62009-04-27 18:41:29 +00003299/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3300/// the specified range (L, H].
3301static bool isUndefOrInRange(int Val, int Low, int Hi) {
3302 return (Val < 0) || (Val >= Low && Val < Hi);
3303}
3304
3305/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3306/// specified value.
3307static bool isUndefOrEqual(int Val, int CmpVal) {
Jakub Staszakb2af3a02012-12-06 18:22:59 +00003308 return (Val < 0 || Val == CmpVal);
Evan Chengc5cdff22006-04-07 21:53:05 +00003309}
3310
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003311/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003312/// from position Pos and ending in Pos+Size, falls within the specified
3313/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003314static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003315 unsigned Pos, unsigned Size, int Low) {
3316 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003317 if (!isUndefOrEqual(Mask[i], Low))
3318 return false;
3319 return true;
3320}
3321
Nate Begeman9008ca62009-04-27 18:41:29 +00003322/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3323/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3324/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003325static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003326 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003327 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003329 return (Mask[0] < 2 && Mask[1] < 2);
3330 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003331}
3332
Nate Begeman9008ca62009-04-27 18:41:29 +00003333/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3334/// is suitable for input to PSHUFHW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003335static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3336 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003337 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003338
Nate Begeman9008ca62009-04-27 18:41:29 +00003339 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003340 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3341 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003342
Evan Cheng506d3df2006-03-29 23:07:14 +00003343 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003344 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003345 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003346 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003347
Craig Toppera9a568a2012-05-02 08:03:44 +00003348 if (VT == MVT::v16i16) {
3349 // Lower quadword copied in order or undef.
3350 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3351 return false;
3352
3353 // Upper quadword shuffled.
3354 for (unsigned i = 12; i != 16; ++i)
3355 if (!isUndefOrInRange(Mask[i], 12, 16))
3356 return false;
3357 }
3358
Evan Cheng506d3df2006-03-29 23:07:14 +00003359 return true;
3360}
3361
Nate Begeman9008ca62009-04-27 18:41:29 +00003362/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3363/// is suitable for input to PSHUFLW.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003364static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
3365 if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003366 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003367
Rafael Espindola15684b22009-04-24 12:40:33 +00003368 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003369 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3370 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003371
Rafael Espindola15684b22009-04-24 12:40:33 +00003372 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003373 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003374 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003375 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003376
Craig Toppera9a568a2012-05-02 08:03:44 +00003377 if (VT == MVT::v16i16) {
3378 // Upper quadword copied in order.
3379 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3380 return false;
3381
3382 // Lower quadword shuffled.
3383 for (unsigned i = 8; i != 12; ++i)
3384 if (!isUndefOrInRange(Mask[i], 8, 12))
3385 return false;
3386 }
3387
Rafael Espindola15684b22009-04-24 12:40:33 +00003388 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003389}
3390
Nate Begemana09008b2009-10-19 02:17:23 +00003391/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3392/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003393static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3394 const X86Subtarget *Subtarget) {
Craig Topper5a529e42013-01-18 06:44:29 +00003395 if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) ||
3396 (VT.is256BitVector() && !Subtarget->hasInt256()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003397 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003398
Craig Topper0e2037b2012-01-20 05:53:00 +00003399 unsigned NumElts = VT.getVectorNumElements();
3400 unsigned NumLanes = VT.getSizeInBits()/128;
3401 unsigned NumLaneElts = NumElts/NumLanes;
3402
3403 // Do not handle 64-bit element shuffles with palignr.
3404 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003405 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003406
Craig Topper0e2037b2012-01-20 05:53:00 +00003407 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3408 unsigned i;
3409 for (i = 0; i != NumLaneElts; ++i) {
3410 if (Mask[i+l] >= 0)
3411 break;
3412 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003413
Craig Topper0e2037b2012-01-20 05:53:00 +00003414 // Lane is all undef, go to next lane
3415 if (i == NumLaneElts)
3416 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003417
Craig Topper0e2037b2012-01-20 05:53:00 +00003418 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003419
Craig Topper0e2037b2012-01-20 05:53:00 +00003420 // Make sure its in this lane in one of the sources
3421 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3422 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003423 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003424
3425 // If not lane 0, then we must match lane 0
3426 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3427 return false;
3428
3429 // Correct second source to be contiguous with first source
3430 if (Start >= (int)NumElts)
3431 Start -= NumElts - NumLaneElts;
3432
3433 // Make sure we're shifting in the right direction.
3434 if (Start <= (int)(i+l))
3435 return false;
3436
3437 Start -= i;
3438
3439 // Check the rest of the elements to see if they are consecutive.
3440 for (++i; i != NumLaneElts; ++i) {
3441 int Idx = Mask[i+l];
3442
3443 // Make sure its in this lane
3444 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3445 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3446 return false;
3447
3448 // If not lane 0, then we must match lane 0
3449 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3450 return false;
3451
3452 if (Idx >= (int)NumElts)
3453 Idx -= NumElts - NumLaneElts;
3454
3455 if (!isUndefOrEqual(Idx, Start+i))
3456 return false;
3457
3458 }
Nate Begemana09008b2009-10-19 02:17:23 +00003459 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003460
Nate Begemana09008b2009-10-19 02:17:23 +00003461 return true;
3462}
3463
Craig Topper1a7700a2012-01-19 08:19:12 +00003464/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3465/// the two vector operands have swapped position.
3466static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3467 unsigned NumElems) {
3468 for (unsigned i = 0; i != NumElems; ++i) {
3469 int idx = Mask[i];
3470 if (idx < 0)
3471 continue;
3472 else if (idx < (int)NumElems)
3473 Mask[i] = idx + NumElems;
3474 else
3475 Mask[i] = idx - NumElems;
3476 }
3477}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003478
Craig Topper1a7700a2012-01-19 08:19:12 +00003479/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3480/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3481/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3482/// reverse of what x86 shuffles want.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003483static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256,
Craig Topper1a7700a2012-01-19 08:19:12 +00003484 bool Commuted = false) {
Craig Topper5a529e42013-01-18 06:44:29 +00003485 if (!HasFp256 && VT.is256BitVector())
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003486 return false;
3487
Craig Topper1a7700a2012-01-19 08:19:12 +00003488 unsigned NumElems = VT.getVectorNumElements();
3489 unsigned NumLanes = VT.getSizeInBits()/128;
3490 unsigned NumLaneElems = NumElems/NumLanes;
3491
3492 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003493 return false;
3494
3495 // VSHUFPSY divides the resulting vector into 4 chunks.
3496 // The sources are also splitted into 4 chunks, and each destination
3497 // chunk must come from a different source chunk.
3498 //
3499 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3500 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3501 //
3502 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3503 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3504 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003505 // VSHUFPDY divides the resulting vector into 4 chunks.
3506 // The sources are also splitted into 4 chunks, and each destination
3507 // chunk must come from a different source chunk.
3508 //
3509 // SRC1 => X3 X2 X1 X0
3510 // SRC2 => Y3 Y2 Y1 Y0
3511 //
3512 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3513 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003514 unsigned HalfLaneElems = NumLaneElems/2;
3515 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3516 for (unsigned i = 0; i != NumLaneElems; ++i) {
3517 int Idx = Mask[i+l];
3518 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3519 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3520 return false;
3521 // For VSHUFPSY, the mask of the second half must be the same as the
3522 // first but with the appropriate offsets. This works in the same way as
3523 // VPERMILPS works with masks.
3524 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3525 continue;
3526 if (!isUndefOrEqual(Idx, Mask[i]+l))
3527 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003528 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003529 }
3530
3531 return true;
3532}
3533
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003534/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3535/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003536static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003537 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003538 return false;
3539
Craig Topper7a9a28b2012-08-12 02:23:29 +00003540 unsigned NumElems = VT.getVectorNumElements();
3541
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003542 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003543 return false;
3544
Evan Cheng2064a2b2006-03-28 06:50:32 +00003545 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003546 return isUndefOrEqual(Mask[0], 6) &&
3547 isUndefOrEqual(Mask[1], 7) &&
3548 isUndefOrEqual(Mask[2], 2) &&
3549 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003550}
3551
Nate Begeman0b10b912009-11-07 23:17:15 +00003552/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3553/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3554/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003555static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003556 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003557 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003558
Craig Topper7a9a28b2012-08-12 02:23:29 +00003559 unsigned NumElems = VT.getVectorNumElements();
3560
Nate Begeman0b10b912009-11-07 23:17:15 +00003561 if (NumElems != 4)
3562 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003563
Craig Topperdd637ae2012-02-19 05:41:45 +00003564 return isUndefOrEqual(Mask[0], 2) &&
3565 isUndefOrEqual(Mask[1], 3) &&
3566 isUndefOrEqual(Mask[2], 2) &&
3567 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003568}
3569
Evan Cheng5ced1d82006-04-06 23:23:56 +00003570/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3571/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003572static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003573 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003574 return false;
3575
Craig Topperdd637ae2012-02-19 05:41:45 +00003576 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003577
Evan Cheng5ced1d82006-04-06 23:23:56 +00003578 if (NumElems != 2 && NumElems != 4)
3579 return false;
3580
Chad Rosier238ae312012-04-30 17:47:15 +00003581 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003582 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003583 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003584
Chad Rosier238ae312012-04-30 17:47:15 +00003585 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003586 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003587 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003588
3589 return true;
3590}
3591
Nate Begeman0b10b912009-11-07 23:17:15 +00003592/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3593/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003594static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003595 if (!VT.is128BitVector())
3596 return false;
3597
Craig Topperdd637ae2012-02-19 05:41:45 +00003598 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003599
Craig Topper7a9a28b2012-08-12 02:23:29 +00003600 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003601 return false;
3602
Chad Rosier238ae312012-04-30 17:47:15 +00003603 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003604 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003605 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003606
Chad Rosier238ae312012-04-30 17:47:15 +00003607 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3608 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003609 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003610
3611 return true;
3612}
3613
Elena Demikhovsky15963732012-06-26 08:04:10 +00003614//
3615// Some special combinations that can be optimized.
3616//
3617static
3618SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3619 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00003620 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky15963732012-06-26 08:04:10 +00003621 DebugLoc dl = SVOp->getDebugLoc();
3622
3623 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3624 return SDValue();
3625
3626 ArrayRef<int> Mask = SVOp->getMask();
3627
3628 // These are the special masks that may be optimized.
3629 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3630 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3631 bool MatchEvenMask = true;
3632 bool MatchOddMask = true;
3633 for (int i=0; i<8; ++i) {
3634 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3635 MatchEvenMask = false;
3636 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3637 MatchOddMask = false;
3638 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003639
Elena Demikhovsky32510202012-09-04 12:49:02 +00003640 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003641 return SDValue();
Michael Liao471b9172012-10-03 23:43:52 +00003642
Elena Demikhovsky15963732012-06-26 08:04:10 +00003643 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3644
Elena Demikhovsky32510202012-09-04 12:49:02 +00003645 SDValue Op0 = SVOp->getOperand(0);
3646 SDValue Op1 = SVOp->getOperand(1);
3647
3648 if (MatchEvenMask) {
3649 // Shift the second operand right to 32 bits.
3650 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3651 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3652 } else {
3653 // Shift the first operand left to 32 bits.
3654 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3655 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3656 }
3657 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3658 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003659}
3660
Evan Cheng0038e592006-03-28 00:39:58 +00003661/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3662/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003663static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003664 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003665 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003666
3667 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3668 "Unsupported vector type for unpckh");
3669
Craig Topper5a529e42013-01-18 06:44:29 +00003670 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003671 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003672 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003673
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003674 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3675 // independently on 128-bit lanes.
3676 unsigned NumLanes = VT.getSizeInBits()/128;
3677 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003678
Craig Topper94438ba2011-12-16 08:06:31 +00003679 for (unsigned l = 0; l != NumLanes; ++l) {
3680 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3681 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003682 i += 2, ++j) {
3683 int BitI = Mask[i];
3684 int BitI1 = Mask[i+1];
3685 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003686 return false;
David Greenea20244d2011-03-02 17:23:43 +00003687 if (V2IsSplat) {
3688 if (!isUndefOrEqual(BitI1, NumElts))
3689 return false;
3690 } else {
3691 if (!isUndefOrEqual(BitI1, j + NumElts))
3692 return false;
3693 }
Evan Cheng39623da2006-04-20 08:58:49 +00003694 }
Evan Cheng0038e592006-03-28 00:39:58 +00003695 }
David Greenea20244d2011-03-02 17:23:43 +00003696
Evan Cheng0038e592006-03-28 00:39:58 +00003697 return true;
3698}
3699
Evan Cheng4fcb9222006-03-28 02:43:26 +00003700/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3701/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003702static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003703 bool HasInt256, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003704 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003705
3706 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3707 "Unsupported vector type for unpckh");
3708
Craig Topper5a529e42013-01-18 06:44:29 +00003709 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003710 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003711 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003712
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003713 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3714 // independently on 128-bit lanes.
3715 unsigned NumLanes = VT.getSizeInBits()/128;
3716 unsigned NumLaneElts = NumElts/NumLanes;
3717
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003718 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003719 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3720 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003721 int BitI = Mask[i];
3722 int BitI1 = Mask[i+1];
3723 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003724 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003725 if (V2IsSplat) {
3726 if (isUndefOrEqual(BitI1, NumElts))
3727 return false;
3728 } else {
3729 if (!isUndefOrEqual(BitI1, j+NumElts))
3730 return false;
3731 }
Evan Cheng39623da2006-04-20 08:58:49 +00003732 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003733 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003734 return true;
3735}
3736
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003737/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3738/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3739/// <0, 0, 1, 1>
Craig Topper5a529e42013-01-18 06:44:29 +00003740static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003741 unsigned NumElts = VT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00003742 bool Is256BitVec = VT.is256BitVector();
Craig Topper94438ba2011-12-16 08:06:31 +00003743
3744 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3745 "Unsupported vector type for unpckh");
3746
Craig Topper5a529e42013-01-18 06:44:29 +00003747 if (Is256BitVec && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003748 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003749 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003750
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003751 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3752 // FIXME: Need a better way to get rid of this, there's no latency difference
3753 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3754 // the former later. We should also remove the "_undef" special mask.
Craig Topper5a529e42013-01-18 06:44:29 +00003755 if (NumElts == 4 && Is256BitVec)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003756 return false;
3757
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003758 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3759 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003760 unsigned NumLanes = VT.getSizeInBits()/128;
3761 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003762
Craig Topper94438ba2011-12-16 08:06:31 +00003763 for (unsigned l = 0; l != NumLanes; ++l) {
3764 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3765 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003766 i += 2, ++j) {
3767 int BitI = Mask[i];
3768 int BitI1 = Mask[i+1];
3769
3770 if (!isUndefOrEqual(BitI, j))
3771 return false;
3772 if (!isUndefOrEqual(BitI1, j))
3773 return false;
3774 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003775 }
David Greenea20244d2011-03-02 17:23:43 +00003776
Rafael Espindola15684b22009-04-24 12:40:33 +00003777 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003778}
3779
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003780/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3781/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3782/// <2, 2, 3, 3>
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003783static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) {
Craig Topper94438ba2011-12-16 08:06:31 +00003784 unsigned NumElts = VT.getVectorNumElements();
3785
3786 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3787 "Unsupported vector type for unpckh");
3788
Craig Topper5a529e42013-01-18 06:44:29 +00003789 if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 &&
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003790 (!HasInt256 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003791 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003792
Craig Topper94438ba2011-12-16 08:06:31 +00003793 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3794 // independently on 128-bit lanes.
3795 unsigned NumLanes = VT.getSizeInBits()/128;
3796 unsigned NumLaneElts = NumElts/NumLanes;
3797
3798 for (unsigned l = 0; l != NumLanes; ++l) {
3799 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3800 i != (l+1)*NumLaneElts; i += 2, ++j) {
3801 int BitI = Mask[i];
3802 int BitI1 = Mask[i+1];
3803 if (!isUndefOrEqual(BitI, j))
3804 return false;
3805 if (!isUndefOrEqual(BitI1, j))
3806 return false;
3807 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003808 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003809 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003810}
3811
Evan Cheng017dcc62006-04-21 01:05:10 +00003812/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3813/// specifies a shuffle of elements that is suitable for input to MOVSS,
3814/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003815static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003816 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003817 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003818 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003819 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003820
Craig Topperc612d792012-01-02 09:17:37 +00003821 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003822
Nate Begeman9008ca62009-04-27 18:41:29 +00003823 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003824 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003825
Craig Topperc612d792012-01-02 09:17:37 +00003826 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003827 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003828 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003829
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003830 return true;
3831}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003832
Craig Topper70b883b2011-11-28 10:14:51 +00003833/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003834/// as permutations between 128-bit chunks or halves. As an example: this
3835/// shuffle bellow:
3836/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3837/// The first half comes from the second half of V1 and the second half from the
3838/// the second half of V2.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003839static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3840 if (!HasFp256 || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003841 return false;
3842
3843 // The shuffle result is divided into half A and half B. In total the two
3844 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3845 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003846 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003847 bool MatchA = false, MatchB = false;
3848
3849 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003850 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003851 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3852 MatchA = true;
3853 break;
3854 }
3855 }
3856
3857 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003858 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003859 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3860 MatchB = true;
3861 break;
3862 }
3863 }
3864
3865 return MatchA && MatchB;
3866}
3867
Craig Topper70b883b2011-11-28 10:14:51 +00003868/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3869/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003870static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00003871 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003872
Craig Topperc612d792012-01-02 09:17:37 +00003873 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003874
Craig Topperc612d792012-01-02 09:17:37 +00003875 unsigned FstHalf = 0, SndHalf = 0;
3876 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003877 if (SVOp->getMaskElt(i) > 0) {
3878 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3879 break;
3880 }
3881 }
Craig Topperc612d792012-01-02 09:17:37 +00003882 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003883 if (SVOp->getMaskElt(i) > 0) {
3884 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3885 break;
3886 }
3887 }
3888
3889 return (FstHalf | (SndHalf << 4));
3890}
3891
Craig Topper70b883b2011-11-28 10:14:51 +00003892/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003893/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3894/// Note that VPERMIL mask matching is different depending whether theunderlying
3895/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3896/// to the same elements of the low, but to the higher half of the source.
3897/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003898/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00003899static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
3900 if (!HasFp256)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003901 return false;
3902
Craig Topperc612d792012-01-02 09:17:37 +00003903 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003904 // Only match 256-bit with 32/64-bit types
Craig Topper5a529e42013-01-18 06:44:29 +00003905 if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003906 return false;
3907
Craig Topperc612d792012-01-02 09:17:37 +00003908 unsigned NumLanes = VT.getSizeInBits()/128;
3909 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003910 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003911 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003912 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003913 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003914 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003915 continue;
3916 // VPERMILPS handling
3917 if (Mask[i] < 0)
3918 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003919 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003920 return false;
3921 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003922 }
3923
3924 return true;
3925}
3926
Craig Topper5aaffa82012-02-19 02:53:47 +00003927/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003928/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003929/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003930static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003931 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003932 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003933 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003934
3935 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003936 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003937 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003938
Nate Begeman9008ca62009-04-27 18:41:29 +00003939 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003940 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003941
Craig Topperc612d792012-01-02 09:17:37 +00003942 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003943 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3944 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3945 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003946 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003947
Evan Cheng39623da2006-04-20 08:58:49 +00003948 return true;
3949}
3950
Evan Chengd9539472006-04-14 21:59:03 +00003951/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3952/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003953/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003954static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003955 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003956 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003957 return false;
3958
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003959 unsigned NumElems = VT.getVectorNumElements();
3960
Craig Topper5a529e42013-01-18 06:44:29 +00003961 if ((VT.is128BitVector() && NumElems != 4) ||
3962 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003963 return false;
3964
3965 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003966 for (unsigned i = 0; i != NumElems; i += 2)
3967 if (!isUndefOrEqual(Mask[i], i+1) ||
3968 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003969 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003970
3971 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003972}
3973
3974/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3975/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003976/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003977static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003978 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003979 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003980 return false;
3981
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003982 unsigned NumElems = VT.getVectorNumElements();
3983
Craig Topper5a529e42013-01-18 06:44:29 +00003984 if ((VT.is128BitVector() && NumElems != 4) ||
3985 (VT.is256BitVector() && NumElems != 8))
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003986 return false;
3987
3988 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003989 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003990 if (!isUndefOrEqual(Mask[i], i) ||
3991 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003992 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003993
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003994 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003995}
3996
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003997/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3998/// specifies a shuffle of elements that is suitable for input to 256-bit
3999/// version of MOVDDUP.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004000static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) {
4001 if (!HasFp256 || !VT.is256BitVector())
Craig Topper7a9a28b2012-08-12 02:23:29 +00004002 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004003
Craig Topper7a9a28b2012-08-12 02:23:29 +00004004 unsigned NumElts = VT.getVectorNumElements();
4005 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004006 return false;
4007
Craig Topperc612d792012-01-02 09:17:37 +00004008 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004009 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004010 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004011 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00004012 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00004013 return false;
4014 return true;
4015}
4016
Evan Cheng0b457f02008-09-25 20:50:48 +00004017/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004018/// specifies a shuffle of elements that is suitable for input to 128-bit
4019/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00004020static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004021 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00004022 return false;
4023
Craig Topperc612d792012-01-02 09:17:37 +00004024 unsigned e = VT.getVectorNumElements() / 2;
4025 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004026 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004027 return false;
Craig Topperc612d792012-01-02 09:17:37 +00004028 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004029 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00004030 return false;
4031 return true;
4032}
4033
David Greenec38a03e2011-02-03 15:50:00 +00004034/// isVEXTRACTF128Index - Return true if the specified
4035/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
4036/// suitable for input to VEXTRACTF128.
4037bool X86::isVEXTRACTF128Index(SDNode *N) {
4038 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4039 return false;
4040
4041 // The index should be aligned on a 128-bit boundary.
4042 uint64_t Index =
4043 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4044
Craig Topper5141d972013-01-18 08:41:28 +00004045 MVT VT = N->getValueType(0).getSimpleVT();
4046 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004047 bool Result = (Index * ElSize) % 128 == 0;
4048
4049 return Result;
4050}
4051
David Greeneccacdc12011-02-04 16:08:29 +00004052/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
4053/// operand specifies a subvector insert that is suitable for input to
4054/// VINSERTF128.
4055bool X86::isVINSERTF128Index(SDNode *N) {
4056 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4057 return false;
4058
4059 // The index should be aligned on a 128-bit boundary.
4060 uint64_t Index =
4061 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4062
Craig Topper5141d972013-01-18 08:41:28 +00004063 MVT VT = N->getValueType(0).getSimpleVT();
4064 unsigned ElSize = VT.getVectorElementType().getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004065 bool Result = (Index * ElSize) % 128 == 0;
4066
4067 return Result;
4068}
4069
Evan Cheng63d33002006-03-22 08:01:21 +00004070/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004071/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00004072/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00004073static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004074 MVT VT = N->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00004075
Craig Topper1a7700a2012-01-19 08:19:12 +00004076 assert((VT.is128BitVector() || VT.is256BitVector()) &&
4077 "Unsupported vector type for PSHUF/SHUFP");
4078
4079 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
4080 // independently on 128-bit lanes.
4081 unsigned NumElts = VT.getVectorNumElements();
4082 unsigned NumLanes = VT.getSizeInBits()/128;
4083 unsigned NumLaneElts = NumElts/NumLanes;
4084
4085 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
4086 "Only supports 2 or 4 elements per lane");
4087
4088 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00004089 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00004090 for (unsigned i = 0; i != NumElts; ++i) {
4091 int Elt = N->getMaskElt(i);
4092 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00004093 Elt &= NumLaneElts - 1;
4094 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00004095 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00004096 }
Craig Topper1a7700a2012-01-19 08:19:12 +00004097
Evan Cheng63d33002006-03-22 08:01:21 +00004098 return Mask;
4099}
4100
Evan Cheng506d3df2006-03-29 23:07:14 +00004101/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004102/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004103static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004104 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004105
4106 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4107 "Unsupported vector type for PSHUFHW");
4108
4109 unsigned NumElts = VT.getVectorNumElements();
4110
Evan Cheng506d3df2006-03-29 23:07:14 +00004111 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004112 for (unsigned l = 0; l != NumElts; l += 8) {
4113 // 8 nodes per lane, but we only care about the last 4.
4114 for (unsigned i = 0; i < 4; ++i) {
4115 int Elt = N->getMaskElt(l+i+4);
4116 if (Elt < 0) continue;
4117 Elt &= 0x3; // only 2-bits.
4118 Mask |= Elt << (i * 2);
4119 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004120 }
Craig Topper6b28d352012-05-03 07:12:59 +00004121
Evan Cheng506d3df2006-03-29 23:07:14 +00004122 return Mask;
4123}
4124
4125/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004126/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004127static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004128 MVT VT = N->getValueType(0).getSimpleVT();
Craig Topper6b28d352012-05-03 07:12:59 +00004129
4130 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4131 "Unsupported vector type for PSHUFHW");
4132
4133 unsigned NumElts = VT.getVectorNumElements();
4134
Evan Cheng506d3df2006-03-29 23:07:14 +00004135 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004136 for (unsigned l = 0; l != NumElts; l += 8) {
4137 // 8 nodes per lane, but we only care about the first 4.
4138 for (unsigned i = 0; i < 4; ++i) {
4139 int Elt = N->getMaskElt(l+i);
4140 if (Elt < 0) continue;
4141 Elt &= 0x3; // only 2-bits
4142 Mask |= Elt << (i * 2);
4143 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004144 }
Craig Topper6b28d352012-05-03 07:12:59 +00004145
Evan Cheng506d3df2006-03-29 23:07:14 +00004146 return Mask;
4147}
4148
Nate Begemana09008b2009-10-19 02:17:23 +00004149/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4150/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004151static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
Craig Toppercfcab212013-01-19 08:27:45 +00004152 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topperd93e4c32011-12-11 19:12:35 +00004153 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004154
Craig Topper0e2037b2012-01-20 05:53:00 +00004155 unsigned NumElts = VT.getVectorNumElements();
4156 unsigned NumLanes = VT.getSizeInBits()/128;
4157 unsigned NumLaneElts = NumElts/NumLanes;
4158
4159 int Val = 0;
4160 unsigned i;
4161 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004162 Val = SVOp->getMaskElt(i);
4163 if (Val >= 0)
4164 break;
4165 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004166 if (Val >= (int)NumElts)
4167 Val -= NumElts - NumLaneElts;
4168
Eli Friedman63f8dde2011-07-25 21:36:45 +00004169 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004170 return (Val - i) * EltSize;
4171}
4172
David Greenec38a03e2011-02-03 15:50:00 +00004173/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4174/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4175/// instructions.
4176unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4177 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4178 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4179
4180 uint64_t Index =
4181 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4182
Craig Toppercfcab212013-01-19 08:27:45 +00004183 MVT VecVT = N->getOperand(0).getValueType().getSimpleVT();
4184 MVT ElVT = VecVT.getVectorElementType();
David Greenec38a03e2011-02-03 15:50:00 +00004185
4186 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004187 return Index / NumElemsPerChunk;
4188}
4189
David Greeneccacdc12011-02-04 16:08:29 +00004190/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4191/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4192/// instructions.
4193unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4194 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4195 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4196
4197 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004198 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004199
Craig Toppercfcab212013-01-19 08:27:45 +00004200 MVT VecVT = N->getValueType(0).getSimpleVT();
4201 MVT ElVT = VecVT.getVectorElementType();
David Greeneccacdc12011-02-04 16:08:29 +00004202
4203 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004204 return Index / NumElemsPerChunk;
4205}
4206
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004207/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4208/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4209/// Handles 256-bit.
4210static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
Craig Toppercfcab212013-01-19 08:27:45 +00004211 MVT VT = N->getValueType(0).getSimpleVT();
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004212
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004213 unsigned NumElts = VT.getVectorNumElements();
4214
Craig Topper095c5282012-04-15 23:48:57 +00004215 assert((VT.is256BitVector() && NumElts == 4) &&
4216 "Unsupported vector type for VPERMQ/VPERMPD");
4217
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004218 unsigned Mask = 0;
4219 for (unsigned i = 0; i != NumElts; ++i) {
4220 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004221 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004222 continue;
4223 Mask |= Elt << (i*2);
4224 }
4225
4226 return Mask;
4227}
Evan Cheng37b73872009-07-30 08:33:02 +00004228/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4229/// constant +0.0.
4230bool X86::isZeroNode(SDValue Elt) {
Jakub Staszak30fcfc32013-02-16 13:34:26 +00004231 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Elt))
4232 return CN->isNullValue();
4233 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Elt))
4234 return CFP->getValueAPF().isPosZero();
4235 return false;
Evan Cheng37b73872009-07-30 08:33:02 +00004236}
4237
Nate Begeman9008ca62009-04-27 18:41:29 +00004238/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4239/// their permute mask.
4240static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4241 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00004242 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman5a5ca152009-04-29 05:20:52 +00004243 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004244 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004245
Nate Begeman5a5ca152009-04-29 05:20:52 +00004246 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004247 int Idx = SVOp->getMaskElt(i);
4248 if (Idx >= 0) {
4249 if (Idx < (int)NumElems)
4250 Idx += NumElems;
4251 else
4252 Idx -= NumElems;
4253 }
4254 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004255 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004256 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4257 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004258}
4259
Evan Cheng533a0aa2006-04-19 20:35:22 +00004260/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4261/// match movhlps. The lower half elements should come from upper half of
4262/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004263/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004264static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004265 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004266 return false;
4267 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004268 return false;
4269 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004270 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004271 return false;
4272 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004273 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004274 return false;
4275 return true;
4276}
4277
Evan Cheng5ced1d82006-04-06 23:23:56 +00004278/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004279/// is promoted to a vector. It also returns the LoadSDNode by reference if
4280/// required.
4281static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004282 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4283 return false;
4284 N = N->getOperand(0).getNode();
4285 if (!ISD::isNON_EXTLoad(N))
4286 return false;
4287 if (LD)
4288 *LD = cast<LoadSDNode>(N);
4289 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004290}
4291
Dan Gohman65fd6562011-11-03 21:49:52 +00004292// Test whether the given value is a vector value which will be legalized
4293// into a load.
4294static bool WillBeConstantPoolLoad(SDNode *N) {
4295 if (N->getOpcode() != ISD::BUILD_VECTOR)
4296 return false;
4297
4298 // Check for any non-constant elements.
4299 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4300 switch (N->getOperand(i).getNode()->getOpcode()) {
4301 case ISD::UNDEF:
4302 case ISD::ConstantFP:
4303 case ISD::Constant:
4304 break;
4305 default:
4306 return false;
4307 }
4308
4309 // Vectors of all-zeros and all-ones are materialized with special
4310 // instructions rather than being loaded.
4311 return !ISD::isBuildVectorAllZeros(N) &&
4312 !ISD::isBuildVectorAllOnes(N);
4313}
4314
Evan Cheng533a0aa2006-04-19 20:35:22 +00004315/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4316/// match movlp{s|d}. The lower half elements should come from lower half of
4317/// V1 (and in order), and the upper half elements should come from the upper
4318/// half of V2 (and in order). And since V1 will become the source of the
4319/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004320static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004321 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004322 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004323 return false;
4324
Evan Cheng466685d2006-10-09 20:57:25 +00004325 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004326 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004327 // Is V2 is a vector load, don't do this transformation. We will try to use
4328 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004329 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004330 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004331
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004332 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004333
Evan Cheng533a0aa2006-04-19 20:35:22 +00004334 if (NumElems != 2 && NumElems != 4)
4335 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004336 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004337 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004338 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004339 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004340 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004341 return false;
4342 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004343}
4344
Evan Cheng39623da2006-04-20 08:58:49 +00004345/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4346/// all the same.
4347static bool isSplatVector(SDNode *N) {
4348 if (N->getOpcode() != ISD::BUILD_VECTOR)
4349 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004350
Dan Gohman475871a2008-07-27 21:46:04 +00004351 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004352 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4353 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004354 return false;
4355 return true;
4356}
4357
Evan Cheng213d2cf2007-05-17 18:45:50 +00004358/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004359/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004360/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004361static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004362 SDValue V1 = N->getOperand(0);
4363 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004364 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4365 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004367 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004368 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004369 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4370 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004371 if (Opc != ISD::BUILD_VECTOR ||
4372 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004373 return false;
4374 } else if (Idx >= 0) {
4375 unsigned Opc = V1.getOpcode();
4376 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4377 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004378 if (Opc != ISD::BUILD_VECTOR ||
4379 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004380 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004381 }
4382 }
4383 return true;
4384}
4385
4386/// getZeroVector - Returns a vector of specified type with all zero elements.
4387///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004388static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004389 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004390 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004391
Dale Johannesen0488fb62010-09-30 23:57:10 +00004392 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004393 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004394 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004395 if (VT.is128BitVector()) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004396 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004397 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4398 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4399 } else { // SSE1
4400 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4401 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4402 }
Craig Topper5a529e42013-01-18 06:44:29 +00004403 } else if (VT.is256BitVector()) { // AVX
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004404 if (Subtarget->hasInt256()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004405 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4406 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4407 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4408 } else {
4409 // 256-bit logic and arithmetic instructions in AVX are all
4410 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4411 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4412 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4413 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4414 }
Craig Topper9d352402012-04-23 07:24:41 +00004415 } else
4416 llvm_unreachable("Unexpected vector type");
4417
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004418 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004419}
4420
Chris Lattner8a594482007-11-25 00:24:49 +00004421/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004422/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4423/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4424/// Then bitcast to their original type, ensuring they get CSE'd.
Craig Topper45e1c752013-01-20 00:38:18 +00004425static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG,
Craig Topper745a86b2011-11-19 22:34:59 +00004426 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004427 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00004428
Owen Anderson825b72b2009-08-11 20:47:22 +00004429 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004430 SDValue Vec;
Craig Topper5a529e42013-01-18 06:44:29 +00004431 if (VT.is256BitVector()) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00004432 if (HasInt256) { // AVX2
Craig Topper745a86b2011-11-19 22:34:59 +00004433 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4434 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4435 } else { // AVX
4436 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004437 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004438 }
Craig Topper5a529e42013-01-18 06:44:29 +00004439 } else if (VT.is128BitVector()) {
Craig Topper745a86b2011-11-19 22:34:59 +00004440 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004441 } else
4442 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004443
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004444 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004445}
4446
Evan Cheng39623da2006-04-20 08:58:49 +00004447/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4448/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004449static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004450 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004451 if (Mask[i] > (int)NumElems) {
4452 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004453 }
Evan Cheng39623da2006-04-20 08:58:49 +00004454 }
Evan Cheng39623da2006-04-20 08:58:49 +00004455}
4456
Evan Cheng017dcc62006-04-21 01:05:10 +00004457/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4458/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004459static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 SDValue V2) {
4461 unsigned NumElems = VT.getVectorNumElements();
4462 SmallVector<int, 8> Mask;
4463 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004464 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004465 Mask.push_back(i);
4466 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004467}
4468
Nate Begeman9008ca62009-04-27 18:41:29 +00004469/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004470static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004471 SDValue V2) {
4472 unsigned NumElems = VT.getVectorNumElements();
4473 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004474 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004475 Mask.push_back(i);
4476 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004477 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004479}
4480
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004481/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004482static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004483 SDValue V2) {
4484 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004486 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004487 Mask.push_back(i + Half);
4488 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004489 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004490 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004491}
4492
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004493// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004494// a generic shuffle instruction because the target has no such instructions.
4495// Generate shuffles which repeat i16 and i8 several times until they can be
4496// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004497static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004498 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004499 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004500 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004501
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 while (NumElems > 4) {
4503 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004504 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004505 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004506 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004507 EltNo -= NumElems/2;
4508 }
4509 NumElems >>= 1;
4510 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004511 return V;
4512}
Eric Christopherfd179292009-08-27 18:07:15 +00004513
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004514/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4515static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4516 EVT VT = V.getValueType();
4517 DebugLoc dl = V.getDebugLoc();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004518
Craig Topper5a529e42013-01-18 06:44:29 +00004519 if (VT.is128BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004520 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004521 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004522 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4523 &SplatMask[0]);
Craig Topper5a529e42013-01-18 06:44:29 +00004524 } else if (VT.is256BitVector()) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004525 // To use VPERMILPS to splat scalars, the second half of indicies must
4526 // refer to the higher part, which is a duplication of the lower one,
4527 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004528 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4529 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004530
4531 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4532 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4533 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004534 } else
4535 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004536
4537 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4538}
4539
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004540/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004541static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4542 EVT SrcVT = SV->getValueType(0);
4543 SDValue V1 = SV->getOperand(0);
4544 DebugLoc dl = SV->getDebugLoc();
4545
4546 int EltNo = SV->getSplatIndex();
4547 int NumElems = SrcVT.getVectorNumElements();
Craig Topper5a529e42013-01-18 06:44:29 +00004548 bool Is256BitVec = SrcVT.is256BitVector();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004549
Craig Topper5a529e42013-01-18 06:44:29 +00004550 assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) &&
4551 "Unknown how to promote splat for type");
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004552
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004553 // Extract the 128-bit part containing the splat element and update
4554 // the splat element index when it refers to the higher register.
Craig Topper5a529e42013-01-18 06:44:29 +00004555 if (Is256BitVec) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004556 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4557 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004558 EltNo -= NumElems/2;
4559 }
4560
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004561 // All i16 and i8 vector types can't be used directly by a generic shuffle
4562 // instruction because the target has no such instruction. Generate shuffles
4563 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004564 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004565 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004566 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004567 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004568
4569 // Recreate the 256-bit vector and place the same 128-bit vector
4570 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004571 // to use VPERM* to shuffle the vectors
Craig Topper5a529e42013-01-18 06:44:29 +00004572 if (Is256BitVec) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004573 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004574 }
4575
4576 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004577}
4578
Evan Chengba05f722006-04-21 23:03:30 +00004579/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004580/// vector of zero or undef vector. This produces a shuffle where the low
4581/// element of V2 is swizzled into the zero/undef vector, landing at element
4582/// 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 +00004583static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004584 bool IsZero,
4585 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004586 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004587 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004588 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004589 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004590 unsigned NumElems = VT.getVectorNumElements();
4591 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004592 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004593 // If this is the insertion idx, put the low elt of V2 here.
4594 MaskVec.push_back(i == Idx ? NumElems : i);
4595 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004596}
4597
Craig Toppera1ffc682012-03-20 06:42:26 +00004598/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4599/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004600/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004601static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004602 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004603 unsigned NumElems = VT.getVectorNumElements();
4604 SDValue ImmN;
4605
Craig Topper89f4e662012-03-20 07:17:59 +00004606 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004607 switch(N->getOpcode()) {
4608 case X86ISD::SHUFP:
4609 ImmN = N->getOperand(N->getNumOperands()-1);
4610 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4611 break;
4612 case X86ISD::UNPCKH:
4613 DecodeUNPCKHMask(VT, Mask);
4614 break;
4615 case X86ISD::UNPCKL:
4616 DecodeUNPCKLMask(VT, Mask);
4617 break;
4618 case X86ISD::MOVHLPS:
4619 DecodeMOVHLPSMask(NumElems, Mask);
4620 break;
4621 case X86ISD::MOVLHPS:
4622 DecodeMOVLHPSMask(NumElems, Mask);
4623 break;
Craig Topper4aee1bb2013-01-28 06:48:25 +00004624 case X86ISD::PALIGNR:
Benjamin Kramer200b3062013-01-26 13:31:37 +00004625 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Topper4aee1bb2013-01-28 06:48:25 +00004626 DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Benjamin Kramer200b3062013-01-26 13:31:37 +00004627 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004628 case X86ISD::PSHUFD:
4629 case X86ISD::VPERMILP:
4630 ImmN = N->getOperand(N->getNumOperands()-1);
4631 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004632 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004633 break;
4634 case X86ISD::PSHUFHW:
4635 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004636 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004637 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004638 break;
4639 case X86ISD::PSHUFLW:
4640 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004641 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004642 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004643 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004644 case X86ISD::VPERMI:
4645 ImmN = N->getOperand(N->getNumOperands()-1);
4646 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4647 IsUnary = true;
4648 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004649 case X86ISD::MOVSS:
4650 case X86ISD::MOVSD: {
4651 // The index 0 always comes from the first element of the second source,
4652 // this is why MOVSS and MOVSD are used in the first place. The other
4653 // elements come from the other positions of the first source vector
4654 Mask.push_back(NumElems);
4655 for (unsigned i = 1; i != NumElems; ++i) {
4656 Mask.push_back(i);
4657 }
4658 break;
4659 }
4660 case X86ISD::VPERM2X128:
4661 ImmN = N->getOperand(N->getNumOperands()-1);
4662 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004663 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004664 break;
4665 case X86ISD::MOVDDUP:
4666 case X86ISD::MOVLHPD:
4667 case X86ISD::MOVLPD:
4668 case X86ISD::MOVLPS:
4669 case X86ISD::MOVSHDUP:
4670 case X86ISD::MOVSLDUP:
Craig Toppera1ffc682012-03-20 06:42:26 +00004671 // Not yet implemented
4672 return false;
4673 default: llvm_unreachable("unknown target shuffle node");
4674 }
4675
4676 return true;
4677}
4678
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004679/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4680/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004681static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004682 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004683 if (Depth == 6)
4684 return SDValue(); // Limit search depth.
4685
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004686 SDValue V = SDValue(N, 0);
4687 EVT VT = V.getValueType();
4688 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004689
4690 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4691 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004692 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004693
Craig Topper3d092db2012-03-21 02:14:01 +00004694 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004695 return DAG.getUNDEF(VT.getVectorElementType());
4696
Craig Topperd156dc12012-02-06 07:17:51 +00004697 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004698 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4699 : SV->getOperand(1);
4700 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004701 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004702
4703 // Recurse into target specific vector shuffles to find scalars.
4704 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004705 MVT ShufVT = V.getValueType().getSimpleVT();
4706 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004707 SmallVector<int, 16> ShuffleMask;
Craig Topper89f4e662012-03-20 07:17:59 +00004708 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004709
Craig Topperd978c542012-05-06 19:46:21 +00004710 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004711 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004712
Craig Topper3d092db2012-03-21 02:14:01 +00004713 int Elt = ShuffleMask[Index];
4714 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004715 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004716
Craig Topper3d092db2012-03-21 02:14:01 +00004717 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004718 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004719 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004720 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004721 }
4722
4723 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004724 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004725 V = V.getOperand(0);
4726 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004727 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004728
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004729 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004730 return SDValue();
4731 }
4732
4733 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4734 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004735 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004736
4737 if (V.getOpcode() == ISD::BUILD_VECTOR)
4738 return V.getOperand(Index);
4739
4740 return SDValue();
4741}
4742
4743/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4744/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004745/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004746static
Craig Topper3d092db2012-03-21 02:14:01 +00004747unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004748 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004749 unsigned i;
4750 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004751 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004752 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004753 if (!(Elt.getNode() &&
4754 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4755 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004756 }
4757
4758 return i;
4759}
4760
Craig Topper3d092db2012-03-21 02:14:01 +00004761/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4762/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004763/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4764static
Craig Topper3d092db2012-03-21 02:14:01 +00004765bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4766 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4767 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004768 bool SeenV1 = false;
4769 bool SeenV2 = false;
4770
Craig Topper3d092db2012-03-21 02:14:01 +00004771 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004772 int Idx = SVOp->getMaskElt(i);
4773 // Ignore undef indicies
4774 if (Idx < 0)
4775 continue;
4776
Craig Topper3d092db2012-03-21 02:14:01 +00004777 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004778 SeenV1 = true;
4779 else
4780 SeenV2 = true;
4781
4782 // Only accept consecutive elements from the same vector
4783 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4784 return false;
4785 }
4786
4787 OpNum = SeenV1 ? 0 : 1;
4788 return true;
4789}
4790
4791/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4792/// logical left shift of a vector.
4793static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4794 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4795 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4796 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4797 false /* check zeros from right */, DAG);
4798 unsigned OpSrc;
4799
4800 if (!NumZeros)
4801 return false;
4802
4803 // Considering the elements in the mask that are not consecutive zeros,
4804 // check if they consecutively come from only one of the source vectors.
4805 //
4806 // V1 = {X, A, B, C} 0
4807 // \ \ \ /
4808 // vector_shuffle V1, V2 <1, 2, 3, X>
4809 //
4810 if (!isShuffleMaskConsecutive(SVOp,
4811 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004812 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004813 NumZeros, // Where to start looking in the src vector
4814 NumElems, // Number of elements in vector
4815 OpSrc)) // Which source operand ?
4816 return false;
4817
4818 isLeft = false;
4819 ShAmt = NumZeros;
4820 ShVal = SVOp->getOperand(OpSrc);
4821 return true;
4822}
4823
4824/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4825/// logical left shift of a vector.
4826static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4827 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4828 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4829 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4830 true /* check zeros from left */, DAG);
4831 unsigned OpSrc;
4832
4833 if (!NumZeros)
4834 return false;
4835
4836 // Considering the elements in the mask that are not consecutive zeros,
4837 // check if they consecutively come from only one of the source vectors.
4838 //
4839 // 0 { A, B, X, X } = V2
4840 // / \ / /
4841 // vector_shuffle V1, V2 <X, X, 4, 5>
4842 //
4843 if (!isShuffleMaskConsecutive(SVOp,
4844 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004845 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004846 0, // Where to start looking in the src vector
4847 NumElems, // Number of elements in vector
4848 OpSrc)) // Which source operand ?
4849 return false;
4850
4851 isLeft = true;
4852 ShAmt = NumZeros;
4853 ShVal = SVOp->getOperand(OpSrc);
4854 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004855}
4856
4857/// isVectorShift - Returns true if the shuffle can be implemented as a
4858/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004859static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004860 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004861 // Although the logic below support any bitwidth size, there are no
4862 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004863 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004864 return false;
4865
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004866 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4867 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4868 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004869
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004870 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004871}
4872
Evan Chengc78d3b42006-04-24 18:01:45 +00004873/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4874///
Dan Gohman475871a2008-07-27 21:46:04 +00004875static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004876 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004877 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004878 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004879 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004880 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004881 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004882
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004883 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004884 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004885 bool First = true;
4886 for (unsigned i = 0; i < 16; ++i) {
4887 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4888 if (ThisIsNonZero && First) {
4889 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004890 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004891 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004892 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004893 First = false;
4894 }
4895
4896 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004897 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004898 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4899 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004900 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004901 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004902 }
4903 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004904 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4905 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4906 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004907 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004908 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004909 } else
4910 ThisElt = LastElt;
4911
Gabor Greifba36cb52008-08-28 21:40:38 +00004912 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004913 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004914 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004915 }
4916 }
4917
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004918 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004919}
4920
Bill Wendlinga348c562007-03-22 18:42:45 +00004921/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004922///
Dan Gohman475871a2008-07-27 21:46:04 +00004923static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004924 unsigned NumNonZero, unsigned NumZero,
4925 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004926 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004927 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004928 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004929 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004930
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004931 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004932 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004933 bool First = true;
4934 for (unsigned i = 0; i < 8; ++i) {
4935 bool isNonZero = (NonZeros & (1 << i)) != 0;
4936 if (isNonZero) {
4937 if (First) {
4938 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004939 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004940 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004941 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004942 First = false;
4943 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004944 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004945 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004946 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004947 }
4948 }
4949
4950 return V;
4951}
4952
Evan Chengf26ffe92008-05-29 08:22:04 +00004953/// getVShift - Return a vector logical shift node.
4954///
Owen Andersone50ed302009-08-10 22:56:29 +00004955static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004956 unsigned NumBits, SelectionDAG &DAG,
4957 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004958 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004959 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004960 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004961 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4962 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004963 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004964 DAG.getConstant(NumBits,
Michael Liaoa6b20ce2013-03-01 18:40:30 +00004965 TLI.getScalarShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004966}
4967
Dan Gohman475871a2008-07-27 21:46:04 +00004968SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004969X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004970 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004971
Evan Chengc3630942009-12-09 21:00:30 +00004972 // Check if the scalar load can be widened into a vector load. And if
4973 // the address is "base + cst" see if the cst can be "absorbed" into
4974 // the shuffle mask.
4975 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4976 SDValue Ptr = LD->getBasePtr();
4977 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4978 return SDValue();
4979 EVT PVT = LD->getValueType(0);
4980 if (PVT != MVT::i32 && PVT != MVT::f32)
4981 return SDValue();
4982
4983 int FI = -1;
4984 int64_t Offset = 0;
4985 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4986 FI = FINode->getIndex();
4987 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004988 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004989 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4990 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4991 Offset = Ptr.getConstantOperandVal(1);
4992 Ptr = Ptr.getOperand(0);
4993 } else {
4994 return SDValue();
4995 }
4996
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004997 // FIXME: 256-bit vector instructions don't require a strict alignment,
4998 // improve this code to support it better.
4999 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00005000 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005001 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00005002 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005003 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00005004 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00005005 // Can't change the alignment. FIXME: It's possible to compute
5006 // the exact stack offset and reference FI + adjust offset instead.
5007 // If someone *really* cares about this. That's the way to implement it.
5008 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005009 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005010 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00005011 }
5012 }
5013
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005014 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00005015 // Ptr + (Offset & ~15).
5016 if (Offset < 0)
5017 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005018 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00005019 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005020 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00005021 if (StartOffset)
5022 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
5023 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
5024
5025 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00005026 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005027
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005028 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5029 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00005030 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005031 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005032
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005033 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00005034 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00005035 Mask.push_back(EltNo);
5036
Craig Toppercc3000632012-01-30 07:50:31 +00005037 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00005038 }
5039
5040 return SDValue();
5041}
5042
Michael J. Spencerec38de22010-10-10 22:04:20 +00005043/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
5044/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00005045/// load which has the same value as a build_vector whose operands are 'elts'.
5046///
5047/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00005048///
Nate Begeman1449f292010-03-24 22:19:06 +00005049/// FIXME: we'd also like to handle the case where the last elements are zero
5050/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5051/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005052static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00005053 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005054 EVT EltVT = VT.getVectorElementType();
5055 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005056
Nate Begemanfdea31a2010-03-24 20:49:50 +00005057 LoadSDNode *LDBase = NULL;
5058 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005059
Nate Begeman1449f292010-03-24 22:19:06 +00005060 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00005061 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00005062 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005063 for (unsigned i = 0; i < NumElems; ++i) {
5064 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00005065
Nate Begemanfdea31a2010-03-24 20:49:50 +00005066 if (!Elt.getNode() ||
5067 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5068 return SDValue();
5069 if (!LDBase) {
5070 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5071 return SDValue();
5072 LDBase = cast<LoadSDNode>(Elt.getNode());
5073 LastLoadedElt = i;
5074 continue;
5075 }
5076 if (Elt.getOpcode() == ISD::UNDEF)
5077 continue;
5078
5079 LoadSDNode *LD = cast<LoadSDNode>(Elt);
5080 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
5081 return SDValue();
5082 LastLoadedElt = i;
5083 }
Nate Begeman1449f292010-03-24 22:19:06 +00005084
5085 // If we have found an entire vector of loads and undefs, then return a large
5086 // load of the entire vector width starting at the base pointer. If we found
5087 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00005088 if (LastLoadedElt == NumElems - 1) {
5089 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00005090 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005091 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005092 LDBase->isVolatile(), LDBase->isNonTemporal(),
5093 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00005094 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00005095 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00005096 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005097 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00005098 }
5099 if (NumElems == 4 && LastLoadedElt == 1 &&
5100 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005101 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5102 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00005103 SDValue ResNode =
5104 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
5105 LDBase->getPointerInfo(),
5106 LDBase->getAlignment(),
5107 false/*isVolatile*/, true/*ReadMem*/,
5108 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005109
5110 // Make sure the newly-created LOAD is in the same position as LDBase in
5111 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5112 // update uses of LDBase's output chain to use the TokenFactor.
5113 if (LDBase->hasAnyUseOfValue(1)) {
5114 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5115 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5116 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5117 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5118 SDValue(ResNode.getNode(), 1));
5119 }
5120
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005121 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005122 }
5123 return SDValue();
5124}
5125
Nadav Rotem9d68b062012-04-08 12:54:54 +00005126/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5127/// to generate a splat value for the following cases:
5128/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005129/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005130/// a scalar load, or a constant.
5131/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005132/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005133SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005134X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005135 if (!Subtarget->hasFp256())
Craig Toppera9376332012-01-10 08:23:59 +00005136 return SDValue();
5137
Craig Topper45e1c752013-01-20 00:38:18 +00005138 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem154819d2012-04-09 07:45:58 +00005139 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005140
Craig Topper5da8a802012-05-04 05:49:51 +00005141 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5142 "Unsupported vector type for broadcast.");
5143
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005144 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005145 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005146
Nadav Rotem9d68b062012-04-08 12:54:54 +00005147 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005148 default:
5149 // Unknown pattern found.
5150 return SDValue();
5151
5152 case ISD::BUILD_VECTOR: {
5153 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005154 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005155 return SDValue();
5156
Nadav Rotem9d68b062012-04-08 12:54:54 +00005157 Ld = Op.getOperand(0);
5158 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5159 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005160
5161 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005162 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005163 // Constants may have multiple users.
5164 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005165 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005166 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005167 }
5168
5169 case ISD::VECTOR_SHUFFLE: {
5170 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5171
5172 // Shuffles must have a splat mask where the first element is
5173 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005174 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005175 return SDValue();
5176
5177 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005178 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005179 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5180
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005181 if (!Subtarget->hasInt256())
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005182 return SDValue();
5183
5184 // Use the register form of the broadcast instruction available on AVX2.
5185 if (VT.is256BitVector())
5186 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5187 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5188 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005189
5190 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005191 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005192 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005193
5194 // The scalar_to_vector node and the suspected
5195 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005196 // Constants may have multiple users.
5197 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005198 return SDValue();
5199 break;
5200 }
5201 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005202
Craig Topper7a9a28b2012-08-12 02:23:29 +00005203 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005204
5205 // Handle the broadcasting a single constant scalar from the constant pool
5206 // into a vector. On Sandybridge it is still better to load a constant vector
5207 // from the constant pool and not to broadcast it from a scalar.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005208 if (ConstSplatVal && Subtarget->hasInt256()) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005209 EVT CVT = Ld.getValueType();
5210 assert(!CVT.isVector() && "Must not broadcast a vector type");
5211 unsigned ScalarSize = CVT.getSizeInBits();
5212
Craig Topper5da8a802012-05-04 05:49:51 +00005213 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005214 const Constant *C = 0;
5215 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5216 C = CI->getConstantIntValue();
5217 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5218 C = CF->getConstantFPValue();
5219
5220 assert(C && "Invalid constant type");
5221
Nadav Rotem154819d2012-04-09 07:45:58 +00005222 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005223 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005224 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005225 MachinePointerInfo::getConstantPool(),
5226 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005227
Nadav Rotem9d68b062012-04-08 12:54:54 +00005228 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5229 }
5230 }
5231
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005232 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005233 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5234
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005235 // Handle AVX2 in-register broadcasts.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005236 if (!IsLoad && Subtarget->hasInt256() &&
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005237 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5238 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5239
5240 // The scalar source must be a normal load.
5241 if (!IsLoad)
5242 return SDValue();
5243
Craig Topper5da8a802012-05-04 05:49:51 +00005244 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005245 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005246
Craig Toppera9376332012-01-10 08:23:59 +00005247 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005248 // double since there is no vbroadcastsd xmm
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005249 if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005250 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005251 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005252 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005253
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005254 // Unsupported broadcast.
5255 return SDValue();
5256}
5257
Evan Chengc3630942009-12-09 21:00:30 +00005258SDValue
Michael Liaofacace82012-10-19 17:15:18 +00005259X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const {
5260 EVT VT = Op.getValueType();
5261
5262 // Skip if insert_vec_elt is not supported.
5263 if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5264 return SDValue();
5265
5266 DebugLoc DL = Op.getDebugLoc();
5267 unsigned NumElems = Op.getNumOperands();
5268
5269 SDValue VecIn1;
5270 SDValue VecIn2;
5271 SmallVector<unsigned, 4> InsertIndices;
5272 SmallVector<int, 8> Mask(NumElems, -1);
5273
5274 for (unsigned i = 0; i != NumElems; ++i) {
5275 unsigned Opc = Op.getOperand(i).getOpcode();
5276
5277 if (Opc == ISD::UNDEF)
5278 continue;
5279
5280 if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5281 // Quit if more than 1 elements need inserting.
5282 if (InsertIndices.size() > 1)
5283 return SDValue();
5284
5285 InsertIndices.push_back(i);
5286 continue;
5287 }
5288
5289 SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5290 SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5291
5292 // Quit if extracted from vector of different type.
5293 if (ExtractedFromVec.getValueType() != VT)
5294 return SDValue();
5295
5296 // Quit if non-constant index.
5297 if (!isa<ConstantSDNode>(ExtIdx))
5298 return SDValue();
5299
5300 if (VecIn1.getNode() == 0)
5301 VecIn1 = ExtractedFromVec;
5302 else if (VecIn1 != ExtractedFromVec) {
5303 if (VecIn2.getNode() == 0)
5304 VecIn2 = ExtractedFromVec;
5305 else if (VecIn2 != ExtractedFromVec)
5306 // Quit if more than 2 vectors to shuffle
5307 return SDValue();
5308 }
5309
5310 unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5311
5312 if (ExtractedFromVec == VecIn1)
5313 Mask[i] = Idx;
5314 else if (ExtractedFromVec == VecIn2)
5315 Mask[i] = Idx + NumElems;
5316 }
5317
5318 if (VecIn1.getNode() == 0)
5319 return SDValue();
5320
5321 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5322 SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5323 for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5324 unsigned Idx = InsertIndices[i];
5325 NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5326 DAG.getIntPtrConstant(Idx));
5327 }
5328
5329 return NV;
5330}
5331
5332SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005333X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005334 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005335
Craig Topper45e1c752013-01-20 00:38:18 +00005336 MVT VT = Op.getValueType().getSimpleVT();
5337 MVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005338 unsigned NumElems = Op.getNumOperands();
5339
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005340 // Vectors containing all zeros can be matched by pxor and xorps later
5341 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5342 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5343 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005344 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005345 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005346
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005347 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005348 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005349
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005350 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005351 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5352 // vpcmpeqd on 256-bit vectors.
Michael Liaod09318f2013-02-25 23:16:36 +00005353 if (Subtarget->hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005354 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005355 return Op;
5356
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00005357 return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005358 }
5359
Nadav Rotem154819d2012-04-09 07:45:58 +00005360 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005361 if (Broadcast.getNode())
5362 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005363
Owen Andersone50ed302009-08-10 22:56:29 +00005364 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005365
Evan Cheng0db9fe62006-04-25 20:13:52 +00005366 unsigned NumZero = 0;
5367 unsigned NumNonZero = 0;
5368 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005369 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005370 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005371 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005372 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005373 if (Elt.getOpcode() == ISD::UNDEF)
5374 continue;
5375 Values.insert(Elt);
5376 if (Elt.getOpcode() != ISD::Constant &&
5377 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005378 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005379 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005380 NumZero++;
5381 else {
5382 NonZeros |= (1 << i);
5383 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005384 }
5385 }
5386
Chris Lattner97a2a562010-08-26 05:24:29 +00005387 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5388 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005389 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005390
Chris Lattner67f453a2008-03-09 05:42:06 +00005391 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005392 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005393 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005394 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005395
Chris Lattner62098042008-03-09 01:05:04 +00005396 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5397 // the value are obviously zero, truncate the value to i32 and do the
5398 // insertion that way. Only do this if the value is non-constant or if the
5399 // value is a constant being inserted into element 0. It is cheaper to do
5400 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005401 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005402 (!IsAllConstants || Idx == 0)) {
5403 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005404 // Handle SSE only.
5405 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5406 EVT VecVT = MVT::v4i32;
5407 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005408
Chris Lattner62098042008-03-09 01:05:04 +00005409 // Truncate the value (which may itself be a constant) to i32, and
5410 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005411 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005412 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005413 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005414
Chris Lattner62098042008-03-09 01:05:04 +00005415 // Now we have our 32-bit value zero extended in the low element of
5416 // a vector. If Idx != 0, swizzle it into place.
5417 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005418 SmallVector<int, 4> Mask;
5419 Mask.push_back(Idx);
5420 for (unsigned i = 1; i != VecElts; ++i)
5421 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005422 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005423 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005424 }
Craig Topper07a27622012-01-22 03:07:48 +00005425 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005426 }
5427 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005428
Chris Lattner19f79692008-03-08 22:59:52 +00005429 // If we have a constant or non-constant insertion into the low element of
5430 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5431 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005432 // depending on what the source datatype is.
5433 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005434 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005435 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005436
5437 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005438 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005439 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005440 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005441 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5442 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005443 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005444 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005445 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5446 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005447 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005448 }
5449
5450 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005451 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005452 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005453 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005454 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005455 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005456 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005457 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005458 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005459 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005460 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005461 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005462 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005463
5464 // Is it a vector logical left shift?
5465 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005466 X86::isZeroNode(Op.getOperand(0)) &&
5467 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005468 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005469 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005470 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005471 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005472 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005473 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005474
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005475 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005476 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005477
Chris Lattner19f79692008-03-08 22:59:52 +00005478 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5479 // is a non-constant being inserted into an element other than the low one,
5480 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5481 // movd/movss) to move this into the low element, then shuffle it into
5482 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005483 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005484 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005485
Evan Cheng0db9fe62006-04-25 20:13:52 +00005486 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005487 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005488 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005489 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005490 MaskVec.push_back(i == Idx ? 0 : 1);
5491 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005492 }
5493 }
5494
Chris Lattner67f453a2008-03-09 05:42:06 +00005495 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005496 if (Values.size() == 1) {
5497 if (EVTBits == 32) {
5498 // Instead of a shuffle like this:
5499 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5500 // Check if it's possible to issue this instead.
5501 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5502 unsigned Idx = CountTrailingZeros_32(NonZeros);
5503 SDValue Item = Op.getOperand(Idx);
5504 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5505 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5506 }
Dan Gohman475871a2008-07-27 21:46:04 +00005507 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005508 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005509
Dan Gohmana3941172007-07-24 22:55:08 +00005510 // A vector full of immediates; various special cases are already
5511 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005512 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005513 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005514
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005515 // For AVX-length vectors, build the individual 128-bit pieces and use
5516 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005517 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005518 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005519 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005520 V.push_back(Op.getOperand(i));
5521
5522 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5523
5524 // Build both the lower and upper subvector.
5525 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5526 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5527 NumElems/2);
5528
5529 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005530 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005531 }
5532
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005533 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005534 if (EVTBits == 64) {
5535 if (NumNonZero == 1) {
5536 // One half is zero or undef.
5537 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005538 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005539 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005540 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005541 }
Dan Gohman475871a2008-07-27 21:46:04 +00005542 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005543 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005544
5545 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005546 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005547 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005548 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005549 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005550 }
5551
Bill Wendling826f36f2007-03-28 00:57:11 +00005552 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005553 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005554 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005555 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005556 }
5557
5558 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005559 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005560 if (NumElems == 4 && NumZero > 0) {
5561 for (unsigned i = 0; i < 4; ++i) {
5562 bool isZero = !(NonZeros & (1 << i));
5563 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005564 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005565 else
Dale Johannesenace16102009-02-03 19:33:06 +00005566 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005567 }
5568
5569 for (unsigned i = 0; i < 2; ++i) {
5570 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5571 default: break;
5572 case 0:
5573 V[i] = V[i*2]; // Must be a zero vector.
5574 break;
5575 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005576 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005577 break;
5578 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005579 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005580 break;
5581 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005582 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005583 break;
5584 }
5585 }
5586
Benjamin Kramer9c683542012-01-30 15:16:21 +00005587 bool Reverse1 = (NonZeros & 0x3) == 2;
5588 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5589 int MaskVec[] = {
5590 Reverse1 ? 1 : 0,
5591 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005592 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5593 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005594 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005595 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005596 }
5597
Craig Topper7a9a28b2012-08-12 02:23:29 +00005598 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005599 // Check for a build vector of consecutive loads.
5600 for (unsigned i = 0; i < NumElems; ++i)
5601 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005602
Nate Begemanfdea31a2010-03-24 20:49:50 +00005603 // Check for elements which are consecutive loads.
5604 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5605 if (LD.getNode())
5606 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005607
Michael Liaofacace82012-10-19 17:15:18 +00005608 // Check for a build vector from mostly shuffle plus few inserting.
5609 SDValue Sh = buildFromShuffleMostly(Op, DAG);
5610 if (Sh.getNode())
5611 return Sh;
5612
Michael J. Spencerec38de22010-10-10 22:04:20 +00005613 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005614 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005615 SDValue Result;
5616 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5617 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5618 else
5619 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005620
Chris Lattner24faf612010-08-28 17:59:08 +00005621 for (unsigned i = 1; i < NumElems; ++i) {
5622 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5623 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005624 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005625 }
5626 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005627 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005628
Chris Lattner6e80e442010-08-28 17:15:43 +00005629 // Otherwise, expand into a number of unpckl*, start by extending each of
5630 // our (non-undef) elements to the full vector width with the element in the
5631 // bottom slot of the vector (which generates no code for SSE).
5632 for (unsigned i = 0; i < NumElems; ++i) {
5633 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5634 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5635 else
5636 V[i] = DAG.getUNDEF(VT);
5637 }
5638
5639 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005640 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5641 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5642 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005643 unsigned EltStride = NumElems >> 1;
5644 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005645 for (unsigned i = 0; i < EltStride; ++i) {
5646 // If V[i+EltStride] is undef and this is the first round of mixing,
5647 // then it is safe to just drop this shuffle: V[i] is already in the
5648 // right place, the one element (since it's the first round) being
5649 // inserted as undef can be dropped. This isn't safe for successive
5650 // rounds because they will permute elements within both vectors.
5651 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5652 EltStride == NumElems/2)
5653 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005654
Chris Lattner6e80e442010-08-28 17:15:43 +00005655 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005656 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005657 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005658 }
5659 return V[0];
5660 }
Dan Gohman475871a2008-07-27 21:46:04 +00005661 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005662}
5663
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005664// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5665// to create 256-bit vectors from two other 128-bit ones.
5666static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5667 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00005668 MVT ResVT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005669
Craig Topper7a9a28b2012-08-12 02:23:29 +00005670 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005671
5672 SDValue V1 = Op.getOperand(0);
5673 SDValue V2 = Op.getOperand(1);
5674 unsigned NumElems = ResVT.getVectorNumElements();
5675
Craig Topper4c7972d2012-04-22 18:15:59 +00005676 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005677}
5678
Craig Topper55b24052012-09-11 06:15:32 +00005679static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005680 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005681
5682 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5683 // from two other 128-bit ones.
5684 return LowerAVXCONCAT_VECTORS(Op, DAG);
5685}
5686
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005687// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005688static SDValue
5689LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5690 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005691 SDValue V1 = SVOp->getOperand(0);
5692 SDValue V2 = SVOp->getOperand(1);
5693 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00005694 MVT VT = SVOp->getValueType(0).getSimpleVT();
5695 MVT EltVT = VT.getVectorElementType();
Craig Topper1842ba02012-04-23 06:38:28 +00005696 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005697
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005698 if (!Subtarget->hasSSE41() || EltVT == MVT::i8)
5699 return SDValue();
5700 if (!Subtarget->hasInt256() && VT == MVT::v16i16)
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005701 return SDValue();
5702
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005703 // Check the mask for BLEND and build the value.
5704 unsigned MaskValue = 0;
5705 // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
Craig Topper9b33ef72013-01-21 06:57:59 +00005706 unsigned NumLanes = (NumElems-1)/8 + 1;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005707 unsigned NumElemsInLane = NumElems / NumLanes;
Nadav Roteme6113782012-04-11 06:40:27 +00005708
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005709 // Blend for v16i16 should be symetric for the both lanes.
5710 for (unsigned i = 0; i < NumElemsInLane; ++i) {
Nadav Roteme6113782012-04-11 06:40:27 +00005711
Craig Topper9b33ef72013-01-21 06:57:59 +00005712 int SndLaneEltIdx = (NumLanes == 2) ?
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005713 SVOp->getMaskElt(i + NumElemsInLane) : -1;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005714 int EltIdx = SVOp->getMaskElt(i);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005715
Craig Topper04f74a12013-01-21 07:25:16 +00005716 if ((EltIdx < 0 || EltIdx == (int)i) &&
5717 (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane)))
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005718 continue;
5719
Craig Topper9b33ef72013-01-21 06:57:59 +00005720 if (((unsigned)EltIdx == (i + NumElems)) &&
Craig Topper04f74a12013-01-21 07:25:16 +00005721 (SndLaneEltIdx < 0 ||
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005722 (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane))
5723 MaskValue |= (1<<i);
Craig Topper9b33ef72013-01-21 06:57:59 +00005724 else
Craig Topper1842ba02012-04-23 06:38:28 +00005725 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005726 }
5727
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005728 // Convert i32 vectors to floating point if it is not AVX2.
5729 // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors.
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005730 MVT BlendVT = VT;
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005731 if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) {
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005732 BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()),
5733 NumElems);
Elena Demikhovsky226e0e62012-12-05 09:24:57 +00005734 V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1);
5735 V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2);
5736 }
Craig Topper9b33ef72013-01-21 06:57:59 +00005737
Craig Topperbbf9d3e2013-01-21 07:19:54 +00005738 SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2,
5739 DAG.getConstant(MaskValue, MVT::i32));
Nadav Roteme6113782012-04-11 06:40:27 +00005740 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005741}
5742
Nate Begemanb9a47b82009-02-23 08:49:38 +00005743// v8i16 shuffles - Prefer shuffles in the following order:
5744// 1. [all] pshuflw, pshufhw, optional move
5745// 2. [ssse3] 1 x pshufb
5746// 3. [ssse3] 2 x pshufb + 1 x por
5747// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005748static SDValue
5749LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5750 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005751 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005752 SDValue V1 = SVOp->getOperand(0);
5753 SDValue V2 = SVOp->getOperand(1);
5754 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005755 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005756
Nate Begemanb9a47b82009-02-23 08:49:38 +00005757 // Determine if more than 1 of the words in each of the low and high quadwords
5758 // of the result come from the same quadword of one of the two inputs. Undef
5759 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005760 unsigned LoQuad[] = { 0, 0, 0, 0 };
5761 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005762 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005763 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005764 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005765 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005766 MaskVals.push_back(EltIdx);
5767 if (EltIdx < 0) {
5768 ++Quad[0];
5769 ++Quad[1];
5770 ++Quad[2];
5771 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005772 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 }
5774 ++Quad[EltIdx / 4];
5775 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005776 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005777
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005779 unsigned MaxQuad = 1;
5780 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 if (LoQuad[i] > MaxQuad) {
5782 BestLoQuad = i;
5783 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005784 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005785 }
5786
Nate Begemanb9a47b82009-02-23 08:49:38 +00005787 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005788 MaxQuad = 1;
5789 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 if (HiQuad[i] > MaxQuad) {
5791 BestHiQuad = i;
5792 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005793 }
5794 }
5795
Nate Begemanb9a47b82009-02-23 08:49:38 +00005796 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005797 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 // single pshufb instruction is necessary. If There are more than 2 input
5799 // quads, disable the next transformation since it does not help SSSE3.
5800 bool V1Used = InputQuads[0] || InputQuads[1];
5801 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005802 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005804 BestLoQuad = InputQuads[0] ? 0 : 1;
5805 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005806 }
5807 if (InputQuads.count() > 2) {
5808 BestLoQuad = -1;
5809 BestHiQuad = -1;
5810 }
5811 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005812
Nate Begemanb9a47b82009-02-23 08:49:38 +00005813 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5814 // the shuffle mask. If a quad is scored as -1, that means that it contains
5815 // words from all 4 input quadwords.
5816 SDValue NewV;
5817 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005818 int MaskV[] = {
5819 BestLoQuad < 0 ? 0 : BestLoQuad,
5820 BestHiQuad < 0 ? 1 : BestHiQuad
5821 };
Eric Christopherfd179292009-08-27 18:07:15 +00005822 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005823 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5824 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5825 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005826
Nate Begemanb9a47b82009-02-23 08:49:38 +00005827 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5828 // source words for the shuffle, to aid later transformations.
5829 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005830 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005831 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005833 if (idx != (int)i)
5834 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005835 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005836 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005837 AllWordsInNewV = false;
5838 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005839 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005840
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5842 if (AllWordsInNewV) {
5843 for (int i = 0; i != 8; ++i) {
5844 int idx = MaskVals[i];
5845 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005846 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005847 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 if ((idx != i) && idx < 4)
5849 pshufhw = false;
5850 if ((idx != i) && idx > 3)
5851 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005852 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005853 V1 = NewV;
5854 V2Used = false;
5855 BestLoQuad = 0;
5856 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005857 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005858
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5860 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005861 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005862 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5863 unsigned TargetMask = 0;
5864 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005865 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005866 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5867 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5868 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005869 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005870 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005871 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005872 }
Eric Christopherfd179292009-08-27 18:07:15 +00005873
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00005874 // Promote splats to a larger type which usually leads to more efficient code.
5875 // FIXME: Is this true if pshufb is available?
5876 if (SVOp->isSplat())
5877 return PromoteSplat(SVOp, DAG);
5878
Nate Begemanb9a47b82009-02-23 08:49:38 +00005879 // If we have SSSE3, and all words of the result are from 1 input vector,
5880 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5881 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005882 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005883 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005884
Nate Begemanb9a47b82009-02-23 08:49:38 +00005885 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005886 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005887 // mask, and elements that come from V1 in the V2 mask, so that the two
5888 // results can be OR'd together.
5889 bool TwoInputs = V1Used && V2Used;
5890 for (unsigned i = 0; i != 8; ++i) {
5891 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005892 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5893 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
Craig Toppere6d8fa72013-01-18 07:27:20 +00005894 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
Craig Topperbe97ae92012-05-18 07:07:36 +00005895 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005896 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005897 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005898 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005899 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005900 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005901 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005902 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005903
Nate Begemanb9a47b82009-02-23 08:49:38 +00005904 // Calculate the shuffle mask for the second input, shuffle it, and
5905 // OR it with the first shuffled input.
5906 pshufbMask.clear();
5907 for (unsigned i = 0; i != 8; ++i) {
5908 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005909 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5910 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5911 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5912 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005913 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005914 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005915 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005916 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005917 MVT::v16i8, &pshufbMask[0], 16));
5918 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005919 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 }
5921
5922 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5923 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005924 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005926 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005927 for (int i = 0; i != 4; ++i) {
5928 int idx = MaskVals[i];
5929 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005930 InOrder.set(i);
5931 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005932 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005933 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005934 }
5935 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005937 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005938
Craig Topperdd637ae2012-02-19 05:41:45 +00005939 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5940 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005941 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005942 NewV.getOperand(0),
5943 getShufflePSHUFLWImmediate(SVOp), DAG);
5944 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005945 }
Eric Christopherfd179292009-08-27 18:07:15 +00005946
Nate Begemanb9a47b82009-02-23 08:49:38 +00005947 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5948 // and update MaskVals with the new element order.
5949 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005950 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005951 for (unsigned i = 4; i != 8; ++i) {
5952 int idx = MaskVals[i];
5953 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005954 InOrder.set(i);
5955 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005956 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005957 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005958 }
5959 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005960 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005961 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005962
Craig Topperdd637ae2012-02-19 05:41:45 +00005963 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5964 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005965 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005966 NewV.getOperand(0),
5967 getShufflePSHUFHWImmediate(SVOp), DAG);
5968 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005969 }
Eric Christopherfd179292009-08-27 18:07:15 +00005970
Nate Begemanb9a47b82009-02-23 08:49:38 +00005971 // In case BestHi & BestLo were both -1, which means each quadword has a word
5972 // from each of the four input quadwords, calculate the InOrder bitvector now
5973 // before falling through to the insert/extract cleanup.
5974 if (BestLoQuad == -1 && BestHiQuad == -1) {
5975 NewV = V1;
5976 for (int i = 0; i != 8; ++i)
5977 if (MaskVals[i] < 0 || MaskVals[i] == i)
5978 InOrder.set(i);
5979 }
Eric Christopherfd179292009-08-27 18:07:15 +00005980
Nate Begemanb9a47b82009-02-23 08:49:38 +00005981 // The other elements are put in the right place using pextrw and pinsrw.
5982 for (unsigned i = 0; i != 8; ++i) {
5983 if (InOrder[i])
5984 continue;
5985 int EltIdx = MaskVals[i];
5986 if (EltIdx < 0)
5987 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005988 SDValue ExtOp = (EltIdx < 8) ?
5989 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5990 DAG.getIntPtrConstant(EltIdx)) :
5991 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005992 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005993 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005994 DAG.getIntPtrConstant(i));
5995 }
5996 return NewV;
5997}
5998
5999// v16i8 shuffles - Prefer shuffles in the following order:
6000// 1. [ssse3] 1 x pshufb
6001// 2. [ssse3] 2 x pshufb + 1 x por
6002// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
6003static
Nate Begeman9008ca62009-04-27 18:41:29 +00006004SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00006005 SelectionDAG &DAG,
6006 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006007 SDValue V1 = SVOp->getOperand(0);
6008 SDValue V2 = SVOp->getOperand(1);
6009 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006010 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00006011
Benjamin Kramer11f2bf72013-01-26 11:44:21 +00006012 // Promote splats to a larger type which usually leads to more efficient code.
6013 // FIXME: Is this true if pshufb is available?
6014 if (SVOp->isSplat())
6015 return PromoteSplat(SVOp, DAG);
6016
Nate Begemanb9a47b82009-02-23 08:49:38 +00006017 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00006018 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00006019 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00006020
Nate Begemanb9a47b82009-02-23 08:49:38 +00006021 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00006022 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00006023 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00006024
Nate Begemanb9a47b82009-02-23 08:49:38 +00006025 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00006026 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006027 //
6028 // Otherwise, we have elements from both input vectors, and must zero out
6029 // elements that come from V2 in the first mask, and V1 in the second mask
6030 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006031 for (unsigned i = 0; i != 16; ++i) {
6032 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006033 if (EltIdx < 0 || EltIdx >= 16)
6034 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00006035 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006036 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006037 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00006038 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006039 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00006040
6041 // As PSHUFB will zero elements with negative indices, it's safe to ignore
6042 // the 2nd operand if it's undefined or zero.
6043 if (V2.getOpcode() == ISD::UNDEF ||
6044 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006045 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00006046
Nate Begemanb9a47b82009-02-23 08:49:38 +00006047 // Calculate the shuffle mask for the second input, shuffle it, and
6048 // OR it with the first shuffled input.
6049 pshufbMask.clear();
6050 for (unsigned i = 0; i != 16; ++i) {
6051 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00006052 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00006053 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006054 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006055 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00006056 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006057 MVT::v16i8, &pshufbMask[0], 16));
6058 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00006059 }
Eric Christopherfd179292009-08-27 18:07:15 +00006060
Nate Begemanb9a47b82009-02-23 08:49:38 +00006061 // No SSSE3 - Calculate in place words and then fix all out of place words
6062 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
6063 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006064 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
6065 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00006066 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00006067 for (int i = 0; i != 8; ++i) {
6068 int Elt0 = MaskVals[i*2];
6069 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00006070
Nate Begemanb9a47b82009-02-23 08:49:38 +00006071 // This word of the result is all undef, skip it.
6072 if (Elt0 < 0 && Elt1 < 0)
6073 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006074
Nate Begemanb9a47b82009-02-23 08:49:38 +00006075 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00006076 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00006077 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00006078
Nate Begemanb9a47b82009-02-23 08:49:38 +00006079 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
6080 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
6081 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00006082
6083 // If Elt0 and Elt1 are defined, are consecutive, and can be load
6084 // using a single extract together, load it and store it.
6085 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006086 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006087 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00006088 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00006089 DAG.getIntPtrConstant(i));
6090 continue;
6091 }
6092
Nate Begemanb9a47b82009-02-23 08:49:38 +00006093 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00006094 // source byte is not also odd, shift the extracted word left 8 bits
6095 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00006096 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006097 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006098 DAG.getIntPtrConstant(Elt1 / 2));
6099 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006100 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006101 DAG.getConstant(8,
6102 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006103 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006104 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6105 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006106 }
6107 // If Elt0 is defined, extract it from the appropriate source. If the
6108 // source byte is not also even, shift the extracted word right 8 bits. If
6109 // Elt1 was also defined, OR the extracted values together before
6110 // inserting them in the result.
6111 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006112 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006113 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6114 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006115 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006116 DAG.getConstant(8,
6117 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006118 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006119 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6120 DAG.getConstant(0x00FF, MVT::i16));
6121 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006122 : InsElt0;
6123 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006124 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006125 DAG.getIntPtrConstant(i));
6126 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006127 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006128}
6129
Elena Demikhovsky41789462012-09-06 12:42:01 +00006130// v32i8 shuffles - Translate to VPSHUFB if possible.
6131static
6132SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006133 const X86Subtarget *Subtarget,
6134 SelectionDAG &DAG) {
Craig Topper657a99c2013-01-19 23:36:09 +00006135 MVT VT = SVOp->getValueType(0).getSimpleVT();
Elena Demikhovsky41789462012-09-06 12:42:01 +00006136 SDValue V1 = SVOp->getOperand(0);
6137 SDValue V2 = SVOp->getOperand(1);
6138 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006139 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006140
6141 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006142 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6143 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006144
Michael Liao471b9172012-10-03 23:43:52 +00006145 // VPSHUFB may be generated if
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006146 // (1) one of input vector is undefined or zeroinitializer.
6147 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6148 // And (2) the mask indexes don't cross the 128-bit lane.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006149 if (VT != MVT::v32i8 || !Subtarget->hasInt256() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006150 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006151 return SDValue();
6152
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006153 if (V1IsAllZero && !V2IsAllZero) {
6154 CommuteVectorShuffleMask(MaskVals, 32);
6155 V1 = V2;
6156 }
6157 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006158 for (unsigned i = 0; i != 32; i++) {
6159 int EltIdx = MaskVals[i];
6160 if (EltIdx < 0 || EltIdx >= 32)
6161 EltIdx = 0x80;
6162 else {
6163 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6164 // Cross lane is not allowed.
6165 return SDValue();
6166 EltIdx &= 0xf;
6167 }
6168 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6169 }
6170 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6171 DAG.getNode(ISD::BUILD_VECTOR, dl,
6172 MVT::v32i8, &pshufbMask[0], 32));
6173}
6174
Evan Cheng7a831ce2007-12-15 03:00:47 +00006175/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006176/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006177/// done when every pair / quad of shuffle mask elements point to elements in
6178/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006179/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006180static
Nate Begeman9008ca62009-04-27 18:41:29 +00006181SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Craig Topper3b2aba02013-01-20 00:43:42 +00006182 SelectionDAG &DAG) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006183 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper3b2aba02013-01-20 00:43:42 +00006184 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006185 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006186 MVT NewVT;
6187 unsigned Scale;
6188 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006189 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006190 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6191 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6192 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6193 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6194 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6195 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006196 }
6197
Nate Begeman9008ca62009-04-27 18:41:29 +00006198 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006199 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006200 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006201 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006202 int EltIdx = SVOp->getMaskElt(i+j);
6203 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006204 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006205 if (StartIdx < 0)
6206 StartIdx = (EltIdx / Scale);
6207 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006208 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006209 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006210 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006211 }
6212
Craig Topper11ac1f82012-05-04 04:08:44 +00006213 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6214 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006215 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006216}
6217
Evan Chengd880b972008-05-09 21:53:03 +00006218/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006219///
Craig Topperf84b7502013-01-20 00:50:58 +00006220static SDValue getVZextMovL(MVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006221 SDValue SrcOp, SelectionDAG &DAG,
6222 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006223 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006224 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006225 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006226 LD = dyn_cast<LoadSDNode>(SrcOp);
6227 if (!LD) {
6228 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6229 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006230 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006231 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006232 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006233 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006234 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006235 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006236 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006237 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006238 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6239 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6240 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006241 SrcOp.getOperand(0)
6242 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006243 }
6244 }
6245 }
6246
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006247 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006248 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006249 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006250 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006251}
6252
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006253/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6254/// which could not be matched by any known target speficic shuffle
6255static SDValue
6256LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006257
6258 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6259 if (NewOp.getNode())
6260 return NewOp;
6261
Craig Topper657a99c2013-01-19 23:36:09 +00006262 MVT VT = SVOp->getValueType(0).getSimpleVT();
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006263
Craig Topper8f35c132012-01-20 09:29:03 +00006264 unsigned NumElems = VT.getVectorNumElements();
6265 unsigned NumLaneElems = NumElems / 2;
6266
Craig Topper8f35c132012-01-20 09:29:03 +00006267 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006268 MVT EltVT = VT.getVectorElementType();
6269 MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006270 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006271
Craig Topper9a2b6e12012-04-06 07:45:23 +00006272 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006273 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006274 // Build a shuffle mask for the output, discovering on the fly which
6275 // input vectors to use as shuffle operands (recorded in InputUsed).
6276 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006277 // out with UseBuildVector set.
6278 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006279 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006280 unsigned LaneStart = l * NumLaneElems;
6281 for (unsigned i = 0; i != NumLaneElems; ++i) {
6282 // The mask element. This indexes into the input.
6283 int Idx = SVOp->getMaskElt(i+LaneStart);
6284 if (Idx < 0) {
6285 // the mask element does not index into any input vector.
6286 Mask.push_back(-1);
6287 continue;
6288 }
Craig Topper8f35c132012-01-20 09:29:03 +00006289
Craig Topper9a2b6e12012-04-06 07:45:23 +00006290 // The input vector this mask element indexes into.
6291 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006292
Craig Topper9a2b6e12012-04-06 07:45:23 +00006293 // Turn the index into an offset from the start of the input vector.
6294 Idx -= Input * NumLaneElems;
6295
6296 // Find or create a shuffle vector operand to hold this input.
6297 unsigned OpNo;
6298 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6299 if (InputUsed[OpNo] == Input)
6300 // This input vector is already an operand.
6301 break;
6302 if (InputUsed[OpNo] < 0) {
6303 // Create a new operand for this input vector.
6304 InputUsed[OpNo] = Input;
6305 break;
6306 }
6307 }
6308
6309 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006310 // More than two input vectors used! Give up on trying to create a
6311 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6312 UseBuildVector = true;
6313 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006314 }
6315
6316 // Add the mask index for the new shuffle vector.
6317 Mask.push_back(Idx + OpNo * NumLaneElems);
6318 }
6319
Craig Topper8ae97ba2012-05-21 06:40:16 +00006320 if (UseBuildVector) {
6321 SmallVector<SDValue, 16> SVOps;
6322 for (unsigned i = 0; i != NumLaneElems; ++i) {
6323 // The mask element. This indexes into the input.
6324 int Idx = SVOp->getMaskElt(i+LaneStart);
6325 if (Idx < 0) {
6326 SVOps.push_back(DAG.getUNDEF(EltVT));
6327 continue;
6328 }
6329
6330 // The input vector this mask element indexes into.
6331 int Input = Idx / NumElems;
6332
6333 // Turn the index into an offset from the start of the input vector.
6334 Idx -= Input * NumElems;
6335
6336 // Extract the vector element by hand.
6337 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6338 SVOp->getOperand(Input),
6339 DAG.getIntPtrConstant(Idx)));
6340 }
6341
6342 // Construct the output using a BUILD_VECTOR.
6343 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6344 SVOps.size());
6345 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006346 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006347 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006348 } else {
6349 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006350 (InputUsed[0] % 2) * NumLaneElems,
6351 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006352 // If only one input was used, use an undefined vector for the other.
6353 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6354 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006355 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006356 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006357 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006358 }
6359
6360 Mask.clear();
6361 }
Craig Topper8f35c132012-01-20 09:29:03 +00006362
6363 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006364 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006365}
6366
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006367/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6368/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006369static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006370LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006371 SDValue V1 = SVOp->getOperand(0);
6372 SDValue V2 = SVOp->getOperand(1);
6373 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper657a99c2013-01-19 23:36:09 +00006374 MVT VT = SVOp->getValueType(0).getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00006375
Craig Topper7a9a28b2012-08-12 02:23:29 +00006376 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006377
Benjamin Kramer9c683542012-01-30 15:16:21 +00006378 std::pair<int, int> Locs[4];
6379 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006380 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006381
Evan Chengace3c172008-07-22 21:13:36 +00006382 unsigned NumHi = 0;
6383 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006384 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006385 int Idx = PermMask[i];
6386 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006387 Locs[i] = std::make_pair(-1, -1);
6388 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006389 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6390 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006391 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006392 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006393 NumLo++;
6394 } else {
6395 Locs[i] = std::make_pair(1, NumHi);
6396 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006397 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006398 NumHi++;
6399 }
6400 }
6401 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006402
Evan Chengace3c172008-07-22 21:13:36 +00006403 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006404 // If no more than two elements come from either vector. This can be
6405 // implemented with two shuffles. First shuffle gather the elements.
6406 // The second shuffle, which takes the first shuffle as both of its
6407 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006408 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006409
Benjamin Kramer9c683542012-01-30 15:16:21 +00006410 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006411
Benjamin Kramer9c683542012-01-30 15:16:21 +00006412 for (unsigned i = 0; i != 4; ++i)
6413 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006414 unsigned Idx = (i < 2) ? 0 : 4;
6415 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006416 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006417 }
Evan Chengace3c172008-07-22 21:13:36 +00006418
Nate Begeman9008ca62009-04-27 18:41:29 +00006419 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006420 }
6421
6422 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006423 // Otherwise, we must have three elements from one vector, call it X, and
6424 // one element from the other, call it Y. First, use a shufps to build an
6425 // intermediate vector with the one element from Y and the element from X
6426 // that will be in the same half in the final destination (the indexes don't
6427 // matter). Then, use a shufps to build the final vector, taking the half
6428 // containing the element from Y from the intermediate, and the other half
6429 // from X.
6430 if (NumHi == 3) {
6431 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006432 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006433 std::swap(V1, V2);
6434 }
6435
6436 // Find the element from V2.
6437 unsigned HiIndex;
6438 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006439 int Val = PermMask[HiIndex];
6440 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006441 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006442 if (Val >= 4)
6443 break;
6444 }
6445
Nate Begeman9008ca62009-04-27 18:41:29 +00006446 Mask1[0] = PermMask[HiIndex];
6447 Mask1[1] = -1;
6448 Mask1[2] = PermMask[HiIndex^1];
6449 Mask1[3] = -1;
6450 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006451
6452 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006453 Mask1[0] = PermMask[0];
6454 Mask1[1] = PermMask[1];
6455 Mask1[2] = HiIndex & 1 ? 6 : 4;
6456 Mask1[3] = HiIndex & 1 ? 4 : 6;
6457 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006458 }
Craig Topper69947b92012-04-23 06:57:04 +00006459
6460 Mask1[0] = HiIndex & 1 ? 2 : 0;
6461 Mask1[1] = HiIndex & 1 ? 0 : 2;
6462 Mask1[2] = PermMask[2];
6463 Mask1[3] = PermMask[3];
6464 if (Mask1[2] >= 0)
6465 Mask1[2] += 4;
6466 if (Mask1[3] >= 0)
6467 Mask1[3] += 4;
6468 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006469 }
6470
6471 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006472 int LoMask[] = { -1, -1, -1, -1 };
6473 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006474
Benjamin Kramer9c683542012-01-30 15:16:21 +00006475 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006476 unsigned MaskIdx = 0;
6477 unsigned LoIdx = 0;
6478 unsigned HiIdx = 2;
6479 for (unsigned i = 0; i != 4; ++i) {
6480 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006481 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006482 MaskIdx = 1;
6483 LoIdx = 0;
6484 HiIdx = 2;
6485 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006486 int Idx = PermMask[i];
6487 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006488 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006489 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006490 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006491 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006492 LoIdx++;
6493 } else {
6494 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006495 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006496 HiIdx++;
6497 }
6498 }
6499
Nate Begeman9008ca62009-04-27 18:41:29 +00006500 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6501 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006502 int MaskOps[] = { -1, -1, -1, -1 };
6503 for (unsigned i = 0; i != 4; ++i)
6504 if (Locs[i].first != -1)
6505 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006506 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006507}
6508
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006509static bool MayFoldVectorLoad(SDValue V) {
Jakub Staszaka24262a2012-10-30 00:01:57 +00006510 while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006511 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006512
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006513 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6514 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006515 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6516 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6517 // BUILD_VECTOR (load), undef
6518 V = V.getOperand(0);
Jakub Staszaka24262a2012-10-30 00:01:57 +00006519
6520 return MayFoldLoad(V);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006521}
6522
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006523static
Evan Cheng835580f2010-10-07 20:50:20 +00006524SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6525 EVT VT = Op.getValueType();
6526
6527 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006528 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6529 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006530 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6531 V1, DAG));
6532}
6533
6534static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006535SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006536 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006537 SDValue V1 = Op.getOperand(0);
6538 SDValue V2 = Op.getOperand(1);
6539 EVT VT = Op.getValueType();
6540
6541 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6542
Craig Topper1accb7e2012-01-10 06:54:16 +00006543 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006544 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6545
Evan Cheng0899f5c2011-08-31 02:05:24 +00006546 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6547 return DAG.getNode(ISD::BITCAST, dl, VT,
6548 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6549 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6550 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006551}
6552
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006553static
6554SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6555 SDValue V1 = Op.getOperand(0);
6556 SDValue V2 = Op.getOperand(1);
6557 EVT VT = Op.getValueType();
6558
6559 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6560 "unsupported shuffle type");
6561
6562 if (V2.getOpcode() == ISD::UNDEF)
6563 V2 = V1;
6564
6565 // v4i32 or v4f32
6566 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6567}
6568
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006569static
Craig Topper1accb7e2012-01-10 06:54:16 +00006570SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006571 SDValue V1 = Op.getOperand(0);
6572 SDValue V2 = Op.getOperand(1);
6573 EVT VT = Op.getValueType();
6574 unsigned NumElems = VT.getVectorNumElements();
6575
6576 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6577 // operand of these instructions is only memory, so check if there's a
6578 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6579 // same masks.
6580 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006581
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006582 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006583 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006584 CanFoldLoad = true;
6585
6586 // When V1 is a load, it can be folded later into a store in isel, example:
6587 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6588 // turns into:
6589 // (MOVLPSmr addr:$src1, VR128:$src2)
6590 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006591 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006592 CanFoldLoad = true;
6593
Dan Gohman65fd6562011-11-03 21:49:52 +00006594 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006595 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006596 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006597 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6598
6599 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006600 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006601 if (SVOp->getMaskElt(1) != -1)
6602 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006603 }
6604
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006605 // movl and movlp will both match v2i64, but v2i64 is never matched by
6606 // movl earlier because we make it strict to avoid messing with the movlp load
6607 // folding logic (see the code above getMOVLP call). Match it here then,
6608 // this is horrible, but will stay like this until we move all shuffle
6609 // matching to x86 specific nodes. Note that for the 1st condition all
6610 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006611 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006612 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6613 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006614 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006615 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006616 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006617 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006618
6619 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6620
6621 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006622 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006623 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006624}
6625
Michael Liaod9d09602012-10-23 17:34:00 +00006626// Reduce a vector shuffle to zext.
6627SDValue
Craig Topper00a312c2013-01-19 23:14:09 +00006628X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const {
Michael Liaod9d09602012-10-23 17:34:00 +00006629 // PMOVZX is only available from SSE41.
6630 if (!Subtarget->hasSSE41())
6631 return SDValue();
6632
6633 EVT VT = Op.getValueType();
6634
6635 // Only AVX2 support 256-bit vector integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006636 if (!Subtarget->hasInt256() && VT.is256BitVector())
Michael Liaod9d09602012-10-23 17:34:00 +00006637 return SDValue();
6638
6639 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6640 DebugLoc DL = Op.getDebugLoc();
6641 SDValue V1 = Op.getOperand(0);
6642 SDValue V2 = Op.getOperand(1);
6643 unsigned NumElems = VT.getVectorNumElements();
6644
6645 // Extending is an unary operation and the element type of the source vector
6646 // won't be equal to or larger than i64.
6647 if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() ||
6648 VT.getVectorElementType() == MVT::i64)
6649 return SDValue();
6650
6651 // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4.
6652 unsigned Shift = 1; // Start from 2, i.e. 1 << 1.
Duncan Sands34739052012-10-29 11:29:53 +00006653 while ((1U << Shift) < NumElems) {
6654 if (SVOp->getMaskElt(1U << Shift) == 1)
Michael Liaod9d09602012-10-23 17:34:00 +00006655 break;
6656 Shift += 1;
6657 // The maximal ratio is 8, i.e. from i8 to i64.
6658 if (Shift > 3)
6659 return SDValue();
6660 }
6661
6662 // Check the shuffle mask.
6663 unsigned Mask = (1U << Shift) - 1;
6664 for (unsigned i = 0; i != NumElems; ++i) {
6665 int EltIdx = SVOp->getMaskElt(i);
6666 if ((i & Mask) != 0 && EltIdx != -1)
6667 return SDValue();
Matt Beaumont-Gaya999de02012-10-23 19:46:36 +00006668 if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift))
Michael Liaod9d09602012-10-23 17:34:00 +00006669 return SDValue();
6670 }
6671
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006672 LLVMContext *Context = DAG.getContext();
Michael Liaod9d09602012-10-23 17:34:00 +00006673 unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift;
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006674 EVT NeVT = EVT::getIntegerVT(*Context, NBits);
6675 EVT NVT = EVT::getVectorVT(*Context, NeVT, NumElems >> Shift);
Michael Liaod9d09602012-10-23 17:34:00 +00006676
6677 if (!isTypeLegal(NVT))
6678 return SDValue();
6679
6680 // Simplify the operand as it's prepared to be fed into shuffle.
6681 unsigned SignificantBits = NVT.getSizeInBits() >> Shift;
6682 if (V1.getOpcode() == ISD::BITCAST &&
6683 V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
6684 V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
6685 V1.getOperand(0)
6686 .getOperand(0).getValueType().getSizeInBits() == SignificantBits) {
6687 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
6688 SDValue V = V1.getOperand(0).getOperand(0).getOperand(0);
Michael Liao07872742012-10-23 21:40:15 +00006689 ConstantSDNode *CIdx =
6690 dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1));
Michael Liaod9d09602012-10-23 17:34:00 +00006691 // If it's foldable, i.e. normal load with single use, we will let code
6692 // selection to fold it. Otherwise, we will short the conversion sequence.
Michael Liao07872742012-10-23 21:40:15 +00006693 if (CIdx && CIdx->getZExtValue() == 0 &&
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006694 (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse())) {
6695 if (V.getValueSizeInBits() > V1.getValueSizeInBits()) {
6696 // The "ext_vec_elt" node is wider than the result node.
6697 // In this case we should extract subvector from V.
6698 // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast (extract_subvector x)).
6699 unsigned Ratio = V.getValueSizeInBits() / V1.getValueSizeInBits();
6700 EVT FullVT = V.getValueType();
6701 EVT SubVecVT = EVT::getVectorVT(*Context,
6702 FullVT.getVectorElementType(),
6703 FullVT.getVectorNumElements()/Ratio);
6704 V = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, V,
6705 DAG.getIntPtrConstant(0));
6706 }
Michael Liaod9d09602012-10-23 17:34:00 +00006707 V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V);
Elena Demikhovsky60b3e182013-02-14 08:20:26 +00006708 }
Michael Liaod9d09602012-10-23 17:34:00 +00006709 }
6710
6711 return DAG.getNode(ISD::BITCAST, DL, VT,
6712 DAG.getNode(X86ISD::VZEXT, DL, NVT, V1));
6713}
6714
Nadav Rotem154819d2012-04-09 07:45:58 +00006715SDValue
6716X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006717 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Craig Topper657a99c2013-01-19 23:36:09 +00006718 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006719 DebugLoc dl = Op.getDebugLoc();
6720 SDValue V1 = Op.getOperand(0);
6721 SDValue V2 = Op.getOperand(1);
6722
6723 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006724 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006725
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006726 // Handle splat operations
6727 if (SVOp->isSplat()) {
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006728 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006729 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006730 if (Broadcast.getNode())
6731 return Broadcast;
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006732 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006733
Michael Liaod9d09602012-10-23 17:34:00 +00006734 // Check integer expanding shuffles.
Craig Topper00a312c2013-01-19 23:14:09 +00006735 SDValue NewOp = LowerVectorIntExtend(Op, DAG);
Michael Liaod9d09602012-10-23 17:34:00 +00006736 if (NewOp.getNode())
6737 return NewOp;
6738
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006739 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6740 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006741 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6742 VT == MVT::v16i16 || VT == MVT::v32i8) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006743 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006744 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006745 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006746 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006747 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006748 // FIXME: Figure out a cleaner way to do this.
6749 // Try to make use of movq to zero out the top part.
6750 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006751 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006752 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006753 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006754 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6755 NewVT, true, false))
6756 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006757 DAG, Subtarget, dl);
6758 }
6759 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Craig Topper3b2aba02013-01-20 00:43:42 +00006760 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006761 if (NewOp.getNode()) {
Craig Topper657a99c2013-01-19 23:36:09 +00006762 MVT NewVT = NewOp.getValueType().getSimpleVT();
Craig Topper5aaffa82012-02-19 02:53:47 +00006763 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6764 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6765 DAG, Subtarget, dl);
6766 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006767 }
6768 }
6769 return SDValue();
6770}
6771
Dan Gohman475871a2008-07-27 21:46:04 +00006772SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006773X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006774 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006775 SDValue V1 = Op.getOperand(0);
6776 SDValue V2 = Op.getOperand(1);
Craig Topper657a99c2013-01-19 23:36:09 +00006777 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006778 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006779 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006780 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006781 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006782 bool V1IsSplat = false;
6783 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006784 bool HasSSE2 = Subtarget->hasSSE2();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006785 bool HasFp256 = Subtarget->hasFp256();
6786 bool HasInt256 = Subtarget->hasInt256();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006787 MachineFunction &MF = DAG.getMachineFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00006788 bool OptForSize = MF.getFunction()->getAttributes().
6789 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006790
Craig Topper3426a3e2011-11-14 06:46:21 +00006791 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006792
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006793 if (V1IsUndef && V2IsUndef)
6794 return DAG.getUNDEF(VT);
6795
6796 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006797
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006798 // Vector shuffle lowering takes 3 steps:
6799 //
6800 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6801 // narrowing and commutation of operands should be handled.
6802 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6803 // shuffle nodes.
6804 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6805 // so the shuffle can be broken into other shuffles and the legalizer can
6806 // try the lowering again.
6807 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006808 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006809 // be matched during isel, all of them must be converted to a target specific
6810 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006811
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006812 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6813 // narrowing and commutation of operands should be handled. The actual code
6814 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006815 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006816 if (NewOp.getNode())
6817 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006818
Craig Topper5aaffa82012-02-19 02:53:47 +00006819 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6820
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006821 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6822 // unpckh_undef). Only use pshufd if speed is more important than size.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006823 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006824 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006825 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006826 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006827
Craig Topperdd637ae2012-02-19 05:41:45 +00006828 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Jakub Staszakd3a05632012-12-06 19:05:46 +00006829 V2IsUndef && MayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006830 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006831
Craig Topperdd637ae2012-02-19 05:41:45 +00006832 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006833 return getMOVHighToLow(Op, dl, DAG);
6834
6835 // Use to match splats
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006836 if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006837 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006838 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006839
Craig Topper5aaffa82012-02-19 02:53:47 +00006840 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006841 // The actual implementation will match the mask in the if above and then
6842 // during isel it can match several different instructions, not only pshufd
6843 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006844 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6845 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006846
Craig Topper5aaffa82012-02-19 02:53:47 +00006847 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006848
Craig Topper1accb7e2012-01-10 06:54:16 +00006849 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006850 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6851
Nadav Roteme4ccfef2012-12-07 19:01:13 +00006852 if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64))
6853 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask,
6854 DAG);
6855
Craig Topperb3982da2011-12-31 23:50:21 +00006856 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006857 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006858 }
Eric Christopherfd179292009-08-27 18:07:15 +00006859
Evan Chengf26ffe92008-05-29 08:22:04 +00006860 // Check if this can be converted into a logical shift.
6861 bool isLeft = false;
6862 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006863 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006864 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006865 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006866 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006867 // v_set0 + movlhps or movhlps, etc.
Craig Topper657a99c2013-01-19 23:36:09 +00006868 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006869 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006870 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006871 }
Eric Christopherfd179292009-08-27 18:07:15 +00006872
Craig Topper5aaffa82012-02-19 02:53:47 +00006873 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006874 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006875 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006876 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006877 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006878 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6879
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006880 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006881 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6882 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006883 }
Eric Christopherfd179292009-08-27 18:07:15 +00006884
Nate Begeman9008ca62009-04-27 18:41:29 +00006885 // FIXME: fold these into legal mask.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006886 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256))
Craig Topper1accb7e2012-01-10 06:54:16 +00006887 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006888
Craig Topperdd637ae2012-02-19 05:41:45 +00006889 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006890 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006891
Craig Topperdd637ae2012-02-19 05:41:45 +00006892 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006893 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006894
Craig Topperdd637ae2012-02-19 05:41:45 +00006895 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006896 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006897
Craig Topperdd637ae2012-02-19 05:41:45 +00006898 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006899 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006900
Craig Topperdd637ae2012-02-19 05:41:45 +00006901 if (ShouldXformToMOVHLPS(M, VT) ||
6902 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006903 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006904
Evan Chengf26ffe92008-05-29 08:22:04 +00006905 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006906 // No better options. Use a vshldq / vsrldq.
Craig Topper657a99c2013-01-19 23:36:09 +00006907 MVT EltVT = VT.getVectorElementType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00006908 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006909 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006910 }
Eric Christopherfd179292009-08-27 18:07:15 +00006911
Evan Cheng9eca5e82006-10-25 21:49:50 +00006912 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006913 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6914 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006915 V1IsSplat = isSplatVector(V1.getNode());
6916 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006917
Chris Lattner8a594482007-11-25 00:24:49 +00006918 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006919 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6920 CommuteVectorShuffleMask(M, NumElems);
6921 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006922 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006923 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006924 }
6925
Craig Topperbeabc6c2011-12-05 06:56:46 +00006926 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006927 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006928 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006929 return V1;
6930 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6931 // the instruction selector will not match, so get a canonical MOVL with
6932 // swapped operands to undo the commute.
6933 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006934 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006935
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006936 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006937 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006938
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006939 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00006940 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006941
Evan Cheng9bbbb982006-10-25 20:48:19 +00006942 if (V2IsSplat) {
6943 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006944 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006945 // new vector_shuffle with the corrected mask.p
6946 SmallVector<int, 8> NewMask(M.begin(), M.end());
6947 NormalizeMask(NewMask, NumElems);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006948 if (isUNPCKLMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006949 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006950 if (isUNPCKHMask(NewMask, VT, HasInt256, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006951 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006952 }
6953
Evan Cheng9eca5e82006-10-25 21:49:50 +00006954 if (Commuted) {
6955 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006956 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006957 CommuteVectorShuffleMask(M, NumElems);
6958 std::swap(V1, V2);
6959 std::swap(V1IsSplat, V2IsSplat);
6960 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006961
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006962 if (isUNPCKLMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006963 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006964
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006965 if (isUNPCKHMask(M, VT, HasInt256))
Craig Topper39a9e482012-02-11 06:24:48 +00006966 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006967 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006968
Nate Begeman9008ca62009-04-27 18:41:29 +00006969 // Normalize the node to match x86 shuffle ops if needed
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006970 if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006971 return CommuteVectorShuffle(SVOp, DAG);
6972
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006973 // The checks below are all present in isShuffleMaskLegal, but they are
6974 // inlined here right now to enable us to directly emit target specific
6975 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006976
Craig Topper0e2037b2012-01-20 05:53:00 +00006977 if (isPALIGNRMask(M, VT, Subtarget))
Craig Topper4aee1bb2013-01-28 06:48:25 +00006978 return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006979 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006980 DAG);
6981
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006982 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6983 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006984 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006985 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006986 }
6987
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006988 if (isPSHUFHWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006989 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006990 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006991 DAG);
6992
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006993 if (isPSHUFLWMask(M, VT, HasInt256))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006994 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006995 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006996 DAG);
6997
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00006998 if (isSHUFPMask(M, VT, HasFp256))
Craig Topperb3982da2011-12-31 23:50:21 +00006999 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00007000 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00007001
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007002 if (isUNPCKL_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007003 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007004 if (isUNPCKH_v_undef_Mask(M, VT, HasInt256))
Craig Topper34671b82011-12-06 08:21:25 +00007005 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00007006
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007007 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007008 // Generate target specific nodes for 128 or 256-bit shuffles only
7009 // supported in the AVX instruction set.
7010 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007011
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007012 // Handle VMOVDDUPY permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007013 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00007014 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
7015
Craig Topper70b883b2011-11-28 10:14:51 +00007016 // Handle VPERMILPS/D* permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007017 if (isVPERMILPMask(M, VT, HasFp256)) {
7018 if (HasInt256 && VT == MVT::v8i32)
Craig Topperdbd98a42012-02-07 06:28:42 +00007019 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007020 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00007021 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00007022 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00007023 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007024
Craig Topper70b883b2011-11-28 10:14:51 +00007025 // Handle VPERM2F128/VPERM2I128 permutations
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007026 if (isVPERM2X128Mask(M, VT, HasFp256))
Craig Topperec24e612011-11-30 07:47:51 +00007027 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00007028 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007029
Craig Topper1842ba02012-04-23 06:38:28 +00007030 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00007031 if (BlendOp.getNode())
7032 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00007033
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007034 if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00007035 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007036 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00007037 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007038 }
Craig Topper92040742012-04-16 06:43:40 +00007039 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
7040 &permclMask[0], 8);
7041 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00007042 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00007043 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007044 }
Craig Topper095c5282012-04-15 23:48:57 +00007045
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007046 if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64))
Craig Topper8325c112012-04-16 00:41:45 +00007047 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00007048 getShuffleCLImmediate(SVOp), DAG);
7049
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00007050 //===--------------------------------------------------------------------===//
7051 // Since no target specific shuffle was selected for this generic one,
7052 // lower it into other known shuffles. FIXME: this isn't true yet, but
7053 // this is the plan.
7054 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00007055
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007056 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
7057 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00007058 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007059 if (NewOp.getNode())
7060 return NewOp;
7061 }
7062
7063 if (VT == MVT::v16i8) {
7064 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
7065 if (NewOp.getNode())
7066 return NewOp;
7067 }
7068
Elena Demikhovsky41789462012-09-06 12:42:01 +00007069 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00007070 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00007071 if (NewOp.getNode())
7072 return NewOp;
7073 }
7074
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007075 // Handle all 128-bit wide vectors with 4 elements, and match them with
7076 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007077 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00007078 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
7079
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00007080 // Handle general 256-bit shuffles
7081 if (VT.is256BitVector())
7082 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
7083
Dan Gohman475871a2008-07-27 21:46:04 +00007084 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007085}
7086
Craig Topperf84b7502013-01-20 00:50:58 +00007087static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007088 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007089 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007090
Craig Topper45e1c752013-01-20 00:38:18 +00007091 if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007092 return SDValue();
7093
Duncan Sands83ec4b62008-06-06 12:08:01 +00007094 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007095 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007096 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007097 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007098 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007099 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007100 }
7101
7102 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00007103 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
7104 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
7105 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007106 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7107 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007108 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007109 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00007110 Op.getOperand(0)),
7111 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00007112 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00007113 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007114 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007115 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007116 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007117 }
7118
7119 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00007120 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
7121 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007122 // result has a single use which is a store or a bitcast to i32. And in
7123 // the case of a store, it's not worth it if the index is a constant 0,
7124 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00007125 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00007126 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007127 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00007128 if ((User->getOpcode() != ISD::STORE ||
7129 (isa<ConstantSDNode>(Op.getOperand(1)) &&
7130 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007131 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007132 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00007133 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007135 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00007136 Op.getOperand(0)),
7137 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007138 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00007139 }
7140
7141 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00007142 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00007143 if (isa<ConstantSDNode>(Op.getOperand(1)))
7144 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007145 }
Dan Gohman475871a2008-07-27 21:46:04 +00007146 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007147}
7148
Dan Gohman475871a2008-07-27 21:46:04 +00007149SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007150X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
7151 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007152 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00007153 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154
David Greene74a579d2011-02-10 16:57:36 +00007155 SDValue Vec = Op.getOperand(0);
Craig Topper45e1c752013-01-20 00:38:18 +00007156 MVT VecVT = Vec.getValueType().getSimpleVT();
David Greene74a579d2011-02-10 16:57:36 +00007157
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007158 // If this is a 256-bit vector result, first extract the 128-bit vector and
7159 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007160 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00007161 DebugLoc dl = Op.getNode()->getDebugLoc();
7162 unsigned NumElems = VecVT.getVectorNumElements();
7163 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00007164 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7165
7166 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007167 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007168
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007169 if (IdxVal >= NumElems/2)
7170 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007171 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007172 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007173 }
7174
Craig Topper7a9a28b2012-08-12 02:23:29 +00007175 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007176
Craig Topperd0a31172012-01-10 06:37:29 +00007177 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007178 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007179 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007180 return Res;
7181 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007182
Craig Topper45e1c752013-01-20 00:38:18 +00007183 MVT VT = Op.getValueType().getSimpleVT();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007184 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007185 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007186 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007187 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007188 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007189 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007190 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7191 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007192 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007193 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007194 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007195 // Transform it so it match pextrw which produces a 32-bit result.
Craig Topper45e1c752013-01-20 00:38:18 +00007196 MVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007197 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007198 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007199 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007200 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007201 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007202 }
7203
7204 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007205 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007206 if (Idx == 0)
7207 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007208
Evan Cheng0db9fe62006-04-25 20:13:52 +00007209 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007210 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007211 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007212 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007213 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007214 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007215 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007216 }
7217
7218 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007219 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7220 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7221 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007222 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007223 if (Idx == 0)
7224 return Op;
7225
7226 // UNPCKHPD the element to the lowest double word, then movsd.
7227 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7228 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007229 int Mask[2] = { 1, -1 };
Craig Topper45e1c752013-01-20 00:38:18 +00007230 MVT VVT = Op.getOperand(0).getValueType().getSimpleVT();
Eric Christopherfd179292009-08-27 18:07:15 +00007231 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007232 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007233 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007234 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007235 }
7236
Dan Gohman475871a2008-07-27 21:46:04 +00007237 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007238}
7239
Craig Topperf84b7502013-01-20 00:50:58 +00007240static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
Craig Topper45e1c752013-01-20 00:38:18 +00007241 MVT VT = Op.getValueType().getSimpleVT();
7242 MVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007243 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007244
Dan Gohman475871a2008-07-27 21:46:04 +00007245 SDValue N0 = Op.getOperand(0);
7246 SDValue N1 = Op.getOperand(1);
7247 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007248
Craig Topper7a9a28b2012-08-12 02:23:29 +00007249 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007250 return SDValue();
7251
Dan Gohman8a55ce42009-09-23 21:02:20 +00007252 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007253 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007254 unsigned Opc;
7255 if (VT == MVT::v8i16)
7256 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007257 else if (VT == MVT::v16i8)
7258 Opc = X86ISD::PINSRB;
7259 else
7260 Opc = X86ISD::PINSRB;
7261
Nate Begeman14d12ca2008-02-11 04:19:36 +00007262 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7263 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007264 if (N1.getValueType() != MVT::i32)
7265 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7266 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007267 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007268 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007269 }
7270
7271 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007272 // Bits [7:6] of the constant are the source select. This will always be
7273 // zero here. The DAG Combiner may combine an extract_elt index into these
7274 // bits. For example (insert (extract, 3), 2) could be matched by putting
7275 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007276 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007277 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007278 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007279 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007280 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007281 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007282 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007283 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007284 }
7285
7286 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007287 // PINSR* works with constant index.
7288 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007289 }
Dan Gohman475871a2008-07-27 21:46:04 +00007290 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007291}
7292
Dan Gohman475871a2008-07-27 21:46:04 +00007293SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007294X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Craig Topper45e1c752013-01-20 00:38:18 +00007295 MVT VT = Op.getValueType().getSimpleVT();
7296 MVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007297
David Greene6b381262011-02-09 15:32:06 +00007298 DebugLoc dl = Op.getDebugLoc();
7299 SDValue N0 = Op.getOperand(0);
7300 SDValue N1 = Op.getOperand(1);
7301 SDValue N2 = Op.getOperand(2);
7302
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007303 // If this is a 256-bit vector result, first extract the 128-bit vector,
7304 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007305 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007306 if (!isa<ConstantSDNode>(N2))
7307 return SDValue();
7308
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007309 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007310 unsigned NumElems = VT.getVectorNumElements();
7311 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007312 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007313
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007314 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007315 bool Upper = IdxVal >= NumElems/2;
7316 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7317 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007318
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007319 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007320 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007321 }
7322
Craig Topperd0a31172012-01-10 06:37:29 +00007323 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007324 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7325
Dan Gohman8a55ce42009-09-23 21:02:20 +00007326 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007327 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007328
Dan Gohman8a55ce42009-09-23 21:02:20 +00007329 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007330 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7331 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007332 if (N1.getValueType() != MVT::i32)
7333 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7334 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007335 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007336 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007337 }
Dan Gohman475871a2008-07-27 21:46:04 +00007338 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007339}
7340
Craig Topper55b24052012-09-11 06:15:32 +00007341static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007342 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007343 DebugLoc dl = Op.getDebugLoc();
Craig Topper45e1c752013-01-20 00:38:18 +00007344 MVT OpVT = Op.getValueType().getSimpleVT();
David Greene2fcdfb42011-02-10 23:11:29 +00007345
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007346 // If this is a 256-bit vector result, first insert into a 128-bit
7347 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007348 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007349 // Insert into a 128-bit vector.
7350 EVT VT128 = EVT::getVectorVT(*Context,
7351 OpVT.getVectorElementType(),
7352 OpVT.getVectorNumElements() / 2);
7353
7354 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7355
7356 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007357 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007358 }
7359
Craig Topperd77d2fe2012-04-29 20:22:05 +00007360 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007361 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007362 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007363
Owen Anderson825b72b2009-08-11 20:47:22 +00007364 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007365 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007366 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007367 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007368}
7369
David Greene91585092011-01-26 15:38:49 +00007370// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7371// a simple subregister reference or explicit instructions to grab
7372// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007373static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7374 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007375 if (Subtarget->hasFp256()) {
David Greenea5f26012011-02-07 19:36:54 +00007376 DebugLoc dl = Op.getNode()->getDebugLoc();
7377 SDValue Vec = Op.getNode()->getOperand(0);
7378 SDValue Idx = Op.getNode()->getOperand(1);
7379
Craig Topper7a9a28b2012-08-12 02:23:29 +00007380 if (Op.getNode()->getValueType(0).is128BitVector() &&
7381 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007382 isa<ConstantSDNode>(Idx)) {
7383 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7384 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007385 }
David Greene91585092011-01-26 15:38:49 +00007386 }
7387 return SDValue();
7388}
7389
David Greenecfe33c42011-01-26 19:13:22 +00007390// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7391// simple superregister reference or explicit instructions to insert
7392// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007393static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7394 SelectionDAG &DAG) {
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00007395 if (Subtarget->hasFp256()) {
David Greenecfe33c42011-01-26 19:13:22 +00007396 DebugLoc dl = Op.getNode()->getDebugLoc();
7397 SDValue Vec = Op.getNode()->getOperand(0);
7398 SDValue SubVec = Op.getNode()->getOperand(1);
7399 SDValue Idx = Op.getNode()->getOperand(2);
7400
Craig Topper7a9a28b2012-08-12 02:23:29 +00007401 if (Op.getNode()->getValueType(0).is256BitVector() &&
7402 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007403 isa<ConstantSDNode>(Idx)) {
7404 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7405 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007406 }
7407 }
7408 return SDValue();
7409}
7410
Bill Wendling056292f2008-09-16 21:48:12 +00007411// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7412// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7413// one of the above mentioned nodes. It has to be wrapped because otherwise
7414// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7415// be used to form addressing mode. These wrapped nodes will be selected
7416// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007417SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007418X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007419 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007420
Chris Lattner41621a22009-06-26 19:22:52 +00007421 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7422 // global base reg.
7423 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007424 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007425 CodeModel::Model M = getTargetMachine().getCodeModel();
7426
Chris Lattner4f066492009-07-11 20:29:19 +00007427 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007428 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007429 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007430 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007431 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007432 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007433 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007434
Evan Cheng1606e8e2009-03-13 07:51:59 +00007435 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007436 CP->getAlignment(),
7437 CP->getOffset(), OpFlag);
7438 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007439 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007440 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007441 if (OpFlag) {
7442 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007443 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007444 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007445 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007446 }
7447
7448 return Result;
7449}
7450
Dan Gohmand858e902010-04-17 15:26:15 +00007451SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007452 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007453
Chris Lattner18c59872009-06-27 04:16:01 +00007454 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7455 // global base reg.
7456 unsigned char OpFlag = 0;
7457 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007458 CodeModel::Model M = getTargetMachine().getCodeModel();
7459
Chris Lattner4f066492009-07-11 20:29:19 +00007460 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007461 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007462 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007463 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007464 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007465 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007466 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007467
Chris Lattner18c59872009-06-27 04:16:01 +00007468 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7469 OpFlag);
7470 DebugLoc DL = JT->getDebugLoc();
7471 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007472
Chris Lattner18c59872009-06-27 04:16:01 +00007473 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007474 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007475 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7476 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007477 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007478 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007479
Chris Lattner18c59872009-06-27 04:16:01 +00007480 return Result;
7481}
7482
7483SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007484X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007485 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007486
Chris Lattner18c59872009-06-27 04:16:01 +00007487 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7488 // global base reg.
7489 unsigned char OpFlag = 0;
7490 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007491 CodeModel::Model M = getTargetMachine().getCodeModel();
7492
Chris Lattner4f066492009-07-11 20:29:19 +00007493 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007494 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7495 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7496 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007497 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007498 } else if (Subtarget->isPICStyleGOT()) {
7499 OpFlag = X86II::MO_GOT;
7500 } else if (Subtarget->isPICStyleStubPIC()) {
7501 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7502 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7503 OpFlag = X86II::MO_DARWIN_NONLAZY;
7504 }
Eric Christopherfd179292009-08-27 18:07:15 +00007505
Chris Lattner18c59872009-06-27 04:16:01 +00007506 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007507
Chris Lattner18c59872009-06-27 04:16:01 +00007508 DebugLoc DL = Op.getDebugLoc();
7509 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007510
Chris Lattner18c59872009-06-27 04:16:01 +00007511 // With PIC, the address is actually $g + Offset.
7512 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007513 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007514 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7515 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007516 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007517 Result);
7518 }
Eric Christopherfd179292009-08-27 18:07:15 +00007519
Eli Friedman586272d2011-08-11 01:48:05 +00007520 // For symbols that require a load from a stub to get the address, emit the
7521 // load.
7522 if (isGlobalStubReference(OpFlag))
7523 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007524 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007525
Chris Lattner18c59872009-06-27 04:16:01 +00007526 return Result;
7527}
7528
Dan Gohman475871a2008-07-27 21:46:04 +00007529SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007530X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007531 // Create the TargetBlockAddressAddress node.
7532 unsigned char OpFlags =
7533 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007534 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007535 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007536 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007537 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007538 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7539 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007540
Dan Gohmanf705adb2009-10-30 01:28:02 +00007541 if (Subtarget->isPICStyleRIPRel() &&
7542 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007543 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7544 else
7545 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007546
Dan Gohman29cbade2009-11-20 23:18:13 +00007547 // With PIC, the address is actually $g + Offset.
7548 if (isGlobalRelativeToPICBase(OpFlags)) {
7549 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7550 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7551 Result);
7552 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007553
7554 return Result;
7555}
7556
7557SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007558X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Craig Topperb99bafe2013-01-21 06:21:54 +00007559 int64_t Offset, SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007560 // Create the TargetGlobalAddress node, folding in the constant
7561 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007562 unsigned char OpFlags =
7563 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007564 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007565 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007566 if (OpFlags == X86II::MO_NO_FLAG &&
7567 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007568 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007569 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007570 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007571 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007572 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007573 }
Eric Christopherfd179292009-08-27 18:07:15 +00007574
Chris Lattner4f066492009-07-11 20:29:19 +00007575 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007576 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007577 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7578 else
7579 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007580
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007581 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007582 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007583 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7584 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007585 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007586 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007587
Chris Lattner36c25012009-07-10 07:34:39 +00007588 // For globals that require a load from a stub to get the address, emit the
7589 // load.
7590 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007591 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007592 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007593
Dan Gohman6520e202008-10-18 02:06:02 +00007594 // If there was a non-zero offset that we didn't fold, create an explicit
7595 // addition for it.
7596 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007597 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007598 DAG.getConstant(Offset, getPointerTy()));
7599
Evan Cheng0db9fe62006-04-25 20:13:52 +00007600 return Result;
7601}
7602
Evan Chengda43bcf2008-09-24 00:05:32 +00007603SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007604X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007605 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007606 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007607 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007608}
7609
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007610static SDValue
7611GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007612 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007613 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007614 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007615 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007616 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007617 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007618 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007619 GA->getOffset(),
7620 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007621
7622 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7623 : X86ISD::TLSADDR;
7624
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007625 if (InFlag) {
7626 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007627 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007628 } else {
7629 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007630 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007631 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007632
7633 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007634 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007635
Rafael Espindola15f1b662009-04-24 12:59:40 +00007636 SDValue Flag = Chain.getValue(1);
7637 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007638}
7639
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007640// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007641static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007642LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007643 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007644 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007645 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7646 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007647 DAG.getNode(X86ISD::GlobalBaseReg,
7648 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007649 InFlag = Chain.getValue(1);
7650
Chris Lattnerb903bed2009-06-26 21:20:29 +00007651 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007652}
7653
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007654// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007655static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007656LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007657 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007658 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7659 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007660}
7661
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007662static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7663 SelectionDAG &DAG,
7664 const EVT PtrVT,
7665 bool is64Bit) {
7666 DebugLoc dl = GA->getDebugLoc();
7667
7668 // Get the start address of the TLS block for this module.
7669 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7670 .getInfo<X86MachineFunctionInfo>();
7671 MFI->incNumLocalDynamicTLSAccesses();
7672
7673 SDValue Base;
7674 if (is64Bit) {
7675 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7676 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7677 } else {
7678 SDValue InFlag;
7679 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7680 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7681 InFlag = Chain.getValue(1);
7682 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7683 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7684 }
7685
7686 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7687 // of Base.
7688
7689 // Build x@dtpoff.
7690 unsigned char OperandFlags = X86II::MO_DTPOFF;
7691 unsigned WrapperKind = X86ISD::Wrapper;
7692 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7693 GA->getValueType(0),
7694 GA->getOffset(), OperandFlags);
7695 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7696
7697 // Add x@dtpoff with the base.
7698 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7699}
7700
Hans Wennborg228756c2012-05-11 10:11:01 +00007701// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007702static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007703 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007704 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007705 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007706
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007707 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7708 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7709 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007710
Michael J. Spencerec38de22010-10-10 22:04:20 +00007711 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007712 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007713 MachinePointerInfo(Ptr),
7714 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007715
Chris Lattnerb903bed2009-06-26 21:20:29 +00007716 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007717 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7718 // initialexec.
7719 unsigned WrapperKind = X86ISD::Wrapper;
7720 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007721 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007722 } else if (model == TLSModel::InitialExec) {
7723 if (is64Bit) {
7724 OperandFlags = X86II::MO_GOTTPOFF;
7725 WrapperKind = X86ISD::WrapperRIP;
7726 } else {
7727 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7728 }
Chris Lattner18c59872009-06-27 04:16:01 +00007729 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007730 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007731 }
Eric Christopherfd179292009-08-27 18:07:15 +00007732
Hans Wennborg228756c2012-05-11 10:11:01 +00007733 // emit "addl x@ntpoff,%eax" (local exec)
7734 // or "addl x@indntpoff,%eax" (initial exec)
7735 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007736 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007737 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007738 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007739 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007740
Hans Wennborg228756c2012-05-11 10:11:01 +00007741 if (model == TLSModel::InitialExec) {
7742 if (isPIC && !is64Bit) {
7743 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7744 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7745 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007746 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007747
7748 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7749 MachinePointerInfo::getGOT(), false, false, false,
7750 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007751 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007752
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007753 // The address of the thread local variable is the add of the thread
7754 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007755 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007756}
7757
Dan Gohman475871a2008-07-27 21:46:04 +00007758SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007759X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007760
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007761 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007762 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007763
Eric Christopher30ef0e52010-06-03 04:07:48 +00007764 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007765 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007766
Eric Christopher30ef0e52010-06-03 04:07:48 +00007767 switch (model) {
7768 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007769 if (Subtarget->is64Bit())
7770 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7771 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007772 case TLSModel::LocalDynamic:
7773 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7774 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007775 case TLSModel::InitialExec:
7776 case TLSModel::LocalExec:
7777 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007778 Subtarget->is64Bit(),
Craig Topperb99bafe2013-01-21 06:21:54 +00007779 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007780 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007781 llvm_unreachable("Unknown TLS model.");
7782 }
7783
7784 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007785 // Darwin only has one model of TLS. Lower to that.
7786 unsigned char OpFlag = 0;
7787 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7788 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007789
Eric Christopher30ef0e52010-06-03 04:07:48 +00007790 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7791 // global base reg.
7792 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7793 !Subtarget->is64Bit();
7794 if (PIC32)
7795 OpFlag = X86II::MO_TLVP_PIC_BASE;
7796 else
7797 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007798 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007799 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007800 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007801 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007802 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007803
Eric Christopher30ef0e52010-06-03 04:07:48 +00007804 // With PIC32, the address is actually $g + Offset.
7805 if (PIC32)
7806 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7807 DAG.getNode(X86ISD::GlobalBaseReg,
7808 DebugLoc(), getPointerTy()),
7809 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007810
Eric Christopher30ef0e52010-06-03 04:07:48 +00007811 // Lowering the machine isd will make sure everything is in the right
7812 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007813 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007814 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007815 SDValue Args[] = { Chain, Offset };
7816 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007817
Eric Christopher30ef0e52010-06-03 04:07:48 +00007818 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7819 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7820 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007821
Eric Christopher30ef0e52010-06-03 04:07:48 +00007822 // And our return value (tls address) is in the standard call return value
7823 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007824 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007825 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7826 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007827 }
7828
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007829 if (Subtarget->isTargetWindows() || Subtarget->isTargetMingw()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007830 // Just use the implicit TLS architecture
7831 // Need to generate someting similar to:
7832 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7833 // ; from TEB
7834 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7835 // mov rcx, qword [rdx+rcx*8]
7836 // mov eax, .tls$:tlsvar
7837 // [rax+rcx] contains the address
7838 // Windows 64bit: gs:0x58
7839 // Windows 32bit: fs:__tls_array
7840
7841 // If GV is an alias then use the aliasee for determining
7842 // thread-localness.
7843 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7844 GV = GA->resolveAliasedGlobal(false);
7845 DebugLoc dl = GA->getDebugLoc();
7846 SDValue Chain = DAG.getEntryNode();
7847
7848 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007849 // %gs:0x58 (64-bit). On MinGW, __tls_array is not available, so directly
7850 // use its literal value of 0x2C.
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007851 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7852 ? Type::getInt8PtrTy(*DAG.getContext(),
7853 256)
7854 : Type::getInt32PtrTy(*DAG.getContext(),
7855 257));
7856
Anton Korobeynikov2ee4e422013-03-18 08:12:28 +00007857 SDValue TlsArray = Subtarget->is64Bit() ? DAG.getIntPtrConstant(0x58) :
7858 (Subtarget->isTargetMingw() ? DAG.getIntPtrConstant(0x2C) :
7859 DAG.getExternalSymbol("_tls_array", getPointerTy()));
7860
7861 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain, TlsArray,
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007862 MachinePointerInfo(Ptr),
7863 false, false, false, 0);
7864
7865 // Load the _tls_index variable
7866 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7867 if (Subtarget->is64Bit())
7868 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7869 IDX, MachinePointerInfo(), MVT::i32,
7870 false, false, 0);
7871 else
7872 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7873 false, false, false, 0);
7874
Chandler Carruth426c2bf2012-11-01 09:14:31 +00007875 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007876 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007877 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7878
7879 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7880 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7881 false, false, false, 0);
7882
7883 // Get the offset of start of .tls section
7884 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7885 GA->getValueType(0),
7886 GA->getOffset(), X86II::MO_SECREL);
7887 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7888
7889 // The address of the thread local variable is the add of the thread
7890 // pointer with the offset of the variable.
7891 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007892 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007893
David Blaikie4d6ccb52012-01-20 21:51:11 +00007894 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007895}
7896
Chad Rosierb90d2a92012-01-03 23:19:12 +00007897/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7898/// and take a 2 x i32 value to shift plus a shift amount.
7899SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007900 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007901 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007902 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007903 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007904 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007905 SDValue ShOpLo = Op.getOperand(0);
7906 SDValue ShOpHi = Op.getOperand(1);
7907 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007908 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007909 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007910 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007911
Dan Gohman475871a2008-07-27 21:46:04 +00007912 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007913 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007914 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7915 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007916 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007917 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7918 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007919 }
Evan Chenge3413162006-01-09 18:33:28 +00007920
Owen Anderson825b72b2009-08-11 20:47:22 +00007921 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7922 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007923 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007924 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007925
Dan Gohman475871a2008-07-27 21:46:04 +00007926 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007927 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007928 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7929 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007930
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007931 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007932 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7933 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007934 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007935 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7936 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007937 }
7938
Dan Gohman475871a2008-07-27 21:46:04 +00007939 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007940 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007941}
Evan Chenga3195e82006-01-12 22:54:21 +00007942
Dan Gohmand858e902010-04-17 15:26:15 +00007943SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7944 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007945 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007946
Dale Johannesen0488fb62010-09-30 23:57:10 +00007947 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007948 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007949
Owen Anderson825b72b2009-08-11 20:47:22 +00007950 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007951 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007952
Eli Friedman36df4992009-05-27 00:47:34 +00007953 // These are really Legal; return the operand so the caller accepts it as
7954 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007955 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007956 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007957 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007958 Subtarget->is64Bit()) {
7959 return Op;
7960 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007961
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007962 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007963 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007964 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007965 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007966 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007967 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007968 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007969 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007970 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007971 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7972}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007973
Owen Andersone50ed302009-08-10 22:56:29 +00007974SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007975 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007976 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007977 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007978 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007979 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007980 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007981 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007982 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007983 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007984 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007985
Chris Lattner492a43e2010-09-22 01:28:21 +00007986 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007987
Stuart Hastings84be9582011-06-02 15:57:11 +00007988 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7989 MachineMemOperand *MMO;
7990 if (FI) {
7991 int SSFI = FI->getIndex();
7992 MMO =
7993 DAG.getMachineFunction()
7994 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7995 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7996 } else {
7997 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7998 StackSlot = StackSlot.getOperand(1);
7999 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008000 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008001 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
8002 X86ISD::FILD, DL,
8003 Tys, Ops, array_lengthof(Ops),
8004 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008005
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008006 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008007 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008008 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008009
8010 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
8011 // shouldn't be necessary except that RFP cannot be live across
8012 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008013 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008014 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
8015 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008016 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00008017 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008018 SDValue Ops[] = {
8019 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
8020 };
Chris Lattner492a43e2010-09-22 01:28:21 +00008021 MachineMemOperand *MMO =
8022 DAG.getMachineFunction()
8023 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00008024 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008025
Chris Lattner492a43e2010-09-22 01:28:21 +00008026 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
8027 Ops, array_lengthof(Ops),
8028 Op.getValueType(), MMO);
8029 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008030 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008031 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008032 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008033
Evan Cheng0db9fe62006-04-25 20:13:52 +00008034 return Result;
8035}
8036
Bill Wendling8b8a6362009-01-17 03:56:04 +00008037// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008038SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
8039 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00008040 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00008041 /*
Bill Wendling397ae212012-01-05 02:13:20 +00008042 movq %rax, %xmm0
8043 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
8044 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
8045 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00008046 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00008047 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00008048 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00008049 addpd %xmm1, %xmm0
8050 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00008051 */
Dale Johannesen040225f2008-10-21 23:07:49 +00008052
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008053 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00008054 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00008055
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008056 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00008057 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
8058 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008059 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008060
Chris Lattner97484792012-01-25 09:56:22 +00008061 SmallVector<Constant*,2> CV1;
8062 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008063 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8064 APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008065 CV1.push_back(
Tim Northover0a29cb02013-01-22 09:46:31 +00008066 ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8067 APInt(64, 0x4530000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00008068 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008069 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008070
Bill Wendling397ae212012-01-05 02:13:20 +00008071 // Load the 64-bit value into an XMM register.
8072 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
8073 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008074 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00008075 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008076 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008077 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
8078 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
8079 CLod0);
8080
Owen Anderson825b72b2009-08-11 20:47:22 +00008081 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00008082 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008083 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00008084 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008085 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00008086 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008087
Craig Topperd0a31172012-01-10 06:37:29 +00008088 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00008089 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
8090 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
8091 } else {
8092 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
8093 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
8094 S2F, 0x4E, DAG);
8095 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
8096 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
8097 Sub);
8098 }
8099
8100 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008101 DAG.getIntPtrConstant(0));
8102}
8103
Bill Wendling8b8a6362009-01-17 03:56:04 +00008104// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00008105SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
8106 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008107 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008108 // FP constant to bias correct the final result.
8109 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00008110 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008111
8112 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00008113 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00008114 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008115
Eli Friedmanf3704762011-08-29 21:15:46 +00008116 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00008117 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00008118
Owen Anderson825b72b2009-08-11 20:47:22 +00008119 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008120 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008121 DAG.getIntPtrConstant(0));
8122
8123 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008124 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008125 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008126 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008127 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008128 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00008129 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008130 MVT::v2f64, Bias)));
8131 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008132 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00008133 DAG.getIntPtrConstant(0));
8134
8135 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00008136 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00008137
8138 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00008139 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00008140
Craig Topper69947b92012-04-23 06:57:04 +00008141 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008142 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00008143 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00008144 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00008145 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00008146
8147 // Handle final rounding.
8148 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00008149}
8150
Michael Liaoa7554632012-10-23 17:36:08 +00008151SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
8152 SelectionDAG &DAG) const {
8153 SDValue N0 = Op.getOperand(0);
8154 EVT SVT = N0.getValueType();
8155 DebugLoc dl = Op.getDebugLoc();
8156
8157 assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 ||
8158 SVT == MVT::v8i8 || SVT == MVT::v8i16) &&
8159 "Custom UINT_TO_FP is not supported!");
8160
Craig Topperb99bafe2013-01-21 06:21:54 +00008161 EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
8162 SVT.getVectorNumElements());
Michael Liaoa7554632012-10-23 17:36:08 +00008163 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
8164 DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
8165}
8166
Dan Gohmand858e902010-04-17 15:26:15 +00008167SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
8168 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00008169 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008170 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00008171
Michael Liaoa7554632012-10-23 17:36:08 +00008172 if (Op.getValueType().isVector())
8173 return lowerUINT_TO_FP_vec(Op, DAG);
8174
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008175 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00008176 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
8177 // the optimization here.
8178 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00008179 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00008180
Owen Andersone50ed302009-08-10 22:56:29 +00008181 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008182 EVT DstVT = Op.getValueType();
8183 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008184 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008185 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008186 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008187 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008188 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008189
8190 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008191 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008192 if (SrcVT == MVT::i32) {
8193 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8194 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8195 getPointerTy(), StackSlot, WordOff);
8196 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008197 StackSlot, MachinePointerInfo(),
8198 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008199 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008200 OffsetSlot, MachinePointerInfo(),
8201 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008202 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8203 return Fild;
8204 }
8205
8206 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8207 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008208 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008209 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008210 // For i64 source, we need to add the appropriate power of 2 if the input
8211 // was negative. This is the same as the optimization in
8212 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8213 // we must be careful to do the computation in x87 extended precision, not
8214 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008215 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8216 MachineMemOperand *MMO =
8217 DAG.getMachineFunction()
8218 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8219 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008220
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008221 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8222 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008223 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8224 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008225
8226 APInt FF(32, 0x5F800000ULL);
8227
8228 // Check whether the sign bit is set.
8229 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8230 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8231 ISD::SETLT);
8232
8233 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8234 SDValue FudgePtr = DAG.getConstantPool(
8235 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8236 getPointerTy());
8237
8238 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8239 SDValue Zero = DAG.getIntPtrConstant(0);
8240 SDValue Four = DAG.getIntPtrConstant(4);
8241 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8242 Zero, Four);
8243 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8244
8245 // Load the value out, extending it from f32 to f80.
8246 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008247 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008248 FudgePtr, MachinePointerInfo::getConstantPool(),
8249 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008250 // Extend everything to 80 bits to force it to be done on x87.
8251 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8252 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008253}
8254
Craig Topperb99bafe2013-01-21 06:21:54 +00008255std::pair<SDValue,SDValue>
8256X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
8257 bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008258 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008259
Owen Andersone50ed302009-08-10 22:56:29 +00008260 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008261
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008262 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008263 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8264 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008265 }
8266
Owen Anderson825b72b2009-08-11 20:47:22 +00008267 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8268 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008269 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008270
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008271 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008272 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008273 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008274 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008275 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008276 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008277 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008278 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008279
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008280 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8281 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008282 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008283 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008284 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008285 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008286
Evan Cheng0db9fe62006-04-25 20:13:52 +00008287 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008288 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8289 Opc = X86ISD::WIN_FTOL;
8290 else
8291 switch (DstTy.getSimpleVT().SimpleTy) {
8292 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8293 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8294 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8295 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8296 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008297
Dan Gohman475871a2008-07-27 21:46:04 +00008298 SDValue Chain = DAG.getEntryNode();
8299 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008300 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008301 // FIXME This causes a redundant load/store if the SSE-class value is already
8302 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008303 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008304 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008305 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008306 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008307 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008308 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008309 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008310 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008311 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008312
Chris Lattner492a43e2010-09-22 01:28:21 +00008313 MachineMemOperand *MMO =
8314 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8315 MachineMemOperand::MOLoad, MemSize, MemSize);
8316 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8317 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008318 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008319 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008320 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8321 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008322
Chris Lattner07290932010-09-22 01:05:16 +00008323 MachineMemOperand *MMO =
8324 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8325 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008326
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008327 if (Opc != X86ISD::WIN_FTOL) {
8328 // Build the FP_TO_INT*_IN_MEM
8329 SDValue Ops[] = { Chain, Value, StackSlot };
8330 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8331 Ops, 3, DstTy, MMO);
8332 return std::make_pair(FIST, StackSlot);
8333 } else {
8334 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8335 DAG.getVTList(MVT::Other, MVT::Glue),
8336 Chain, Value);
8337 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8338 MVT::i32, ftol.getValue(1));
8339 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8340 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008341 SDValue Ops[] = { eax, edx };
8342 SDValue pair = IsReplace
8343 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8344 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008345 return std::make_pair(pair, SDValue());
8346 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008347}
8348
Nadav Rotem0509db22012-12-28 05:45:24 +00008349static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
8350 const X86Subtarget *Subtarget) {
Craig Toppera080daf2013-01-20 21:50:27 +00008351 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008352 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008353 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem0509db22012-12-28 05:45:24 +00008354 DebugLoc dl = Op->getDebugLoc();
8355
8356 // Optimize vectors in AVX mode:
8357 //
8358 // v8i16 -> v8i32
8359 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
8360 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
8361 // Concat upper and lower parts.
8362 //
8363 // v4i32 -> v4i64
8364 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
8365 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
8366 // Concat upper and lower parts.
8367 //
8368
8369 if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
8370 ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
8371 return SDValue();
8372
8373 if (Subtarget->hasInt256())
8374 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In);
8375
8376 SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
8377 SDValue Undef = DAG.getUNDEF(InVT);
8378 bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
8379 SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8380 SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
8381
Craig Toppera080daf2013-01-20 21:50:27 +00008382 MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
Nadav Rotem0509db22012-12-28 05:45:24 +00008383 VT.getVectorNumElements()/2);
8384
8385 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
8386 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
8387
8388 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
8389}
8390
8391SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op,
8392 SelectionDAG &DAG) const {
8393 if (Subtarget->hasFp256()) {
8394 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8395 if (Res.getNode())
8396 return Res;
8397 }
8398
8399 return SDValue();
8400}
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008401SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op,
8402 SelectionDAG &DAG) const {
Michael Liaoa7554632012-10-23 17:36:08 +00008403 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008404 MVT VT = Op.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008405 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008406 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaoa7554632012-10-23 17:36:08 +00008407
Nadav Rotem0509db22012-12-28 05:45:24 +00008408 if (Subtarget->hasFp256()) {
8409 SDValue Res = LowerAVXExtend(Op, DAG, Subtarget);
8410 if (Res.getNode())
8411 return Res;
8412 }
8413
Michael Liaoa7554632012-10-23 17:36:08 +00008414 if (!VT.is256BitVector() || !SVT.is128BitVector() ||
8415 VT.getVectorNumElements() != SVT.getVectorNumElements())
8416 return SDValue();
8417
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008418 assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!");
Michael Liaoa7554632012-10-23 17:36:08 +00008419
8420 // AVX2 has better support of integer extending.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00008421 if (Subtarget->hasInt256())
Michael Liaoa7554632012-10-23 17:36:08 +00008422 return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
8423
8424 SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In);
8425 static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1};
8426 SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32,
Nadav Rotem40ef8b72012-12-28 07:28:43 +00008427 DAG.getVectorShuffle(MVT::v8i16, DL, In,
8428 DAG.getUNDEF(MVT::v8i16),
8429 &Mask[0]));
Michael Liaoa7554632012-10-23 17:36:08 +00008430
8431 return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi);
8432}
8433
Craig Topperd713c0f2013-01-20 21:34:37 +00008434SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
Michael Liaobedcbd42012-10-16 18:14:11 +00008435 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008436 MVT VT = Op.getValueType().getSimpleVT();
Nadav Rotem3c22a442012-12-27 07:45:10 +00008437 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008438 MVT SVT = In.getValueType().getSimpleVT();
Michael Liaobedcbd42012-10-16 18:14:11 +00008439
Nadav Rotem3c22a442012-12-27 07:45:10 +00008440 if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) {
8441 // On AVX2, v4i64 -> v4i32 becomes VPERMD.
8442 if (Subtarget->hasInt256()) {
8443 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
8444 In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In);
8445 In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
8446 ShufMask);
8447 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
8448 DAG.getIntPtrConstant(0));
8449 }
8450
8451 // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS.
8452 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8453 DAG.getIntPtrConstant(0));
8454 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8455 DAG.getIntPtrConstant(2));
8456
8457 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8458 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8459
8460 // The PSHUFD mask:
8461 static const int ShufMask1[] = {0, 2, 0, 0};
8462 SDValue Undef = DAG.getUNDEF(VT);
8463 OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1);
8464 OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1);
8465
8466 // The MOVLHPS mask:
8467 static const int ShufMask2[] = {0, 1, 4, 5};
8468 return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2);
8469 }
8470
8471 if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) {
8472 // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
8473 if (Subtarget->hasInt256()) {
8474 In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In);
8475
8476 SmallVector<SDValue,32> pshufbMask;
8477 for (unsigned i = 0; i < 2; ++i) {
8478 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
8479 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
8480 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
8481 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
8482 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
8483 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
8484 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
8485 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
8486 for (unsigned j = 0; j < 8; ++j)
8487 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
8488 }
8489 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8,
8490 &pshufbMask[0], 32);
8491 In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
8492 In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In);
8493
8494 static const int ShufMask[] = {0, 2, -1, -1};
8495 In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64),
8496 &ShufMask[0]);
8497 In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
8498 DAG.getIntPtrConstant(0));
8499 return DAG.getNode(ISD::BITCAST, DL, VT, In);
8500 }
8501
8502 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8503 DAG.getIntPtrConstant(0));
8504
8505 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
8506 DAG.getIntPtrConstant(4));
8507
8508 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo);
8509 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi);
8510
8511 // The PSHUFB mask:
8512 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
8513 -1, -1, -1, -1, -1, -1, -1, -1};
8514
8515 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
8516 OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
8517 OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
8518
8519 OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo);
8520 OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi);
8521
8522 // The MOVLHPS Mask:
8523 static const int ShufMask2[] = {0, 1, 4, 5};
8524 SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
8525 return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res);
8526 }
8527
8528 // Handle truncation of V256 to V128 using shuffles.
8529 if (!VT.is128BitVector() || !SVT.is256BitVector())
Michael Liaobedcbd42012-10-16 18:14:11 +00008530 return SDValue();
8531
Nadav Rotem3c22a442012-12-27 07:45:10 +00008532 assert(VT.getVectorNumElements() != SVT.getVectorNumElements() &&
8533 "Invalid op");
8534 assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
Michael Liaobedcbd42012-10-16 18:14:11 +00008535
8536 unsigned NumElems = VT.getVectorNumElements();
8537 EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
8538 NumElems * 2);
8539
Michael Liaobedcbd42012-10-16 18:14:11 +00008540 SmallVector<int, 16> MaskVec(NumElems * 2, -1);
8541 // Prepare truncation shuffle mask
8542 for (unsigned i = 0; i != NumElems; ++i)
8543 MaskVec[i] = i * 2;
8544 SDValue V = DAG.getVectorShuffle(NVT, DL,
8545 DAG.getNode(ISD::BITCAST, DL, NVT, In),
8546 DAG.getUNDEF(NVT), &MaskVec[0]);
8547 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
8548 DAG.getIntPtrConstant(0));
8549}
8550
Dan Gohmand858e902010-04-17 15:26:15 +00008551SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8552 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00008553 MVT VT = Op.getValueType().getSimpleVT();
8554 if (VT.isVector()) {
8555 if (VT == MVT::v8i16)
8556 return DAG.getNode(ISD::TRUNCATE, Op.getDebugLoc(), VT,
Michael Liaobedcbd42012-10-16 18:14:11 +00008557 DAG.getNode(ISD::FP_TO_SINT, Op.getDebugLoc(),
8558 MVT::v8i32, Op.getOperand(0)));
Eli Friedman23ef1052009-06-06 03:57:58 +00008559 return SDValue();
Michael Liaobedcbd42012-10-16 18:14:11 +00008560 }
Eli Friedman23ef1052009-06-06 03:57:58 +00008561
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008562 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8563 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008564 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008565 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8566 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008567
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008568 if (StackSlot.getNode())
8569 // Load the result.
8570 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8571 FIST, StackSlot, MachinePointerInfo(),
8572 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008573
8574 // The node is the result.
8575 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008576}
8577
Dan Gohmand858e902010-04-17 15:26:15 +00008578SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8579 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008580 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8581 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008582 SDValue FIST = Vals.first, StackSlot = Vals.second;
8583 assert(FIST.getNode() && "Unexpected failure");
8584
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008585 if (StackSlot.getNode())
8586 // Load the result.
8587 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8588 FIST, StackSlot, MachinePointerInfo(),
8589 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008590
8591 // The node is the result.
8592 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008593}
8594
Craig Topperb84b4232013-01-21 06:13:28 +00008595static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
Michael Liao9d796db2012-10-10 16:32:15 +00008596 DebugLoc DL = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008597 MVT VT = Op.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008598 SDValue In = Op.getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00008599 MVT SVT = In.getValueType().getSimpleVT();
Michael Liao9d796db2012-10-10 16:32:15 +00008600
8601 assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
8602
8603 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
8604 DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
8605 In, DAG.getUNDEF(SVT)));
8606}
8607
Craig Topper43620672012-09-08 07:31:51 +00008608SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008609 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008610 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008611 MVT VT = Op.getValueType().getSimpleVT();
8612 MVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008613 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8614 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008615 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008616 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008617 }
Craig Topper43620672012-09-08 07:31:51 +00008618 Constant *C;
8619 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008620 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8621 APInt(64, ~(1ULL << 63))));
Craig Topper43620672012-09-08 07:31:51 +00008622 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008623 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8624 APInt(32, ~(1U << 31))));
Craig Topper43620672012-09-08 07:31:51 +00008625 C = ConstantVector::getSplat(NumElts, C);
8626 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8627 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008628 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008629 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008630 false, false, false, Alignment);
8631 if (VT.isVector()) {
8632 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8633 return DAG.getNode(ISD::BITCAST, dl, VT,
8634 DAG.getNode(ISD::AND, dl, ANDVT,
8635 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8636 Op.getOperand(0)),
8637 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8638 }
Dale Johannesenace16102009-02-03 19:33:06 +00008639 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008640}
8641
Dan Gohmand858e902010-04-17 15:26:15 +00008642SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008643 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008644 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008645 MVT VT = Op.getValueType().getSimpleVT();
8646 MVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008647 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8648 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008649 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008650 NumElts = VT.getVectorNumElements();
8651 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008652 Constant *C;
8653 if (EltVT == MVT::f64)
Tim Northover0a29cb02013-01-22 09:46:31 +00008654 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
8655 APInt(64, 1ULL << 63)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008656 else
Tim Northover0a29cb02013-01-22 09:46:31 +00008657 C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle,
8658 APInt(32, 1U << 31)));
Chris Lattner4ca829e2012-01-25 06:02:56 +00008659 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008660 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8661 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008662 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008663 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008664 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008665 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008666 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008667 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008668 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008669 DAG.getNode(ISD::BITCAST, dl, XORVT,
8670 Op.getOperand(0)),
8671 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008672 }
Craig Topper69947b92012-04-23 06:57:04 +00008673
8674 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008675}
8676
Dan Gohmand858e902010-04-17 15:26:15 +00008677SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008678 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008679 SDValue Op0 = Op.getOperand(0);
8680 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008681 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008682 MVT VT = Op.getValueType().getSimpleVT();
8683 MVT SrcVT = Op1.getValueType().getSimpleVT();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008684
8685 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008686 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008687 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008688 SrcVT = VT;
8689 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008690 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008691 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008692 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008693 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008694 }
8695
8696 // At this point the operands and the result should have the same
8697 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008698
Evan Cheng68c47cb2007-01-05 07:55:56 +00008699 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008700 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008701 if (SrcVT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008702 const fltSemantics &Sem = APFloat::IEEEdouble;
8703 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63))));
8704 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008705 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008706 const fltSemantics &Sem = APFloat::IEEEsingle;
8707 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31))));
8708 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8709 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8710 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008711 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008712 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008713 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008714 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008715 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008716 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008717 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008718
8719 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008720 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008721 // Op0 is MVT::f32, Op1 is MVT::f64.
8722 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8723 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8724 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008725 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008726 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008727 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008728 }
8729
Evan Cheng73d6cf12007-01-05 21:37:56 +00008730 // Clear first operand sign bit.
8731 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008732 if (VT == MVT::f64) {
Tim Northover0a29cb02013-01-22 09:46:31 +00008733 const fltSemantics &Sem = APFloat::IEEEdouble;
8734 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8735 APInt(64, ~(1ULL << 63)))));
8736 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008737 } else {
Tim Northover0a29cb02013-01-22 09:46:31 +00008738 const fltSemantics &Sem = APFloat::IEEEsingle;
8739 CV.push_back(ConstantFP::get(*Context, APFloat(Sem,
8740 APInt(32, ~(1U << 31)))));
8741 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8742 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
8743 CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008744 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008745 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008746 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008747 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008748 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008749 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008750 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008751
8752 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008753 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008754}
8755
Craig Topper55b24052012-09-11 06:15:32 +00008756static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008757 SDValue N0 = Op.getOperand(0);
8758 DebugLoc dl = Op.getDebugLoc();
Craig Toppera080daf2013-01-20 21:50:27 +00008759 MVT VT = Op.getValueType().getSimpleVT();
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008760
8761 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8762 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8763 DAG.getConstant(1, VT));
8764 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8765}
8766
Michael Liaof966e4e2012-09-13 20:24:54 +00008767// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8768//
Craig Topperb99bafe2013-01-21 06:21:54 +00008769SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op,
8770 SelectionDAG &DAG) const {
Michael Liaof966e4e2012-09-13 20:24:54 +00008771 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8772
8773 if (!Subtarget->hasSSE41())
8774 return SDValue();
8775
8776 if (!Op->hasOneUse())
8777 return SDValue();
8778
8779 SDNode *N = Op.getNode();
8780 DebugLoc DL = N->getDebugLoc();
8781
8782 SmallVector<SDValue, 8> Opnds;
8783 DenseMap<SDValue, unsigned> VecInMap;
8784 EVT VT = MVT::Other;
8785
8786 // Recognize a special case where a vector is casted into wide integer to
8787 // test all 0s.
8788 Opnds.push_back(N->getOperand(0));
8789 Opnds.push_back(N->getOperand(1));
8790
8791 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8792 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8793 // BFS traverse all OR'd operands.
8794 if (I->getOpcode() == ISD::OR) {
8795 Opnds.push_back(I->getOperand(0));
8796 Opnds.push_back(I->getOperand(1));
8797 // Re-evaluate the number of nodes to be traversed.
8798 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8799 continue;
8800 }
8801
8802 // Quit if a non-EXTRACT_VECTOR_ELT
8803 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8804 return SDValue();
8805
8806 // Quit if without a constant index.
8807 SDValue Idx = I->getOperand(1);
8808 if (!isa<ConstantSDNode>(Idx))
8809 return SDValue();
8810
8811 SDValue ExtractedFromVec = I->getOperand(0);
8812 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8813 if (M == VecInMap.end()) {
8814 VT = ExtractedFromVec.getValueType();
8815 // Quit if not 128/256-bit vector.
8816 if (!VT.is128BitVector() && !VT.is256BitVector())
8817 return SDValue();
8818 // Quit if not the same type.
8819 if (VecInMap.begin() != VecInMap.end() &&
8820 VT != VecInMap.begin()->first.getValueType())
8821 return SDValue();
8822 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8823 }
8824 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8825 }
8826
8827 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008828 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008829
8830 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8831 SmallVector<SDValue, 8> VecIns;
8832
8833 for (DenseMap<SDValue, unsigned>::const_iterator
8834 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8835 // Quit if not all elements are used.
8836 if (I->second != FullMask)
8837 return SDValue();
8838 VecIns.push_back(I->first);
8839 }
8840
8841 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8842
8843 // Cast all vectors into TestVT for PTEST.
8844 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8845 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8846
8847 // If more than one full vectors are evaluated, OR them first before PTEST.
8848 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8849 // Each iteration will OR 2 nodes and append the result until there is only
8850 // 1 node left, i.e. the final OR'd value of all vectors.
8851 SDValue LHS = VecIns[Slot];
8852 SDValue RHS = VecIns[Slot + 1];
8853 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8854 }
8855
8856 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8857 VecIns.back(), VecIns.back());
8858}
8859
Dan Gohman076aee32009-03-04 19:44:21 +00008860/// Emit nodes that will be selected as "test Op0,Op0", or something
8861/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008862SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008863 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008864 DebugLoc dl = Op.getDebugLoc();
8865
Dan Gohman31125812009-03-07 01:58:32 +00008866 // CF and OF aren't always set the way we want. Determine which
8867 // of these we need.
8868 bool NeedCF = false;
8869 bool NeedOF = false;
8870 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008871 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008872 case X86::COND_A: case X86::COND_AE:
8873 case X86::COND_B: case X86::COND_BE:
8874 NeedCF = true;
8875 break;
8876 case X86::COND_G: case X86::COND_GE:
8877 case X86::COND_L: case X86::COND_LE:
8878 case X86::COND_O: case X86::COND_NO:
8879 NeedOF = true;
8880 break;
Dan Gohman31125812009-03-07 01:58:32 +00008881 }
8882
Dan Gohman076aee32009-03-04 19:44:21 +00008883 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008884 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8885 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008886 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8887 // Emit a CMP with 0, which is the TEST pattern.
8888 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8889 DAG.getConstant(0, Op.getValueType()));
8890
8891 unsigned Opcode = 0;
8892 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008893
8894 // Truncate operations may prevent the merge of the SETCC instruction
8895 // and the arithmetic intruction before it. Attempt to truncate the operands
8896 // of the arithmetic instruction and use a reduced bit-width instruction.
8897 bool NeedTruncation = false;
8898 SDValue ArithOp = Op;
8899 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8900 SDValue Arith = Op->getOperand(0);
8901 // Both the trunc and the arithmetic op need to have one user each.
8902 if (Arith->hasOneUse())
8903 switch (Arith.getOpcode()) {
8904 default: break;
8905 case ISD::ADD:
8906 case ISD::SUB:
8907 case ISD::AND:
8908 case ISD::OR:
8909 case ISD::XOR: {
8910 NeedTruncation = true;
8911 ArithOp = Arith;
8912 }
8913 }
8914 }
8915
8916 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8917 // which may be the result of a CAST. We use the variable 'Op', which is the
8918 // non-casted variable when we check for possible users.
8919 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008920 case ISD::ADD:
8921 // Due to an isel shortcoming, be conservative if this add is likely to be
8922 // selected as part of a load-modify-store instruction. When the root node
8923 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8924 // uses of other nodes in the match, such as the ADD in this case. This
8925 // leads to the ADD being left around and reselected, with the result being
8926 // two adds in the output. Alas, even if none our users are stores, that
8927 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8928 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8929 // climbing the DAG back to the root, and it doesn't seem to be worth the
8930 // effort.
8931 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008932 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8933 if (UI->getOpcode() != ISD::CopyToReg &&
8934 UI->getOpcode() != ISD::SETCC &&
8935 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008936 goto default_case;
8937
8938 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008939 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008940 // An add of one will be selected as an INC.
8941 if (C->getAPIntValue() == 1) {
8942 Opcode = X86ISD::INC;
8943 NumOperands = 1;
8944 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008945 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008946
8947 // An add of negative one (subtract of one) will be selected as a DEC.
8948 if (C->getAPIntValue().isAllOnesValue()) {
8949 Opcode = X86ISD::DEC;
8950 NumOperands = 1;
8951 break;
8952 }
Dan Gohman076aee32009-03-04 19:44:21 +00008953 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008954
8955 // Otherwise use a regular EFLAGS-setting add.
8956 Opcode = X86ISD::ADD;
8957 NumOperands = 2;
8958 break;
8959 case ISD::AND: {
8960 // If the primary and result isn't used, don't bother using X86ISD::AND,
8961 // because a TEST instruction will be better.
8962 bool NonFlagUse = false;
8963 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8964 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8965 SDNode *User = *UI;
8966 unsigned UOpNo = UI.getOperandNo();
8967 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8968 // Look pass truncate.
8969 UOpNo = User->use_begin().getOperandNo();
8970 User = *User->use_begin();
8971 }
8972
8973 if (User->getOpcode() != ISD::BRCOND &&
8974 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008975 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008976 NonFlagUse = true;
8977 break;
8978 }
Dan Gohman076aee32009-03-04 19:44:21 +00008979 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008980
8981 if (!NonFlagUse)
8982 break;
8983 }
8984 // FALL THROUGH
8985 case ISD::SUB:
8986 case ISD::OR:
8987 case ISD::XOR:
8988 // Due to the ISEL shortcoming noted above, be conservative if this op is
8989 // likely to be selected as part of a load-modify-store instruction.
8990 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8991 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8992 if (UI->getOpcode() == ISD::STORE)
8993 goto default_case;
8994
8995 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008996 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008997 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008998 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008999 case ISD::XOR: Opcode = X86ISD::XOR; break;
9000 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00009001 case ISD::OR: {
9002 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
9003 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
9004 if (EFLAGS.getNode())
9005 return EFLAGS;
9006 }
9007 Opcode = X86ISD::OR;
9008 break;
9009 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009010 }
9011
9012 NumOperands = 2;
9013 break;
9014 case X86ISD::ADD:
9015 case X86ISD::SUB:
9016 case X86ISD::INC:
9017 case X86ISD::DEC:
9018 case X86ISD::OR:
9019 case X86ISD::XOR:
9020 case X86ISD::AND:
9021 return SDValue(Op.getNode(), 1);
9022 default:
9023 default_case:
9024 break;
Dan Gohman076aee32009-03-04 19:44:21 +00009025 }
9026
Nadav Rotemb9d6b842012-08-18 17:53:03 +00009027 // If we found that truncation is beneficial, perform the truncation and
9028 // update 'Op'.
9029 if (NeedTruncation) {
9030 EVT VT = Op.getValueType();
9031 SDValue WideVal = Op->getOperand(0);
9032 EVT WideVT = WideVal.getValueType();
9033 unsigned ConvertedOp = 0;
9034 // Use a target machine opcode to prevent further DAGCombine
9035 // optimizations that may separate the arithmetic operations
9036 // from the setcc node.
9037 switch (WideVal.getOpcode()) {
9038 default: break;
9039 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
9040 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
9041 case ISD::AND: ConvertedOp = X86ISD::AND; break;
9042 case ISD::OR: ConvertedOp = X86ISD::OR; break;
9043 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
9044 }
9045
9046 if (ConvertedOp) {
9047 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9048 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
9049 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
9050 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
9051 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
9052 }
9053 }
9054 }
9055
Bill Wendlingc25ccf82010-06-28 21:08:32 +00009056 if (Opcode == 0)
9057 // Emit a CMP with 0, which is the TEST pattern.
9058 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
9059 DAG.getConstant(0, Op.getValueType()));
9060
9061 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
9062 SmallVector<SDValue, 4> Ops;
9063 for (unsigned i = 0; i != NumOperands; ++i)
9064 Ops.push_back(Op.getOperand(i));
9065
9066 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
9067 DAG.ReplaceAllUsesWith(Op, New);
9068 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00009069}
9070
9071/// Emit nodes that will be selected as "cmp Op0,Op1", or something
9072/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00009073SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00009074 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00009075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
9076 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00009077 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00009078
9079 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00009080 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
9081 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
9082 // Use SUB instead of CMP to enable CSE between SUB and CMP.
9083 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
9084 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
9085 Op0, Op1);
9086 return SDValue(Sub.getNode(), 1);
9087 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009088 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00009089}
9090
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009091/// Convert a comparison if required by the subtarget.
9092SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
9093 SelectionDAG &DAG) const {
9094 // If the subtarget does not support the FUCOMI instruction, floating-point
9095 // comparisons have to be converted.
9096 if (Subtarget->hasCMov() ||
9097 Cmp.getOpcode() != X86ISD::CMP ||
9098 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
9099 !Cmp.getOperand(1).getValueType().isFloatingPoint())
9100 return Cmp;
9101
9102 // The instruction selector will select an FUCOM instruction instead of
9103 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
9104 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
9105 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
9106 DebugLoc dl = Cmp.getDebugLoc();
9107 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
9108 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
9109 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
9110 DAG.getConstant(8, MVT::i8));
9111 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
9112 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
9113}
9114
Evan Cheng4e544802012-12-05 00:10:38 +00009115static bool isAllOnes(SDValue V) {
9116 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9117 return C && C->isAllOnesValue();
9118}
9119
Evan Chengd40d03e2010-01-06 19:38:29 +00009120/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
9121/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00009122SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
9123 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009124 SDValue Op0 = And.getOperand(0);
9125 SDValue Op1 = And.getOperand(1);
9126 if (Op0.getOpcode() == ISD::TRUNCATE)
9127 Op0 = Op0.getOperand(0);
9128 if (Op1.getOpcode() == ISD::TRUNCATE)
9129 Op1 = Op1.getOperand(0);
9130
Evan Chengd40d03e2010-01-06 19:38:29 +00009131 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009132 if (Op1.getOpcode() == ISD::SHL)
9133 std::swap(Op0, Op1);
9134 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00009135 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
9136 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009137 // If we looked past a truncate, check that it's only truncating away
9138 // known zeros.
9139 unsigned BitWidth = Op0.getValueSizeInBits();
9140 unsigned AndBitWidth = And.getValueSizeInBits();
9141 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00009142 APInt Zeros, Ones;
9143 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00009144 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
9145 return SDValue();
9146 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009147 LHS = Op1;
9148 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00009149 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00009150 } else if (Op1.getOpcode() == ISD::Constant) {
9151 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00009152 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00009153 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00009154
9155 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009156 LHS = AndLHS.getOperand(0);
9157 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009158 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00009159
9160 // Use BT if the immediate can't be encoded in a TEST instruction.
9161 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
9162 LHS = AndLHS;
9163 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
9164 }
Evan Chengd40d03e2010-01-06 19:38:29 +00009165 }
Evan Cheng0488db92007-09-25 01:57:46 +00009166
Evan Chengd40d03e2010-01-06 19:38:29 +00009167 if (LHS.getNode()) {
Evan Cheng4e544802012-12-05 00:10:38 +00009168 // If the LHS is of the form (x ^ -1) then replace the LHS with x and flip
9169 // the condition code later.
9170 bool Invert = false;
9171 if (LHS.getOpcode() == ISD::XOR && isAllOnes(LHS.getOperand(1))) {
9172 Invert = true;
9173 LHS = LHS.getOperand(0);
9174 }
9175
Evan Chenge5b51ac2010-04-17 06:13:15 +00009176 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00009177 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00009178 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00009179 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009180 // Also promote i16 to i32 for performance / code size reason.
9181 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009182 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00009183 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00009184
Evan Chengd40d03e2010-01-06 19:38:29 +00009185 // If the operand types disagree, extend the shift amount to match. Since
9186 // BT ignores high bits (like shifts) we can use anyextend.
9187 if (LHS.getValueType() != RHS.getValueType())
9188 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009189
Evan Chengd40d03e2010-01-06 19:38:29 +00009190 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Evan Cheng4e544802012-12-05 00:10:38 +00009191 X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
9192 // Flip the condition if the LHS was a not instruction
9193 if (Invert)
9194 Cond = X86::GetOppositeBranchCondition(Cond);
Evan Chengd40d03e2010-01-06 19:38:29 +00009195 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9196 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00009197 }
9198
Evan Cheng54de3ea2010-01-05 06:52:31 +00009199 return SDValue();
9200}
9201
Craig Topper89af15e2011-09-18 08:03:58 +00009202// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009203// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00009204static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Craig Topper26827f32013-01-20 09:02:22 +00009205 MVT VT = Op.getValueType().getSimpleVT();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009206
Craig Topper7a9a28b2012-08-12 02:23:29 +00009207 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009208 "Unsupported value type for operation");
9209
Craig Topper66ddd152012-04-27 22:54:43 +00009210 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009211 DebugLoc dl = Op.getDebugLoc();
9212 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009213
9214 // Extract the LHS vectors
9215 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00009216 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
9217 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009218
9219 // Extract the RHS vectors
9220 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00009221 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
9222 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009223
9224 // Issue the operation on the smaller types and concatenate the result back
Craig Topper26827f32013-01-20 09:02:22 +00009225 MVT EltVT = VT.getVectorElementType();
9226 MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009227 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
9228 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
9229 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
9230}
9231
Craig Topper26827f32013-01-20 09:02:22 +00009232static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
9233 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00009234 SDValue Cond;
9235 SDValue Op0 = Op.getOperand(0);
9236 SDValue Op1 = Op.getOperand(1);
9237 SDValue CC = Op.getOperand(2);
Craig Topper26827f32013-01-20 09:02:22 +00009238 MVT VT = Op.getValueType().getSimpleVT();
Nate Begeman30a0de92008-07-17 16:51:19 +00009239 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Craig Topper26827f32013-01-20 09:02:22 +00009240 bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009241 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00009242
9243 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00009244#ifndef NDEBUG
Craig Topper26827f32013-01-20 09:02:22 +00009245 MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT();
Craig Topper523908d2012-08-13 02:34:03 +00009246 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
9247#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009248
Craig Topper523908d2012-08-13 02:34:03 +00009249 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00009250 bool Swap = false;
9251
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009252 // SSE Condition code mapping:
9253 // 0 - EQ
9254 // 1 - LT
9255 // 2 - LE
9256 // 3 - UNORD
9257 // 4 - NEQ
9258 // 5 - NLT
9259 // 6 - NLE
9260 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00009261 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009262 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00009263 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00009264 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00009265 case ISD::SETOGT:
9266 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00009267 case ISD::SETLT:
9268 case ISD::SETOLT: SSECC = 1; break;
9269 case ISD::SETOGE:
9270 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009271 case ISD::SETLE:
9272 case ISD::SETOLE: SSECC = 2; break;
9273 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009274 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00009275 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00009276 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009277 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00009278 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00009279 case ISD::SETUGT: SSECC = 6; break;
9280 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00009281 case ISD::SETUEQ:
9282 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009283 }
9284 if (Swap)
9285 std::swap(Op0, Op1);
9286
Nate Begemanfb8ead02008-07-25 19:05:58 +00009287 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00009288 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00009289 unsigned CC0, CC1;
9290 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00009291 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00009292 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
9293 } else {
9294 assert(SetCCOpcode == ISD::SETONE);
9295 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00009296 }
Craig Topper523908d2012-08-13 02:34:03 +00009297
9298 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9299 DAG.getConstant(CC0, MVT::i8));
9300 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9301 DAG.getConstant(CC1, MVT::i8));
9302 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009303 }
9304 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00009305 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
9306 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00009307 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009308
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00009309 // Break 256-bit integer vector compare into smaller ones.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +00009310 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper89af15e2011-09-18 08:03:58 +00009311 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00009312
Nate Begeman30a0de92008-07-17 16:51:19 +00009313 // We are handling one of the integer comparisons here. Since SSE only has
9314 // GT and EQ comparisons for integer, swapping operands and multiple
9315 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009316 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00009317 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009318
Nate Begeman30a0de92008-07-17 16:51:19 +00009319 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009320 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00009321 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009322 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009323 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009324 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009325 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009326 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009327 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009328 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009329 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00009330 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00009331 }
9332 if (Swap)
9333 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009334
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009335 // Check that the operation in question is available (most are plain SSE2,
9336 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009337 if (VT == MVT::v2i64) {
9338 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
9339 return SDValue();
Benjamin Kramer382ed782012-12-25 12:54:19 +00009340 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
9341 // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
Benjamin Kramer99f78062012-12-25 13:09:08 +00009342 // pcmpeqd + pshufd + pand.
Benjamin Kramer382ed782012-12-25 12:54:19 +00009343 assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
9344
9345 // First cast everything to the right type,
9346 Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0);
9347 Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1);
9348
9349 // Do the compare.
9350 SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
9351
9352 // Make sure the lower and upper halves are both all-ones.
Benjamin Kramer99f78062012-12-25 13:09:08 +00009353 const int Mask[] = { 1, 0, 3, 2 };
9354 SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
9355 Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
Benjamin Kramer382ed782012-12-25 12:54:19 +00009356
9357 if (Invert)
9358 Result = DAG.getNOT(dl, Result, MVT::v4i32);
9359
9360 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9361 }
Craig Topper2f1b2ec2012-08-13 03:42:38 +00009362 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00009363
Nate Begeman30a0de92008-07-17 16:51:19 +00009364 // Since SSE has no unsigned integer comparisons, we need to flip the sign
9365 // bits of the inputs before performing those operations.
9366 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00009367 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00009368 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
9369 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00009370 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00009371 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
9372 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00009373 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
9374 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00009375 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009376
Dale Johannesenace16102009-02-03 19:33:06 +00009377 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00009378
9379 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00009380 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00009381 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00009382
Nate Begeman30a0de92008-07-17 16:51:19 +00009383 return Result;
9384}
Evan Cheng0488db92007-09-25 01:57:46 +00009385
Craig Topper26827f32013-01-20 09:02:22 +00009386SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
9387
9388 MVT VT = Op.getValueType().getSimpleVT();
9389
9390 if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
9391
9392 assert(VT == MVT::i8 && "SetCC type must be 8-bit integer");
9393 SDValue Op0 = Op.getOperand(0);
9394 SDValue Op1 = Op.getOperand(1);
9395 DebugLoc dl = Op.getDebugLoc();
9396 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
9397
9398 // Optimize to BT if possible.
9399 // Lower (X & (1 << N)) == 0 to BT(X, N).
9400 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
9401 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
9402 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
9403 Op1.getOpcode() == ISD::Constant &&
9404 cast<ConstantSDNode>(Op1)->isNullValue() &&
9405 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9406 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
9407 if (NewSetCC.getNode())
9408 return NewSetCC;
9409 }
9410
9411 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
9412 // these.
9413 if (Op1.getOpcode() == ISD::Constant &&
9414 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
9415 cast<ConstantSDNode>(Op1)->isNullValue()) &&
9416 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
9417
9418 // If the input is a setcc, then reuse the input setcc or use a new one with
9419 // the inverted condition.
9420 if (Op0.getOpcode() == X86ISD::SETCC) {
9421 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
9422 bool Invert = (CC == ISD::SETNE) ^
9423 cast<ConstantSDNode>(Op1)->isNullValue();
9424 if (!Invert) return Op0;
9425
9426 CCode = X86::GetOppositeBranchCondition(CCode);
9427 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9428 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
9429 }
9430 }
9431
9432 bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint();
9433 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
9434 if (X86CC == X86::COND_INVALID)
9435 return SDValue();
9436
9437 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
9438 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
9439 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9440 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
9441}
9442
Evan Cheng370e5342008-12-03 08:38:43 +00009443// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00009444static bool isX86LogicalCmp(SDValue Op) {
9445 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009446 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
9447 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00009448 return true;
9449 if (Op.getResNo() == 1 &&
9450 (Opc == X86ISD::ADD ||
9451 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00009452 Opc == X86ISD::ADC ||
9453 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009454 Opc == X86ISD::SMUL ||
9455 Opc == X86ISD::UMUL ||
9456 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009457 Opc == X86ISD::DEC ||
9458 Opc == X86ISD::OR ||
9459 Opc == X86ISD::XOR ||
9460 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009461 return true;
9462
Chris Lattner9637d5b2010-12-05 07:49:54 +00009463 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9464 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009465
Dan Gohman076aee32009-03-04 19:44:21 +00009466 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009467}
9468
Chris Lattnera2b56002010-12-05 01:23:24 +00009469static bool isZero(SDValue V) {
9470 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9471 return C && C->isNullValue();
9472}
9473
Evan Chengb64dd5f2012-08-07 22:21:00 +00009474static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9475 if (V.getOpcode() != ISD::TRUNCATE)
9476 return false;
9477
9478 SDValue VOp0 = V.getOperand(0);
9479 unsigned InBits = VOp0.getValueSizeInBits();
9480 unsigned Bits = V.getValueSizeInBits();
9481 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9482}
9483
Dan Gohmand858e902010-04-17 15:26:15 +00009484SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009485 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009486 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009487 SDValue Op1 = Op.getOperand(1);
9488 SDValue Op2 = Op.getOperand(2);
9489 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009490 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009491
Dan Gohman1a492952009-10-20 16:22:37 +00009492 if (Cond.getOpcode() == ISD::SETCC) {
9493 SDValue NewCond = LowerSETCC(Cond, DAG);
9494 if (NewCond.getNode())
9495 Cond = NewCond;
9496 }
Evan Cheng734503b2006-09-11 02:19:56 +00009497
Chris Lattnera2b56002010-12-05 01:23:24 +00009498 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009499 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009500 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009501 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009502 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009503 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9504 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009505 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009506
Chris Lattnera2b56002010-12-05 01:23:24 +00009507 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009508
9509 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009510 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9511 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009512
9513 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009514 // Apply further optimizations for special cases
9515 // (select (x != 0), -1, 0) -> neg & sbb
9516 // (select (x == 0), 0, -1) -> neg & sbb
9517 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009518 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009519 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9520 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009521 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9522 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009523 CmpOp0);
9524 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9525 DAG.getConstant(X86::COND_B, MVT::i8),
9526 SDValue(Neg.getNode(), 1));
9527 return Res;
9528 }
9529
Chris Lattnera2b56002010-12-05 01:23:24 +00009530 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9531 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009532 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009533
Chris Lattner96908b12010-12-05 02:00:51 +00009534 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009535 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9536 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009537
Chris Lattner96908b12010-12-05 02:00:51 +00009538 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9539 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009540
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009541 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009542 if (N2C == 0 || !N2C->isNullValue())
9543 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9544 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009545 }
9546 }
9547
Chris Lattnera2b56002010-12-05 01:23:24 +00009548 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009549 if (Cond.getOpcode() == ISD::AND &&
9550 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9551 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009552 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009553 Cond = Cond.getOperand(0);
9554 }
9555
Evan Cheng3f41d662007-10-08 22:16:29 +00009556 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9557 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009558 unsigned CondOpcode = Cond.getOpcode();
9559 if (CondOpcode == X86ISD::SETCC ||
9560 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009561 CC = Cond.getOperand(0);
9562
Dan Gohman475871a2008-07-27 21:46:04 +00009563 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009564 unsigned Opc = Cmp.getOpcode();
Craig Toppera080daf2013-01-20 21:50:27 +00009565 MVT VT = Op.getValueType().getSimpleVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00009566
Evan Cheng3f41d662007-10-08 22:16:29 +00009567 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009568 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009569 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009570 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009571
Chris Lattnerd1980a52009-03-12 06:52:53 +00009572 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9573 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009574 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009575 addTest = false;
9576 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009577 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9578 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9579 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9580 Cond.getOperand(0).getValueType() != MVT::i8)) {
9581 SDValue LHS = Cond.getOperand(0);
9582 SDValue RHS = Cond.getOperand(1);
9583 unsigned X86Opcode;
9584 unsigned X86Cond;
9585 SDVTList VTs;
9586 switch (CondOpcode) {
9587 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9588 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9589 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9590 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9591 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9592 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9593 default: llvm_unreachable("unexpected overflowing operator");
9594 }
9595 if (CondOpcode == ISD::UMULO)
9596 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9597 MVT::i32);
9598 else
9599 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9600
9601 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9602
9603 if (CondOpcode == ISD::UMULO)
9604 Cond = X86Op.getValue(2);
9605 else
9606 Cond = X86Op.getValue(1);
9607
9608 CC = DAG.getConstant(X86Cond, MVT::i8);
9609 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009610 }
9611
9612 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009613 // Look pass the truncate if the high bits are known zero.
9614 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9615 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009616
9617 // We know the result of AND is compared against zero. Try to match
9618 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009619 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009620 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009621 if (NewSetCC.getNode()) {
9622 CC = NewSetCC.getOperand(0);
9623 Cond = NewSetCC.getOperand(1);
9624 addTest = false;
9625 }
9626 }
9627 }
9628
9629 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009630 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009631 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009632 }
9633
Benjamin Kramere915ff32010-12-22 23:09:28 +00009634 // a < b ? -1 : 0 -> RES = ~setcc_carry
9635 // a < b ? 0 : -1 -> RES = setcc_carry
9636 // a >= b ? -1 : 0 -> RES = setcc_carry
9637 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009638 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009639 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009640 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9641
9642 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9643 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9644 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9645 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9646 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9647 return DAG.getNOT(DL, Res, Res.getValueType());
9648 return Res;
9649 }
9650 }
9651
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009652 // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
9653 // widen the cmov and push the truncate through. This avoids introducing a new
9654 // branch during isel and doesn't add any extensions.
9655 if (Op.getValueType() == MVT::i8 &&
9656 Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
9657 SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
9658 if (T1.getValueType() == T2.getValueType() &&
9659 // Blacklist CopyFromReg to avoid partial register stalls.
9660 T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
9661 SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
Benjamin Kramerf8b65aa2012-10-13 12:50:19 +00009662 SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
Benjamin Kramer444dcce2012-10-13 10:39:49 +00009663 return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
9664 }
9665 }
9666
Evan Cheng0488db92007-09-25 01:57:46 +00009667 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9668 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009669 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009670 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009671 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009672}
9673
Nadav Rotem1a330af2012-12-27 22:47:16 +00009674SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op,
9675 SelectionDAG &DAG) const {
Craig Toppera080daf2013-01-20 21:50:27 +00009676 MVT VT = Op->getValueType(0).getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009677 SDValue In = Op->getOperand(0);
Craig Toppera080daf2013-01-20 21:50:27 +00009678 MVT InVT = In.getValueType().getSimpleVT();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009679 DebugLoc dl = Op->getDebugLoc();
9680
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009681 if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
9682 (VT != MVT::v8i32 || InVT != MVT::v8i16))
9683 return SDValue();
Nadav Rotem1a330af2012-12-27 22:47:16 +00009684
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009685 if (Subtarget->hasInt256())
9686 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009687
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009688 // Optimize vectors in AVX mode
9689 // Sign extend v8i16 to v8i32 and
9690 // v4i32 to v4i64
9691 //
9692 // Divide input vector into two parts
9693 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
9694 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
9695 // concat the vectors to original VT
Nadav Rotem1a330af2012-12-27 22:47:16 +00009696
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009697 unsigned NumElems = InVT.getVectorNumElements();
9698 SDValue Undef = DAG.getUNDEF(InVT);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009699
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009700 SmallVector<int,8> ShufMask1(NumElems, -1);
9701 for (unsigned i = 0; i != NumElems/2; ++i)
9702 ShufMask1[i] = i;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009703
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009704 SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009705
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009706 SmallVector<int,8> ShufMask2(NumElems, -1);
9707 for (unsigned i = 0; i != NumElems/2; ++i)
9708 ShufMask2[i] = i + NumElems/2;
Nadav Rotem1a330af2012-12-27 22:47:16 +00009709
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009710 SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009711
Craig Toppera080daf2013-01-20 21:50:27 +00009712 MVT HalfVT = MVT::getVectorVT(VT.getScalarType(),
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009713 VT.getVectorNumElements()/2);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009714
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009715 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
9716 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009717
Nadav Rotem587fb1d2012-12-27 23:08:05 +00009718 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Nadav Rotem1a330af2012-12-27 22:47:16 +00009719}
9720
Evan Cheng370e5342008-12-03 08:38:43 +00009721// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9722// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9723// from the AND / OR.
9724static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9725 Opc = Op.getOpcode();
9726 if (Opc != ISD::OR && Opc != ISD::AND)
9727 return false;
9728 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9729 Op.getOperand(0).hasOneUse() &&
9730 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9731 Op.getOperand(1).hasOneUse());
9732}
9733
Evan Cheng961d6d42009-02-02 08:19:07 +00009734// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9735// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009736static bool isXor1OfSetCC(SDValue Op) {
9737 if (Op.getOpcode() != ISD::XOR)
9738 return false;
9739 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9740 if (N1C && N1C->getAPIntValue() == 1) {
9741 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9742 Op.getOperand(0).hasOneUse();
9743 }
9744 return false;
9745}
9746
Dan Gohmand858e902010-04-17 15:26:15 +00009747SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009748 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009749 SDValue Chain = Op.getOperand(0);
9750 SDValue Cond = Op.getOperand(1);
9751 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009752 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009753 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009754 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009755
Dan Gohman1a492952009-10-20 16:22:37 +00009756 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009757 // Check for setcc([su]{add,sub,mul}o == 0).
9758 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9759 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9760 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9761 Cond.getOperand(0).getResNo() == 1 &&
9762 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9763 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9764 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9765 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9766 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9767 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9768 Inverted = true;
9769 Cond = Cond.getOperand(0);
9770 } else {
9771 SDValue NewCond = LowerSETCC(Cond, DAG);
9772 if (NewCond.getNode())
9773 Cond = NewCond;
9774 }
Dan Gohman1a492952009-10-20 16:22:37 +00009775 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009776#if 0
9777 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009778 else if (Cond.getOpcode() == X86ISD::ADD ||
9779 Cond.getOpcode() == X86ISD::SUB ||
9780 Cond.getOpcode() == X86ISD::SMUL ||
9781 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009782 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009783#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009784
Evan Chengad9c0a32009-12-15 00:53:42 +00009785 // Look pass (and (setcc_carry (cmp ...)), 1).
9786 if (Cond.getOpcode() == ISD::AND &&
9787 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9788 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009789 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009790 Cond = Cond.getOperand(0);
9791 }
9792
Evan Cheng3f41d662007-10-08 22:16:29 +00009793 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9794 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009795 unsigned CondOpcode = Cond.getOpcode();
9796 if (CondOpcode == X86ISD::SETCC ||
9797 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009798 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009799
Dan Gohman475871a2008-07-27 21:46:04 +00009800 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009801 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009802 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009803 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009804 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009805 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009806 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009807 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009808 default: break;
9809 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009810 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009811 // These can only come from an arithmetic instruction with overflow,
9812 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009813 Cond = Cond.getNode()->getOperand(1);
9814 addTest = false;
9815 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009816 }
Evan Cheng0488db92007-09-25 01:57:46 +00009817 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009818 }
9819 CondOpcode = Cond.getOpcode();
9820 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9821 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9822 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9823 Cond.getOperand(0).getValueType() != MVT::i8)) {
9824 SDValue LHS = Cond.getOperand(0);
9825 SDValue RHS = Cond.getOperand(1);
9826 unsigned X86Opcode;
9827 unsigned X86Cond;
9828 SDVTList VTs;
9829 switch (CondOpcode) {
9830 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9831 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9832 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9833 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9834 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9835 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9836 default: llvm_unreachable("unexpected overflowing operator");
9837 }
9838 if (Inverted)
9839 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9840 if (CondOpcode == ISD::UMULO)
9841 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9842 MVT::i32);
9843 else
9844 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9845
9846 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9847
9848 if (CondOpcode == ISD::UMULO)
9849 Cond = X86Op.getValue(2);
9850 else
9851 Cond = X86Op.getValue(1);
9852
9853 CC = DAG.getConstant(X86Cond, MVT::i8);
9854 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009855 } else {
9856 unsigned CondOpc;
9857 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9858 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009859 if (CondOpc == ISD::OR) {
9860 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9861 // two branches instead of an explicit OR instruction with a
9862 // separate test.
9863 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009864 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009865 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009866 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009867 Chain, Dest, CC, Cmp);
9868 CC = Cond.getOperand(1).getOperand(0);
9869 Cond = Cmp;
9870 addTest = false;
9871 }
9872 } else { // ISD::AND
9873 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9874 // two branches instead of an explicit AND instruction with a
9875 // separate test. However, we only do this if this block doesn't
9876 // have a fall-through edge, because this requires an explicit
9877 // jmp when the condition is false.
9878 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009879 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009880 Op.getNode()->hasOneUse()) {
9881 X86::CondCode CCode =
9882 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9883 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009884 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009885 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009886 // Look for an unconditional branch following this conditional branch.
9887 // We need this because we need to reverse the successors in order
9888 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009889 if (User->getOpcode() == ISD::BR) {
9890 SDValue FalseBB = User->getOperand(1);
9891 SDNode *NewBR =
9892 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009893 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009894 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009895 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009896
Dale Johannesene4d209d2009-02-03 20:21:25 +00009897 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009898 Chain, Dest, CC, Cmp);
9899 X86::CondCode CCode =
9900 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9901 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009902 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009903 Cond = Cmp;
9904 addTest = false;
9905 }
9906 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009907 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009908 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9909 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9910 // It should be transformed during dag combiner except when the condition
9911 // is set by a arithmetics with overflow node.
9912 X86::CondCode CCode =
9913 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9914 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009915 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009916 Cond = Cond.getOperand(0).getOperand(1);
9917 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009918 } else if (Cond.getOpcode() == ISD::SETCC &&
9919 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9920 // For FCMP_OEQ, we can emit
9921 // two branches instead of an explicit AND instruction with a
9922 // separate test. However, we only do this if this block doesn't
9923 // have a fall-through edge, because this requires an explicit
9924 // jmp when the condition is false.
9925 if (Op.getNode()->hasOneUse()) {
9926 SDNode *User = *Op.getNode()->use_begin();
9927 // Look for an unconditional branch following this conditional branch.
9928 // We need this because we need to reverse the successors in order
9929 // to implement FCMP_OEQ.
9930 if (User->getOpcode() == ISD::BR) {
9931 SDValue FalseBB = User->getOperand(1);
9932 SDNode *NewBR =
9933 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9934 assert(NewBR == User);
9935 (void)NewBR;
9936 Dest = FalseBB;
9937
9938 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9939 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009940 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009941 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9942 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9943 Chain, Dest, CC, Cmp);
9944 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9945 Cond = Cmp;
9946 addTest = false;
9947 }
9948 }
9949 } else if (Cond.getOpcode() == ISD::SETCC &&
9950 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9951 // For FCMP_UNE, we can emit
9952 // two branches instead of an explicit AND instruction with a
9953 // separate test. However, we only do this if this block doesn't
9954 // have a fall-through edge, because this requires an explicit
9955 // jmp when the condition is false.
9956 if (Op.getNode()->hasOneUse()) {
9957 SDNode *User = *Op.getNode()->use_begin();
9958 // Look for an unconditional branch following this conditional branch.
9959 // We need this because we need to reverse the successors in order
9960 // to implement FCMP_UNE.
9961 if (User->getOpcode() == ISD::BR) {
9962 SDValue FalseBB = User->getOperand(1);
9963 SDNode *NewBR =
9964 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9965 assert(NewBR == User);
9966 (void)NewBR;
9967
9968 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9969 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009970 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009971 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9972 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9973 Chain, Dest, CC, Cmp);
9974 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9975 Cond = Cmp;
9976 addTest = false;
9977 Dest = FalseBB;
9978 }
9979 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009980 }
Evan Cheng0488db92007-09-25 01:57:46 +00009981 }
9982
9983 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009984 // Look pass the truncate if the high bits are known zero.
9985 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9986 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009987
9988 // We know the result of AND is compared against zero. Try to match
9989 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009990 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009991 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9992 if (NewSetCC.getNode()) {
9993 CC = NewSetCC.getOperand(0);
9994 Cond = NewSetCC.getOperand(1);
9995 addTest = false;
9996 }
9997 }
9998 }
9999
10000 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010001 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +000010002 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +000010003 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +000010004 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010005 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +000010006 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +000010007}
10008
Anton Korobeynikove060b532007-04-17 19:34:00 +000010009// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
10010// Calls to _alloca is needed to probe the stack when allocating more than 4k
10011// bytes in one go. Touching the stack at 4K increments is necessary to ensure
10012// that the guard pages used by the OS virtual memory manager are allocated in
10013// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +000010014SDValue
10015X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010016 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010017 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010018 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010019 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +000010020 "are being used");
10021 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010022 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010023
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010024 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +000010025 SDValue Chain = Op.getOperand(0);
10026 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010027 // FIXME: Ensure alignment here
10028
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010029 bool Is64Bit = Subtarget->is64Bit();
10030 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010031
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010032 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010033 MachineFunction &MF = DAG.getMachineFunction();
10034 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010035
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010036 if (Is64Bit) {
10037 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +000010038 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010039 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010040
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010041 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +000010042 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010043 if (I->hasNestAttr())
10044 report_fatal_error("Cannot use segmented stacks with functions that "
10045 "have nested arguments.");
10046 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +000010047
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010048 const TargetRegisterClass *AddrRegClass =
10049 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
10050 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
10051 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
10052 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
10053 DAG.getRegister(Vreg, SPTy));
10054 SDValue Ops1[2] = { Value, Chain };
10055 return DAG.getMergeValues(Ops1, 2, dl);
10056 } else {
10057 SDValue Flag;
10058 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010059
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010060 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
10061 Flag = Chain.getValue(1);
10062 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +000010063
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010064 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
10065 Flag = Chain.getValue(1);
10066
Michael Liaoc5c970e2012-10-31 04:14:09 +000010067 Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
10068 SPTy).getValue(1);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000010069
10070 SDValue Ops1[2] = { Chain.getValue(0), Chain };
10071 return DAG.getMergeValues(Ops1, 2, dl);
10072 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000010073}
10074
Dan Gohmand858e902010-04-17 15:26:15 +000010075SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +000010076 MachineFunction &MF = DAG.getMachineFunction();
10077 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
10078
Dan Gohman69de1932008-02-06 22:27:42 +000010079 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +000010080 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +000010081
Anton Korobeynikove7beda12010-10-03 22:52:07 +000010082 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +000010083 // vastart just stores the address of the VarArgsFrameIndex slot into the
10084 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +000010085 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10086 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010087 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
10088 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010089 }
10090
10091 // __va_list_tag:
10092 // gp_offset (0 - 6 * 8)
10093 // fp_offset (48 - 48 + 8 * 16)
10094 // overflow_arg_area (point to parameters coming in memory).
10095 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +000010096 SmallVector<SDValue, 8> MemOps;
10097 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +000010098 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010099 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010100 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
10101 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010102 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010103 MemOps.push_back(Store);
10104
10105 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +000010106 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010107 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010108 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +000010109 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
10110 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010111 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010112 MemOps.push_back(Store);
10113
10114 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +000010115 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010116 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +000010117 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
10118 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010119 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
10120 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +000010121 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010122 MemOps.push_back(Store);
10123
10124 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +000010125 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010126 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +000010127 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
10128 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010129 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
10130 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +000010131 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010132 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010133 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +000010134}
10135
Dan Gohmand858e902010-04-17 15:26:15 +000010136SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +000010137 assert(Subtarget->is64Bit() &&
10138 "LowerVAARG only handles 64-bit va_arg!");
10139 assert((Subtarget->isTargetLinux() ||
10140 Subtarget->isTargetDarwin()) &&
10141 "Unhandled target in LowerVAARG");
10142 assert(Op.getNode()->getNumOperands() == 4);
10143 SDValue Chain = Op.getOperand(0);
10144 SDValue SrcPtr = Op.getOperand(1);
10145 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
10146 unsigned Align = Op.getConstantOperandVal(3);
10147 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +000010148
Dan Gohman320afb82010-10-12 18:00:49 +000010149 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010150 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +000010151 uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy);
Dan Gohman320afb82010-10-12 18:00:49 +000010152 uint8_t ArgMode;
10153
10154 // Decide which area this value should be read from.
10155 // TODO: Implement the AMD64 ABI in its entirety. This simple
10156 // selection mechanism works only for the basic types.
10157 if (ArgVT == MVT::f80) {
10158 llvm_unreachable("va_arg for f80 not yet implemented");
10159 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
10160 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
10161 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
10162 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
10163 } else {
10164 llvm_unreachable("Unhandled argument type in LowerVAARG");
10165 }
10166
10167 if (ArgMode == 2) {
10168 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000010169 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +000010170 !(DAG.getMachineFunction()
Bill Wendling831737d2012-12-30 10:32:01 +000010171 .getFunction()->getAttributes()
10172 .hasAttribute(AttributeSet::FunctionIndex,
10173 Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000010174 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +000010175 }
10176
10177 // Insert VAARG_64 node into the DAG
10178 // VAARG_64 returns two values: Variable Argument Address, Chain
10179 SmallVector<SDValue, 11> InstOps;
10180 InstOps.push_back(Chain);
10181 InstOps.push_back(SrcPtr);
10182 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
10183 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
10184 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
10185 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
10186 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
10187 VTs, &InstOps[0], InstOps.size(),
10188 MVT::i64,
10189 MachinePointerInfo(SV),
10190 /*Align=*/0,
10191 /*Volatile=*/false,
10192 /*ReadMem=*/true,
10193 /*WriteMem=*/true);
10194 Chain = VAARG.getValue(1);
10195
10196 // Load the next argument and return it
10197 return DAG.getLoad(ArgVT, dl,
10198 Chain,
10199 VAARG,
10200 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010201 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +000010202}
10203
Craig Topper55b24052012-09-11 06:15:32 +000010204static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
10205 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +000010206 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +000010207 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +000010208 SDValue Chain = Op.getOperand(0);
10209 SDValue DstPtr = Op.getOperand(1);
10210 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +000010211 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
10212 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +000010213 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +000010214
Chris Lattnere72f2022010-09-21 05:40:29 +000010215 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +000010216 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +000010217 false,
Chris Lattnere72f2022010-09-21 05:40:29 +000010218 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +000010219}
10220
Craig Topperff3139f2013-02-19 07:43:59 +000010221// getTargetVShiftNode - Handle vector element shifts where the shift amount
Craig Topper80e46362012-01-23 06:16:53 +000010222// may or may not be a constant. Takes immediate version of shift as input.
10223static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
10224 SDValue SrcOp, SDValue ShAmt,
10225 SelectionDAG &DAG) {
10226 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
10227
10228 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +000010229 // Constant may be a TargetConstant. Use a regular constant.
10230 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +000010231 switch (Opc) {
10232 default: llvm_unreachable("Unknown target vector shift node");
10233 case X86ISD::VSHLI:
10234 case X86ISD::VSRLI:
10235 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +000010236 return DAG.getNode(Opc, dl, VT, SrcOp,
10237 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +000010238 }
10239 }
10240
10241 // Change opcode to non-immediate version
10242 switch (Opc) {
10243 default: llvm_unreachable("Unknown target vector shift node");
10244 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
10245 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
10246 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
10247 }
10248
10249 // Need to build a vector containing shift amount
10250 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
10251 SDValue ShOps[4];
10252 ShOps[0] = ShAmt;
10253 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +000010254 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +000010255 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +000010256
10257 // The return type has to be a 128-bit type with the same element
10258 // type as the input type.
10259 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10260 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
10261
10262 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +000010263 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
10264}
10265
Craig Topper55b24052012-09-11 06:15:32 +000010266static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010267 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010268 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +000010269 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +000010270 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +000010271 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +000010272 case Intrinsic::x86_sse_comieq_ss:
10273 case Intrinsic::x86_sse_comilt_ss:
10274 case Intrinsic::x86_sse_comile_ss:
10275 case Intrinsic::x86_sse_comigt_ss:
10276 case Intrinsic::x86_sse_comige_ss:
10277 case Intrinsic::x86_sse_comineq_ss:
10278 case Intrinsic::x86_sse_ucomieq_ss:
10279 case Intrinsic::x86_sse_ucomilt_ss:
10280 case Intrinsic::x86_sse_ucomile_ss:
10281 case Intrinsic::x86_sse_ucomigt_ss:
10282 case Intrinsic::x86_sse_ucomige_ss:
10283 case Intrinsic::x86_sse_ucomineq_ss:
10284 case Intrinsic::x86_sse2_comieq_sd:
10285 case Intrinsic::x86_sse2_comilt_sd:
10286 case Intrinsic::x86_sse2_comile_sd:
10287 case Intrinsic::x86_sse2_comigt_sd:
10288 case Intrinsic::x86_sse2_comige_sd:
10289 case Intrinsic::x86_sse2_comineq_sd:
10290 case Intrinsic::x86_sse2_ucomieq_sd:
10291 case Intrinsic::x86_sse2_ucomilt_sd:
10292 case Intrinsic::x86_sse2_ucomile_sd:
10293 case Intrinsic::x86_sse2_ucomigt_sd:
10294 case Intrinsic::x86_sse2_ucomige_sd:
10295 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +000010296 unsigned Opc;
10297 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +000010298 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +000010299 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010300 case Intrinsic::x86_sse_comieq_ss:
10301 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010302 Opc = X86ISD::COMI;
10303 CC = ISD::SETEQ;
10304 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010305 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010306 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010307 Opc = X86ISD::COMI;
10308 CC = ISD::SETLT;
10309 break;
10310 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010311 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010312 Opc = X86ISD::COMI;
10313 CC = ISD::SETLE;
10314 break;
10315 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010316 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010317 Opc = X86ISD::COMI;
10318 CC = ISD::SETGT;
10319 break;
10320 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010321 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010322 Opc = X86ISD::COMI;
10323 CC = ISD::SETGE;
10324 break;
10325 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010326 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010327 Opc = X86ISD::COMI;
10328 CC = ISD::SETNE;
10329 break;
10330 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010331 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010332 Opc = X86ISD::UCOMI;
10333 CC = ISD::SETEQ;
10334 break;
10335 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010336 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010337 Opc = X86ISD::UCOMI;
10338 CC = ISD::SETLT;
10339 break;
10340 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010341 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010342 Opc = X86ISD::UCOMI;
10343 CC = ISD::SETLE;
10344 break;
10345 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010346 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010347 Opc = X86ISD::UCOMI;
10348 CC = ISD::SETGT;
10349 break;
10350 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +000010351 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +000010352 Opc = X86ISD::UCOMI;
10353 CC = ISD::SETGE;
10354 break;
10355 case Intrinsic::x86_sse_ucomineq_ss:
10356 case Intrinsic::x86_sse2_ucomineq_sd:
10357 Opc = X86ISD::UCOMI;
10358 CC = ISD::SETNE;
10359 break;
Evan Cheng6be2c582006-04-05 23:38:46 +000010360 }
Evan Cheng734503b2006-09-11 02:19:56 +000010361
Dan Gohman475871a2008-07-27 21:46:04 +000010362 SDValue LHS = Op.getOperand(1);
10363 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +000010364 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +000010365 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +000010366 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
10367 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10368 DAG.getConstant(X86CC, MVT::i8), Cond);
10369 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +000010370 }
Craig Topper6d688152012-08-14 07:43:25 +000010371
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010372 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +000010373 case Intrinsic::x86_sse2_pmulu_dq:
10374 case Intrinsic::x86_avx2_pmulu_dq:
10375 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
10376 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010377
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000010378 // SSE2/AVX2 sub with unsigned saturation intrinsics
10379 case Intrinsic::x86_sse2_psubus_b:
10380 case Intrinsic::x86_sse2_psubus_w:
10381 case Intrinsic::x86_avx2_psubus_b:
10382 case Intrinsic::x86_avx2_psubus_w:
10383 return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(),
10384 Op.getOperand(1), Op.getOperand(2));
10385
Craig Topper6d688152012-08-14 07:43:25 +000010386 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010387 case Intrinsic::x86_sse3_hadd_ps:
10388 case Intrinsic::x86_sse3_hadd_pd:
10389 case Intrinsic::x86_avx_hadd_ps_256:
10390 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +000010391 case Intrinsic::x86_sse3_hsub_ps:
10392 case Intrinsic::x86_sse3_hsub_pd:
10393 case Intrinsic::x86_avx_hsub_ps_256:
10394 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +000010395 case Intrinsic::x86_ssse3_phadd_w_128:
10396 case Intrinsic::x86_ssse3_phadd_d_128:
10397 case Intrinsic::x86_avx2_phadd_w:
10398 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +000010399 case Intrinsic::x86_ssse3_phsub_w_128:
10400 case Intrinsic::x86_ssse3_phsub_d_128:
10401 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +000010402 case Intrinsic::x86_avx2_phsub_d: {
10403 unsigned Opcode;
10404 switch (IntNo) {
10405 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10406 case Intrinsic::x86_sse3_hadd_ps:
10407 case Intrinsic::x86_sse3_hadd_pd:
10408 case Intrinsic::x86_avx_hadd_ps_256:
10409 case Intrinsic::x86_avx_hadd_pd_256:
10410 Opcode = X86ISD::FHADD;
10411 break;
10412 case Intrinsic::x86_sse3_hsub_ps:
10413 case Intrinsic::x86_sse3_hsub_pd:
10414 case Intrinsic::x86_avx_hsub_ps_256:
10415 case Intrinsic::x86_avx_hsub_pd_256:
10416 Opcode = X86ISD::FHSUB;
10417 break;
10418 case Intrinsic::x86_ssse3_phadd_w_128:
10419 case Intrinsic::x86_ssse3_phadd_d_128:
10420 case Intrinsic::x86_avx2_phadd_w:
10421 case Intrinsic::x86_avx2_phadd_d:
10422 Opcode = X86ISD::HADD;
10423 break;
10424 case Intrinsic::x86_ssse3_phsub_w_128:
10425 case Intrinsic::x86_ssse3_phsub_d_128:
10426 case Intrinsic::x86_avx2_phsub_w:
10427 case Intrinsic::x86_avx2_phsub_d:
10428 Opcode = X86ISD::HSUB;
10429 break;
10430 }
10431 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +000010432 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010433 }
10434
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010435 // SSE2/SSE41/AVX2 integer max/min intrinsics.
10436 case Intrinsic::x86_sse2_pmaxu_b:
10437 case Intrinsic::x86_sse41_pmaxuw:
10438 case Intrinsic::x86_sse41_pmaxud:
10439 case Intrinsic::x86_avx2_pmaxu_b:
10440 case Intrinsic::x86_avx2_pmaxu_w:
10441 case Intrinsic::x86_avx2_pmaxu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010442 case Intrinsic::x86_sse2_pminu_b:
10443 case Intrinsic::x86_sse41_pminuw:
10444 case Intrinsic::x86_sse41_pminud:
10445 case Intrinsic::x86_avx2_pminu_b:
10446 case Intrinsic::x86_avx2_pminu_w:
10447 case Intrinsic::x86_avx2_pminu_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010448 case Intrinsic::x86_sse41_pmaxsb:
10449 case Intrinsic::x86_sse2_pmaxs_w:
10450 case Intrinsic::x86_sse41_pmaxsd:
10451 case Intrinsic::x86_avx2_pmaxs_b:
10452 case Intrinsic::x86_avx2_pmaxs_w:
10453 case Intrinsic::x86_avx2_pmaxs_d:
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010454 case Intrinsic::x86_sse41_pminsb:
10455 case Intrinsic::x86_sse2_pmins_w:
10456 case Intrinsic::x86_sse41_pminsd:
10457 case Intrinsic::x86_avx2_pmins_b:
10458 case Intrinsic::x86_avx2_pmins_w:
Craig Topper6f57f392012-12-29 17:19:06 +000010459 case Intrinsic::x86_avx2_pmins_d: {
10460 unsigned Opcode;
10461 switch (IntNo) {
10462 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10463 case Intrinsic::x86_sse2_pmaxu_b:
10464 case Intrinsic::x86_sse41_pmaxuw:
10465 case Intrinsic::x86_sse41_pmaxud:
10466 case Intrinsic::x86_avx2_pmaxu_b:
10467 case Intrinsic::x86_avx2_pmaxu_w:
10468 case Intrinsic::x86_avx2_pmaxu_d:
10469 Opcode = X86ISD::UMAX;
10470 break;
10471 case Intrinsic::x86_sse2_pminu_b:
10472 case Intrinsic::x86_sse41_pminuw:
10473 case Intrinsic::x86_sse41_pminud:
10474 case Intrinsic::x86_avx2_pminu_b:
10475 case Intrinsic::x86_avx2_pminu_w:
10476 case Intrinsic::x86_avx2_pminu_d:
10477 Opcode = X86ISD::UMIN;
10478 break;
10479 case Intrinsic::x86_sse41_pmaxsb:
10480 case Intrinsic::x86_sse2_pmaxs_w:
10481 case Intrinsic::x86_sse41_pmaxsd:
10482 case Intrinsic::x86_avx2_pmaxs_b:
10483 case Intrinsic::x86_avx2_pmaxs_w:
10484 case Intrinsic::x86_avx2_pmaxs_d:
10485 Opcode = X86ISD::SMAX;
10486 break;
10487 case Intrinsic::x86_sse41_pminsb:
10488 case Intrinsic::x86_sse2_pmins_w:
10489 case Intrinsic::x86_sse41_pminsd:
10490 case Intrinsic::x86_avx2_pmins_b:
10491 case Intrinsic::x86_avx2_pmins_w:
10492 case Intrinsic::x86_avx2_pmins_d:
10493 Opcode = X86ISD::SMIN;
10494 break;
10495 }
10496 return DAG.getNode(Opcode, dl, Op.getValueType(),
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010497 Op.getOperand(1), Op.getOperand(2));
Craig Topper6f57f392012-12-29 17:19:06 +000010498 }
Benjamin Kramer739c7a82012-12-21 14:04:55 +000010499
Craig Topper6d183e42012-12-29 16:44:25 +000010500 // SSE/SSE2/AVX floating point max/min intrinsics.
10501 case Intrinsic::x86_sse_max_ps:
10502 case Intrinsic::x86_sse2_max_pd:
10503 case Intrinsic::x86_avx_max_ps_256:
10504 case Intrinsic::x86_avx_max_pd_256:
10505 case Intrinsic::x86_sse_min_ps:
10506 case Intrinsic::x86_sse2_min_pd:
10507 case Intrinsic::x86_avx_min_ps_256:
10508 case Intrinsic::x86_avx_min_pd_256: {
10509 unsigned Opcode;
10510 switch (IntNo) {
10511 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10512 case Intrinsic::x86_sse_max_ps:
10513 case Intrinsic::x86_sse2_max_pd:
10514 case Intrinsic::x86_avx_max_ps_256:
10515 case Intrinsic::x86_avx_max_pd_256:
10516 Opcode = X86ISD::FMAX;
10517 break;
10518 case Intrinsic::x86_sse_min_ps:
10519 case Intrinsic::x86_sse2_min_pd:
10520 case Intrinsic::x86_avx_min_ps_256:
10521 case Intrinsic::x86_avx_min_pd_256:
10522 Opcode = X86ISD::FMIN;
10523 break;
10524 }
10525 return DAG.getNode(Opcode, dl, Op.getValueType(),
10526 Op.getOperand(1), Op.getOperand(2));
10527 }
10528
Craig Topper6d688152012-08-14 07:43:25 +000010529 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +000010530 case Intrinsic::x86_avx2_psllv_d:
10531 case Intrinsic::x86_avx2_psllv_q:
10532 case Intrinsic::x86_avx2_psllv_d_256:
10533 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010534 case Intrinsic::x86_avx2_psrlv_d:
10535 case Intrinsic::x86_avx2_psrlv_q:
10536 case Intrinsic::x86_avx2_psrlv_d_256:
10537 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +000010538 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +000010539 case Intrinsic::x86_avx2_psrav_d_256: {
10540 unsigned Opcode;
10541 switch (IntNo) {
10542 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10543 case Intrinsic::x86_avx2_psllv_d:
10544 case Intrinsic::x86_avx2_psllv_q:
10545 case Intrinsic::x86_avx2_psllv_d_256:
10546 case Intrinsic::x86_avx2_psllv_q_256:
10547 Opcode = ISD::SHL;
10548 break;
10549 case Intrinsic::x86_avx2_psrlv_d:
10550 case Intrinsic::x86_avx2_psrlv_q:
10551 case Intrinsic::x86_avx2_psrlv_d_256:
10552 case Intrinsic::x86_avx2_psrlv_q_256:
10553 Opcode = ISD::SRL;
10554 break;
10555 case Intrinsic::x86_avx2_psrav_d:
10556 case Intrinsic::x86_avx2_psrav_d_256:
10557 Opcode = ISD::SRA;
10558 break;
10559 }
10560 return DAG.getNode(Opcode, dl, Op.getValueType(),
10561 Op.getOperand(1), Op.getOperand(2));
10562 }
10563
Craig Topper969ba282012-01-25 06:43:11 +000010564 case Intrinsic::x86_ssse3_pshuf_b_128:
10565 case Intrinsic::x86_avx2_pshuf_b:
10566 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
10567 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010568
Craig Topper969ba282012-01-25 06:43:11 +000010569 case Intrinsic::x86_ssse3_psign_b_128:
10570 case Intrinsic::x86_ssse3_psign_w_128:
10571 case Intrinsic::x86_ssse3_psign_d_128:
10572 case Intrinsic::x86_avx2_psign_b:
10573 case Intrinsic::x86_avx2_psign_w:
10574 case Intrinsic::x86_avx2_psign_d:
10575 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
10576 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010577
Craig Toppere566cd02012-01-26 07:18:03 +000010578 case Intrinsic::x86_sse41_insertps:
10579 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
10580 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010581
Craig Toppere566cd02012-01-26 07:18:03 +000010582 case Intrinsic::x86_avx_vperm2f128_ps_256:
10583 case Intrinsic::x86_avx_vperm2f128_pd_256:
10584 case Intrinsic::x86_avx_vperm2f128_si_256:
10585 case Intrinsic::x86_avx2_vperm2i128:
10586 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
10587 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +000010588
Craig Topperffa6c402012-04-16 07:13:00 +000010589 case Intrinsic::x86_avx2_permd:
10590 case Intrinsic::x86_avx2_permps:
10591 // Operands intentionally swapped. Mask is last operand to intrinsic,
10592 // but second operand for node/intruction.
10593 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
10594 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +000010595
Craig Topper22d8f0d2012-12-29 18:18:20 +000010596 case Intrinsic::x86_sse_sqrt_ps:
10597 case Intrinsic::x86_sse2_sqrt_pd:
10598 case Intrinsic::x86_avx_sqrt_ps_256:
10599 case Intrinsic::x86_avx_sqrt_pd_256:
10600 return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1));
10601
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010602 // ptest and testp intrinsics. The intrinsic these come from are designed to
10603 // return an integer value, not just an instruction so lower it to the ptest
10604 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +000010605 case Intrinsic::x86_sse41_ptestz:
10606 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010607 case Intrinsic::x86_sse41_ptestnzc:
10608 case Intrinsic::x86_avx_ptestz_256:
10609 case Intrinsic::x86_avx_ptestc_256:
10610 case Intrinsic::x86_avx_ptestnzc_256:
10611 case Intrinsic::x86_avx_vtestz_ps:
10612 case Intrinsic::x86_avx_vtestc_ps:
10613 case Intrinsic::x86_avx_vtestnzc_ps:
10614 case Intrinsic::x86_avx_vtestz_pd:
10615 case Intrinsic::x86_avx_vtestc_pd:
10616 case Intrinsic::x86_avx_vtestnzc_pd:
10617 case Intrinsic::x86_avx_vtestz_ps_256:
10618 case Intrinsic::x86_avx_vtestc_ps_256:
10619 case Intrinsic::x86_avx_vtestnzc_ps_256:
10620 case Intrinsic::x86_avx_vtestz_pd_256:
10621 case Intrinsic::x86_avx_vtestc_pd_256:
10622 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10623 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010624 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010625 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010626 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010627 case Intrinsic::x86_avx_vtestz_ps:
10628 case Intrinsic::x86_avx_vtestz_pd:
10629 case Intrinsic::x86_avx_vtestz_ps_256:
10630 case Intrinsic::x86_avx_vtestz_pd_256:
10631 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010632 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010633 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010634 // ZF = 1
10635 X86CC = X86::COND_E;
10636 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010637 case Intrinsic::x86_avx_vtestc_ps:
10638 case Intrinsic::x86_avx_vtestc_pd:
10639 case Intrinsic::x86_avx_vtestc_ps_256:
10640 case Intrinsic::x86_avx_vtestc_pd_256:
10641 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010642 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010643 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010644 // CF = 1
10645 X86CC = X86::COND_B;
10646 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010647 case Intrinsic::x86_avx_vtestnzc_ps:
10648 case Intrinsic::x86_avx_vtestnzc_pd:
10649 case Intrinsic::x86_avx_vtestnzc_ps_256:
10650 case Intrinsic::x86_avx_vtestnzc_pd_256:
10651 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010652 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010653 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010654 // ZF and CF = 0
10655 X86CC = X86::COND_A;
10656 break;
10657 }
Eric Christopherfd179292009-08-27 18:07:15 +000010658
Eric Christopher71c67532009-07-29 00:28:05 +000010659 SDValue LHS = Op.getOperand(1);
10660 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010661 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10662 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010663 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10664 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10665 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010666 }
Evan Cheng5759f972008-05-04 09:15:50 +000010667
Craig Topper80e46362012-01-23 06:16:53 +000010668 // SSE/AVX shift intrinsics
10669 case Intrinsic::x86_sse2_psll_w:
10670 case Intrinsic::x86_sse2_psll_d:
10671 case Intrinsic::x86_sse2_psll_q:
10672 case Intrinsic::x86_avx2_psll_w:
10673 case Intrinsic::x86_avx2_psll_d:
10674 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010675 case Intrinsic::x86_sse2_psrl_w:
10676 case Intrinsic::x86_sse2_psrl_d:
10677 case Intrinsic::x86_sse2_psrl_q:
10678 case Intrinsic::x86_avx2_psrl_w:
10679 case Intrinsic::x86_avx2_psrl_d:
10680 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010681 case Intrinsic::x86_sse2_psra_w:
10682 case Intrinsic::x86_sse2_psra_d:
10683 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010684 case Intrinsic::x86_avx2_psra_d: {
10685 unsigned Opcode;
10686 switch (IntNo) {
10687 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10688 case Intrinsic::x86_sse2_psll_w:
10689 case Intrinsic::x86_sse2_psll_d:
10690 case Intrinsic::x86_sse2_psll_q:
10691 case Intrinsic::x86_avx2_psll_w:
10692 case Intrinsic::x86_avx2_psll_d:
10693 case Intrinsic::x86_avx2_psll_q:
10694 Opcode = X86ISD::VSHL;
10695 break;
10696 case Intrinsic::x86_sse2_psrl_w:
10697 case Intrinsic::x86_sse2_psrl_d:
10698 case Intrinsic::x86_sse2_psrl_q:
10699 case Intrinsic::x86_avx2_psrl_w:
10700 case Intrinsic::x86_avx2_psrl_d:
10701 case Intrinsic::x86_avx2_psrl_q:
10702 Opcode = X86ISD::VSRL;
10703 break;
10704 case Intrinsic::x86_sse2_psra_w:
10705 case Intrinsic::x86_sse2_psra_d:
10706 case Intrinsic::x86_avx2_psra_w:
10707 case Intrinsic::x86_avx2_psra_d:
10708 Opcode = X86ISD::VSRA;
10709 break;
10710 }
10711 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010712 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010713 }
10714
10715 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010716 case Intrinsic::x86_sse2_pslli_w:
10717 case Intrinsic::x86_sse2_pslli_d:
10718 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010719 case Intrinsic::x86_avx2_pslli_w:
10720 case Intrinsic::x86_avx2_pslli_d:
10721 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010722 case Intrinsic::x86_sse2_psrli_w:
10723 case Intrinsic::x86_sse2_psrli_d:
10724 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010725 case Intrinsic::x86_avx2_psrli_w:
10726 case Intrinsic::x86_avx2_psrli_d:
10727 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010728 case Intrinsic::x86_sse2_psrai_w:
10729 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010730 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010731 case Intrinsic::x86_avx2_psrai_d: {
10732 unsigned Opcode;
10733 switch (IntNo) {
10734 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10735 case Intrinsic::x86_sse2_pslli_w:
10736 case Intrinsic::x86_sse2_pslli_d:
10737 case Intrinsic::x86_sse2_pslli_q:
10738 case Intrinsic::x86_avx2_pslli_w:
10739 case Intrinsic::x86_avx2_pslli_d:
10740 case Intrinsic::x86_avx2_pslli_q:
10741 Opcode = X86ISD::VSHLI;
10742 break;
10743 case Intrinsic::x86_sse2_psrli_w:
10744 case Intrinsic::x86_sse2_psrli_d:
10745 case Intrinsic::x86_sse2_psrli_q:
10746 case Intrinsic::x86_avx2_psrli_w:
10747 case Intrinsic::x86_avx2_psrli_d:
10748 case Intrinsic::x86_avx2_psrli_q:
10749 Opcode = X86ISD::VSRLI;
10750 break;
10751 case Intrinsic::x86_sse2_psrai_w:
10752 case Intrinsic::x86_sse2_psrai_d:
10753 case Intrinsic::x86_avx2_psrai_w:
10754 case Intrinsic::x86_avx2_psrai_d:
10755 Opcode = X86ISD::VSRAI;
10756 break;
10757 }
10758 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010759 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010760 }
10761
Craig Topper4feb6472012-08-06 06:22:36 +000010762 case Intrinsic::x86_sse42_pcmpistria128:
10763 case Intrinsic::x86_sse42_pcmpestria128:
10764 case Intrinsic::x86_sse42_pcmpistric128:
10765 case Intrinsic::x86_sse42_pcmpestric128:
10766 case Intrinsic::x86_sse42_pcmpistrio128:
10767 case Intrinsic::x86_sse42_pcmpestrio128:
10768 case Intrinsic::x86_sse42_pcmpistris128:
10769 case Intrinsic::x86_sse42_pcmpestris128:
10770 case Intrinsic::x86_sse42_pcmpistriz128:
10771 case Intrinsic::x86_sse42_pcmpestriz128: {
10772 unsigned Opcode;
10773 unsigned X86CC;
10774 switch (IntNo) {
10775 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10776 case Intrinsic::x86_sse42_pcmpistria128:
10777 Opcode = X86ISD::PCMPISTRI;
10778 X86CC = X86::COND_A;
10779 break;
10780 case Intrinsic::x86_sse42_pcmpestria128:
10781 Opcode = X86ISD::PCMPESTRI;
10782 X86CC = X86::COND_A;
10783 break;
10784 case Intrinsic::x86_sse42_pcmpistric128:
10785 Opcode = X86ISD::PCMPISTRI;
10786 X86CC = X86::COND_B;
10787 break;
10788 case Intrinsic::x86_sse42_pcmpestric128:
10789 Opcode = X86ISD::PCMPESTRI;
10790 X86CC = X86::COND_B;
10791 break;
10792 case Intrinsic::x86_sse42_pcmpistrio128:
10793 Opcode = X86ISD::PCMPISTRI;
10794 X86CC = X86::COND_O;
10795 break;
10796 case Intrinsic::x86_sse42_pcmpestrio128:
10797 Opcode = X86ISD::PCMPESTRI;
10798 X86CC = X86::COND_O;
10799 break;
10800 case Intrinsic::x86_sse42_pcmpistris128:
10801 Opcode = X86ISD::PCMPISTRI;
10802 X86CC = X86::COND_S;
10803 break;
10804 case Intrinsic::x86_sse42_pcmpestris128:
10805 Opcode = X86ISD::PCMPESTRI;
10806 X86CC = X86::COND_S;
10807 break;
10808 case Intrinsic::x86_sse42_pcmpistriz128:
10809 Opcode = X86ISD::PCMPISTRI;
10810 X86CC = X86::COND_E;
10811 break;
10812 case Intrinsic::x86_sse42_pcmpestriz128:
10813 Opcode = X86ISD::PCMPESTRI;
10814 X86CC = X86::COND_E;
10815 break;
10816 }
10817 SmallVector<SDValue, 5> NewOps;
10818 NewOps.append(Op->op_begin()+1, Op->op_end());
10819 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10820 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10821 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10822 DAG.getConstant(X86CC, MVT::i8),
10823 SDValue(PCMP.getNode(), 1));
10824 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10825 }
Craig Topper6d688152012-08-14 07:43:25 +000010826
Craig Topper4feb6472012-08-06 06:22:36 +000010827 case Intrinsic::x86_sse42_pcmpistri128:
10828 case Intrinsic::x86_sse42_pcmpestri128: {
10829 unsigned Opcode;
10830 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10831 Opcode = X86ISD::PCMPISTRI;
10832 else
10833 Opcode = X86ISD::PCMPESTRI;
10834
10835 SmallVector<SDValue, 5> NewOps;
10836 NewOps.append(Op->op_begin()+1, Op->op_end());
10837 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10838 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10839 }
Craig Topper0e292372012-08-24 04:03:22 +000010840 case Intrinsic::x86_fma_vfmadd_ps:
10841 case Intrinsic::x86_fma_vfmadd_pd:
10842 case Intrinsic::x86_fma_vfmsub_ps:
10843 case Intrinsic::x86_fma_vfmsub_pd:
10844 case Intrinsic::x86_fma_vfnmadd_ps:
10845 case Intrinsic::x86_fma_vfnmadd_pd:
10846 case Intrinsic::x86_fma_vfnmsub_ps:
10847 case Intrinsic::x86_fma_vfnmsub_pd:
10848 case Intrinsic::x86_fma_vfmaddsub_ps:
10849 case Intrinsic::x86_fma_vfmaddsub_pd:
10850 case Intrinsic::x86_fma_vfmsubadd_ps:
10851 case Intrinsic::x86_fma_vfmsubadd_pd:
10852 case Intrinsic::x86_fma_vfmadd_ps_256:
10853 case Intrinsic::x86_fma_vfmadd_pd_256:
10854 case Intrinsic::x86_fma_vfmsub_ps_256:
10855 case Intrinsic::x86_fma_vfmsub_pd_256:
10856 case Intrinsic::x86_fma_vfnmadd_ps_256:
10857 case Intrinsic::x86_fma_vfnmadd_pd_256:
10858 case Intrinsic::x86_fma_vfnmsub_ps_256:
10859 case Intrinsic::x86_fma_vfnmsub_pd_256:
10860 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10861 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10862 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10863 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010864 unsigned Opc;
10865 switch (IntNo) {
10866 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10867 case Intrinsic::x86_fma_vfmadd_ps:
10868 case Intrinsic::x86_fma_vfmadd_pd:
10869 case Intrinsic::x86_fma_vfmadd_ps_256:
10870 case Intrinsic::x86_fma_vfmadd_pd_256:
10871 Opc = X86ISD::FMADD;
10872 break;
10873 case Intrinsic::x86_fma_vfmsub_ps:
10874 case Intrinsic::x86_fma_vfmsub_pd:
10875 case Intrinsic::x86_fma_vfmsub_ps_256:
10876 case Intrinsic::x86_fma_vfmsub_pd_256:
10877 Opc = X86ISD::FMSUB;
10878 break;
10879 case Intrinsic::x86_fma_vfnmadd_ps:
10880 case Intrinsic::x86_fma_vfnmadd_pd:
10881 case Intrinsic::x86_fma_vfnmadd_ps_256:
10882 case Intrinsic::x86_fma_vfnmadd_pd_256:
10883 Opc = X86ISD::FNMADD;
10884 break;
10885 case Intrinsic::x86_fma_vfnmsub_ps:
10886 case Intrinsic::x86_fma_vfnmsub_pd:
10887 case Intrinsic::x86_fma_vfnmsub_ps_256:
10888 case Intrinsic::x86_fma_vfnmsub_pd_256:
10889 Opc = X86ISD::FNMSUB;
10890 break;
10891 case Intrinsic::x86_fma_vfmaddsub_ps:
10892 case Intrinsic::x86_fma_vfmaddsub_pd:
10893 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10894 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10895 Opc = X86ISD::FMADDSUB;
10896 break;
10897 case Intrinsic::x86_fma_vfmsubadd_ps:
10898 case Intrinsic::x86_fma_vfmsubadd_pd:
10899 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10900 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10901 Opc = X86ISD::FMSUBADD;
10902 break;
10903 }
10904
10905 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10906 Op.getOperand(2), Op.getOperand(3));
10907 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010908 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010909}
Evan Cheng72261582005-12-20 06:22:03 +000010910
Craig Topper55b24052012-09-11 06:15:32 +000010911static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010912 DebugLoc dl = Op.getDebugLoc();
10913 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10914 switch (IntNo) {
10915 default: return SDValue(); // Don't custom lower most intrinsics.
10916
10917 // RDRAND intrinsics.
10918 case Intrinsic::x86_rdrand_16:
10919 case Intrinsic::x86_rdrand_32:
10920 case Intrinsic::x86_rdrand_64: {
10921 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010922 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10923 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010924
10925 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10926 // return the value from Rand, which is always 0, casted to i32.
10927 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10928 DAG.getConstant(1, Op->getValueType(1)),
10929 DAG.getConstant(X86::COND_B, MVT::i32),
10930 SDValue(Result.getNode(), 1) };
10931 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10932 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10933 Ops, 4);
10934
10935 // Return { result, isValid, chain }.
10936 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010937 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010938 }
Michael Liaof8fd8832013-03-26 22:47:01 +000010939
10940 // XTEST intrinsics.
10941 case Intrinsic::x86_xtest: {
10942 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Other);
10943 SDValue InTrans = DAG.getNode(X86ISD::XTEST, dl, VTs, Op.getOperand(0));
10944 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10945 DAG.getConstant(X86::COND_NE, MVT::i8),
10946 InTrans);
10947 SDValue Ret = DAG.getNode(ISD::ZERO_EXTEND, dl, Op->getValueType(0), SetCC);
10948 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(),
10949 Ret, SDValue(InTrans.getNode(), 1));
10950 }
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010951 }
10952}
10953
Dan Gohmand858e902010-04-17 15:26:15 +000010954SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10955 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010956 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10957 MFI->setReturnAddressIsTaken(true);
10958
Bill Wendling64e87322009-01-16 19:25:27 +000010959 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010960 DebugLoc dl = Op.getDebugLoc();
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010961 EVT PtrVT = getPointerTy();
Bill Wendling64e87322009-01-16 19:25:27 +000010962
10963 if (Depth > 0) {
10964 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10965 SDValue Offset =
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010966 DAG.getConstant(RegInfo->getSlotSize(), PtrVT);
10967 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
10968 DAG.getNode(ISD::ADD, dl, PtrVT,
Dale Johannesene4d209d2009-02-03 20:21:25 +000010969 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010970 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010971 }
10972
10973 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010974 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010975 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010976 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010977}
10978
Dan Gohmand858e902010-04-17 15:26:15 +000010979SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010980 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10981 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010982
Owen Andersone50ed302009-08-10 22:56:29 +000010983 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010984 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010985 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10986 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010987 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010988 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010989 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10990 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010991 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010992 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010993}
10994
Dan Gohman475871a2008-07-27 21:46:04 +000010995SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010996 SelectionDAG &DAG) const {
Michael Liaoaa3c2c02012-10-25 06:29:14 +000010997 return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010998}
10999
Dan Gohmand858e902010-04-17 15:26:15 +000011000SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011001 SDValue Chain = Op.getOperand(0);
11002 SDValue Offset = Op.getOperand(1);
11003 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011004 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011005
Dan Gohmand8816272010-08-11 18:14:00 +000011006 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
11007 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
11008 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000011009 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011010
Dan Gohmand8816272010-08-11 18:14:00 +000011011 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
Michael Liaoaa3c2c02012-10-25 06:29:14 +000011012 DAG.getIntPtrConstant(RegInfo->getSlotSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011013 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000011014 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
11015 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000011016 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011017
Dale Johannesene4d209d2009-02-03 20:21:25 +000011018 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011019 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000011020 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011021}
11022
Michael Liao6c0e04c2012-10-15 22:39:43 +000011023SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
11024 SelectionDAG &DAG) const {
11025 DebugLoc DL = Op.getDebugLoc();
11026 return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
11027 DAG.getVTList(MVT::i32, MVT::Other),
11028 Op.getOperand(0), Op.getOperand(1));
11029}
11030
11031SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
11032 SelectionDAG &DAG) const {
11033 DebugLoc DL = Op.getDebugLoc();
11034 return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
11035 Op.getOperand(0), Op.getOperand(1));
11036}
11037
Craig Topper55b24052012-09-11 06:15:32 +000011038static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000011039 return Op.getOperand(0);
11040}
11041
11042SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
11043 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000011044 SDValue Root = Op.getOperand(0);
11045 SDValue Trmp = Op.getOperand(1); // trampoline
11046 SDValue FPtr = Op.getOperand(2); // nested function
11047 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011048 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011049
Dan Gohman69de1932008-02-06 22:27:42 +000011050 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Michael Liao7abf67a2012-10-04 19:50:43 +000011051 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011052
11053 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000011054 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000011055
11056 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000011057 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
11058 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000011059
Michael Liao7abf67a2012-10-04 19:50:43 +000011060 const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
11061 const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
Duncan Sands339e14f2008-01-16 22:55:25 +000011062
11063 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
11064
11065 // Load the pointer to the nested function into R11.
11066 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000011067 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000011068 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011069 Addr, MachinePointerInfo(TrmpAddr),
11070 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011071
Owen Anderson825b72b2009-08-11 20:47:22 +000011072 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11073 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011074 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
11075 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000011076 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011077
11078 // Load the 'nest' parameter value into R10.
11079 // R10 is specified in X86CallingConv.td
11080 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000011081 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11082 DAG.getConstant(10, MVT::i64));
11083 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011084 Addr, MachinePointerInfo(TrmpAddr, 10),
11085 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011086
Owen Anderson825b72b2009-08-11 20:47:22 +000011087 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11088 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011089 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
11090 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000011091 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000011092
11093 // Jump to the nested function.
11094 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000011095 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11096 DAG.getConstant(20, MVT::i64));
11097 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011098 Addr, MachinePointerInfo(TrmpAddr, 20),
11099 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011100
11101 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000011102 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
11103 DAG.getConstant(22, MVT::i64));
11104 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011105 MachinePointerInfo(TrmpAddr, 22),
11106 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000011107
Duncan Sands4a544a72011-09-06 13:37:06 +000011108 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011109 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000011110 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000011111 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000011112 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000011113 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011114
11115 switch (CC) {
11116 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011117 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011118 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011119 case CallingConv::X86_StdCall: {
11120 // Pass 'nest' parameter in ECX.
11121 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011122 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011123
11124 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011125 FunctionType *FTy = Func->getFunctionType();
Bill Wendling99faa3b2012-12-07 23:16:57 +000011126 const AttributeSet &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000011127
Chris Lattner58d74912008-03-12 17:45:29 +000011128 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000011129 unsigned InRegCount = 0;
11130 unsigned Idx = 1;
11131
11132 for (FunctionType::param_iterator I = FTy->param_begin(),
11133 E = FTy->param_end(); I != E; ++I, ++Idx)
Bill Wendling94e94b32012-12-30 13:50:49 +000011134 if (Attrs.hasAttribute(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000011135 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000011136 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011137
11138 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000011139 report_fatal_error("Nest register in use - reduce number of inreg"
11140 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000011141 }
11142 }
11143 break;
11144 }
11145 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000011146 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000011147 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000011148 // Pass 'nest' parameter in EAX.
11149 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000011150 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011151 break;
11152 }
11153
Dan Gohman475871a2008-07-27 21:46:04 +000011154 SDValue OutChains[4];
11155 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000011156
Owen Anderson825b72b2009-08-11 20:47:22 +000011157 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11158 DAG.getConstant(10, MVT::i32));
11159 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011160
Chris Lattnera62fe662010-02-05 19:20:30 +000011161 // This is storing the opcode for MOV32ri.
11162 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Michael Liao7abf67a2012-10-04 19:50:43 +000011163 const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
Scott Michelfdc40a02009-02-17 22:15:04 +000011164 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000011165 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000011166 Trmp, MachinePointerInfo(TrmpAddr),
11167 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011168
Owen Anderson825b72b2009-08-11 20:47:22 +000011169 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11170 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011171 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
11172 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000011173 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011174
Chris Lattnera62fe662010-02-05 19:20:30 +000011175 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000011176 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11177 DAG.getConstant(5, MVT::i32));
11178 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011179 MachinePointerInfo(TrmpAddr, 5),
11180 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011181
Owen Anderson825b72b2009-08-11 20:47:22 +000011182 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
11183 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000011184 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
11185 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000011186 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011187
Duncan Sands4a544a72011-09-06 13:37:06 +000011188 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000011189 }
11190}
11191
Dan Gohmand858e902010-04-17 15:26:15 +000011192SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
11193 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011194 /*
11195 The rounding mode is in bits 11:10 of FPSR, and has the following
11196 settings:
11197 00 Round to nearest
11198 01 Round to -inf
11199 10 Round to +inf
11200 11 Round to 0
11201
11202 FLT_ROUNDS, on the other hand, expects the following:
11203 -1 Undefined
11204 0 Round to 0
11205 1 Round to nearest
11206 2 Round to +inf
11207 3 Round to -inf
11208
11209 To perform the conversion, we do:
11210 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
11211 */
11212
11213 MachineFunction &MF = DAG.getMachineFunction();
11214 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000011215 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011216 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000011217 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000011218 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011219
11220 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000011221 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000011222 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011223
Chris Lattner2156b792010-09-22 01:11:26 +000011224 MachineMemOperand *MMO =
11225 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
11226 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011227
Chris Lattner2156b792010-09-22 01:11:26 +000011228 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
11229 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
11230 DAG.getVTList(MVT::Other),
11231 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011232
11233 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000011234 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000011235 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011236
11237 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000011238 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000011239 DAG.getNode(ISD::SRL, DL, MVT::i16,
11240 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011241 CWD, DAG.getConstant(0x800, MVT::i16)),
11242 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000011243 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000011244 DAG.getNode(ISD::SRL, DL, MVT::i16,
11245 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000011246 CWD, DAG.getConstant(0x400, MVT::i16)),
11247 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011248
Dan Gohman475871a2008-07-27 21:46:04 +000011249 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000011250 DAG.getNode(ISD::AND, DL, MVT::i16,
11251 DAG.getNode(ISD::ADD, DL, MVT::i16,
11252 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000011253 DAG.getConstant(1, MVT::i16)),
11254 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011255
Duncan Sands83ec4b62008-06-06 12:08:01 +000011256 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000011257 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011258}
11259
Craig Topper55b24052012-09-11 06:15:32 +000011260static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011261 EVT VT = Op.getValueType();
11262 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011263 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011264 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011265
11266 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000011267 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000011268 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000011269 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000011270 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011271 }
Evan Cheng18efe262007-12-14 02:13:44 +000011272
Evan Cheng152804e2007-12-14 08:30:15 +000011273 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011274 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011275 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011276
11277 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011278 SDValue Ops[] = {
11279 Op,
11280 DAG.getConstant(NumBits+NumBits-1, OpVT),
11281 DAG.getConstant(X86::COND_E, MVT::i8),
11282 Op.getValue(1)
11283 };
11284 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000011285
11286 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000011287 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000011288
Owen Anderson825b72b2009-08-11 20:47:22 +000011289 if (VT == MVT::i8)
11290 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000011291 return Op;
11292}
11293
Craig Topper55b24052012-09-11 06:15:32 +000011294static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000011295 EVT VT = Op.getValueType();
11296 EVT OpVT = VT;
11297 unsigned NumBits = VT.getSizeInBits();
11298 DebugLoc dl = Op.getDebugLoc();
11299
11300 Op = Op.getOperand(0);
11301 if (VT == MVT::i8) {
11302 // Zero extend to i32 since there is not an i8 bsr.
11303 OpVT = MVT::i32;
11304 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
11305 }
11306
11307 // Issue a bsr (scan bits in reverse).
11308 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
11309 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
11310
11311 // And xor with NumBits-1.
11312 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
11313
11314 if (VT == MVT::i8)
11315 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
11316 return Op;
11317}
11318
Craig Topper55b24052012-09-11 06:15:32 +000011319static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011320 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000011321 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011322 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000011323 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000011324
11325 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000011326 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011327 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000011328
11329 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011330 SDValue Ops[] = {
11331 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000011332 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000011333 DAG.getConstant(X86::COND_E, MVT::i8),
11334 Op.getValue(1)
11335 };
Chandler Carruth77821022011-12-24 12:12:34 +000011336 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000011337}
11338
Craig Topper13894fa2011-08-24 06:14:18 +000011339// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
11340// ones, and then concatenate the result back.
11341static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011342 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000011343
Craig Topper7a9a28b2012-08-12 02:23:29 +000011344 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011345 "Unsupported value type for operation");
11346
Craig Topper66ddd152012-04-27 22:54:43 +000011347 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000011348 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011349
11350 // Extract the LHS vectors
11351 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011352 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11353 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011354
11355 // Extract the RHS vectors
11356 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000011357 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
11358 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000011359
11360 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11361 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11362
11363 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
11364 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
11365 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
11366}
11367
Craig Topper55b24052012-09-11 06:15:32 +000011368static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011369 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011370 Op.getValueType().isInteger() &&
11371 "Only handle AVX 256-bit vector integer operation");
11372 return Lower256IntArith(Op, DAG);
11373}
11374
Craig Topper55b24052012-09-11 06:15:32 +000011375static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000011376 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000011377 Op.getValueType().isInteger() &&
11378 "Only handle AVX 256-bit vector integer operation");
11379 return Lower256IntArith(Op, DAG);
11380}
11381
Craig Topper55b24052012-09-11 06:15:32 +000011382static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
11383 SelectionDAG &DAG) {
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011384 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000011385 EVT VT = Op.getValueType();
11386
11387 // Decompose 256-bit ops into smaller 128-bit ops.
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011388 if (VT.is256BitVector() && !Subtarget->hasInt256())
Craig Topper13894fa2011-08-24 06:14:18 +000011389 return Lower256IntArith(Op, DAG);
11390
Benjamin Kramer2f8a6cd2012-12-22 16:07:56 +000011391 SDValue A = Op.getOperand(0);
11392 SDValue B = Op.getOperand(1);
11393
11394 // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
11395 if (VT == MVT::v4i32) {
11396 assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
11397 "Should not custom lower when pmuldq is available!");
11398
11399 // Extract the odd parts.
11400 const int UnpackMask[] = { 1, -1, 3, -1 };
11401 SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
11402 SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
11403
11404 // Multiply the even parts.
11405 SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
11406 // Now multiply odd parts.
11407 SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
11408
11409 Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens);
11410 Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds);
11411
11412 // Merge the two vectors back together with a shuffle. This expands into 2
11413 // shuffles.
11414 const int ShufMask[] = { 0, 4, 2, 6 };
11415 return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
11416 }
11417
Craig Topper5b209e82012-02-05 03:14:49 +000011418 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
11419 "Only know how to lower V2I64/V4I64 multiply");
11420
Craig Topper5b209e82012-02-05 03:14:49 +000011421 // Ahi = psrlqi(a, 32);
11422 // Bhi = psrlqi(b, 32);
11423 //
11424 // AloBlo = pmuludq(a, b);
11425 // AloBhi = pmuludq(a, Bhi);
11426 // AhiBlo = pmuludq(Ahi, b);
11427
11428 // AloBhi = psllqi(AloBhi, 32);
11429 // AhiBlo = psllqi(AhiBlo, 32);
11430 // return AloBlo + AloBhi + AhiBlo;
11431
Craig Topper5b209e82012-02-05 03:14:49 +000011432 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000011433
Craig Topper5b209e82012-02-05 03:14:49 +000011434 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
11435 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000011436
Craig Topper5b209e82012-02-05 03:14:49 +000011437 // Bit cast to 32-bit vectors for MULUDQ
11438 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
11439 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
11440 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
11441 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
11442 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000011443
Craig Topper5b209e82012-02-05 03:14:49 +000011444 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
11445 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
11446 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000011447
Craig Topper5b209e82012-02-05 03:14:49 +000011448 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
11449 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011450
Dale Johannesene4d209d2009-02-03 20:21:25 +000011451 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000011452 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000011453}
11454
Nadav Rotem13f8cf52013-01-09 05:14:33 +000011455SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
11456 EVT VT = Op.getValueType();
11457 EVT EltTy = VT.getVectorElementType();
11458 unsigned NumElts = VT.getVectorNumElements();
11459 SDValue N0 = Op.getOperand(0);
11460 DebugLoc dl = Op.getDebugLoc();
11461
11462 // Lower sdiv X, pow2-const.
11463 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1));
11464 if (!C)
11465 return SDValue();
11466
11467 APInt SplatValue, SplatUndef;
11468 unsigned MinSplatBits;
11469 bool HasAnyUndefs;
11470 if (!C->isConstantSplat(SplatValue, SplatUndef, MinSplatBits, HasAnyUndefs))
11471 return SDValue();
11472
11473 if ((SplatValue != 0) &&
11474 (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) {
11475 unsigned lg2 = SplatValue.countTrailingZeros();
11476 // Splat the sign bit.
11477 SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32);
11478 SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG);
11479 // Add (N0 < 0) ? abs2 - 1 : 0;
11480 SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32);
11481 SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG);
11482 SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL);
11483 SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32);
11484 SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG);
11485
11486 // If we're dividing by a positive value, we're done. Otherwise, we must
11487 // negate the result.
11488 if (SplatValue.isNonNegative())
11489 return SRA;
11490
11491 SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy));
11492 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts);
11493 return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA);
11494 }
11495 return SDValue();
11496}
11497
Michael Liao4b7ab122013-03-20 02:20:36 +000011498static SDValue LowerScalarImmediateShift(SDValue Op, SelectionDAG &DAG,
11499 const X86Subtarget *Subtarget) {
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011500 EVT VT = Op.getValueType();
11501 DebugLoc dl = Op.getDebugLoc();
11502 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000011503 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011504
Nadav Rotem43012222011-05-11 08:12:09 +000011505 // Optimize shl/srl/sra with constant shift amount.
11506 if (isSplatVector(Amt.getNode())) {
11507 SDValue SclrAmt = Amt->getOperand(0);
11508 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
11509 uint64_t ShiftAmt = C->getZExtValue();
11510
Craig Toppered2e13d2012-01-22 19:15:14 +000011511 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011512 (Subtarget->hasInt256() &&
Craig Toppered2e13d2012-01-22 19:15:14 +000011513 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
11514 if (Op.getOpcode() == ISD::SHL)
11515 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11516 DAG.getConstant(ShiftAmt, MVT::i32));
11517 if (Op.getOpcode() == ISD::SRL)
11518 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11519 DAG.getConstant(ShiftAmt, MVT::i32));
11520 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
11521 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11522 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000011523 }
11524
Craig Toppered2e13d2012-01-22 19:15:14 +000011525 if (VT == MVT::v16i8) {
11526 if (Op.getOpcode() == ISD::SHL) {
11527 // Make a large shift.
11528 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
11529 DAG.getConstant(ShiftAmt, MVT::i32));
11530 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
11531 // Zero out the rightmost bits.
11532 SmallVector<SDValue, 16> V(16,
11533 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11534 MVT::i8));
11535 return DAG.getNode(ISD::AND, dl, VT, SHL,
11536 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011537 }
Craig Toppered2e13d2012-01-22 19:15:14 +000011538 if (Op.getOpcode() == ISD::SRL) {
11539 // Make a large shift.
11540 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
11541 DAG.getConstant(ShiftAmt, MVT::i32));
11542 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
11543 // Zero out the leftmost bits.
11544 SmallVector<SDValue, 16> V(16,
11545 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11546 MVT::i8));
11547 return DAG.getNode(ISD::AND, dl, VT, SRL,
11548 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
11549 }
11550 if (Op.getOpcode() == ISD::SRA) {
11551 if (ShiftAmt == 7) {
11552 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011553 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011554 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000011555 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011556
Craig Toppered2e13d2012-01-22 19:15:14 +000011557 // R s>> a === ((R u>> a) ^ m) - m
11558 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11559 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
11560 MVT::i8));
11561 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
11562 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11563 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11564 return Res;
11565 }
Craig Topper731dfd02012-04-23 03:42:40 +000011566 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000011567 }
Craig Topper46154eb2011-11-11 07:39:23 +000011568
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000011569 if (Subtarget->hasInt256() && VT == MVT::v32i8) {
Craig Topper0d86d462011-11-20 00:12:05 +000011570 if (Op.getOpcode() == ISD::SHL) {
11571 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011572 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
11573 DAG.getConstant(ShiftAmt, MVT::i32));
11574 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000011575 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011576 SmallVector<SDValue, 32> V(32,
11577 DAG.getConstant(uint8_t(-1U << ShiftAmt),
11578 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011579 return DAG.getNode(ISD::AND, dl, VT, SHL,
11580 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000011581 }
Craig Topper0d86d462011-11-20 00:12:05 +000011582 if (Op.getOpcode() == ISD::SRL) {
11583 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000011584 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
11585 DAG.getConstant(ShiftAmt, MVT::i32));
11586 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000011587 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000011588 SmallVector<SDValue, 32> V(32,
11589 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
11590 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000011591 return DAG.getNode(ISD::AND, dl, VT, SRL,
11592 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
11593 }
11594 if (Op.getOpcode() == ISD::SRA) {
11595 if (ShiftAmt == 7) {
11596 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000011597 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000011598 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000011599 }
11600
11601 // R s>> a === ((R u>> a) ^ m) - m
11602 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
11603 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
11604 MVT::i8));
11605 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
11606 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
11607 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
11608 return Res;
11609 }
Craig Topper731dfd02012-04-23 03:42:40 +000011610 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000011611 }
Nadav Rotem43012222011-05-11 08:12:09 +000011612 }
11613 }
11614
Michael Liao42317cc2013-03-20 02:33:21 +000011615 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11616 if (!Subtarget->is64Bit() &&
11617 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11618 Amt.getOpcode() == ISD::BITCAST &&
11619 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11620 Amt = Amt.getOperand(0);
11621 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11622 VT.getVectorNumElements();
11623 unsigned RatioInLog2 = Log2_32_Ceil(Ratio);
11624 uint64_t ShiftAmt = 0;
11625 for (unsigned i = 0; i != Ratio; ++i) {
11626 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Amt.getOperand(i));
11627 if (C == 0)
11628 return SDValue();
11629 // 6 == Log2(64)
11630 ShiftAmt |= C->getZExtValue() << (i * (1 << (6 - RatioInLog2)));
11631 }
11632 // Check remaining shift amounts.
11633 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11634 uint64_t ShAmt = 0;
11635 for (unsigned j = 0; j != Ratio; ++j) {
11636 ConstantSDNode *C =
11637 dyn_cast<ConstantSDNode>(Amt.getOperand(i + j));
11638 if (C == 0)
11639 return SDValue();
11640 // 6 == Log2(64)
11641 ShAmt |= C->getZExtValue() << (j * (1 << (6 - RatioInLog2)));
11642 }
11643 if (ShAmt != ShiftAmt)
11644 return SDValue();
11645 }
11646 switch (Op.getOpcode()) {
11647 default:
11648 llvm_unreachable("Unknown shift opcode!");
11649 case ISD::SHL:
11650 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
11651 DAG.getConstant(ShiftAmt, MVT::i32));
11652 case ISD::SRL:
11653 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
11654 DAG.getConstant(ShiftAmt, MVT::i32));
11655 case ISD::SRA:
11656 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
11657 DAG.getConstant(ShiftAmt, MVT::i32));
11658 }
11659 }
11660
11661 return SDValue();
11662}
11663
11664static SDValue LowerScalarVariableShift(SDValue Op, SelectionDAG &DAG,
11665 const X86Subtarget* Subtarget) {
11666 EVT VT = Op.getValueType();
11667 DebugLoc dl = Op.getDebugLoc();
11668 SDValue R = Op.getOperand(0);
11669 SDValue Amt = Op.getOperand(1);
11670
11671 if ((VT == MVT::v2i64 && Op.getOpcode() != ISD::SRA) ||
11672 VT == MVT::v4i32 || VT == MVT::v8i16 ||
11673 (Subtarget->hasInt256() &&
11674 ((VT == MVT::v4i64 && Op.getOpcode() != ISD::SRA) ||
11675 VT == MVT::v8i32 || VT == MVT::v16i16))) {
11676 SDValue BaseShAmt;
11677 EVT EltVT = VT.getVectorElementType();
11678
11679 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11680 unsigned NumElts = VT.getVectorNumElements();
11681 unsigned i, j;
11682 for (i = 0; i != NumElts; ++i) {
11683 if (Amt.getOperand(i).getOpcode() == ISD::UNDEF)
11684 continue;
11685 break;
11686 }
11687 for (j = i; j != NumElts; ++j) {
11688 SDValue Arg = Amt.getOperand(j);
11689 if (Arg.getOpcode() == ISD::UNDEF) continue;
11690 if (Arg != Amt.getOperand(i))
11691 break;
11692 }
11693 if (i != NumElts && j == NumElts)
11694 BaseShAmt = Amt.getOperand(i);
11695 } else {
11696 if (Amt.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11697 Amt = Amt.getOperand(0);
11698 if (Amt.getOpcode() == ISD::VECTOR_SHUFFLE &&
11699 cast<ShuffleVectorSDNode>(Amt)->isSplat()) {
11700 SDValue InVec = Amt.getOperand(0);
11701 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11702 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11703 unsigned i = 0;
11704 for (; i != NumElts; ++i) {
11705 SDValue Arg = InVec.getOperand(i);
11706 if (Arg.getOpcode() == ISD::UNDEF) continue;
11707 BaseShAmt = Arg;
11708 break;
11709 }
11710 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11711 if (ConstantSDNode *C =
11712 dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
11713 unsigned SplatIdx =
11714 cast<ShuffleVectorSDNode>(Amt)->getSplatIndex();
11715 if (C->getZExtValue() == SplatIdx)
11716 BaseShAmt = InVec.getOperand(1);
11717 }
11718 }
11719 if (BaseShAmt.getNode() == 0)
11720 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Amt,
11721 DAG.getIntPtrConstant(0));
11722 }
11723 }
11724
11725 if (BaseShAmt.getNode()) {
11726 if (EltVT.bitsGT(MVT::i32))
11727 BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt);
11728 else if (EltVT.bitsLT(MVT::i32))
11729 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, BaseShAmt);
11730
11731 switch (Op.getOpcode()) {
11732 default:
11733 llvm_unreachable("Unknown shift opcode!");
11734 case ISD::SHL:
11735 switch (VT.getSimpleVT().SimpleTy) {
11736 default: return SDValue();
11737 case MVT::v2i64:
11738 case MVT::v4i32:
11739 case MVT::v8i16:
11740 case MVT::v4i64:
11741 case MVT::v8i32:
11742 case MVT::v16i16:
11743 return getTargetVShiftNode(X86ISD::VSHLI, dl, VT, R, BaseShAmt, DAG);
11744 }
11745 case ISD::SRA:
11746 switch (VT.getSimpleVT().SimpleTy) {
11747 default: return SDValue();
11748 case MVT::v4i32:
11749 case MVT::v8i16:
11750 case MVT::v8i32:
11751 case MVT::v16i16:
11752 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, R, BaseShAmt, DAG);
11753 }
11754 case ISD::SRL:
11755 switch (VT.getSimpleVT().SimpleTy) {
11756 default: return SDValue();
11757 case MVT::v2i64:
11758 case MVT::v4i32:
11759 case MVT::v8i16:
11760 case MVT::v4i64:
11761 case MVT::v8i32:
11762 case MVT::v16i16:
11763 return getTargetVShiftNode(X86ISD::VSRLI, dl, VT, R, BaseShAmt, DAG);
11764 }
11765 }
11766 }
11767 }
11768
11769 // Special case in 32-bit mode, where i64 is expanded into high and low parts.
11770 if (!Subtarget->is64Bit() &&
11771 (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
11772 Amt.getOpcode() == ISD::BITCAST &&
11773 Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
11774 Amt = Amt.getOperand(0);
11775 unsigned Ratio = Amt.getValueType().getVectorNumElements() /
11776 VT.getVectorNumElements();
11777 std::vector<SDValue> Vals(Ratio);
11778 for (unsigned i = 0; i != Ratio; ++i)
11779 Vals[i] = Amt.getOperand(i);
11780 for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
11781 for (unsigned j = 0; j != Ratio; ++j)
11782 if (Vals[j] != Amt.getOperand(i + j))
11783 return SDValue();
11784 }
11785 switch (Op.getOpcode()) {
11786 default:
11787 llvm_unreachable("Unknown shift opcode!");
11788 case ISD::SHL:
11789 return DAG.getNode(X86ISD::VSHL, dl, VT, R, Op.getOperand(1));
11790 case ISD::SRL:
11791 return DAG.getNode(X86ISD::VSRL, dl, VT, R, Op.getOperand(1));
11792 case ISD::SRA:
11793 return DAG.getNode(X86ISD::VSRA, dl, VT, R, Op.getOperand(1));
11794 }
11795 }
11796
Michael Liao4b7ab122013-03-20 02:20:36 +000011797 return SDValue();
11798}
11799
11800SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
11801
11802 EVT VT = Op.getValueType();
11803 DebugLoc dl = Op.getDebugLoc();
11804 SDValue R = Op.getOperand(0);
11805 SDValue Amt = Op.getOperand(1);
11806 SDValue V;
11807
11808 if (!Subtarget->hasSSE2())
11809 return SDValue();
11810
11811 V = LowerScalarImmediateShift(Op, DAG, Subtarget);
11812 if (V.getNode())
11813 return V;
11814
Michael Liao42317cc2013-03-20 02:33:21 +000011815 V = LowerScalarVariableShift(Op, DAG, Subtarget);
11816 if (V.getNode())
11817 return V;
11818
Michael Liao5c5f1902013-03-20 02:28:20 +000011819 // AVX2 has VPSLLV/VPSRAV/VPSRLV.
11820 if (Subtarget->hasInt256()) {
11821 if (Op.getOpcode() == ISD::SRL &&
11822 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
11823 VT == MVT::v4i64 || VT == MVT::v8i32))
11824 return Op;
11825 if (Op.getOpcode() == ISD::SHL &&
11826 (VT == MVT::v2i64 || VT == MVT::v4i32 ||
11827 VT == MVT::v4i64 || VT == MVT::v8i32))
11828 return Op;
11829 if (Op.getOpcode() == ISD::SRA && (VT == MVT::v4i32 || VT == MVT::v8i32))
11830 return Op;
11831 }
11832
Nadav Rotem43012222011-05-11 08:12:09 +000011833 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000011834 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011835 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));
Nate Begeman51409212010-07-28 00:21:48 +000011836
Benjamin Kramer9fa92512013-02-04 15:19:25 +000011837 Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011838 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011839 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
11840 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
11841 }
Nadav Rotem43012222011-05-11 08:12:09 +000011842 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000011843 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000011844
Nate Begeman51409212010-07-28 00:21:48 +000011845 // a = a << 5;
Benjamin Kramera220aeb2013-02-04 15:19:33 +000011846 Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT));
Craig Toppered2e13d2012-01-22 19:15:14 +000011847 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000011848
Lang Hames8b99c1e2011-12-17 01:08:46 +000011849 // Turn 'a' into a mask suitable for VSELECT
11850 SDValue VSelM = DAG.getConstant(0x80, VT);
11851 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011852 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000011853
Lang Hames8b99c1e2011-12-17 01:08:46 +000011854 SDValue CM1 = DAG.getConstant(0x0f, VT);
11855 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000011856
Lang Hames8b99c1e2011-12-17 01:08:46 +000011857 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
11858 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000011859 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11860 DAG.getConstant(4, MVT::i32), DAG);
11861 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011862 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11863
Nate Begeman51409212010-07-28 00:21:48 +000011864 // a += a
11865 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011866 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011867 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011868
Lang Hames8b99c1e2011-12-17 01:08:46 +000011869 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
11870 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011871 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
11872 DAG.getConstant(2, MVT::i32), DAG);
11873 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011874 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
11875
Nate Begeman51409212010-07-28 00:21:48 +000011876 // a += a
11877 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000011878 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000011879 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000011880
Lang Hames8b99c1e2011-12-17 01:08:46 +000011881 // return VSELECT(r, r+r, a);
11882 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000011883 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000011884 return R;
11885 }
Craig Topper46154eb2011-11-11 07:39:23 +000011886
11887 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000011888 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000011889 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000011890 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11891 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
11892
11893 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000011894 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
11895 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011896
11897 // Recreate the shift amount vectors
11898 SDValue Amt1, Amt2;
11899 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
11900 // Constant shift amount
11901 SmallVector<SDValue, 4> Amt1Csts;
11902 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000011903 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011904 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000011905 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000011906 Amt2Csts.push_back(Amt->getOperand(i));
11907
11908 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11909 &Amt1Csts[0], NumElems/2);
11910 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
11911 &Amt2Csts[0], NumElems/2);
11912 } else {
11913 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000011914 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
11915 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000011916 }
11917
11918 // Issue new vector shifts for the smaller types
11919 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11920 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11921
11922 // Concatenate the result back
11923 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11924 }
11925
Nate Begeman51409212010-07-28 00:21:48 +000011926 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011927}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011928
Craig Topper55b24052012-09-11 06:15:32 +000011929static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011930 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11931 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011932 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11933 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011934 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011935 SDValue LHS = N->getOperand(0);
11936 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011937 unsigned BaseOp = 0;
11938 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011939 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011940 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011941 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011942 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011943 // A subtract of one will be selected as a INC. Note that INC doesn't
11944 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011945 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11946 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011947 BaseOp = X86ISD::INC;
11948 Cond = X86::COND_O;
11949 break;
11950 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011951 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011952 Cond = X86::COND_O;
11953 break;
11954 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011955 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011956 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011957 break;
11958 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011959 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11960 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011961 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11962 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011963 BaseOp = X86ISD::DEC;
11964 Cond = X86::COND_O;
11965 break;
11966 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011967 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011968 Cond = X86::COND_O;
11969 break;
11970 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011971 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011972 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011973 break;
11974 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011975 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011976 Cond = X86::COND_O;
11977 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011978 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11979 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11980 MVT::i32);
11981 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011982
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011983 SDValue SetCC =
11984 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11985 DAG.getConstant(X86::COND_O, MVT::i32),
11986 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011987
Dan Gohman6e5fda22011-07-22 18:45:15 +000011988 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011989 }
Bill Wendling74c37652008-12-09 22:08:41 +000011990 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011991
Bill Wendling61edeb52008-12-02 01:06:39 +000011992 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011993 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011994 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011995
Bill Wendling61edeb52008-12-02 01:06:39 +000011996 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011997 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11998 DAG.getConstant(Cond, MVT::i32),
11999 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000012000
Dan Gohman6e5fda22011-07-22 18:45:15 +000012001 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000012002}
12003
Chad Rosier30450e82011-12-22 22:35:21 +000012004SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
12005 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012006 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000012007 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
12008 EVT VT = Op.getValueType();
12009
Craig Toppered2e13d2012-01-22 19:15:14 +000012010 if (!Subtarget->hasSSE2() || !VT.isVector())
12011 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012012
Craig Toppered2e13d2012-01-22 19:15:14 +000012013 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
12014 ExtraVT.getScalarType().getSizeInBits();
12015 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
12016
12017 switch (VT.getSimpleVT().SimpleTy) {
12018 default: return SDValue();
12019 case MVT::v8i32:
12020 case MVT::v16i16:
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012021 if (!Subtarget->hasFp256())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012022 return SDValue();
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012023 if (!Subtarget->hasInt256()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000012024 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000012025 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000012026
Craig Toppered2e13d2012-01-22 19:15:14 +000012027 // Extract the LHS vectors
12028 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000012029 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
12030 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000012031
Craig Toppered2e13d2012-01-22 19:15:14 +000012032 MVT EltVT = VT.getVectorElementType().getSimpleVT();
12033 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000012034
Craig Toppered2e13d2012-01-22 19:15:14 +000012035 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000012036 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000012037 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
12038 ExtraNumElems/2);
12039 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000012040
Craig Toppered2e13d2012-01-22 19:15:14 +000012041 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
12042 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000012043
Dmitri Gribenko2de05722012-09-10 21:26:47 +000012044 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000012045 }
12046 // fall through
12047 case MVT::v4i32:
12048 case MVT::v8i16: {
Nadav Rotemb05130e2013-03-19 18:38:27 +000012049 // (sext (vzext x)) -> (vsext x)
12050 SDValue Op0 = Op.getOperand(0);
12051 SDValue Op00 = Op0.getOperand(0);
12052 SDValue Tmp1;
12053 // Hopefully, this VECTOR_SHUFFLE is just a VZEXT.
12054 if (Op0.getOpcode() == ISD::BITCAST &&
12055 Op00.getOpcode() == ISD::VECTOR_SHUFFLE)
12056 Tmp1 = LowerVectorIntExtend(Op00, DAG);
12057 if (Tmp1.getNode()) {
12058 SDValue Tmp1Op0 = Tmp1.getOperand(0);
12059 assert(Tmp1Op0.getOpcode() == X86ISD::VZEXT &&
12060 "This optimization is invalid without a VZEXT.");
12061 return DAG.getNode(X86ISD::VSEXT, dl, VT, Tmp1Op0.getOperand(0));
12062 }
12063
12064 // If the above didn't work, then just use Shift-Left + Shift-Right.
12065 Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT, Op0, ShAmt, DAG);
Craig Toppered2e13d2012-01-22 19:15:14 +000012066 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012067 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012068 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012069}
12070
Craig Topper55b24052012-09-11 06:15:32 +000012071static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
12072 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000012073 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012074
Eric Christopher77ed1352011-07-08 00:04:56 +000012075 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
12076 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000012077 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000012078 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000012079 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000012080 SDValue Ops[] = {
12081 DAG.getRegister(X86::ESP, MVT::i32), // Base
12082 DAG.getTargetConstant(1, MVT::i8), // Scale
12083 DAG.getRegister(0, MVT::i32), // Index
12084 DAG.getTargetConstant(0, MVT::i32), // Disp
12085 DAG.getRegister(0, MVT::i32), // Segment.
12086 Zero,
12087 Chain
12088 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000012089 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000012090 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
12091 array_lengthof(Ops));
12092 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000012093 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012094
Eric Christopher9a9d2752010-07-22 02:48:34 +000012095 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000012096 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000012097 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000012098
Chris Lattner132929a2010-08-14 17:26:09 +000012099 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
12100 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
12101 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
12102 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000012103
Chris Lattner132929a2010-08-14 17:26:09 +000012104 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
12105 if (!Op1 && !Op2 && !Op3 && Op4)
12106 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000012107
Chris Lattner132929a2010-08-14 17:26:09 +000012108 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
12109 if (Op1 && !Op2 && !Op3 && !Op4)
12110 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000012111
12112 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000012113 // (MFENCE)>;
12114 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000012115}
12116
Craig Topper55b24052012-09-11 06:15:32 +000012117static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
12118 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000012119 DebugLoc dl = Op.getDebugLoc();
12120 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
12121 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
12122 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
12123 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
12124
12125 // The only fence that needs an instruction is a sequentially-consistent
12126 // cross-thread fence.
12127 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
12128 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
12129 // no-sse2). There isn't any reason to disable it if the target processor
12130 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000012131 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000012132 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
12133
12134 SDValue Chain = Op.getOperand(0);
12135 SDValue Zero = DAG.getConstant(0, MVT::i32);
12136 SDValue Ops[] = {
12137 DAG.getRegister(X86::ESP, MVT::i32), // Base
12138 DAG.getTargetConstant(1, MVT::i8), // Scale
12139 DAG.getRegister(0, MVT::i32), // Index
12140 DAG.getTargetConstant(0, MVT::i32), // Disp
12141 DAG.getRegister(0, MVT::i32), // Segment.
12142 Zero,
12143 Chain
12144 };
12145 SDNode *Res =
12146 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
12147 array_lengthof(Ops));
12148 return SDValue(Res, 0);
12149 }
12150
12151 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
12152 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
12153}
12154
Craig Topper55b24052012-09-11 06:15:32 +000012155static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
12156 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000012157 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012158 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000012159 unsigned Reg = 0;
12160 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000012161 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000012162 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000012163 case MVT::i8: Reg = X86::AL; size = 1; break;
12164 case MVT::i16: Reg = X86::AX; size = 2; break;
12165 case MVT::i32: Reg = X86::EAX; size = 4; break;
12166 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000012167 assert(Subtarget->is64Bit() && "Node not type legal!");
12168 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000012169 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000012170 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012171 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000012172 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000012173 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012174 Op.getOperand(1),
12175 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000012176 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000012177 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012178 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012179 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
12180 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
12181 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000012182 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000012183 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000012184 return cpOut;
12185}
12186
Craig Topper55b24052012-09-11 06:15:32 +000012187static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
12188 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000012189 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012190 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012191 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000012192 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000012193 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012194 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
12195 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000012196 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000012197 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
12198 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000012199 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000012200 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000012201 rdx.getValue(1)
12202 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000012203 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012204}
12205
Craig Topper55b24052012-09-11 06:15:32 +000012206SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000012207 EVT SrcVT = Op.getOperand(0).getValueType();
12208 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000012209 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000012210 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012211 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000012212 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012213 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000012214 // i64 <=> MMX conversions are Legal.
12215 if (SrcVT==MVT::i64 && DstVT.isVector())
12216 return Op;
12217 if (DstVT==MVT::i64 && SrcVT.isVector())
12218 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000012219 // MMX <=> MMX conversions are Legal.
12220 if (SrcVT.isVector() && DstVT.isVector())
12221 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000012222 // All other conversions need to be expanded.
12223 return SDValue();
12224}
Chris Lattner5b856542010-12-20 00:59:46 +000012225
Craig Topper55b24052012-09-11 06:15:32 +000012226static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012227 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000012228 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000012229 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012230 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000012231 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000012232 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012233 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000012234 Node->getOperand(0),
12235 Node->getOperand(1), negOp,
12236 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000012237 cast<AtomicSDNode>(Node)->getAlignment(),
12238 cast<AtomicSDNode>(Node)->getOrdering(),
12239 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000012240}
12241
Eli Friedman327236c2011-08-24 20:50:09 +000012242static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
12243 SDNode *Node = Op.getNode();
12244 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012245 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000012246
12247 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012248 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
12249 // FIXME: On 32-bit, store -> fist or movq would be more efficient
12250 // (The only way to get a 16-byte store is cmpxchg16b)
12251 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
12252 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
12253 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000012254 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
12255 cast<AtomicSDNode>(Node)->getMemoryVT(),
12256 Node->getOperand(0),
12257 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012258 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000012259 cast<AtomicSDNode>(Node)->getOrdering(),
12260 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000012261 return Swap.getValue(1);
12262 }
12263 // Other atomic stores have a simple pattern.
12264 return Op;
12265}
12266
Chris Lattner5b856542010-12-20 00:59:46 +000012267static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
12268 EVT VT = Op.getNode()->getValueType(0);
12269
12270 // Let legalize expand this if it isn't a legal type yet.
12271 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
12272 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012273
Chris Lattner5b856542010-12-20 00:59:46 +000012274 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012275
Chris Lattner5b856542010-12-20 00:59:46 +000012276 unsigned Opc;
12277 bool ExtraOp = false;
12278 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012279 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000012280 case ISD::ADDC: Opc = X86ISD::ADD; break;
12281 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
12282 case ISD::SUBC: Opc = X86ISD::SUB; break;
12283 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
12284 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012285
Chris Lattner5b856542010-12-20 00:59:46 +000012286 if (!ExtraOp)
12287 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12288 Op.getOperand(1));
12289 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
12290 Op.getOperand(1), Op.getOperand(2));
12291}
12292
Evan Cheng8688a582013-01-29 02:32:37 +000012293SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
Evan Chenga66f40a2013-01-30 22:56:35 +000012294 assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
Eric Christophere187e252013-01-31 00:50:48 +000012295
Evan Cheng8688a582013-01-29 02:32:37 +000012296 // For MacOSX, we want to call an alternative entry point: __sincos_stret,
12297 // which returns the values in two XMM registers.
12298 DebugLoc dl = Op.getDebugLoc();
12299 SDValue Arg = Op.getOperand(0);
12300 EVT ArgVT = Arg.getValueType();
12301 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Eric Christophere187e252013-01-31 00:50:48 +000012302
Evan Cheng8688a582013-01-29 02:32:37 +000012303 ArgListTy Args;
12304 ArgListEntry Entry;
Eric Christophere187e252013-01-31 00:50:48 +000012305
Evan Cheng8688a582013-01-29 02:32:37 +000012306 Entry.Node = Arg;
12307 Entry.Ty = ArgTy;
12308 Entry.isSExt = false;
12309 Entry.isZExt = false;
12310 Args.push_back(Entry);
Evan Chenga66f40a2013-01-30 22:56:35 +000012311
12312 // Only optimize x86_64 for now. i386 is a bit messy. For f32,
12313 // the small struct {f32, f32} is returned in (eax, edx). For f64,
12314 // the results are returned via SRet in memory.
Evan Cheng8688a582013-01-29 02:32:37 +000012315 const char *LibcallName = (ArgVT == MVT::f64)
12316 ? "__sincos_stret" : "__sincosf_stret";
12317 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
Evan Chenga66f40a2013-01-30 22:56:35 +000012318
Evan Cheng8688a582013-01-29 02:32:37 +000012319 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
12320 TargetLowering::
Evan Chenga66f40a2013-01-30 22:56:35 +000012321 CallLoweringInfo CLI(DAG.getEntryNode(), RetTy,
12322 false, false, false, false, 0,
12323 CallingConv::C, /*isTaillCall=*/false,
12324 /*doesNotRet=*/false, /*isReturnValueUsed*/true,
12325 Callee, Args, DAG, dl);
Evan Cheng8688a582013-01-29 02:32:37 +000012326 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Evan Cheng8688a582013-01-29 02:32:37 +000012327 return CallResult.first;
Evan Cheng8688a582013-01-29 02:32:37 +000012328}
12329
Evan Cheng0db9fe62006-04-25 20:13:52 +000012330/// LowerOperation - Provide custom lowering hooks for some operations.
12331///
Dan Gohmand858e902010-04-17 15:26:15 +000012332SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000012333 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012334 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012335 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012336 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
12337 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
12338 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012339 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000012340 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012341 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000012342 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012343 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
12344 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
12345 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012346 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
12347 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012348 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
12349 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
12350 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012351 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000012352 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000012353 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012354 case ISD::SHL_PARTS:
12355 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000012356 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012357 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000012358 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Craig Topperd713c0f2013-01-20 21:34:37 +000012359 case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG);
Nadav Rotem0509db22012-12-28 05:45:24 +000012360 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
12361 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
12362 case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012363 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000012364 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Craig Topperb84b4232013-01-21 06:13:28 +000012365 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012366 case ISD::FABS: return LowerFABS(Op, DAG);
12367 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000012368 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000012369 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000012370 case ISD::SETCC: return LowerSETCC(Op, DAG);
12371 case ISD::SELECT: return LowerSELECT(Op, DAG);
12372 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012373 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012374 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000012375 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012376 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012377 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012378 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000012379 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
12380 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012381 case ISD::FRAME_TO_ARGS_OFFSET:
12382 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000012383 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012384 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Michael Liao6c0e04c2012-10-15 22:39:43 +000012385 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
12386 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000012387 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
12388 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000012389 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012390 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000012391 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000012392 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012393 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000012394 case ISD::SRA:
12395 case ISD::SRL:
12396 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000012397 case ISD::SADDO:
12398 case ISD::UADDO:
12399 case ISD::SSUBO:
12400 case ISD::USUBO:
12401 case ISD::SMULO:
12402 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000012403 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000012404 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000012405 case ISD::ADDC:
12406 case ISD::ADDE:
12407 case ISD::SUBC:
12408 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000012409 case ISD::ADD: return LowerADD(Op, DAG);
12410 case ISD::SUB: return LowerSUB(Op, DAG);
Nadav Rotem13f8cf52013-01-09 05:14:33 +000012411 case ISD::SDIV: return LowerSDIV(Op, DAG);
Evan Cheng8688a582013-01-29 02:32:37 +000012412 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000012413 }
Chris Lattner27a6c732007-11-24 07:07:01 +000012414}
12415
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012416static void ReplaceATOMIC_LOAD(SDNode *Node,
12417 SmallVectorImpl<SDValue> &Results,
12418 SelectionDAG &DAG) {
12419 DebugLoc dl = Node->getDebugLoc();
12420 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
12421
12422 // Convert wide load -> cmpxchg8b/cmpxchg16b
12423 // FIXME: On 32-bit, load -> fild or movq would be more efficient
12424 // (The only way to get a 16-byte load is cmpxchg16b)
12425 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012426 SDValue Zero = DAG.getConstant(0, VT);
12427 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012428 Node->getOperand(0),
12429 Node->getOperand(1), Zero, Zero,
12430 cast<AtomicSDNode>(Node)->getMemOperand(),
12431 cast<AtomicSDNode>(Node)->getOrdering(),
12432 cast<AtomicSDNode>(Node)->getSynchScope());
12433 Results.push_back(Swap.getValue(0));
12434 Results.push_back(Swap.getValue(1));
12435}
12436
Craig Topperc0878702012-08-17 06:55:11 +000012437static void
Duncan Sands1607f052008-12-01 11:39:25 +000012438ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000012439 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012440 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000012441 assert (Node->getValueType(0) == MVT::i64 &&
12442 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000012443
12444 SDValue Chain = Node->getOperand(0);
12445 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012446 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012447 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000012448 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012449 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000012450 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000012451 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000012452 SDValue Result =
12453 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
12454 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000012455 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000012456 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012457 Results.push_back(Result.getValue(2));
12458}
12459
Duncan Sands126d9072008-07-04 11:47:58 +000012460/// ReplaceNodeResults - Replace a node with an illegal result type
12461/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000012462void X86TargetLowering::ReplaceNodeResults(SDNode *N,
12463 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000012464 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000012465 DebugLoc dl = N->getDebugLoc();
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012466 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner27a6c732007-11-24 07:07:01 +000012467 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000012468 default:
Craig Topperabb94d02012-02-05 03:43:23 +000012469 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000012470 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000012471 case ISD::ADDC:
12472 case ISD::ADDE:
12473 case ISD::SUBC:
12474 case ISD::SUBE:
12475 // We don't want to expand or promote these.
12476 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012477 case ISD::FP_TO_SINT:
12478 case ISD::FP_TO_UINT: {
12479 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
12480
12481 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
12482 return;
12483
Eli Friedman948e95a2009-05-23 09:59:16 +000012484 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000012485 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000012486 SDValue FIST = Vals.first, StackSlot = Vals.second;
12487 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000012488 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000012489 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012490 if (StackSlot.getNode() != 0)
12491 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
12492 MachinePointerInfo(),
12493 false, false, false, 0));
12494 else
12495 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000012496 }
12497 return;
12498 }
Michael Liao991b6a22012-10-24 04:09:32 +000012499 case ISD::UINT_TO_FP: {
Michael Liao6f8c6852013-03-14 06:57:42 +000012500 assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
12501 if (N->getOperand(0).getValueType() != MVT::v2i32 ||
Michael Liao991b6a22012-10-24 04:09:32 +000012502 N->getValueType(0) != MVT::v2f32)
12503 return;
12504 SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
12505 N->getOperand(0));
12506 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
12507 MVT::f64);
12508 SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
12509 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
12510 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias));
12511 Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or);
12512 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
12513 Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
12514 return;
12515 }
Michael Liao44c2d612012-10-10 16:53:28 +000012516 case ISD::FP_ROUND: {
Nadav Rotem0a1e9142012-12-14 21:20:37 +000012517 if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
12518 return;
Michael Liao44c2d612012-10-10 16:53:28 +000012519 SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
12520 Results.push_back(V);
12521 return;
12522 }
Duncan Sands1607f052008-12-01 11:39:25 +000012523 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012524 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000012525 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012526 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000012527 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000012528 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000012529 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000012530 eax.getValue(2));
12531 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
12532 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000012533 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012534 Results.push_back(edx.getValue(1));
12535 return;
12536 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012537 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000012538 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000012539 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000012540 bool Regs64bit = T == MVT::i128;
12541 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000012542 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012543 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12544 DAG.getConstant(0, HalfT));
12545 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
12546 DAG.getConstant(1, HalfT));
12547 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
12548 Regs64bit ? X86::RAX : X86::EAX,
12549 cpInL, SDValue());
12550 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
12551 Regs64bit ? X86::RDX : X86::EDX,
12552 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012553 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000012554 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12555 DAG.getConstant(0, HalfT));
12556 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
12557 DAG.getConstant(1, HalfT));
12558 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
12559 Regs64bit ? X86::RBX : X86::EBX,
12560 swapInL, cpInH.getValue(1));
12561 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000012562 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000012563 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000012564 SDValue Ops[] = { swapInH.getValue(0),
12565 N->getOperand(1),
12566 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000012567 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012568 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000012569 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
12570 X86ISD::LCMPXCHG8_DAG;
12571 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000012572 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000012573 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
12574 Regs64bit ? X86::RAX : X86::EAX,
12575 HalfT, Result.getValue(1));
12576 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
12577 Regs64bit ? X86::RDX : X86::EDX,
12578 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000012579 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000012580 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000012581 Results.push_back(cpOutH.getValue(1));
12582 return;
12583 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012584 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012585 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012586 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012587 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012588 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000012589 case ISD::ATOMIC_LOAD_XOR:
Michael Liaoe5e8f762012-09-25 18:08:13 +000012590 case ISD::ATOMIC_LOAD_MAX:
12591 case ISD::ATOMIC_LOAD_MIN:
12592 case ISD::ATOMIC_LOAD_UMAX:
12593 case ISD::ATOMIC_LOAD_UMIN:
Craig Topperc0878702012-08-17 06:55:11 +000012594 case ISD::ATOMIC_SWAP: {
12595 unsigned Opc;
12596 switch (N->getOpcode()) {
12597 default: llvm_unreachable("Unexpected opcode");
12598 case ISD::ATOMIC_LOAD_ADD:
12599 Opc = X86ISD::ATOMADD64_DAG;
12600 break;
12601 case ISD::ATOMIC_LOAD_AND:
12602 Opc = X86ISD::ATOMAND64_DAG;
12603 break;
12604 case ISD::ATOMIC_LOAD_NAND:
12605 Opc = X86ISD::ATOMNAND64_DAG;
12606 break;
12607 case ISD::ATOMIC_LOAD_OR:
12608 Opc = X86ISD::ATOMOR64_DAG;
12609 break;
12610 case ISD::ATOMIC_LOAD_SUB:
12611 Opc = X86ISD::ATOMSUB64_DAG;
12612 break;
12613 case ISD::ATOMIC_LOAD_XOR:
12614 Opc = X86ISD::ATOMXOR64_DAG;
12615 break;
Michael Liaoe5e8f762012-09-25 18:08:13 +000012616 case ISD::ATOMIC_LOAD_MAX:
12617 Opc = X86ISD::ATOMMAX64_DAG;
12618 break;
12619 case ISD::ATOMIC_LOAD_MIN:
12620 Opc = X86ISD::ATOMMIN64_DAG;
12621 break;
12622 case ISD::ATOMIC_LOAD_UMAX:
12623 Opc = X86ISD::ATOMUMAX64_DAG;
12624 break;
12625 case ISD::ATOMIC_LOAD_UMIN:
12626 Opc = X86ISD::ATOMUMIN64_DAG;
12627 break;
Craig Topperc0878702012-08-17 06:55:11 +000012628 case ISD::ATOMIC_SWAP:
12629 Opc = X86ISD::ATOMSWAP64_DAG;
12630 break;
12631 }
12632 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000012633 return;
Craig Topperc0878702012-08-17 06:55:11 +000012634 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000012635 case ISD::ATOMIC_LOAD:
12636 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000012637 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000012638}
12639
Evan Cheng72261582005-12-20 06:22:03 +000012640const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
12641 switch (Opcode) {
12642 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000012643 case X86ISD::BSF: return "X86ISD::BSF";
12644 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000012645 case X86ISD::SHLD: return "X86ISD::SHLD";
12646 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000012647 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012648 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000012649 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000012650 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000012651 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000012652 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000012653 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
12654 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
12655 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000012656 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000012657 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000012658 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000012659 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000012660 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000012661 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000012662 case X86ISD::COMI: return "X86ISD::COMI";
12663 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000012664 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000012665 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000012666 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
12667 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000012668 case X86ISD::CMOV: return "X86ISD::CMOV";
12669 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000012670 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000012671 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
12672 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000012673 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000012674 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000012675 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012676 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000012677 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000012678 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
12679 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000012680 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000012681 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000012682 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000012683 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000012684 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Elena Demikhovsky226e0e62012-12-05 09:24:57 +000012685 case X86ISD::BLENDI: return "X86ISD::BLENDI";
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000012686 case X86ISD::SUBUS: return "X86ISD::SUBUS";
Craig Topperfe033152011-12-06 09:31:36 +000012687 case X86ISD::HADD: return "X86ISD::HADD";
12688 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000012689 case X86ISD::FHADD: return "X86ISD::FHADD";
12690 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Benjamin Kramer739c7a82012-12-21 14:04:55 +000012691 case X86ISD::UMAX: return "X86ISD::UMAX";
12692 case X86ISD::UMIN: return "X86ISD::UMIN";
12693 case X86ISD::SMAX: return "X86ISD::SMAX";
12694 case X86ISD::SMIN: return "X86ISD::SMIN";
Evan Cheng8ca29322006-11-10 21:43:37 +000012695 case X86ISD::FMAX: return "X86ISD::FMAX";
12696 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000012697 case X86ISD::FMAXC: return "X86ISD::FMAXC";
12698 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000012699 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
12700 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000012701 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000012702 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000012703 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Michael Liao6c0e04c2012-10-15 22:39:43 +000012704 case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP";
12705 case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000012706 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000012707 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000012708 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012709 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000012710 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
12711 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012712 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
12713 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
12714 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
12715 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
12716 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
12717 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000012718 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000012719 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000012720 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liaod9d09602012-10-23 17:34:00 +000012721 case X86ISD::VZEXT: return "X86ISD::VZEXT";
12722 case X86ISD::VSEXT: return "X86ISD::VSEXT";
Michael Liao7091b242012-08-14 21:24:47 +000012723 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Michael Liao44c2d612012-10-10 16:53:28 +000012724 case X86ISD::VFPROUND: return "X86ISD::VFPROUND";
Craig Toppered2e13d2012-01-22 19:15:14 +000012725 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
12726 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000012727 case X86ISD::VSHL: return "X86ISD::VSHL";
12728 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000012729 case X86ISD::VSRA: return "X86ISD::VSRA";
12730 case X86ISD::VSHLI: return "X86ISD::VSHLI";
12731 case X86ISD::VSRLI: return "X86ISD::VSRLI";
12732 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000012733 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000012734 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
12735 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000012736 case X86ISD::ADD: return "X86ISD::ADD";
12737 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000012738 case X86ISD::ADC: return "X86ISD::ADC";
12739 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000012740 case X86ISD::SMUL: return "X86ISD::SMUL";
12741 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000012742 case X86ISD::INC: return "X86ISD::INC";
12743 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000012744 case X86ISD::OR: return "X86ISD::OR";
12745 case X86ISD::XOR: return "X86ISD::XOR";
12746 case X86ISD::AND: return "X86ISD::AND";
Craig Toppere6a62772011-11-13 17:31:07 +000012747 case X86ISD::BLSI: return "X86ISD::BLSI";
12748 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
12749 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000012750 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000012751 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000012752 case X86ISD::TESTP: return "X86ISD::TESTP";
Craig Topper4aee1bb2013-01-28 06:48:25 +000012753 case X86ISD::PALIGNR: return "X86ISD::PALIGNR";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012754 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
12755 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012756 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000012757 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012758 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012759 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000012760 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000012761 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
12762 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012763 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
12764 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
12765 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000012766 case X86ISD::MOVSD: return "X86ISD::MOVSD";
12767 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000012768 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
12769 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000012770 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000012771 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000012772 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000012773 case X86ISD::VPERMV: return "X86ISD::VPERMV";
12774 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000012775 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000012776 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000012777 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012778 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000012779 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000012780 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000012781 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000012782 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000012783 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000012784 case X86ISD::FMADD: return "X86ISD::FMADD";
12785 case X86ISD::FMSUB: return "X86ISD::FMSUB";
12786 case X86ISD::FNMADD: return "X86ISD::FNMADD";
12787 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
12788 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
12789 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Craig Topper9c7ae012012-11-10 01:23:36 +000012790 case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI";
12791 case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI";
Michael Liaof8fd8832013-03-26 22:47:01 +000012792 case X86ISD::XTEST: return "X86ISD::XTEST";
Evan Cheng72261582005-12-20 06:22:03 +000012793 }
12794}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000012795
Chris Lattnerc9addb72007-03-30 23:15:24 +000012796// isLegalAddressingMode - Return true if the addressing mode represented
12797// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000012798bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012799 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000012800 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012801 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000012802 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000012803
Chris Lattnerc9addb72007-03-30 23:15:24 +000012804 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012805 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012806 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000012807
Chris Lattnerc9addb72007-03-30 23:15:24 +000012808 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000012809 unsigned GVFlags =
12810 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012811
Chris Lattnerdfed4132009-07-10 07:38:24 +000012812 // If a reference to this global requires an extra load, we can't fold it.
12813 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000012814 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012815
Chris Lattnerdfed4132009-07-10 07:38:24 +000012816 // If BaseGV requires a register for the PIC base, we cannot also have a
12817 // BaseReg specified.
12818 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000012819 return false;
Evan Cheng52787842007-08-01 23:46:47 +000012820
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012821 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000012822 if ((M != CodeModel::Small || R != Reloc::Static) &&
12823 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000012824 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000012825 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012826
Chris Lattnerc9addb72007-03-30 23:15:24 +000012827 switch (AM.Scale) {
12828 case 0:
12829 case 1:
12830 case 2:
12831 case 4:
12832 case 8:
12833 // These scales always work.
12834 break;
12835 case 3:
12836 case 5:
12837 case 9:
12838 // These scales are formed with basereg+scalereg. Only accept if there is
12839 // no basereg yet.
12840 if (AM.HasBaseReg)
12841 return false;
12842 break;
12843 default: // Other stuff never works.
12844 return false;
12845 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012846
Chris Lattnerc9addb72007-03-30 23:15:24 +000012847 return true;
12848}
12849
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012850bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012851 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000012852 return false;
Evan Chenge127a732007-10-29 07:57:50 +000012853 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
12854 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012855 return NumBits1 > NumBits2;
Evan Cheng2bd122c2007-10-26 01:56:11 +000012856}
12857
Evan Cheng70e10d32012-07-17 06:53:39 +000012858bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakub Staszakc20323a2012-12-29 15:57:26 +000012859 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012860}
12861
12862bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000012863 // Can also use sub to handle negated immediates.
Jakub Staszakc20323a2012-12-29 15:57:26 +000012864 return isInt<32>(Imm);
Evan Cheng70e10d32012-07-17 06:53:39 +000012865}
12866
Owen Andersone50ed302009-08-10 22:56:29 +000012867bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000012868 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012869 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012870 unsigned NumBits1 = VT1.getSizeInBits();
12871 unsigned NumBits2 = VT2.getSizeInBits();
Jakub Staszakc20323a2012-12-29 15:57:26 +000012872 return NumBits1 > NumBits2;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000012873}
Evan Cheng2bd122c2007-10-26 01:56:11 +000012874
Chris Lattnerdb125cf2011-07-18 04:54:35 +000012875bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012876 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000012877 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012878}
12879
Owen Andersone50ed302009-08-10 22:56:29 +000012880bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000012881 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000012882 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000012883}
12884
Evan Cheng2766a472012-12-06 19:13:27 +000012885bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
12886 EVT VT1 = Val.getValueType();
12887 if (isZExtFree(VT1, VT2))
12888 return true;
12889
12890 if (Val.getOpcode() != ISD::LOAD)
12891 return false;
12892
12893 if (!VT1.isSimple() || !VT1.isInteger() ||
12894 !VT2.isSimple() || !VT2.isInteger())
12895 return false;
12896
12897 switch (VT1.getSimpleVT().SimpleTy) {
12898 default: break;
12899 case MVT::i8:
12900 case MVT::i16:
12901 case MVT::i32:
12902 // X86 has 8, 16, and 32-bit zero-extending loads.
12903 return true;
12904 }
12905
12906 return false;
12907}
12908
Owen Andersone50ed302009-08-10 22:56:29 +000012909bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000012910 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000012911 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000012912}
12913
Evan Cheng60c07e12006-07-05 22:17:51 +000012914/// isShuffleMaskLegal - Targets can use this to indicate that they only
12915/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
12916/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
12917/// are assumed to be legal.
12918bool
Eric Christopherfd179292009-08-27 18:07:15 +000012919X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000012920 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000012921 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000012922 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000012923 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000012924
Nate Begemana09008b2009-10-19 02:17:23 +000012925 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000012926 return (VT.getVectorNumElements() == 2 ||
12927 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
12928 isMOVLMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012929 isSHUFPMask(M, VT, Subtarget->hasFp256()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000012930 isPSHUFDMask(M, VT) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012931 isPSHUFHWMask(M, VT, Subtarget->hasInt256()) ||
12932 isPSHUFLWMask(M, VT, Subtarget->hasInt256()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000012933 isPALIGNRMask(M, VT, Subtarget) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012934 isUNPCKLMask(M, VT, Subtarget->hasInt256()) ||
12935 isUNPCKHMask(M, VT, Subtarget->hasInt256()) ||
12936 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) ||
12937 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256()));
Evan Cheng60c07e12006-07-05 22:17:51 +000012938}
12939
Dan Gohman7d8143f2008-04-09 20:09:42 +000012940bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000012941X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000012942 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000012943 unsigned NumElts = VT.getVectorNumElements();
12944 // FIXME: This collection of masks seems suspect.
12945 if (NumElts == 2)
12946 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000012947 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000012948 return (isMOVLMask(Mask, VT) ||
12949 isCommutedMOVLMask(Mask, VT, true) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000012950 isSHUFPMask(Mask, VT, Subtarget->hasFp256()) ||
12951 isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000012952 }
12953 return false;
12954}
12955
12956//===----------------------------------------------------------------------===//
12957// X86 Scheduler Hooks
12958//===----------------------------------------------------------------------===//
12959
Michael Liaobe02a902012-11-08 07:28:54 +000012960/// Utility function to emit xbegin specifying the start of an RTM region.
Craig Topper2da36912012-11-11 22:45:02 +000012961static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
12962 const TargetInstrInfo *TII) {
Michael Liaobe02a902012-11-08 07:28:54 +000012963 DebugLoc DL = MI->getDebugLoc();
Michael Liaobe02a902012-11-08 07:28:54 +000012964
12965 const BasicBlock *BB = MBB->getBasicBlock();
12966 MachineFunction::iterator I = MBB;
12967 ++I;
12968
12969 // For the v = xbegin(), we generate
12970 //
12971 // thisMBB:
12972 // xbegin sinkMBB
12973 //
12974 // mainMBB:
12975 // eax = -1
12976 //
12977 // sinkMBB:
12978 // v = eax
12979
12980 MachineBasicBlock *thisMBB = MBB;
12981 MachineFunction *MF = MBB->getParent();
12982 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
12983 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
12984 MF->insert(I, mainMBB);
12985 MF->insert(I, sinkMBB);
12986
12987 // Transfer the remainder of BB and its successor edges to sinkMBB.
12988 sinkMBB->splice(sinkMBB->begin(), MBB,
12989 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
12990 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
12991
12992 // thisMBB:
12993 // xbegin sinkMBB
12994 // # fallthrough to mainMBB
12995 // # abortion to sinkMBB
12996 BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
12997 thisMBB->addSuccessor(mainMBB);
12998 thisMBB->addSuccessor(sinkMBB);
12999
13000 // mainMBB:
13001 // EAX = -1
13002 BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
13003 mainMBB->addSuccessor(sinkMBB);
13004
13005 // sinkMBB:
13006 // EAX is live into the sinkMBB
13007 sinkMBB->addLiveIn(X86::EAX);
13008 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13009 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13010 .addReg(X86::EAX);
13011
13012 MI->eraseFromParent();
13013 return sinkMBB;
13014}
13015
Michael Liaob118a072012-09-20 03:06:15 +000013016// Get CMPXCHG opcode for the specified data type.
13017static unsigned getCmpXChgOpcode(EVT VT) {
13018 switch (VT.getSimpleVT().SimpleTy) {
13019 case MVT::i8: return X86::LCMPXCHG8;
13020 case MVT::i16: return X86::LCMPXCHG16;
13021 case MVT::i32: return X86::LCMPXCHG32;
13022 case MVT::i64: return X86::LCMPXCHG64;
13023 default:
13024 break;
Richard Smith42fc29e2012-04-13 22:47:00 +000013025 }
Michael Liaob118a072012-09-20 03:06:15 +000013026 llvm_unreachable("Invalid operand size!");
Mon P Wang63307c32008-05-05 19:05:59 +000013027}
13028
Michael Liaob118a072012-09-20 03:06:15 +000013029// Get LOAD opcode for the specified data type.
13030static unsigned getLoadOpcode(EVT VT) {
13031 switch (VT.getSimpleVT().SimpleTy) {
13032 case MVT::i8: return X86::MOV8rm;
13033 case MVT::i16: return X86::MOV16rm;
13034 case MVT::i32: return X86::MOV32rm;
13035 case MVT::i64: return X86::MOV64rm;
13036 default:
13037 break;
13038 }
13039 llvm_unreachable("Invalid operand size!");
13040}
13041
13042// Get opcode of the non-atomic one from the specified atomic instruction.
13043static unsigned getNonAtomicOpcode(unsigned Opc) {
13044 switch (Opc) {
13045 case X86::ATOMAND8: return X86::AND8rr;
13046 case X86::ATOMAND16: return X86::AND16rr;
13047 case X86::ATOMAND32: return X86::AND32rr;
13048 case X86::ATOMAND64: return X86::AND64rr;
13049 case X86::ATOMOR8: return X86::OR8rr;
13050 case X86::ATOMOR16: return X86::OR16rr;
13051 case X86::ATOMOR32: return X86::OR32rr;
13052 case X86::ATOMOR64: return X86::OR64rr;
13053 case X86::ATOMXOR8: return X86::XOR8rr;
13054 case X86::ATOMXOR16: return X86::XOR16rr;
13055 case X86::ATOMXOR32: return X86::XOR32rr;
13056 case X86::ATOMXOR64: return X86::XOR64rr;
13057 }
13058 llvm_unreachable("Unhandled atomic-load-op opcode!");
13059}
13060
13061// Get opcode of the non-atomic one from the specified atomic instruction with
13062// extra opcode.
13063static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc,
13064 unsigned &ExtraOpc) {
13065 switch (Opc) {
13066 case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr;
13067 case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr;
13068 case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr;
13069 case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013070 case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013071 case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr;
13072 case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr;
13073 case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013074 case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013075 case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr;
13076 case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr;
13077 case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013078 case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013079 case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr;
13080 case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr;
13081 case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr;
Michael Liaofe87c302012-09-21 03:18:52 +000013082 case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr;
Michael Liaob118a072012-09-20 03:06:15 +000013083 case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr;
13084 case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr;
13085 case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr;
13086 }
13087 llvm_unreachable("Unhandled atomic-load-op opcode!");
13088}
13089
13090// Get opcode of the non-atomic one from the specified atomic instruction for
13091// 64-bit data type on 32-bit target.
13092static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) {
13093 switch (Opc) {
13094 case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr;
13095 case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr;
13096 case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr;
13097 case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr;
13098 case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr;
13099 case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013100 case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr;
13101 case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr;
13102 case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr;
13103 case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr;
Michael Liaob118a072012-09-20 03:06:15 +000013104 }
13105 llvm_unreachable("Unhandled atomic-load-op opcode!");
13106}
13107
13108// Get opcode of the non-atomic one from the specified atomic instruction for
13109// 64-bit data type on 32-bit target with extra opcode.
13110static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc,
13111 unsigned &HiOpc,
13112 unsigned &ExtraOpc) {
13113 switch (Opc) {
13114 case X86::ATOMNAND6432:
13115 ExtraOpc = X86::NOT32r;
13116 HiOpc = X86::AND32rr;
13117 return X86::AND32rr;
13118 }
13119 llvm_unreachable("Unhandled atomic-load-op opcode!");
13120}
13121
13122// Get pseudo CMOV opcode from the specified data type.
13123static unsigned getPseudoCMOVOpc(EVT VT) {
13124 switch (VT.getSimpleVT().SimpleTy) {
Michael Liaofe87c302012-09-21 03:18:52 +000013125 case MVT::i8: return X86::CMOV_GR8;
Michael Liaob118a072012-09-20 03:06:15 +000013126 case MVT::i16: return X86::CMOV_GR16;
13127 case MVT::i32: return X86::CMOV_GR32;
13128 default:
13129 break;
13130 }
13131 llvm_unreachable("Unknown CMOV opcode!");
13132}
13133
13134// EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions.
13135// They will be translated into a spin-loop or compare-exchange loop from
13136//
13137// ...
13138// dst = atomic-fetch-op MI.addr, MI.val
13139// ...
13140//
13141// to
13142//
13143// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013144// t1 = LOAD MI.addr
Michael Liaob118a072012-09-20 03:06:15 +000013145// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013146// t4 = phi(t1, t3 / loop)
13147// t2 = OP MI.val, t4
13148// EAX = t4
13149// LCMPXCHG [MI.addr], t2, [EAX is implicitly used & defined]
13150// t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013151// JNE loop
13152// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013153// dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013154// ...
Mon P Wang63307c32008-05-05 19:05:59 +000013155MachineBasicBlock *
Michael Liaob118a072012-09-20 03:06:15 +000013156X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI,
13157 MachineBasicBlock *MBB) const {
13158 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13159 DebugLoc DL = MI->getDebugLoc();
13160
13161 MachineFunction *MF = MBB->getParent();
13162 MachineRegisterInfo &MRI = MF->getRegInfo();
13163
13164 const BasicBlock *BB = MBB->getBasicBlock();
13165 MachineFunction::iterator I = MBB;
13166 ++I;
13167
Michael Liao13d08bf2013-01-22 21:47:38 +000013168 assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 &&
Michael Liaob118a072012-09-20 03:06:15 +000013169 "Unexpected number of operands");
13170
13171 assert(MI->hasOneMemOperand() &&
13172 "Expected atomic-load-op to have one memoperand");
13173
13174 // Memory Reference
13175 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13176 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13177
13178 unsigned DstReg, SrcReg;
13179 unsigned MemOpndSlot;
13180
13181 unsigned CurOp = 0;
13182
13183 DstReg = MI->getOperand(CurOp++).getReg();
13184 MemOpndSlot = CurOp;
13185 CurOp += X86::AddrNumOperands;
13186 SrcReg = MI->getOperand(CurOp++).getReg();
13187
13188 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
Craig Topperf4d25a22012-09-30 19:49:56 +000013189 MVT::SimpleValueType VT = *RC->vt_begin();
Michael Liaoc537f792013-03-06 00:17:04 +000013190 unsigned t1 = MRI.createVirtualRegister(RC);
13191 unsigned t2 = MRI.createVirtualRegister(RC);
13192 unsigned t3 = MRI.createVirtualRegister(RC);
13193 unsigned t4 = MRI.createVirtualRegister(RC);
13194 unsigned PhyReg = getX86SubSuperRegister(X86::EAX, VT);
Michael Liaob118a072012-09-20 03:06:15 +000013195
13196 unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT);
13197 unsigned LOADOpc = getLoadOpcode(VT);
13198
13199 // For the atomic load-arith operator, we generate
13200 //
13201 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013202 // t1 = LOAD [MI.addr]
Michael Liaob118a072012-09-20 03:06:15 +000013203 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013204 // t4 = phi(t1 / thisMBB, t3 / mainMBB)
Michael Liaob118a072012-09-20 03:06:15 +000013205 // t1 = OP MI.val, EAX
Michael Liaoc537f792013-03-06 00:17:04 +000013206 // EAX = t4
Michael Liaob118a072012-09-20 03:06:15 +000013207 // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013208 // t3 = EAX
Michael Liaob118a072012-09-20 03:06:15 +000013209 // JNE mainMBB
13210 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013211 // dst = t3
Michael Liaob118a072012-09-20 03:06:15 +000013212
13213 MachineBasicBlock *thisMBB = MBB;
13214 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13215 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13216 MF->insert(I, mainMBB);
13217 MF->insert(I, sinkMBB);
13218
13219 MachineInstrBuilder MIB;
13220
13221 // Transfer the remainder of BB and its successor edges to sinkMBB.
13222 sinkMBB->splice(sinkMBB->begin(), MBB,
13223 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13224 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
13225
13226 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013227 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1);
13228 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13229 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13230 if (NewMO.isReg())
13231 NewMO.setIsKill(false);
13232 MIB.addOperand(NewMO);
13233 }
13234 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13235 unsigned flags = (*MMOI)->getFlags();
13236 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13237 MachineMemOperand *MMO =
13238 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13239 (*MMOI)->getSize(),
13240 (*MMOI)->getBaseAlignment(),
13241 (*MMOI)->getTBAAInfo(),
13242 (*MMOI)->getRanges());
13243 MIB.addMemOperand(MMO);
13244 }
Michael Liaob118a072012-09-20 03:06:15 +000013245
13246 thisMBB->addSuccessor(mainMBB);
13247
13248 // mainMBB:
13249 MachineBasicBlock *origMainMBB = mainMBB;
Michael Liaob118a072012-09-20 03:06:15 +000013250
Michael Liaoc537f792013-03-06 00:17:04 +000013251 // Add a PHI.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013252 MachineInstr *Phi = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4)
13253 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
Michael Liaob118a072012-09-20 03:06:15 +000013254
Michael Liaob118a072012-09-20 03:06:15 +000013255 unsigned Opc = MI->getOpcode();
13256 switch (Opc) {
13257 default:
13258 llvm_unreachable("Unhandled atomic-load-op opcode!");
13259 case X86::ATOMAND8:
13260 case X86::ATOMAND16:
13261 case X86::ATOMAND32:
13262 case X86::ATOMAND64:
13263 case X86::ATOMOR8:
13264 case X86::ATOMOR16:
13265 case X86::ATOMOR32:
13266 case X86::ATOMOR64:
13267 case X86::ATOMXOR8:
13268 case X86::ATOMXOR16:
13269 case X86::ATOMXOR32:
13270 case X86::ATOMXOR64: {
13271 unsigned ARITHOpc = getNonAtomicOpcode(Opc);
Michael Liaoc537f792013-03-06 00:17:04 +000013272 BuildMI(mainMBB, DL, TII->get(ARITHOpc), t2).addReg(SrcReg)
13273 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013274 break;
13275 }
13276 case X86::ATOMNAND8:
13277 case X86::ATOMNAND16:
13278 case X86::ATOMNAND32:
13279 case X86::ATOMNAND64: {
Michael Liaoc537f792013-03-06 00:17:04 +000013280 unsigned Tmp = MRI.createVirtualRegister(RC);
Michael Liaob118a072012-09-20 03:06:15 +000013281 unsigned NOTOpc;
13282 unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013283 BuildMI(mainMBB, DL, TII->get(ANDOpc), Tmp).addReg(SrcReg)
13284 .addReg(t4);
13285 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2).addReg(Tmp);
Michael Liaob118a072012-09-20 03:06:15 +000013286 break;
13287 }
Michael Liao08382492012-09-21 03:00:17 +000013288 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013289 case X86::ATOMMAX16:
13290 case X86::ATOMMAX32:
13291 case X86::ATOMMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013292 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013293 case X86::ATOMMIN16:
13294 case X86::ATOMMIN32:
13295 case X86::ATOMMIN64:
Michael Liaofe87c302012-09-21 03:18:52 +000013296 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000013297 case X86::ATOMUMAX16:
13298 case X86::ATOMUMAX32:
13299 case X86::ATOMUMAX64:
Michael Liaofe87c302012-09-21 03:18:52 +000013300 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000013301 case X86::ATOMUMIN16:
13302 case X86::ATOMUMIN32:
13303 case X86::ATOMUMIN64: {
13304 unsigned CMPOpc;
13305 unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc);
13306
13307 BuildMI(mainMBB, DL, TII->get(CMPOpc))
13308 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013309 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013310
13311 if (Subtarget->hasCMov()) {
Michael Liaofe87c302012-09-21 03:18:52 +000013312 if (VT != MVT::i8) {
13313 // Native support
Michael Liaoc537f792013-03-06 00:17:04 +000013314 BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2)
Michael Liaofe87c302012-09-21 03:18:52 +000013315 .addReg(SrcReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013316 .addReg(t4);
Michael Liaofe87c302012-09-21 03:18:52 +000013317 } else {
13318 // Promote i8 to i32 to use CMOV32
Michael Liaoc537f792013-03-06 00:17:04 +000013319 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
13320 const TargetRegisterClass *RC32 =
13321 TRI->getSubClassWithSubReg(getRegClassFor(MVT::i32), X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013322 unsigned SrcReg32 = MRI.createVirtualRegister(RC32);
13323 unsigned AccReg32 = MRI.createVirtualRegister(RC32);
Michael Liaoc537f792013-03-06 00:17:04 +000013324 unsigned Tmp = MRI.createVirtualRegister(RC32);
Michael Liaofe87c302012-09-21 03:18:52 +000013325
13326 unsigned Undef = MRI.createVirtualRegister(RC32);
13327 BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef);
13328
13329 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32)
13330 .addReg(Undef)
13331 .addReg(SrcReg)
13332 .addImm(X86::sub_8bit);
13333 BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32)
13334 .addReg(Undef)
Michael Liaoc537f792013-03-06 00:17:04 +000013335 .addReg(t4)
Michael Liaofe87c302012-09-21 03:18:52 +000013336 .addImm(X86::sub_8bit);
13337
Michael Liaoc537f792013-03-06 00:17:04 +000013338 BuildMI(mainMBB, DL, TII->get(CMOVOpc), Tmp)
Michael Liaofe87c302012-09-21 03:18:52 +000013339 .addReg(SrcReg32)
13340 .addReg(AccReg32);
13341
Michael Liaoc537f792013-03-06 00:17:04 +000013342 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t2)
13343 .addReg(Tmp, 0, X86::sub_8bit);
Michael Liaofe87c302012-09-21 03:18:52 +000013344 }
Michael Liaob118a072012-09-20 03:06:15 +000013345 } else {
13346 // Use pseudo select and lower them.
Michael Liaofe87c302012-09-21 03:18:52 +000013347 assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) &&
Michael Liaob118a072012-09-20 03:06:15 +000013348 "Invalid atomic-load-op transformation!");
13349 unsigned SelOpc = getPseudoCMOVOpc(VT);
13350 X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc);
13351 assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!");
Michael Liaoc537f792013-03-06 00:17:04 +000013352 MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t2)
13353 .addReg(SrcReg).addReg(t4)
Michael Liaob118a072012-09-20 03:06:15 +000013354 .addImm(CC);
13355 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013356 // Replace the original PHI node as mainMBB is changed after CMOV
13357 // lowering.
13358 BuildMI(*origMainMBB, Phi, DL, TII->get(X86::PHI), t4)
13359 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB);
13360 Phi->eraseFromParent();
Michael Liaob118a072012-09-20 03:06:15 +000013361 }
13362 break;
13363 }
13364 }
13365
Michael Liaoc537f792013-03-06 00:17:04 +000013366 // Copy PhyReg back from virtual register.
13367 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), PhyReg)
13368 .addReg(t4);
Michael Liaob118a072012-09-20 03:06:15 +000013369
13370 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013371 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13372 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13373 if (NewMO.isReg())
13374 NewMO.setIsKill(false);
13375 MIB.addOperand(NewMO);
13376 }
13377 MIB.addReg(t2);
Michael Liaob118a072012-09-20 03:06:15 +000013378 MIB.setMemRefs(MMOBegin, MMOEnd);
13379
Michael Liaoc537f792013-03-06 00:17:04 +000013380 // Copy PhyReg back to virtual register.
13381 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3)
13382 .addReg(PhyReg);
13383
Michael Liaob118a072012-09-20 03:06:15 +000013384 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
13385
13386 mainMBB->addSuccessor(origMainMBB);
13387 mainMBB->addSuccessor(sinkMBB);
13388
13389 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013390 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13391 TII->get(TargetOpcode::COPY), DstReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013392 .addReg(t3);
Michael Liaob118a072012-09-20 03:06:15 +000013393
13394 MI->eraseFromParent();
13395 return sinkMBB;
13396}
13397
13398// EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic
13399// instructions. They will be translated into a spin-loop or compare-exchange
13400// loop from
13401//
13402// ...
13403// dst = atomic-fetch-op MI.addr, MI.val
13404// ...
13405//
13406// to
13407//
13408// ...
Michael Liaoc537f792013-03-06 00:17:04 +000013409// t1L = LOAD [MI.addr + 0]
13410// t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013411// loop:
Michael Liaoc537f792013-03-06 00:17:04 +000013412// t4L = phi(t1L, t3L / loop)
13413// t4H = phi(t1H, t3H / loop)
13414// t2L = OP MI.val.lo, t4L
13415// t2H = OP MI.val.hi, t4H
13416// EAX = t4L
13417// EDX = t4H
13418// EBX = t2L
13419// ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013420// LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013421// t3L = EAX
13422// t3H = EDX
Michael Liaob118a072012-09-20 03:06:15 +000013423// JNE loop
13424// sink:
Michael Liaoc537f792013-03-06 00:17:04 +000013425// dstL = t3L
13426// dstH = t3H
Michael Liaob118a072012-09-20 03:06:15 +000013427// ...
13428MachineBasicBlock *
13429X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI,
13430 MachineBasicBlock *MBB) const {
13431 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13432 DebugLoc DL = MI->getDebugLoc();
13433
13434 MachineFunction *MF = MBB->getParent();
13435 MachineRegisterInfo &MRI = MF->getRegInfo();
13436
13437 const BasicBlock *BB = MBB->getBasicBlock();
13438 MachineFunction::iterator I = MBB;
13439 ++I;
13440
Michael Liao13d08bf2013-01-22 21:47:38 +000013441 assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 &&
Michael Liaob118a072012-09-20 03:06:15 +000013442 "Unexpected number of operands");
13443
13444 assert(MI->hasOneMemOperand() &&
13445 "Expected atomic-load-op32 to have one memoperand");
13446
13447 // Memory Reference
13448 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13449 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13450
13451 unsigned DstLoReg, DstHiReg;
13452 unsigned SrcLoReg, SrcHiReg;
13453 unsigned MemOpndSlot;
13454
13455 unsigned CurOp = 0;
13456
13457 DstLoReg = MI->getOperand(CurOp++).getReg();
13458 DstHiReg = MI->getOperand(CurOp++).getReg();
13459 MemOpndSlot = CurOp;
13460 CurOp += X86::AddrNumOperands;
13461 SrcLoReg = MI->getOperand(CurOp++).getReg();
13462 SrcHiReg = MI->getOperand(CurOp++).getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013463
Craig Topperc9099502012-04-20 06:31:50 +000013464 const TargetRegisterClass *RC = &X86::GR32RegClass;
Michael Liaoe5e8f762012-09-25 18:08:13 +000013465 const TargetRegisterClass *RC8 = &X86::GR8RegClass;
Scott Michelfdc40a02009-02-17 22:15:04 +000013466
Michael Liaoc537f792013-03-06 00:17:04 +000013467 unsigned t1L = MRI.createVirtualRegister(RC);
13468 unsigned t1H = MRI.createVirtualRegister(RC);
13469 unsigned t2L = MRI.createVirtualRegister(RC);
13470 unsigned t2H = MRI.createVirtualRegister(RC);
13471 unsigned t3L = MRI.createVirtualRegister(RC);
13472 unsigned t3H = MRI.createVirtualRegister(RC);
13473 unsigned t4L = MRI.createVirtualRegister(RC);
13474 unsigned t4H = MRI.createVirtualRegister(RC);
13475
Michael Liaob118a072012-09-20 03:06:15 +000013476 unsigned LCMPXCHGOpc = X86::LCMPXCHG8B;
13477 unsigned LOADOpc = X86::MOV32rm;
Scott Michelfdc40a02009-02-17 22:15:04 +000013478
Michael Liaob118a072012-09-20 03:06:15 +000013479 // For the atomic load-arith operator, we generate
Mon P Wang63307c32008-05-05 19:05:59 +000013480 //
Michael Liaob118a072012-09-20 03:06:15 +000013481 // thisMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013482 // t1L = LOAD [MI.addr + 0]
13483 // t1H = LOAD [MI.addr + 4]
Michael Liaob118a072012-09-20 03:06:15 +000013484 // mainMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013485 // t4L = phi(t1L / thisMBB, t3L / mainMBB)
13486 // t4H = phi(t1H / thisMBB, t3H / mainMBB)
13487 // t2L = OP MI.val.lo, t4L
13488 // t2H = OP MI.val.hi, t4H
13489 // EBX = t2L
13490 // ECX = t2H
Michael Liaob118a072012-09-20 03:06:15 +000013491 // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined]
Michael Liaoc537f792013-03-06 00:17:04 +000013492 // t3L = EAX
13493 // t3H = EDX
13494 // JNE loop
Michael Liaob118a072012-09-20 03:06:15 +000013495 // sinkMBB:
Michael Liaoc537f792013-03-06 00:17:04 +000013496 // dstL = t3L
13497 // dstH = t3H
Scott Michelfdc40a02009-02-17 22:15:04 +000013498
Mon P Wang63307c32008-05-05 19:05:59 +000013499 MachineBasicBlock *thisMBB = MBB;
Michael Liaob118a072012-09-20 03:06:15 +000013500 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
13501 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
13502 MF->insert(I, mainMBB);
13503 MF->insert(I, sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013504
Michael Liaob118a072012-09-20 03:06:15 +000013505 MachineInstrBuilder MIB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013506
Michael Liaob118a072012-09-20 03:06:15 +000013507 // Transfer the remainder of BB and its successor edges to sinkMBB.
13508 sinkMBB->splice(sinkMBB->begin(), MBB,
13509 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
13510 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013511
Michael Liaob118a072012-09-20 03:06:15 +000013512 // thisMBB:
13513 // Lo
Michael Liaoc537f792013-03-06 00:17:04 +000013514 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1L);
Michael Liaob118a072012-09-20 03:06:15 +000013515 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
Michael Liaoc537f792013-03-06 00:17:04 +000013516 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13517 if (NewMO.isReg())
13518 NewMO.setIsKill(false);
13519 MIB.addOperand(NewMO);
Michael Liaob118a072012-09-20 03:06:15 +000013520 }
Michael Liaoc537f792013-03-06 00:17:04 +000013521 for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) {
13522 unsigned flags = (*MMOI)->getFlags();
13523 flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad;
13524 MachineMemOperand *MMO =
13525 MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags,
13526 (*MMOI)->getSize(),
13527 (*MMOI)->getBaseAlignment(),
13528 (*MMOI)->getTBAAInfo(),
13529 (*MMOI)->getRanges());
13530 MIB.addMemOperand(MMO);
13531 };
13532 MachineInstr *LowMI = MIB;
13533
13534 // Hi
13535 MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1H);
13536 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13537 if (i == X86::AddrDisp) {
13538 MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32)
13539 } else {
13540 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13541 if (NewMO.isReg())
13542 NewMO.setIsKill(false);
13543 MIB.addOperand(NewMO);
13544 }
13545 }
13546 MIB.setMemRefs(LowMI->memoperands_begin(), LowMI->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000013547
Michael Liaob118a072012-09-20 03:06:15 +000013548 thisMBB->addSuccessor(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013549
Michael Liaob118a072012-09-20 03:06:15 +000013550 // mainMBB:
13551 MachineBasicBlock *origMainMBB = mainMBB;
Scott Michelfdc40a02009-02-17 22:15:04 +000013552
Michael Liaoc537f792013-03-06 00:17:04 +000013553 // Add PHIs.
Michael Liaofe9dbe02013-03-07 01:01:29 +000013554 MachineInstr *PhiL = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4L)
13555 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13556 MachineInstr *PhiH = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4H)
13557 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013558
Michael Liaob118a072012-09-20 03:06:15 +000013559 unsigned Opc = MI->getOpcode();
13560 switch (Opc) {
13561 default:
13562 llvm_unreachable("Unhandled atomic-load-op6432 opcode!");
13563 case X86::ATOMAND6432:
13564 case X86::ATOMOR6432:
13565 case X86::ATOMXOR6432:
13566 case X86::ATOMADD6432:
13567 case X86::ATOMSUB6432: {
13568 unsigned HiOpc;
13569 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013570 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(t4L)
13571 .addReg(SrcLoReg);
13572 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(t4H)
13573 .addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013574 break;
13575 }
13576 case X86::ATOMNAND6432: {
13577 unsigned HiOpc, NOTOpc;
13578 unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013579 unsigned TmpL = MRI.createVirtualRegister(RC);
13580 unsigned TmpH = MRI.createVirtualRegister(RC);
13581 BuildMI(mainMBB, DL, TII->get(LoOpc), TmpL).addReg(SrcLoReg)
13582 .addReg(t4L);
13583 BuildMI(mainMBB, DL, TII->get(HiOpc), TmpH).addReg(SrcHiReg)
13584 .addReg(t4H);
13585 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2L).addReg(TmpL);
13586 BuildMI(mainMBB, DL, TII->get(NOTOpc), t2H).addReg(TmpH);
Michael Liaob118a072012-09-20 03:06:15 +000013587 break;
13588 }
Michael Liaoe5e8f762012-09-25 18:08:13 +000013589 case X86::ATOMMAX6432:
13590 case X86::ATOMMIN6432:
13591 case X86::ATOMUMAX6432:
13592 case X86::ATOMUMIN6432: {
13593 unsigned HiOpc;
13594 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
13595 unsigned cL = MRI.createVirtualRegister(RC8);
13596 unsigned cH = MRI.createVirtualRegister(RC8);
13597 unsigned cL32 = MRI.createVirtualRegister(RC);
13598 unsigned cH32 = MRI.createVirtualRegister(RC);
13599 unsigned cc = MRI.createVirtualRegister(RC);
13600 // cl := cmp src_lo, lo
13601 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013602 .addReg(SrcLoReg).addReg(t4L);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013603 BuildMI(mainMBB, DL, TII->get(LoOpc), cL);
13604 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL);
13605 // ch := cmp src_hi, hi
13606 BuildMI(mainMBB, DL, TII->get(X86::CMP32rr))
Michael Liaoc537f792013-03-06 00:17:04 +000013607 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013608 BuildMI(mainMBB, DL, TII->get(HiOpc), cH);
13609 BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH);
13610 // cc := if (src_hi == hi) ? cl : ch;
13611 if (Subtarget->hasCMov()) {
13612 BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc)
13613 .addReg(cH32).addReg(cL32);
13614 } else {
13615 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc)
13616 .addReg(cH32).addReg(cL32)
13617 .addImm(X86::COND_E);
13618 mainMBB = EmitLoweredSelect(MIB, mainMBB);
13619 }
13620 BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc);
13621 if (Subtarget->hasCMov()) {
Michael Liaoc537f792013-03-06 00:17:04 +000013622 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2L)
13623 .addReg(SrcLoReg).addReg(t4L);
13624 BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2H)
13625 .addReg(SrcHiReg).addReg(t4H);
Michael Liaoe5e8f762012-09-25 18:08:13 +000013626 } else {
Michael Liaoc537f792013-03-06 00:17:04 +000013627 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2L)
13628 .addReg(SrcLoReg).addReg(t4L)
Michael Liaoe5e8f762012-09-25 18:08:13 +000013629 .addImm(X86::COND_NE);
13630 mainMBB = EmitLoweredSelect(MIB, mainMBB);
Michael Liaofe9dbe02013-03-07 01:01:29 +000013631 // As the lowered CMOV won't clobber EFLAGS, we could reuse it for the
13632 // 2nd CMOV lowering.
13633 mainMBB->addLiveIn(X86::EFLAGS);
Michael Liaoc537f792013-03-06 00:17:04 +000013634 MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2H)
13635 .addReg(SrcHiReg).addReg(t4H)
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 // Replace the original PHI node as mainMBB is changed after CMOV
13639 // lowering.
13640 BuildMI(*origMainMBB, PhiL, DL, TII->get(X86::PHI), t4L)
13641 .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB);
13642 BuildMI(*origMainMBB, PhiH, DL, TII->get(X86::PHI), t4H)
13643 .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB);
13644 PhiL->eraseFromParent();
13645 PhiH->eraseFromParent();
Michael Liaoe5e8f762012-09-25 18:08:13 +000013646 }
13647 break;
13648 }
Michael Liaob118a072012-09-20 03:06:15 +000013649 case X86::ATOMSWAP6432: {
13650 unsigned HiOpc;
13651 unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc);
Michael Liaoc537f792013-03-06 00:17:04 +000013652 BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg);
13653 BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg);
Michael Liaob118a072012-09-20 03:06:15 +000013654 break;
13655 }
13656 }
Mon P Wang63307c32008-05-05 19:05:59 +000013657
Michael Liaob118a072012-09-20 03:06:15 +000013658 // Copy EDX:EAX back from HiReg:LoReg
Michael Liaoc537f792013-03-06 00:17:04 +000013659 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(t4L);
13660 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(t4H);
Michael Liaob118a072012-09-20 03:06:15 +000013661 // Copy ECX:EBX from t1H:t1L
Michael Liaoc537f792013-03-06 00:17:04 +000013662 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t2L);
13663 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t2H);
Mon P Wangab3e7472008-05-05 22:56:23 +000013664
Michael Liaob118a072012-09-20 03:06:15 +000013665 MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc));
Michael Liaoc537f792013-03-06 00:17:04 +000013666 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
13667 MachineOperand NewMO = MI->getOperand(MemOpndSlot + i);
13668 if (NewMO.isReg())
13669 NewMO.setIsKill(false);
13670 MIB.addOperand(NewMO);
13671 }
Michael Liaob118a072012-09-20 03:06:15 +000013672 MIB.setMemRefs(MMOBegin, MMOEnd);
Mon P Wang63307c32008-05-05 19:05:59 +000013673
Michael Liaoc537f792013-03-06 00:17:04 +000013674 // Copy EDX:EAX back to t3H:t3L
13675 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3L).addReg(X86::EAX);
13676 BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3H).addReg(X86::EDX);
13677
Michael Liaob118a072012-09-20 03:06:15 +000013678 BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000013679
Michael Liaob118a072012-09-20 03:06:15 +000013680 mainMBB->addSuccessor(origMainMBB);
13681 mainMBB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000013682
Michael Liaob118a072012-09-20 03:06:15 +000013683 // sinkMBB:
Michael Liaob118a072012-09-20 03:06:15 +000013684 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13685 TII->get(TargetOpcode::COPY), DstLoReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013686 .addReg(t3L);
Michael Liaob118a072012-09-20 03:06:15 +000013687 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
13688 TII->get(TargetOpcode::COPY), DstHiReg)
Michael Liaoc537f792013-03-06 00:17:04 +000013689 .addReg(t3H);
Mon P Wang63307c32008-05-05 19:05:59 +000013690
Michael Liaob118a072012-09-20 03:06:15 +000013691 MI->eraseFromParent();
13692 return sinkMBB;
Mon P Wang63307c32008-05-05 19:05:59 +000013693}
13694
Eric Christopherf83a5de2009-08-27 18:08:16 +000013695// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013696// or XMM0_V32I8 in AVX all of this code can be replaced with that
13697// in the .td file.
Craig Topper8cb8c812012-11-10 09:02:47 +000013698static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
13699 const TargetInstrInfo *TII) {
Eric Christopherb120ab42009-08-18 22:50:32 +000013700 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013701 switch (MI->getOpcode()) {
13702 default: llvm_unreachable("illegal opcode!");
13703 case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break;
13704 case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
13705 case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break;
13706 case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
13707 case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break;
13708 case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
13709 case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break;
13710 case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013711 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013712
Craig Topper8aae8dd2012-11-10 08:57:41 +000013713 DebugLoc dl = MI->getDebugLoc();
Eric Christopher41c902f2010-11-30 08:20:21 +000013714 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013715
Craig Topper52ea2452012-11-10 09:25:36 +000013716 unsigned NumArgs = MI->getNumOperands();
13717 for (unsigned i = 1; i < NumArgs; ++i) {
13718 MachineOperand &Op = MI->getOperand(i);
Eric Christopherb120ab42009-08-18 22:50:32 +000013719 if (!(Op.isReg() && Op.isImplicit()))
13720 MIB.addOperand(Op);
13721 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013722 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013723 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13724
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000013725 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000013726 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000013727 .addReg(X86::XMM0);
13728
Dan Gohman14152b42010-07-06 20:24:04 +000013729 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000013730 return BB;
13731}
13732
Craig Topper9c7ae012012-11-10 01:23:36 +000013733// FIXME: Custom handling because TableGen doesn't support multiple implicit
13734// defs in an instruction pattern
Craig Topper8cb8c812012-11-10 09:02:47 +000013735static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
13736 const TargetInstrInfo *TII) {
Craig Topper9c7ae012012-11-10 01:23:36 +000013737 unsigned Opc;
Craig Topper8aae8dd2012-11-10 08:57:41 +000013738 switch (MI->getOpcode()) {
13739 default: llvm_unreachable("illegal opcode!");
13740 case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break;
13741 case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
13742 case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break;
13743 case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
13744 case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break;
13745 case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
13746 case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break;
13747 case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
Craig Topper9c7ae012012-11-10 01:23:36 +000013748 }
13749
Craig Topper8aae8dd2012-11-10 08:57:41 +000013750 DebugLoc dl = MI->getDebugLoc();
Craig Topper9c7ae012012-11-10 01:23:36 +000013751 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Craig Topper8aae8dd2012-11-10 08:57:41 +000013752
Craig Topper52ea2452012-11-10 09:25:36 +000013753 unsigned NumArgs = MI->getNumOperands(); // remove the results
13754 for (unsigned i = 1; i < NumArgs; ++i) {
13755 MachineOperand &Op = MI->getOperand(i);
Craig Topper9c7ae012012-11-10 01:23:36 +000013756 if (!(Op.isReg() && Op.isImplicit()))
13757 MIB.addOperand(Op);
13758 }
Craig Topper8aae8dd2012-11-10 08:57:41 +000013759 if (MI->hasOneMemOperand())
Craig Topper9c7ae012012-11-10 01:23:36 +000013760 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
13761
13762 BuildMI(*BB, MI, dl,
13763 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
13764 .addReg(X86::ECX);
13765
13766 MI->eraseFromParent();
13767 return BB;
13768}
13769
Craig Topper2da36912012-11-11 22:45:02 +000013770static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
13771 const TargetInstrInfo *TII,
13772 const X86Subtarget* Subtarget) {
Eric Christopher228232b2010-11-30 07:20:12 +000013773 DebugLoc dl = MI->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013774
Eric Christopher228232b2010-11-30 07:20:12 +000013775 // Address into RAX/EAX, other two args into ECX, EDX.
13776 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
13777 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
13778 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
13779 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000013780 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013781
Eric Christopher228232b2010-11-30 07:20:12 +000013782 unsigned ValOps = X86::AddrNumOperands;
13783 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
13784 .addReg(MI->getOperand(ValOps).getReg());
13785 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
13786 .addReg(MI->getOperand(ValOps+1).getReg());
13787
13788 // The instruction doesn't actually take any operands though.
13789 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013790
Eric Christopher228232b2010-11-30 07:20:12 +000013791 MI->eraseFromParent(); // The pseudo is gone now.
13792 return BB;
13793}
13794
13795MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000013796X86TargetLowering::EmitVAARG64WithCustomInserter(
13797 MachineInstr *MI,
13798 MachineBasicBlock *MBB) const {
13799 // Emit va_arg instruction on X86-64.
13800
13801 // Operands to this pseudo-instruction:
13802 // 0 ) Output : destination address (reg)
13803 // 1-5) Input : va_list address (addr, i64mem)
13804 // 6 ) ArgSize : Size (in bytes) of vararg type
13805 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
13806 // 8 ) Align : Alignment of type
13807 // 9 ) EFLAGS (implicit-def)
13808
13809 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
13810 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
13811
13812 unsigned DestReg = MI->getOperand(0).getReg();
13813 MachineOperand &Base = MI->getOperand(1);
13814 MachineOperand &Scale = MI->getOperand(2);
13815 MachineOperand &Index = MI->getOperand(3);
13816 MachineOperand &Disp = MI->getOperand(4);
13817 MachineOperand &Segment = MI->getOperand(5);
13818 unsigned ArgSize = MI->getOperand(6).getImm();
13819 unsigned ArgMode = MI->getOperand(7).getImm();
13820 unsigned Align = MI->getOperand(8).getImm();
13821
13822 // Memory Reference
13823 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
13824 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
13825 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
13826
13827 // Machine Information
13828 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13829 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
13830 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
13831 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
13832 DebugLoc DL = MI->getDebugLoc();
13833
13834 // struct va_list {
13835 // i32 gp_offset
13836 // i32 fp_offset
13837 // i64 overflow_area (address)
13838 // i64 reg_save_area (address)
13839 // }
13840 // sizeof(va_list) = 24
13841 // alignment(va_list) = 8
13842
13843 unsigned TotalNumIntRegs = 6;
13844 unsigned TotalNumXMMRegs = 8;
13845 bool UseGPOffset = (ArgMode == 1);
13846 bool UseFPOffset = (ArgMode == 2);
13847 unsigned MaxOffset = TotalNumIntRegs * 8 +
13848 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
13849
13850 /* Align ArgSize to a multiple of 8 */
13851 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
13852 bool NeedsAlign = (Align > 8);
13853
13854 MachineBasicBlock *thisMBB = MBB;
13855 MachineBasicBlock *overflowMBB;
13856 MachineBasicBlock *offsetMBB;
13857 MachineBasicBlock *endMBB;
13858
13859 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
13860 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
13861 unsigned OffsetReg = 0;
13862
13863 if (!UseGPOffset && !UseFPOffset) {
13864 // If we only pull from the overflow region, we don't create a branch.
13865 // We don't need to alter control flow.
13866 OffsetDestReg = 0; // unused
13867 OverflowDestReg = DestReg;
13868
13869 offsetMBB = NULL;
13870 overflowMBB = thisMBB;
13871 endMBB = thisMBB;
13872 } else {
13873 // First emit code to check if gp_offset (or fp_offset) is below the bound.
13874 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
13875 // If not, pull from overflow_area. (branch to overflowMBB)
13876 //
13877 // thisMBB
13878 // | .
13879 // | .
13880 // offsetMBB overflowMBB
13881 // | .
13882 // | .
13883 // endMBB
13884
13885 // Registers for the PHI in endMBB
13886 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
13887 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
13888
13889 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
13890 MachineFunction *MF = MBB->getParent();
13891 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13892 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13893 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
13894
13895 MachineFunction::iterator MBBIter = MBB;
13896 ++MBBIter;
13897
13898 // Insert the new basic blocks
13899 MF->insert(MBBIter, offsetMBB);
13900 MF->insert(MBBIter, overflowMBB);
13901 MF->insert(MBBIter, endMBB);
13902
13903 // Transfer the remainder of MBB and its successor edges to endMBB.
13904 endMBB->splice(endMBB->begin(), thisMBB,
13905 llvm::next(MachineBasicBlock::iterator(MI)),
13906 thisMBB->end());
13907 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
13908
13909 // Make offsetMBB and overflowMBB successors of thisMBB
13910 thisMBB->addSuccessor(offsetMBB);
13911 thisMBB->addSuccessor(overflowMBB);
13912
13913 // endMBB is a successor of both offsetMBB and overflowMBB
13914 offsetMBB->addSuccessor(endMBB);
13915 overflowMBB->addSuccessor(endMBB);
13916
13917 // Load the offset value into a register
13918 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13919 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
13920 .addOperand(Base)
13921 .addOperand(Scale)
13922 .addOperand(Index)
13923 .addDisp(Disp, UseFPOffset ? 4 : 0)
13924 .addOperand(Segment)
13925 .setMemRefs(MMOBegin, MMOEnd);
13926
13927 // Check if there is enough room left to pull this argument.
13928 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
13929 .addReg(OffsetReg)
13930 .addImm(MaxOffset + 8 - ArgSizeA8);
13931
13932 // Branch to "overflowMBB" if offset >= max
13933 // Fall through to "offsetMBB" otherwise
13934 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
13935 .addMBB(overflowMBB);
13936 }
13937
13938 // In offsetMBB, emit code to use the reg_save_area.
13939 if (offsetMBB) {
13940 assert(OffsetReg != 0);
13941
13942 // Read the reg_save_area address.
13943 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
13944 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
13945 .addOperand(Base)
13946 .addOperand(Scale)
13947 .addOperand(Index)
13948 .addDisp(Disp, 16)
13949 .addOperand(Segment)
13950 .setMemRefs(MMOBegin, MMOEnd);
13951
13952 // Zero-extend the offset
13953 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
13954 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
13955 .addImm(0)
13956 .addReg(OffsetReg)
13957 .addImm(X86::sub_32bit);
13958
13959 // Add the offset to the reg_save_area to get the final address.
13960 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
13961 .addReg(OffsetReg64)
13962 .addReg(RegSaveReg);
13963
13964 // Compute the offset for the next argument
13965 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
13966 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
13967 .addReg(OffsetReg)
13968 .addImm(UseFPOffset ? 16 : 8);
13969
13970 // Store it back into the va_list.
13971 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
13972 .addOperand(Base)
13973 .addOperand(Scale)
13974 .addOperand(Index)
13975 .addDisp(Disp, UseFPOffset ? 4 : 0)
13976 .addOperand(Segment)
13977 .addReg(NextOffsetReg)
13978 .setMemRefs(MMOBegin, MMOEnd);
13979
13980 // Jump to endMBB
13981 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
13982 .addMBB(endMBB);
13983 }
13984
13985 //
13986 // Emit code to use overflow area
13987 //
13988
13989 // Load the overflow_area address into a register.
13990 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
13991 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
13992 .addOperand(Base)
13993 .addOperand(Scale)
13994 .addOperand(Index)
13995 .addDisp(Disp, 8)
13996 .addOperand(Segment)
13997 .setMemRefs(MMOBegin, MMOEnd);
13998
13999 // If we need to align it, do so. Otherwise, just copy the address
14000 // to OverflowDestReg.
14001 if (NeedsAlign) {
14002 // Align the overflow address
14003 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
14004 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
14005
14006 // aligned_addr = (addr + (align-1)) & ~(align-1)
14007 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
14008 .addReg(OverflowAddrReg)
14009 .addImm(Align-1);
14010
14011 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
14012 .addReg(TmpReg)
14013 .addImm(~(uint64_t)(Align-1));
14014 } else {
14015 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
14016 .addReg(OverflowAddrReg);
14017 }
14018
14019 // Compute the next overflow address after this argument.
14020 // (the overflow address should be kept 8-byte aligned)
14021 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
14022 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
14023 .addReg(OverflowDestReg)
14024 .addImm(ArgSizeA8);
14025
14026 // Store the new overflow address.
14027 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
14028 .addOperand(Base)
14029 .addOperand(Scale)
14030 .addOperand(Index)
14031 .addDisp(Disp, 8)
14032 .addOperand(Segment)
14033 .addReg(NextAddrReg)
14034 .setMemRefs(MMOBegin, MMOEnd);
14035
14036 // If we branched, emit the PHI to the front of endMBB.
14037 if (offsetMBB) {
14038 BuildMI(*endMBB, endMBB->begin(), DL,
14039 TII->get(X86::PHI), DestReg)
14040 .addReg(OffsetDestReg).addMBB(offsetMBB)
14041 .addReg(OverflowDestReg).addMBB(overflowMBB);
14042 }
14043
14044 // Erase the pseudo instruction
14045 MI->eraseFromParent();
14046
14047 return endMBB;
14048}
14049
14050MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000014051X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
14052 MachineInstr *MI,
14053 MachineBasicBlock *MBB) const {
14054 // Emit code to save XMM registers to the stack. The ABI says that the
14055 // number of registers to save is given in %al, so it's theoretically
14056 // possible to do an indirect jump trick to avoid saving all of them,
14057 // however this code takes a simpler approach and just executes all
14058 // of the stores if %al is non-zero. It's less code, and it's probably
14059 // easier on the hardware branch predictor, and stores aren't all that
14060 // expensive anyway.
14061
14062 // Create the new basic blocks. One block contains all the XMM stores,
14063 // and one block is the final destination regardless of whether any
14064 // stores were performed.
14065 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
14066 MachineFunction *F = MBB->getParent();
14067 MachineFunction::iterator MBBIter = MBB;
14068 ++MBBIter;
14069 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
14070 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
14071 F->insert(MBBIter, XMMSaveMBB);
14072 F->insert(MBBIter, EndMBB);
14073
Dan Gohman14152b42010-07-06 20:24:04 +000014074 // Transfer the remainder of MBB and its successor edges to EndMBB.
14075 EndMBB->splice(EndMBB->begin(), MBB,
14076 llvm::next(MachineBasicBlock::iterator(MI)),
14077 MBB->end());
14078 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
14079
Dan Gohmand6708ea2009-08-15 01:38:56 +000014080 // The original block will now fall through to the XMM save block.
14081 MBB->addSuccessor(XMMSaveMBB);
14082 // The XMMSaveMBB will fall through to the end block.
14083 XMMSaveMBB->addSuccessor(EndMBB);
14084
14085 // Now add the instructions.
14086 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14087 DebugLoc DL = MI->getDebugLoc();
14088
14089 unsigned CountReg = MI->getOperand(0).getReg();
14090 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
14091 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
14092
14093 if (!Subtarget->isTargetWin64()) {
14094 // If %al is 0, branch around the XMM save block.
14095 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000014096 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014097 MBB->addSuccessor(EndMBB);
14098 }
14099
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000014100 unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000014101 // In the XMM save block, save all the XMM argument registers.
14102 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
14103 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000014104 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000014105 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000014106 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000014107 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000014108 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000014109 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000014110 .addFrameIndex(RegSaveFrameIndex)
14111 .addImm(/*Scale=*/1)
14112 .addReg(/*IndexReg=*/0)
14113 .addImm(/*Disp=*/Offset)
14114 .addReg(/*Segment=*/0)
14115 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000014116 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000014117 }
14118
Dan Gohman14152b42010-07-06 20:24:04 +000014119 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000014120
14121 return EndMBB;
14122}
Mon P Wang63307c32008-05-05 19:05:59 +000014123
Lang Hames6e3f7e42012-02-03 01:13:49 +000014124// The EFLAGS operand of SelectItr might be missing a kill marker
14125// because there were multiple uses of EFLAGS, and ISel didn't know
14126// which to mark. Figure out whether SelectItr should have had a
14127// kill marker, and set it if it should. Returns the correct kill
14128// marker value.
14129static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
14130 MachineBasicBlock* BB,
14131 const TargetRegisterInfo* TRI) {
14132 // Scan forward through BB for a use/def of EFLAGS.
14133 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
14134 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000014135 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014136 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000014137 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000014138 if (mi.definesRegister(X86::EFLAGS))
14139 break; // Should have kill-flag - update below.
14140 }
14141
14142 // If we hit the end of the block, check whether EFLAGS is live into a
14143 // successor.
14144 if (miI == BB->end()) {
14145 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
14146 sEnd = BB->succ_end();
14147 sItr != sEnd; ++sItr) {
14148 MachineBasicBlock* succ = *sItr;
14149 if (succ->isLiveIn(X86::EFLAGS))
14150 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000014151 }
14152 }
14153
Lang Hames6e3f7e42012-02-03 01:13:49 +000014154 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
14155 // out. SelectMI should have a kill flag on EFLAGS.
14156 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000014157 return true;
14158}
14159
Evan Cheng60c07e12006-07-05 22:17:51 +000014160MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000014161X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014162 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000014163 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14164 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000014165
Chris Lattner52600972009-09-02 05:57:00 +000014166 // To "insert" a SELECT_CC instruction, we actually have to insert the
14167 // diamond control-flow pattern. The incoming instruction knows the
14168 // destination vreg to set, the condition code register to branch on, the
14169 // true/false values to select between, and a branch opcode to use.
14170 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14171 MachineFunction::iterator It = BB;
14172 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000014173
Chris Lattner52600972009-09-02 05:57:00 +000014174 // thisMBB:
14175 // ...
14176 // TrueVal = ...
14177 // cmpTY ccX, r1, r2
14178 // bCC copy1MBB
14179 // fallthrough --> copy0MBB
14180 MachineBasicBlock *thisMBB = BB;
14181 MachineFunction *F = BB->getParent();
14182 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
14183 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000014184 F->insert(It, copy0MBB);
14185 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000014186
Bill Wendling730c07e2010-06-25 20:48:10 +000014187 // If the EFLAGS register isn't dead in the terminator, then claim that it's
14188 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000014189 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
14190 if (!MI->killsRegister(X86::EFLAGS) &&
14191 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
14192 copy0MBB->addLiveIn(X86::EFLAGS);
14193 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000014194 }
14195
Dan Gohman14152b42010-07-06 20:24:04 +000014196 // Transfer the remainder of BB and its successor edges to sinkMBB.
14197 sinkMBB->splice(sinkMBB->begin(), BB,
14198 llvm::next(MachineBasicBlock::iterator(MI)),
14199 BB->end());
14200 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
14201
14202 // Add the true and fallthrough blocks as its successors.
14203 BB->addSuccessor(copy0MBB);
14204 BB->addSuccessor(sinkMBB);
14205
14206 // Create the conditional branch instruction.
14207 unsigned Opc =
14208 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
14209 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
14210
Chris Lattner52600972009-09-02 05:57:00 +000014211 // copy0MBB:
14212 // %FalseValue = ...
14213 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000014214 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000014215
Chris Lattner52600972009-09-02 05:57:00 +000014216 // sinkMBB:
14217 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
14218 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000014219 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14220 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000014221 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
14222 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
14223
Dan Gohman14152b42010-07-06 20:24:04 +000014224 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000014225 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000014226}
14227
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014228MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014229X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
14230 bool Is64Bit) const {
14231 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14232 DebugLoc DL = MI->getDebugLoc();
14233 MachineFunction *MF = BB->getParent();
14234 const BasicBlock *LLVM_BB = BB->getBasicBlock();
14235
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014236 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014237
14238 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
14239 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
14240
14241 // BB:
14242 // ... [Till the alloca]
14243 // If stacklet is not large enough, jump to mallocMBB
14244 //
14245 // bumpMBB:
14246 // Allocate by subtracting from RSP
14247 // Jump to continueMBB
14248 //
14249 // mallocMBB:
14250 // Allocate by call to runtime
14251 //
14252 // continueMBB:
14253 // ...
14254 // [rest of original BB]
14255 //
14256
14257 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14258 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14259 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
14260
14261 MachineRegisterInfo &MRI = MF->getRegInfo();
14262 const TargetRegisterClass *AddrRegClass =
14263 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
14264
14265 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14266 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
14267 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000014268 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014269 sizeVReg = MI->getOperand(1).getReg(),
14270 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
14271
14272 MachineFunction::iterator MBBIter = BB;
14273 ++MBBIter;
14274
14275 MF->insert(MBBIter, bumpMBB);
14276 MF->insert(MBBIter, mallocMBB);
14277 MF->insert(MBBIter, continueMBB);
14278
14279 continueMBB->splice(continueMBB->begin(), BB, llvm::next
14280 (MachineBasicBlock::iterator(MI)), BB->end());
14281 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
14282
14283 // Add code to the main basic block to check if the stack limit has been hit,
14284 // and if so, jump to mallocMBB otherwise to bumpMBB.
14285 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000014286 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014287 .addReg(tmpSPVReg).addReg(sizeVReg);
14288 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000014289 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014290 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014291 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
14292
14293 // bumpMBB simply decreases the stack pointer, since we know the current
14294 // stacklet has enough space.
14295 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014296 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014297 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000014298 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014299 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14300
14301 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014302 const uint32_t *RegMask =
14303 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014304 if (Is64Bit) {
14305 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
14306 .addReg(sizeVReg);
14307 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014308 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014309 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000014310 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014311 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014312 } else {
14313 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
14314 .addImm(12);
14315 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
14316 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014317 .addExternalSymbol("__morestack_allocate_stack_space")
14318 .addRegMask(RegMask)
14319 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014320 }
14321
14322 if (!Is64Bit)
14323 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
14324 .addImm(16);
14325
14326 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
14327 .addReg(Is64Bit ? X86::RAX : X86::EAX);
14328 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
14329
14330 // Set up the CFG correctly.
14331 BB->addSuccessor(bumpMBB);
14332 BB->addSuccessor(mallocMBB);
14333 mallocMBB->addSuccessor(continueMBB);
14334 bumpMBB->addSuccessor(continueMBB);
14335
14336 // Take care of the PHI nodes.
14337 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
14338 MI->getOperand(0).getReg())
14339 .addReg(mallocPtrVReg).addMBB(mallocMBB)
14340 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
14341
14342 // Delete the original pseudo instruction.
14343 MI->eraseFromParent();
14344
14345 // And we're done.
14346 return continueMBB;
14347}
14348
14349MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014350X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014351 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014352 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14353 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014354
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014355 assert(!Subtarget->isTargetEnvMacho());
14356
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014357 // The lowering is pretty easy: we're just emitting the call to _alloca. The
14358 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014359
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014360 if (Subtarget->isTargetWin64()) {
14361 if (Subtarget->isTargetCygMing()) {
14362 // ___chkstk(Mingw64):
14363 // Clobbers R10, R11, RAX and EFLAGS.
14364 // Updates RSP.
14365 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14366 .addExternalSymbol("___chkstk")
14367 .addReg(X86::RAX, RegState::Implicit)
14368 .addReg(X86::RSP, RegState::Implicit)
14369 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
14370 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
14371 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14372 } else {
14373 // __chkstk(MSVCRT): does not update stack pointer.
14374 // Clobbers R10, R11 and EFLAGS.
14375 // FIXME: RAX(allocated size) might be reused and not killed.
14376 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
14377 .addExternalSymbol("__chkstk")
14378 .addReg(X86::RAX, RegState::Implicit)
14379 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14380 // RAX has the offset to subtracted from RSP.
14381 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
14382 .addReg(X86::RSP)
14383 .addReg(X86::RAX);
14384 }
14385 } else {
14386 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014387 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
14388
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000014389 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
14390 .addExternalSymbol(StackProbeSymbol)
14391 .addReg(X86::EAX, RegState::Implicit)
14392 .addReg(X86::ESP, RegState::Implicit)
14393 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
14394 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
14395 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
14396 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014397
Dan Gohman14152b42010-07-06 20:24:04 +000014398 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000014399 return BB;
14400}
Chris Lattner52600972009-09-02 05:57:00 +000014401
14402MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000014403X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
14404 MachineBasicBlock *BB) const {
14405 // This is pretty easy. We're taking the value that we received from
14406 // our load from the relocation, sticking it in either RDI (x86-64)
14407 // or EAX and doing an indirect call. The return value will then
14408 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000014409 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000014410 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000014411 DebugLoc DL = MI->getDebugLoc();
14412 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000014413
14414 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000014415 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000014416
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014417 // Get a register mask for the lowered call.
14418 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
14419 // proper register mask.
14420 const uint32_t *RegMask =
14421 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014422 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000014423 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14424 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000014425 .addReg(X86::RIP)
14426 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014427 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014428 MI->getOperand(3).getTargetFlags())
14429 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000014430 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000014431 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014432 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000014433 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000014434 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14435 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000014436 .addReg(0)
14437 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014438 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000014439 MI->getOperand(3).getTargetFlags())
14440 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014441 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014442 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014443 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014444 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000014445 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
14446 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000014447 .addReg(TII->getGlobalBaseReg(F))
14448 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000014449 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000014450 MI->getOperand(3).getTargetFlags())
14451 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000014452 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000014453 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000014454 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014455 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000014456
Dan Gohman14152b42010-07-06 20:24:04 +000014457 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000014458 return BB;
14459}
14460
14461MachineBasicBlock *
Michael Liao6c0e04c2012-10-15 22:39:43 +000014462X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
14463 MachineBasicBlock *MBB) const {
14464 DebugLoc DL = MI->getDebugLoc();
14465 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14466
14467 MachineFunction *MF = MBB->getParent();
14468 MachineRegisterInfo &MRI = MF->getRegInfo();
14469
14470 const BasicBlock *BB = MBB->getBasicBlock();
14471 MachineFunction::iterator I = MBB;
14472 ++I;
14473
14474 // Memory Reference
14475 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14476 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14477
14478 unsigned DstReg;
14479 unsigned MemOpndSlot = 0;
14480
14481 unsigned CurOp = 0;
14482
14483 DstReg = MI->getOperand(CurOp++).getReg();
14484 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
14485 assert(RC->hasType(MVT::i32) && "Invalid destination!");
14486 unsigned mainDstReg = MRI.createVirtualRegister(RC);
14487 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
14488
14489 MemOpndSlot = CurOp;
14490
14491 MVT PVT = getPointerTy();
14492 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14493 "Invalid Pointer Size!");
14494
14495 // For v = setjmp(buf), we generate
14496 //
14497 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014498 // buf[LabelOffset] = restoreMBB
Michael Liao6c0e04c2012-10-15 22:39:43 +000014499 // SjLjSetup restoreMBB
14500 //
14501 // mainMBB:
14502 // v_main = 0
14503 //
14504 // sinkMBB:
14505 // v = phi(main, restore)
14506 //
14507 // restoreMBB:
14508 // v_restore = 1
14509
14510 MachineBasicBlock *thisMBB = MBB;
14511 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
14512 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
14513 MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
14514 MF->insert(I, mainMBB);
14515 MF->insert(I, sinkMBB);
14516 MF->push_back(restoreMBB);
14517
14518 MachineInstrBuilder MIB;
14519
14520 // Transfer the remainder of BB and its successor edges to sinkMBB.
14521 sinkMBB->splice(sinkMBB->begin(), MBB,
14522 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
14523 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
14524
14525 // thisMBB:
Michael Liao281ae5a2012-10-17 02:22:27 +000014526 unsigned PtrStoreOpc = 0;
14527 unsigned LabelReg = 0;
14528 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14529 Reloc::Model RM = getTargetMachine().getRelocationModel();
14530 bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) &&
14531 (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014532
Michael Liao281ae5a2012-10-17 02:22:27 +000014533 // Prepare IP either in reg or imm.
14534 if (!UseImmLabel) {
14535 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
14536 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
14537 LabelReg = MRI.createVirtualRegister(PtrRC);
14538 if (Subtarget->is64Bit()) {
14539 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
14540 .addReg(X86::RIP)
14541 .addImm(0)
14542 .addReg(0)
14543 .addMBB(restoreMBB)
14544 .addReg(0);
14545 } else {
14546 const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
14547 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
14548 .addReg(XII->getGlobalBaseReg(MF))
14549 .addImm(0)
14550 .addReg(0)
14551 .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
14552 .addReg(0);
14553 }
14554 } else
14555 PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
Michael Liao6c0e04c2012-10-15 22:39:43 +000014556 // Store IP
Michael Liao281ae5a2012-10-17 02:22:27 +000014557 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
Michael Liao6c0e04c2012-10-15 22:39:43 +000014558 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14559 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014560 MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014561 else
14562 MIB.addOperand(MI->getOperand(MemOpndSlot + i));
14563 }
Michael Liao281ae5a2012-10-17 02:22:27 +000014564 if (!UseImmLabel)
14565 MIB.addReg(LabelReg);
14566 else
14567 MIB.addMBB(restoreMBB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014568 MIB.setMemRefs(MMOBegin, MMOEnd);
14569 // Setup
14570 MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
14571 .addMBB(restoreMBB);
14572 MIB.addRegMask(RegInfo->getNoPreservedMask());
14573 thisMBB->addSuccessor(mainMBB);
14574 thisMBB->addSuccessor(restoreMBB);
14575
14576 // mainMBB:
14577 // EAX = 0
14578 BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
14579 mainMBB->addSuccessor(sinkMBB);
14580
14581 // sinkMBB:
14582 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
14583 TII->get(X86::PHI), DstReg)
14584 .addReg(mainDstReg).addMBB(mainMBB)
14585 .addReg(restoreDstReg).addMBB(restoreMBB);
14586
14587 // restoreMBB:
14588 BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
14589 BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB);
14590 restoreMBB->addSuccessor(sinkMBB);
14591
14592 MI->eraseFromParent();
14593 return sinkMBB;
14594}
14595
14596MachineBasicBlock *
14597X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
14598 MachineBasicBlock *MBB) const {
14599 DebugLoc DL = MI->getDebugLoc();
14600 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14601
14602 MachineFunction *MF = MBB->getParent();
14603 MachineRegisterInfo &MRI = MF->getRegInfo();
14604
14605 // Memory Reference
14606 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
14607 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
14608
14609 MVT PVT = getPointerTy();
14610 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
14611 "Invalid Pointer Size!");
14612
14613 const TargetRegisterClass *RC =
14614 (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
14615 unsigned Tmp = MRI.createVirtualRegister(RC);
14616 // Since FP is only updated here but NOT referenced, it's treated as GPR.
14617 unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
14618 unsigned SP = RegInfo->getStackRegister();
14619
14620 MachineInstrBuilder MIB;
14621
Michael Liao281ae5a2012-10-17 02:22:27 +000014622 const int64_t LabelOffset = 1 * PVT.getStoreSize();
14623 const int64_t SPOffset = 2 * PVT.getStoreSize();
Michael Liao6c0e04c2012-10-15 22:39:43 +000014624
14625 unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
14626 unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
14627
14628 // Reload FP
14629 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
14630 for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
14631 MIB.addOperand(MI->getOperand(i));
14632 MIB.setMemRefs(MMOBegin, MMOEnd);
14633 // Reload IP
14634 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
14635 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14636 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014637 MIB.addDisp(MI->getOperand(i), LabelOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014638 else
14639 MIB.addOperand(MI->getOperand(i));
14640 }
14641 MIB.setMemRefs(MMOBegin, MMOEnd);
14642 // Reload SP
14643 MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
14644 for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
14645 if (i == X86::AddrDisp)
Michael Liao281ae5a2012-10-17 02:22:27 +000014646 MIB.addDisp(MI->getOperand(i), SPOffset);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014647 else
14648 MIB.addOperand(MI->getOperand(i));
14649 }
14650 MIB.setMemRefs(MMOBegin, MMOEnd);
14651 // Jump
14652 BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
14653
14654 MI->eraseFromParent();
14655 return MBB;
14656}
14657
14658MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000014659X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014660 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000014661 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000014662 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014663 case X86::TAILJMPd64:
14664 case X86::TAILJMPr64:
14665 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000014666 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014667 case X86::TCRETURNdi64:
14668 case X86::TCRETURNri64:
14669 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000014670 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000014671 case X86::WIN_ALLOCA:
14672 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000014673 case X86::SEG_ALLOCA_32:
14674 return EmitLoweredSegAlloca(MI, BB, false);
14675 case X86::SEG_ALLOCA_64:
14676 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000014677 case X86::TLSCall_32:
14678 case X86::TLSCall_64:
14679 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000014680 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000014681 case X86::CMOV_FR32:
14682 case X86::CMOV_FR64:
14683 case X86::CMOV_V4F32:
14684 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000014685 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000014686 case X86::CMOV_V8F32:
14687 case X86::CMOV_V4F64:
14688 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000014689 case X86::CMOV_GR16:
14690 case X86::CMOV_GR32:
14691 case X86::CMOV_RFP32:
14692 case X86::CMOV_RFP64:
14693 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000014694 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014695
Dale Johannesen849f2142007-07-03 00:53:03 +000014696 case X86::FP32_TO_INT16_IN_MEM:
14697 case X86::FP32_TO_INT32_IN_MEM:
14698 case X86::FP32_TO_INT64_IN_MEM:
14699 case X86::FP64_TO_INT16_IN_MEM:
14700 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000014701 case X86::FP64_TO_INT64_IN_MEM:
14702 case X86::FP80_TO_INT16_IN_MEM:
14703 case X86::FP80_TO_INT32_IN_MEM:
14704 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000014705 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
14706 DebugLoc DL = MI->getDebugLoc();
14707
Evan Cheng60c07e12006-07-05 22:17:51 +000014708 // Change the floating point control register to use "round towards zero"
14709 // mode when truncating to an integer value.
14710 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000014711 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000014712 addFrameReference(BuildMI(*BB, MI, DL,
14713 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014714
14715 // Load the old value of the high byte of the control word...
14716 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000014717 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000014718 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000014719 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014720
14721 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000014722 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014723 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000014724
14725 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000014726 addFrameReference(BuildMI(*BB, MI, DL,
14727 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014728
14729 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000014730 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000014731 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000014732
14733 // Get the X86 opcode to use.
14734 unsigned Opc;
14735 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000014736 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000014737 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
14738 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
14739 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
14740 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
14741 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
14742 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000014743 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
14744 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
14745 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000014746 }
14747
14748 X86AddressMode AM;
14749 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000014750 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014751 AM.BaseType = X86AddressMode::RegBase;
14752 AM.Base.Reg = Op.getReg();
14753 } else {
14754 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000014755 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000014756 }
14757 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000014758 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014759 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014760 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000014761 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000014762 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014763 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000014764 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000014765 AM.GV = Op.getGlobal();
14766 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000014767 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000014768 }
Dan Gohman14152b42010-07-06 20:24:04 +000014769 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000014770 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000014771
14772 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000014773 addFrameReference(BuildMI(*BB, MI, DL,
14774 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000014775
Dan Gohman14152b42010-07-06 20:24:04 +000014776 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000014777 return BB;
14778 }
Eric Christopherb120ab42009-08-18 22:50:32 +000014779 // String/text processing lowering.
14780 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014781 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014782 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014783 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000014784 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000014785 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000014786 case X86::PCMPESTRM128MEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014787 case X86::VPCMPESTRM128MEM:
14788 assert(Subtarget->hasSSE42() &&
14789 "Target must have SSE4.2 or AVX features enabled");
14790 return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo());
Craig Topper9c7ae012012-11-10 01:23:36 +000014791
14792 // String/text processing lowering.
14793 case X86::PCMPISTRIREG:
14794 case X86::VPCMPISTRIREG:
14795 case X86::PCMPISTRIMEM:
14796 case X86::VPCMPISTRIMEM:
14797 case X86::PCMPESTRIREG:
14798 case X86::VPCMPESTRIREG:
14799 case X86::PCMPESTRIMEM:
Craig Topper8aae8dd2012-11-10 08:57:41 +000014800 case X86::VPCMPESTRIMEM:
14801 assert(Subtarget->hasSSE42() &&
14802 "Target must have SSE4.2 or AVX features enabled");
14803 return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo());
Eric Christopherb120ab42009-08-18 22:50:32 +000014804
Craig Topper8aae8dd2012-11-10 08:57:41 +000014805 // Thread synchronization.
Eric Christopher228232b2010-11-30 07:20:12 +000014806 case X86::MONITOR:
Craig Topper2da36912012-11-11 22:45:02 +000014807 return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget);
Eric Christopher228232b2010-11-30 07:20:12 +000014808
Michael Liaobe02a902012-11-08 07:28:54 +000014809 // xbegin
14810 case X86::XBEGIN:
Craig Topper2da36912012-11-11 22:45:02 +000014811 return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo());
Michael Liaobe02a902012-11-08 07:28:54 +000014812
Craig Topper8aae8dd2012-11-10 08:57:41 +000014813 // Atomic Lowering.
Dale Johannesen140be2d2008-08-19 18:47:28 +000014814 case X86::ATOMAND8:
Michael Liaob118a072012-09-20 03:06:15 +000014815 case X86::ATOMAND16:
14816 case X86::ATOMAND32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014817 case X86::ATOMAND64:
Michael Liaob118a072012-09-20 03:06:15 +000014818 // Fall through
14819 case X86::ATOMOR8:
14820 case X86::ATOMOR16:
14821 case X86::ATOMOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014822 case X86::ATOMOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014823 // Fall through
14824 case X86::ATOMXOR16:
14825 case X86::ATOMXOR8:
14826 case X86::ATOMXOR32:
Dale Johannesena99e3842008-08-20 00:48:50 +000014827 case X86::ATOMXOR64:
Michael Liaob118a072012-09-20 03:06:15 +000014828 // Fall through
14829 case X86::ATOMNAND8:
14830 case X86::ATOMNAND16:
14831 case X86::ATOMNAND32:
14832 case X86::ATOMNAND64:
14833 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014834 case X86::ATOMMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014835 case X86::ATOMMAX16:
14836 case X86::ATOMMAX32:
14837 case X86::ATOMMAX64:
14838 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014839 case X86::ATOMMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014840 case X86::ATOMMIN16:
14841 case X86::ATOMMIN32:
14842 case X86::ATOMMIN64:
14843 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014844 case X86::ATOMUMAX8:
Michael Liaob118a072012-09-20 03:06:15 +000014845 case X86::ATOMUMAX16:
14846 case X86::ATOMUMAX32:
14847 case X86::ATOMUMAX64:
14848 // Fall through
Michael Liaofe87c302012-09-21 03:18:52 +000014849 case X86::ATOMUMIN8:
Michael Liaob118a072012-09-20 03:06:15 +000014850 case X86::ATOMUMIN16:
14851 case X86::ATOMUMIN32:
14852 case X86::ATOMUMIN64:
14853 return EmitAtomicLoadArith(MI, BB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014854
14855 // This group does 64-bit operations on a 32-bit host.
14856 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014857 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014858 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014859 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014860 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000014861 case X86::ATOMSUB6432:
Michael Liaoe5e8f762012-09-25 18:08:13 +000014862 case X86::ATOMMAX6432:
14863 case X86::ATOMMIN6432:
14864 case X86::ATOMUMAX6432:
14865 case X86::ATOMUMIN6432:
Michael Liaob118a072012-09-20 03:06:15 +000014866 case X86::ATOMSWAP6432:
14867 return EmitAtomicLoadArith6432(MI, BB);
Craig Topperacaaa6f2012-08-18 06:39:34 +000014868
Dan Gohmand6708ea2009-08-15 01:38:56 +000014869 case X86::VASTART_SAVE_XMM_REGS:
14870 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000014871
14872 case X86::VAARG_64:
14873 return EmitVAARG64WithCustomInserter(MI, BB);
Michael Liao6c0e04c2012-10-15 22:39:43 +000014874
14875 case X86::EH_SjLj_SetJmp32:
14876 case X86::EH_SjLj_SetJmp64:
14877 return emitEHSjLjSetJmp(MI, BB);
14878
14879 case X86::EH_SjLj_LongJmp32:
14880 case X86::EH_SjLj_LongJmp64:
14881 return emitEHSjLjLongJmp(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000014882 }
14883}
14884
14885//===----------------------------------------------------------------------===//
14886// X86 Optimization Hooks
14887//===----------------------------------------------------------------------===//
14888
Dan Gohman475871a2008-07-27 21:46:04 +000014889void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000014890 APInt &KnownZero,
14891 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000014892 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000014893 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014894 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014895 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000014896 assert((Opc >= ISD::BUILTIN_OP_END ||
14897 Opc == ISD::INTRINSIC_WO_CHAIN ||
14898 Opc == ISD::INTRINSIC_W_CHAIN ||
14899 Opc == ISD::INTRINSIC_VOID) &&
14900 "Should use MaskedValueIsZero if you don't know whether Op"
14901 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014902
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014903 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014904 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000014905 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014906 case X86ISD::ADD:
14907 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000014908 case X86ISD::ADC:
14909 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014910 case X86ISD::SMUL:
14911 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000014912 case X86ISD::INC:
14913 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000014914 case X86ISD::OR:
14915 case X86ISD::XOR:
14916 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000014917 // These nodes' second result is a boolean.
14918 if (Op.getResNo() == 0)
14919 break;
14920 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014921 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014922 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000014923 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014924 case ISD::INTRINSIC_WO_CHAIN: {
14925 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
14926 unsigned NumLoBits = 0;
14927 switch (IntId) {
14928 default: break;
14929 case Intrinsic::x86_sse_movmsk_ps:
14930 case Intrinsic::x86_avx_movmsk_ps_256:
14931 case Intrinsic::x86_sse2_movmsk_pd:
14932 case Intrinsic::x86_avx_movmsk_pd_256:
14933 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000014934 case Intrinsic::x86_sse2_pmovmskb_128:
14935 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000014936 // High bits of movmskp{s|d}, pmovmskb are known zero.
14937 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000014938 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000014939 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
14940 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
14941 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
14942 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
14943 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
14944 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000014945 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000014946 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000014947 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000014948 break;
14949 }
14950 }
14951 break;
14952 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014953 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000014954}
Chris Lattner259e97c2006-01-31 19:43:35 +000014955
Owen Andersonbc146b02010-09-21 20:42:50 +000014956unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
14957 unsigned Depth) const {
14958 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
14959 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
14960 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000014961
Owen Andersonbc146b02010-09-21 20:42:50 +000014962 // Fallback case.
14963 return 1;
14964}
14965
Evan Cheng206ee9d2006-07-07 08:33:52 +000014966/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000014967/// node is a GlobalAddress + offset.
14968bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000014969 const GlobalValue* &GA,
14970 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000014971 if (N->getOpcode() == X86ISD::Wrapper) {
14972 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000014973 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000014974 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000014975 return true;
14976 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000014977 }
Evan Chengad4196b2008-05-12 19:56:52 +000014978 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000014979}
14980
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014981/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
14982/// same as extracting the high 128-bit part of 256-bit vector and then
14983/// inserting the result into the low part of a new 256-bit vector
14984static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
14985 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000014986 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014987
14988 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000014989 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000014990 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
14991 SVOp->getMaskElt(j) >= 0)
14992 return false;
14993
14994 return true;
14995}
14996
14997/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
14998/// same as extracting the low 128-bit part of 256-bit vector and then
14999/// inserting the result into the high part of a new 256-bit vector
15000static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
15001 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015002 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015003
15004 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000015005 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015006 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
15007 SVOp->getMaskElt(j) >= 0)
15008 return false;
15009
15010 return true;
15011}
15012
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015013/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
15014static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000015015 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015016 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015017 DebugLoc dl = N->getDebugLoc();
15018 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
15019 SDValue V1 = SVOp->getOperand(0);
15020 SDValue V2 = SVOp->getOperand(1);
15021 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000015022 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015023
15024 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
15025 V2.getOpcode() == ISD::CONCAT_VECTORS) {
15026 //
15027 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000015028 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015029 // V UNDEF BUILD_VECTOR UNDEF
15030 // \ / \ /
15031 // CONCAT_VECTOR CONCAT_VECTOR
15032 // \ /
15033 // \ /
15034 // RESULT: V + zero extended
15035 //
15036 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
15037 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
15038 V1.getOperand(1).getOpcode() != ISD::UNDEF)
15039 return SDValue();
15040
15041 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
15042 return SDValue();
15043
15044 // To match the shuffle mask, the first half of the mask should
15045 // be exactly the first vector, and all the rest a splat with the
15046 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000015047 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015048 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
15049 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
15050 return SDValue();
15051
Chad Rosier3d1161e2012-01-03 21:05:52 +000015052 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
15053 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000015054 if (Ld->hasNUsesOfValue(1, 0)) {
15055 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
15056 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
15057 SDValue ResNode =
15058 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
15059 Ld->getMemoryVT(),
15060 Ld->getPointerInfo(),
15061 Ld->getAlignment(),
15062 false/*isVolatile*/, true/*ReadMem*/,
15063 false/*WriteMem*/);
Manman Ren2adc5032012-11-13 19:13:05 +000015064
15065 // Make sure the newly-created LOAD is in the same position as Ld in
15066 // terms of dependency. We create a TokenFactor for Ld and ResNode,
15067 // and update uses of Ld's output chain to use the TokenFactor.
15068 if (Ld->hasAnyUseOfValue(1)) {
15069 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
15070 SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
15071 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
15072 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
15073 SDValue(ResNode.getNode(), 1));
15074 }
15075
Chad Rosier42726832012-05-07 18:47:44 +000015076 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
15077 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000015078 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000015079
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015080 // Emit a zeroed vector and insert the desired subvector on its
15081 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015082 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000015083 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015084 return DCI.CombineTo(N, InsV);
15085 }
15086
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015087 //===--------------------------------------------------------------------===//
15088 // Combine some shuffles into subvector extracts and inserts:
15089 //
15090
15091 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
15092 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015093 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
15094 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015095 return DCI.CombineTo(N, InsV);
15096 }
15097
15098 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
15099 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000015100 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
15101 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000015102 return DCI.CombineTo(N, InsV);
15103 }
15104
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015105 return SDValue();
15106}
15107
15108/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000015109static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015110 TargetLowering::DAGCombinerInfo &DCI,
15111 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000015112 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000015113 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000015114
Mon P Wanga0fd0d52010-12-19 23:55:53 +000015115 // Don't create instructions with illegal types after legalize types has run.
15116 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15117 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
15118 return SDValue();
15119
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015120 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000015121 if (Subtarget->hasFp256() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000015122 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000015123 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015124
15125 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015126 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000015127 return SDValue();
15128
15129 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
15130 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
15131 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000015132 SmallVector<SDValue, 16> Elts;
15133 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000015134 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000015135
Nate Begemanfdea31a2010-03-24 20:49:50 +000015136 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000015137}
Evan Chengd880b972008-05-09 21:53:03 +000015138
Nadav Roteme12bf182013-01-04 17:35:21 +000015139/// PerformTruncateCombine - Converts truncate operation to
15140/// a sequence of vector shuffle operations.
15141/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000015142static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
15143 TargetLowering::DAGCombinerInfo &DCI,
15144 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000015145 return SDValue();
15146}
15147
Craig Topper89f4e662012-03-20 07:17:59 +000015148/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
15149/// specific shuffle of a load can be folded into a single element load.
15150/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
15151/// shuffles have been customed lowered so we need to handle those here.
15152static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
15153 TargetLowering::DAGCombinerInfo &DCI) {
15154 if (DCI.isBeforeLegalizeOps())
15155 return SDValue();
15156
15157 SDValue InVec = N->getOperand(0);
15158 SDValue EltNo = N->getOperand(1);
15159
15160 if (!isa<ConstantSDNode>(EltNo))
15161 return SDValue();
15162
15163 EVT VT = InVec.getValueType();
15164
15165 bool HasShuffleIntoBitcast = false;
15166 if (InVec.getOpcode() == ISD::BITCAST) {
15167 // Don't duplicate a load with other uses.
15168 if (!InVec.hasOneUse())
15169 return SDValue();
15170 EVT BCVT = InVec.getOperand(0).getValueType();
15171 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
15172 return SDValue();
15173 InVec = InVec.getOperand(0);
15174 HasShuffleIntoBitcast = true;
15175 }
15176
15177 if (!isTargetShuffle(InVec.getOpcode()))
15178 return SDValue();
15179
15180 // Don't duplicate a load with other uses.
15181 if (!InVec.hasOneUse())
15182 return SDValue();
15183
15184 SmallVector<int, 16> ShuffleMask;
15185 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000015186 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
15187 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000015188 return SDValue();
15189
15190 // Select the input vector, guarding against out of range extract vector.
15191 unsigned NumElems = VT.getVectorNumElements();
15192 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
15193 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
15194 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
15195 : InVec.getOperand(1);
15196
15197 // If inputs to shuffle are the same for both ops, then allow 2 uses
15198 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
15199
15200 if (LdNode.getOpcode() == ISD::BITCAST) {
15201 // Don't duplicate a load with other uses.
15202 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
15203 return SDValue();
15204
15205 AllowedUses = 1; // only allow 1 load use if we have a bitcast
15206 LdNode = LdNode.getOperand(0);
15207 }
15208
15209 if (!ISD::isNormalLoad(LdNode.getNode()))
15210 return SDValue();
15211
15212 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
15213
15214 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
15215 return SDValue();
15216
15217 if (HasShuffleIntoBitcast) {
15218 // If there's a bitcast before the shuffle, check if the load type and
15219 // alignment is valid.
15220 unsigned Align = LN0->getAlignment();
15221 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Micah Villmow3574eca2012-10-08 16:38:25 +000015222 unsigned NewAlign = TLI.getDataLayout()->
Craig Topper89f4e662012-03-20 07:17:59 +000015223 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
15224
15225 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
15226 return SDValue();
15227 }
15228
15229 // All checks match so transform back to vector_shuffle so that DAG combiner
15230 // can finish the job
15231 DebugLoc dl = N->getDebugLoc();
15232
15233 // Create shuffle node taking into account the case that its a unary shuffle
15234 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
15235 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
15236 InVec.getOperand(0), Shuffle,
15237 &ShuffleMask[0]);
15238 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
15239 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
15240 EltNo);
15241}
15242
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000015243/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
15244/// generation and convert it from being a bunch of shuffles and extracts
15245/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015246static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000015247 TargetLowering::DAGCombinerInfo &DCI) {
15248 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
15249 if (NewOp.getNode())
15250 return NewOp;
15251
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015252 SDValue InputVector = N->getOperand(0);
Manman Ren4c74a952012-10-30 22:15:38 +000015253 // Detect whether we are trying to convert from mmx to i32 and the bitcast
15254 // from mmx to v2i32 has a single usage.
15255 if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST &&
15256 InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx &&
15257 InputVector.hasOneUse() && N->getValueType(0) == MVT::i32)
15258 return DAG.getNode(X86ISD::MMX_MOVD2W, InputVector.getDebugLoc(),
15259 N->getValueType(0),
15260 InputVector.getNode()->getOperand(0));
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015261
15262 // Only operate on vectors of 4 elements, where the alternative shuffling
15263 // gets to be more expensive.
15264 if (InputVector.getValueType() != MVT::v4i32)
15265 return SDValue();
15266
15267 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
15268 // single use which is a sign-extend or zero-extend, and all elements are
15269 // used.
15270 SmallVector<SDNode *, 4> Uses;
15271 unsigned ExtractedElements = 0;
15272 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
15273 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
15274 if (UI.getUse().getResNo() != InputVector.getResNo())
15275 return SDValue();
15276
15277 SDNode *Extract = *UI;
15278 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
15279 return SDValue();
15280
15281 if (Extract->getValueType(0) != MVT::i32)
15282 return SDValue();
15283 if (!Extract->hasOneUse())
15284 return SDValue();
15285 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
15286 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
15287 return SDValue();
15288 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
15289 return SDValue();
15290
15291 // Record which element was extracted.
15292 ExtractedElements |=
15293 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
15294
15295 Uses.push_back(Extract);
15296 }
15297
15298 // If not all the elements were used, this may not be worthwhile.
15299 if (ExtractedElements != 15)
15300 return SDValue();
15301
15302 // Ok, we've now decided to do the transformation.
15303 DebugLoc dl = InputVector.getDebugLoc();
15304
15305 // Store the value to a temporary stack slot.
15306 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000015307 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
15308 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015309
15310 // Replace each use (extract) with a load of the appropriate element.
15311 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
15312 UE = Uses.end(); UI != UE; ++UI) {
15313 SDNode *Extract = *UI;
15314
Nadav Rotem86694292011-05-17 08:31:57 +000015315 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015316 SDValue Idx = Extract->getOperand(1);
15317 unsigned EltSize =
15318 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
15319 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000015320 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015321 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
15322
Nadav Rotem86694292011-05-17 08:31:57 +000015323 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015324 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015325
15326 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000015327 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000015328 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015329 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000015330
15331 // Replace the exact with the load.
15332 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
15333 }
15334
15335 // The replacement was made in place; don't return anything.
15336 return SDValue();
15337}
15338
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015339/// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match.
15340static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS,
15341 SDValue RHS, SelectionDAG &DAG,
15342 const X86Subtarget *Subtarget) {
15343 if (!VT.isVector())
15344 return 0;
15345
15346 switch (VT.getSimpleVT().SimpleTy) {
15347 default: return 0;
15348 case MVT::v32i8:
15349 case MVT::v16i16:
15350 case MVT::v8i32:
15351 if (!Subtarget->hasAVX2())
15352 return 0;
15353 case MVT::v16i8:
15354 case MVT::v8i16:
15355 case MVT::v4i32:
15356 if (!Subtarget->hasSSE2())
15357 return 0;
15358 }
15359
15360 // SSE2 has only a small subset of the operations.
15361 bool hasUnsigned = Subtarget->hasSSE41() ||
15362 (Subtarget->hasSSE2() && VT == MVT::v16i8);
15363 bool hasSigned = Subtarget->hasSSE41() ||
15364 (Subtarget->hasSSE2() && VT == MVT::v8i16);
15365
15366 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15367
15368 // Check for x CC y ? x : y.
15369 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15370 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15371 switch (CC) {
15372 default: break;
15373 case ISD::SETULT:
15374 case ISD::SETULE:
15375 return hasUnsigned ? X86ISD::UMIN : 0;
15376 case ISD::SETUGT:
15377 case ISD::SETUGE:
15378 return hasUnsigned ? X86ISD::UMAX : 0;
15379 case ISD::SETLT:
15380 case ISD::SETLE:
15381 return hasSigned ? X86ISD::SMIN : 0;
15382 case ISD::SETGT:
15383 case ISD::SETGE:
15384 return hasSigned ? X86ISD::SMAX : 0;
15385 }
15386 // Check for x CC y ? y : x -- a min/max with reversed arms.
15387 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15388 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
15389 switch (CC) {
15390 default: break;
15391 case ISD::SETULT:
15392 case ISD::SETULE:
15393 return hasUnsigned ? X86ISD::UMAX : 0;
15394 case ISD::SETUGT:
15395 case ISD::SETUGE:
15396 return hasUnsigned ? X86ISD::UMIN : 0;
15397 case ISD::SETLT:
15398 case ISD::SETLE:
15399 return hasSigned ? X86ISD::SMAX : 0;
15400 case ISD::SETGT:
15401 case ISD::SETGE:
15402 return hasSigned ? X86ISD::SMIN : 0;
15403 }
15404 }
15405
15406 return 0;
15407}
15408
Duncan Sands6bcd2192011-09-17 16:49:39 +000015409/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
15410/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015411static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000015412 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000015413 const X86Subtarget *Subtarget) {
15414 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000015415 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000015416 // Get the LHS/RHS of the select.
15417 SDValue LHS = N->getOperand(1);
15418 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000015419 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000015420
Dan Gohman670e5392009-09-21 18:03:22 +000015421 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000015422 // instructions match the semantics of the common C idiom x<y?x:y but not
15423 // x<=y?x:y, because of how they handle negative zero (which can be
15424 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000015425 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
15426 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000015427 (Subtarget->hasSSE2() ||
15428 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015429 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015430
Chris Lattner47b4ce82009-03-11 05:48:52 +000015431 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000015432 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000015433 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15434 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015435 switch (CC) {
15436 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015437 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015438 // Converting this to a min would handle NaNs incorrectly, and swapping
15439 // the operands would cause it to handle comparisons between positive
15440 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015441 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015442 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015443 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15444 break;
15445 std::swap(LHS, RHS);
15446 }
Dan Gohman670e5392009-09-21 18:03:22 +000015447 Opcode = X86ISD::FMIN;
15448 break;
15449 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015450 // Converting this to a min would handle comparisons between positive
15451 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015452 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015453 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
15454 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015455 Opcode = X86ISD::FMIN;
15456 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015457 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015458 // Converting this to a min would handle both negative zeros and NaNs
15459 // incorrectly, but we can swap the operands to fix both.
15460 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015461 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015462 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015463 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015464 Opcode = X86ISD::FMIN;
15465 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015466
Dan Gohman670e5392009-09-21 18:03:22 +000015467 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015468 // Converting this to a max would handle comparisons between positive
15469 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015470 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000015471 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015472 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015473 Opcode = X86ISD::FMAX;
15474 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000015475 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015476 // Converting this to a max would handle NaNs incorrectly, and swapping
15477 // the operands would cause it to handle comparisons between positive
15478 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015479 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015480 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015481 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
15482 break;
15483 std::swap(LHS, RHS);
15484 }
Dan Gohman670e5392009-09-21 18:03:22 +000015485 Opcode = X86ISD::FMAX;
15486 break;
15487 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015488 // Converting this to a max would handle both negative zeros and NaNs
15489 // incorrectly, but we can swap the operands to fix both.
15490 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015491 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015492 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015493 case ISD::SETGE:
15494 Opcode = X86ISD::FMAX;
15495 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000015496 }
Dan Gohman670e5392009-09-21 18:03:22 +000015497 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000015498 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
15499 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000015500 switch (CC) {
15501 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000015502 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015503 // Converting this to a min would handle comparisons between positive
15504 // and negative zero incorrectly, and swapping the operands would
15505 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015506 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015507 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000015508 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015509 break;
15510 std::swap(LHS, RHS);
15511 }
Dan Gohman670e5392009-09-21 18:03:22 +000015512 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000015513 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015514 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000015515 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015516 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015517 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
15518 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015519 Opcode = X86ISD::FMIN;
15520 break;
15521 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000015522 // Converting this to a min would handle both negative zeros and NaNs
15523 // incorrectly, but we can swap the operands to fix both.
15524 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015525 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015526 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015527 case ISD::SETGE:
15528 Opcode = X86ISD::FMIN;
15529 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015530
Dan Gohman670e5392009-09-21 18:03:22 +000015531 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000015532 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000015533 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015534 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015535 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000015536 break;
Dan Gohman670e5392009-09-21 18:03:22 +000015537 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000015538 // Converting this to a max would handle comparisons between positive
15539 // and negative zero incorrectly, and swapping the operands would
15540 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015541 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000015542 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000015543 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000015544 break;
15545 std::swap(LHS, RHS);
15546 }
Dan Gohman670e5392009-09-21 18:03:22 +000015547 Opcode = X86ISD::FMAX;
15548 break;
15549 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000015550 // Converting this to a max would handle both negative zeros and NaNs
15551 // incorrectly, but we can swap the operands to fix both.
15552 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000015553 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015554 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000015555 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000015556 Opcode = X86ISD::FMAX;
15557 break;
15558 }
Chris Lattner83e6c992006-10-04 06:57:07 +000015559 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000015560
Chris Lattner47b4ce82009-03-11 05:48:52 +000015561 if (Opcode)
15562 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000015563 }
Eric Christopherfd179292009-08-27 18:07:15 +000015564
Chris Lattnerd1980a52009-03-12 06:52:53 +000015565 // If this is a select between two integer constants, try to do some
15566 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000015567 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
15568 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000015569 // Don't do this for crazy integer types.
15570 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
15571 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000015572 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015573 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000015574
Chris Lattnercee56e72009-03-13 05:53:31 +000015575 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000015576 // Efficiently invertible.
15577 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
15578 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
15579 isa<ConstantSDNode>(Cond.getOperand(1))))) {
15580 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000015581 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015582 }
Eric Christopherfd179292009-08-27 18:07:15 +000015583
Chris Lattnerd1980a52009-03-12 06:52:53 +000015584 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015585 if (FalseC->getAPIntValue() == 0 &&
15586 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015587 if (NeedsCondInvert) // Invert the condition if needed.
15588 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15589 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015590
Chris Lattnerd1980a52009-03-12 06:52:53 +000015591 // Zero extend the condition if needed.
15592 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015593
Chris Lattnercee56e72009-03-13 05:53:31 +000015594 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000015595 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015596 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015597 }
Eric Christopherfd179292009-08-27 18:07:15 +000015598
Chris Lattner97a29a52009-03-13 05:22:11 +000015599 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000015600 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000015601 if (NeedsCondInvert) // Invert the condition if needed.
15602 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15603 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015604
Chris Lattner97a29a52009-03-13 05:22:11 +000015605 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015606 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15607 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015608 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000015609 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000015610 }
Eric Christopherfd179292009-08-27 18:07:15 +000015611
Chris Lattnercee56e72009-03-13 05:53:31 +000015612 // Optimize cases that will turn into an LEA instruction. This requires
15613 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015614 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015615 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015616 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015617
Chris Lattnercee56e72009-03-13 05:53:31 +000015618 bool isFastMultiplier = false;
15619 if (Diff < 10) {
15620 switch ((unsigned char)Diff) {
15621 default: break;
15622 case 1: // result = add base, cond
15623 case 2: // result = lea base( , cond*2)
15624 case 3: // result = lea base(cond, cond*2)
15625 case 4: // result = lea base( , cond*4)
15626 case 5: // result = lea base(cond, cond*4)
15627 case 8: // result = lea base( , cond*8)
15628 case 9: // result = lea base(cond, cond*8)
15629 isFastMultiplier = true;
15630 break;
15631 }
15632 }
Eric Christopherfd179292009-08-27 18:07:15 +000015633
Chris Lattnercee56e72009-03-13 05:53:31 +000015634 if (isFastMultiplier) {
15635 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
15636 if (NeedsCondInvert) // Invert the condition if needed.
15637 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
15638 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015639
Chris Lattnercee56e72009-03-13 05:53:31 +000015640 // Zero extend the condition if needed.
15641 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15642 Cond);
15643 // Scale the condition by the difference.
15644 if (Diff != 1)
15645 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15646 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000015647
Chris Lattnercee56e72009-03-13 05:53:31 +000015648 // Add the base if non-zero.
15649 if (FalseC->getAPIntValue() != 0)
15650 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15651 SDValue(FalseC, 0));
15652 return Cond;
15653 }
Eric Christopherfd179292009-08-27 18:07:15 +000015654 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000015655 }
15656 }
Eric Christopherfd179292009-08-27 18:07:15 +000015657
Evan Cheng56f582d2012-01-04 01:41:39 +000015658 // Canonicalize max and min:
15659 // (x > y) ? x : y -> (x >= y) ? x : y
15660 // (x < y) ? x : y -> (x <= y) ? x : y
15661 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
15662 // the need for an extra compare
15663 // against zero. e.g.
15664 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
15665 // subl %esi, %edi
15666 // testl %edi, %edi
15667 // movl $0, %eax
15668 // cmovgl %edi, %eax
15669 // =>
15670 // xorl %eax, %eax
15671 // subl %esi, $edi
15672 // cmovsl %eax, %edi
15673 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
15674 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
15675 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
15676 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15677 switch (CC) {
15678 default: break;
15679 case ISD::SETLT:
15680 case ISD::SETGT: {
15681 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
15682 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
15683 Cond.getOperand(0), Cond.getOperand(1), NewCC);
15684 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
15685 }
15686 }
15687 }
15688
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015689 // Match VSELECTs into subs with unsigned saturation.
15690 if (!DCI.isBeforeLegalize() &&
15691 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
15692 // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
15693 ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
15694 (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
15695 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
15696
15697 // Check if one of the arms of the VSELECT is a zero vector. If it's on the
15698 // left side invert the predicate to simplify logic below.
15699 SDValue Other;
15700 if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
15701 Other = RHS;
15702 CC = ISD::getSetCCInverse(CC, true);
15703 } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
15704 Other = LHS;
15705 }
15706
15707 if (Other.getNode() && Other->getNumOperands() == 2 &&
15708 DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
15709 SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
15710 SDValue CondRHS = Cond->getOperand(1);
15711
15712 // Look for a general sub with unsigned saturation first.
15713 // x >= y ? x-y : 0 --> subus x, y
15714 // x > y ? x-y : 0 --> subus x, y
15715 if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
15716 Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
15717 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15718
15719 // If the RHS is a constant we have to reverse the const canonicalization.
15720 // x > C-1 ? x+-C : 0 --> subus x, C
15721 if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
15722 isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) {
15723 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015724 if (CondRHS.getConstantOperandVal(0) == -A-1)
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015725 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS,
Benjamin Kramer9fa92512013-02-04 15:19:25 +000015726 DAG.getConstant(-A, VT));
Benjamin Kramer388fc6a2012-12-15 16:47:44 +000015727 }
15728
15729 // Another special case: If C was a sign bit, the sub has been
15730 // canonicalized into a xor.
15731 // FIXME: Would it be better to use ComputeMaskedBits to determine whether
15732 // it's safe to decanonicalize the xor?
15733 // x s< 0 ? x^C : 0 --> subus x, C
15734 if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
15735 ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
15736 isSplatVector(OpRHS.getNode())) {
15737 APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue();
15738 if (A.isSignBit())
15739 return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
15740 }
15741 }
15742 }
15743
Benjamin Kramer2556c6b2012-12-21 17:46:58 +000015744 // Try to match a min/max vector operation.
15745 if (!DCI.isBeforeLegalize() &&
15746 N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC)
15747 if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget))
15748 return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS);
15749
Nadav Rotemcc616562012-01-15 19:27:55 +000015750 // If we know that this node is legal then we know that it is going to be
15751 // matched by one of the SSE/AVX BLEND instructions. These instructions only
15752 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
15753 // to simplify previous instructions.
15754 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15755 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000015756 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000015757 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000015758
15759 // Don't optimize vector selects that map to mask-registers.
15760 if (BitWidth == 1)
15761 return SDValue();
15762
Nadav Rotemcc616562012-01-15 19:27:55 +000015763 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
15764 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
15765
15766 APInt KnownZero, KnownOne;
15767 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
15768 DCI.isBeforeLegalizeOps());
15769 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
15770 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
15771 DCI.CommitTargetLoweringOpt(TLO);
15772 }
15773
Dan Gohman475871a2008-07-27 21:46:04 +000015774 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000015775}
15776
Michael Liao2a33cec2012-08-10 19:58:13 +000015777// Check whether a boolean test is testing a boolean value generated by
15778// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
15779// code.
15780//
15781// Simplify the following patterns:
15782// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
15783// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
15784// to (Op EFLAGS Cond)
15785//
15786// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
15787// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
15788// to (Op EFLAGS !Cond)
15789//
15790// where Op could be BRCOND or CMOV.
15791//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015792static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000015793 // Quit if not CMP and SUB with its value result used.
15794 if (Cmp.getOpcode() != X86ISD::CMP &&
15795 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
15796 return SDValue();
15797
15798 // Quit if not used as a boolean value.
15799 if (CC != X86::COND_E && CC != X86::COND_NE)
15800 return SDValue();
15801
15802 // Check CMP operands. One of them should be 0 or 1 and the other should be
15803 // an SetCC or extended from it.
15804 SDValue Op1 = Cmp.getOperand(0);
15805 SDValue Op2 = Cmp.getOperand(1);
15806
15807 SDValue SetCC;
15808 const ConstantSDNode* C = 0;
15809 bool needOppositeCond = (CC == X86::COND_E);
15810
15811 if ((C = dyn_cast<ConstantSDNode>(Op1)))
15812 SetCC = Op2;
15813 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
15814 SetCC = Op1;
15815 else // Quit if all operands are not constants.
15816 return SDValue();
15817
15818 if (C->getZExtValue() == 1)
15819 needOppositeCond = !needOppositeCond;
15820 else if (C->getZExtValue() != 0)
15821 // Quit if the constant is neither 0 or 1.
15822 return SDValue();
15823
Michael Liao258d9b72013-03-28 23:38:52 +000015824 // Skip 'zext' or 'trunc' node.
15825 if (SetCC.getOpcode() == ISD::ZERO_EXTEND ||
15826 SetCC.getOpcode() == ISD::TRUNCATE)
Michael Liao2a33cec2012-08-10 19:58:13 +000015827 SetCC = SetCC.getOperand(0);
15828
Michael Liao7fdc66b2012-09-10 16:36:16 +000015829 switch (SetCC.getOpcode()) {
15830 case X86ISD::SETCC:
15831 // Set the condition code or opposite one if necessary.
15832 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
15833 if (needOppositeCond)
15834 CC = X86::GetOppositeBranchCondition(CC);
15835 return SetCC.getOperand(1);
15836 case X86ISD::CMOV: {
15837 // Check whether false/true value has canonical one, i.e. 0 or 1.
15838 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
15839 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
15840 // Quit if true value is not a constant.
15841 if (!TVal)
15842 return SDValue();
15843 // Quit if false value is not a constant.
15844 if (!FVal) {
Michael Liao7fdc66b2012-09-10 16:36:16 +000015845 SDValue Op = SetCC.getOperand(0);
Michael Liao258d9b72013-03-28 23:38:52 +000015846 // Skip 'zext' or 'trunc' node.
15847 if (Op.getOpcode() == ISD::ZERO_EXTEND ||
15848 Op.getOpcode() == ISD::TRUNCATE)
15849 Op = Op.getOperand(0);
15850 // A special case for rdrand, where 0 is set if false cond is found.
15851 if (Op.getOpcode() != X86ISD::RDRAND || Op.getResNo() != 0)
Michael Liao7fdc66b2012-09-10 16:36:16 +000015852 return SDValue();
15853 }
15854 // Quit if false value is not the constant 0 or 1.
15855 bool FValIsFalse = true;
15856 if (FVal && FVal->getZExtValue() != 0) {
15857 if (FVal->getZExtValue() != 1)
15858 return SDValue();
15859 // If FVal is 1, opposite cond is needed.
15860 needOppositeCond = !needOppositeCond;
15861 FValIsFalse = false;
15862 }
15863 // Quit if TVal is not the constant opposite of FVal.
15864 if (FValIsFalse && TVal->getZExtValue() != 1)
15865 return SDValue();
15866 if (!FValIsFalse && TVal->getZExtValue() != 0)
15867 return SDValue();
15868 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
15869 if (needOppositeCond)
15870 CC = X86::GetOppositeBranchCondition(CC);
15871 return SetCC.getOperand(3);
15872 }
15873 }
Michael Liao2a33cec2012-08-10 19:58:13 +000015874
Michael Liao7fdc66b2012-09-10 16:36:16 +000015875 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000015876}
15877
Chris Lattnerd1980a52009-03-12 06:52:53 +000015878/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
15879static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015880 TargetLowering::DAGCombinerInfo &DCI,
15881 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015882 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000015883
Chris Lattnerd1980a52009-03-12 06:52:53 +000015884 // If the flag operand isn't dead, don't touch this CMOV.
15885 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
15886 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000015887
Evan Chengb5a55d92011-05-24 01:48:22 +000015888 SDValue FalseOp = N->getOperand(0);
15889 SDValue TrueOp = N->getOperand(1);
15890 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
15891 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000015892
Evan Chengb5a55d92011-05-24 01:48:22 +000015893 if (CC == X86::COND_E || CC == X86::COND_NE) {
15894 switch (Cond.getOpcode()) {
15895 default: break;
15896 case X86ISD::BSR:
15897 case X86ISD::BSF:
15898 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
15899 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
15900 return (CC == X86::COND_E) ? FalseOp : TrueOp;
15901 }
15902 }
15903
Michael Liao2a33cec2012-08-10 19:58:13 +000015904 SDValue Flags;
15905
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015906 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000015907 if (Flags.getNode() &&
15908 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000015909 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015910 SDValue Ops[] = { FalseOp, TrueOp,
15911 DAG.getConstant(CC, MVT::i8), Flags };
15912 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
15913 Ops, array_lengthof(Ops));
15914 }
15915
Chris Lattnerd1980a52009-03-12 06:52:53 +000015916 // If this is a select between two integer constants, try to do some
15917 // optimizations. Note that the operands are ordered the opposite of SELECT
15918 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000015919 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
15920 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000015921 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
15922 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000015923 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
15924 CC = X86::GetOppositeBranchCondition(CC);
15925 std::swap(TrueC, FalseC);
NAKAMURA Takumie2687452012-10-16 06:28:34 +000015926 std::swap(TrueOp, FalseOp);
Chris Lattnerd1980a52009-03-12 06:52:53 +000015927 }
Eric Christopherfd179292009-08-27 18:07:15 +000015928
Chris Lattnerd1980a52009-03-12 06:52:53 +000015929 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000015930 // This is efficient for any integer data type (including i8/i16) and
15931 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000015932 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015933 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15934 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015935
Chris Lattnerd1980a52009-03-12 06:52:53 +000015936 // Zero extend the condition if needed.
15937 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015938
Chris Lattnerd1980a52009-03-12 06:52:53 +000015939 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
15940 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000015941 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000015942 if (N->getNumValues() == 2) // Dead flag value?
15943 return DCI.CombineTo(N, Cond, SDValue());
15944 return Cond;
15945 }
Eric Christopherfd179292009-08-27 18:07:15 +000015946
Chris Lattnercee56e72009-03-13 05:53:31 +000015947 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
15948 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000015949 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015950 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15951 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000015952
Chris Lattner97a29a52009-03-13 05:22:11 +000015953 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000015954 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
15955 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000015956 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
15957 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000015958
Chris Lattner97a29a52009-03-13 05:22:11 +000015959 if (N->getNumValues() == 2) // Dead flag value?
15960 return DCI.CombineTo(N, Cond, SDValue());
15961 return Cond;
15962 }
Eric Christopherfd179292009-08-27 18:07:15 +000015963
Chris Lattnercee56e72009-03-13 05:53:31 +000015964 // Optimize cases that will turn into an LEA instruction. This requires
15965 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000015966 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000015967 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015968 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000015969
Chris Lattnercee56e72009-03-13 05:53:31 +000015970 bool isFastMultiplier = false;
15971 if (Diff < 10) {
15972 switch ((unsigned char)Diff) {
15973 default: break;
15974 case 1: // result = add base, cond
15975 case 2: // result = lea base( , cond*2)
15976 case 3: // result = lea base(cond, cond*2)
15977 case 4: // result = lea base( , cond*4)
15978 case 5: // result = lea base(cond, cond*4)
15979 case 8: // result = lea base( , cond*8)
15980 case 9: // result = lea base(cond, cond*8)
15981 isFastMultiplier = true;
15982 break;
15983 }
15984 }
Eric Christopherfd179292009-08-27 18:07:15 +000015985
Chris Lattnercee56e72009-03-13 05:53:31 +000015986 if (isFastMultiplier) {
15987 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000015988 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
15989 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000015990 // Zero extend the condition if needed.
15991 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
15992 Cond);
15993 // Scale the condition by the difference.
15994 if (Diff != 1)
15995 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
15996 DAG.getConstant(Diff, Cond.getValueType()));
15997
15998 // Add the base if non-zero.
15999 if (FalseC->getAPIntValue() != 0)
16000 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
16001 SDValue(FalseC, 0));
16002 if (N->getNumValues() == 2) // Dead flag value?
16003 return DCI.CombineTo(N, Cond, SDValue());
16004 return Cond;
16005 }
Eric Christopherfd179292009-08-27 18:07:15 +000016006 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000016007 }
16008 }
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016009
16010 // Handle these cases:
16011 // (select (x != c), e, c) -> select (x != c), e, x),
16012 // (select (x == c), c, e) -> select (x == c), x, e)
16013 // where the c is an integer constant, and the "select" is the combination
16014 // of CMOV and CMP.
16015 //
16016 // The rationale for this change is that the conditional-move from a constant
16017 // needs two instructions, however, conditional-move from a register needs
16018 // only one instruction.
16019 //
16020 // CAVEAT: By replacing a constant with a symbolic value, it may obscure
16021 // some instruction-combining opportunities. This opt needs to be
16022 // postponed as late as possible.
16023 //
16024 if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
16025 // the DCI.xxxx conditions are provided to postpone the optimization as
16026 // late as possible.
16027
16028 ConstantSDNode *CmpAgainst = 0;
16029 if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
16030 (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016031 !isa<ConstantSDNode>(Cond.getOperand(0))) {
NAKAMURA Takumie2687452012-10-16 06:28:34 +000016032
16033 if (CC == X86::COND_NE &&
16034 CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
16035 CC = X86::GetOppositeBranchCondition(CC);
16036 std::swap(TrueOp, FalseOp);
16037 }
16038
16039 if (CC == X86::COND_E &&
16040 CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
16041 SDValue Ops[] = { FalseOp, Cond.getOperand(0),
16042 DAG.getConstant(CC, MVT::i8), Cond };
16043 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops,
16044 array_lengthof(Ops));
16045 }
16046 }
16047 }
16048
Chris Lattnerd1980a52009-03-12 06:52:53 +000016049 return SDValue();
16050}
16051
Evan Cheng0b0cd912009-03-28 05:57:29 +000016052/// PerformMulCombine - Optimize a single multiply with constant into two
16053/// in order to implement it with two cheaper instructions, e.g.
16054/// LEA + SHL, LEA + LEA.
16055static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
16056 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000016057 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
16058 return SDValue();
16059
Owen Andersone50ed302009-08-10 22:56:29 +000016060 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000016061 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000016062 return SDValue();
16063
16064 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
16065 if (!C)
16066 return SDValue();
16067 uint64_t MulAmt = C->getZExtValue();
16068 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
16069 return SDValue();
16070
16071 uint64_t MulAmt1 = 0;
16072 uint64_t MulAmt2 = 0;
16073 if ((MulAmt % 9) == 0) {
16074 MulAmt1 = 9;
16075 MulAmt2 = MulAmt / 9;
16076 } else if ((MulAmt % 5) == 0) {
16077 MulAmt1 = 5;
16078 MulAmt2 = MulAmt / 5;
16079 } else if ((MulAmt % 3) == 0) {
16080 MulAmt1 = 3;
16081 MulAmt2 = MulAmt / 3;
16082 }
16083 if (MulAmt2 &&
16084 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
16085 DebugLoc DL = N->getDebugLoc();
16086
16087 if (isPowerOf2_64(MulAmt2) &&
16088 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
16089 // If second multiplifer is pow2, issue it first. We want the multiply by
16090 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
16091 // is an add.
16092 std::swap(MulAmt1, MulAmt2);
16093
16094 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000016095 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016096 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000016097 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000016098 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016099 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000016100 DAG.getConstant(MulAmt1, VT));
16101
Eric Christopherfd179292009-08-27 18:07:15 +000016102 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000016103 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000016104 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000016105 else
Evan Cheng73f24c92009-03-30 21:36:47 +000016106 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000016107 DAG.getConstant(MulAmt2, VT));
16108
16109 // Do not add new nodes to DAG combiner worklist.
16110 DCI.CombineTo(N, NewMul, false);
16111 }
16112 return SDValue();
16113}
16114
Evan Chengad9c0a32009-12-15 00:53:42 +000016115static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
16116 SDValue N0 = N->getOperand(0);
16117 SDValue N1 = N->getOperand(1);
16118 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
16119 EVT VT = N0.getValueType();
16120
16121 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
16122 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016123 if (VT.isInteger() && !VT.isVector() &&
16124 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000016125 N0.getOperand(1).getOpcode() == ISD::Constant) {
16126 SDValue N00 = N0.getOperand(0);
16127 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
16128 ((N00.getOpcode() == ISD::ANY_EXTEND ||
16129 N00.getOpcode() == ISD::ZERO_EXTEND) &&
16130 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
16131 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
16132 APInt ShAmt = N1C->getAPIntValue();
16133 Mask = Mask.shl(ShAmt);
16134 if (Mask != 0)
16135 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
16136 N00, DAG.getConstant(Mask, VT));
16137 }
16138 }
16139
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016140 // Hardware support for vector shifts is sparse which makes us scalarize the
16141 // vector operations in many cases. Also, on sandybridge ADD is faster than
16142 // shl.
16143 // (shl V, 1) -> add V,V
16144 if (isSplatVector(N1.getNode())) {
16145 assert(N0.getValueType().isVector() && "Invalid vector shift type");
16146 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
16147 // We shift all of the values by one. In many cases we do not have
16148 // hardware support for this operation. This is better expressed as an ADD
16149 // of two values.
16150 if (N1C && (1 == N1C->getZExtValue())) {
16151 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
16152 }
16153 }
16154
Evan Chengad9c0a32009-12-15 00:53:42 +000016155 return SDValue();
16156}
Evan Cheng0b0cd912009-03-28 05:57:29 +000016157
Nate Begeman740ab032009-01-26 00:52:55 +000016158/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
16159/// when possible.
16160static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000016161 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000016162 const X86Subtarget *Subtarget) {
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000016163 if (N->getOpcode() == ISD::SHL) {
16164 SDValue V = PerformSHLCombine(N, DAG);
16165 if (V.getNode()) return V;
16166 }
Evan Chengad9c0a32009-12-15 00:53:42 +000016167
Michael Liao42317cc2013-03-20 02:33:21 +000016168 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000016169}
16170
Stuart Hastings865f0932011-06-03 23:53:54 +000016171// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
16172// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
16173// and friends. Likewise for OR -> CMPNEQSS.
16174static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
16175 TargetLowering::DAGCombinerInfo &DCI,
16176 const X86Subtarget *Subtarget) {
16177 unsigned opcode;
16178
16179 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
16180 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000016181 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000016182 SDValue N0 = N->getOperand(0);
16183 SDValue N1 = N->getOperand(1);
16184 SDValue CMP0 = N0->getOperand(1);
16185 SDValue CMP1 = N1->getOperand(1);
16186 DebugLoc DL = N->getDebugLoc();
16187
16188 // The SETCCs should both refer to the same CMP.
16189 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
16190 return SDValue();
16191
16192 SDValue CMP00 = CMP0->getOperand(0);
16193 SDValue CMP01 = CMP0->getOperand(1);
16194 EVT VT = CMP00.getValueType();
16195
16196 if (VT == MVT::f32 || VT == MVT::f64) {
16197 bool ExpectingFlags = false;
16198 // Check for any users that want flags:
Jakub Staszak30fcfc32013-02-16 13:34:26 +000016199 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Stuart Hastings865f0932011-06-03 23:53:54 +000016200 !ExpectingFlags && UI != UE; ++UI)
16201 switch (UI->getOpcode()) {
16202 default:
16203 case ISD::BR_CC:
16204 case ISD::BRCOND:
16205 case ISD::SELECT:
16206 ExpectingFlags = true;
16207 break;
16208 case ISD::CopyToReg:
16209 case ISD::SIGN_EXTEND:
16210 case ISD::ZERO_EXTEND:
16211 case ISD::ANY_EXTEND:
16212 break;
16213 }
16214
16215 if (!ExpectingFlags) {
16216 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
16217 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
16218
16219 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
16220 X86::CondCode tmp = cc0;
16221 cc0 = cc1;
16222 cc1 = tmp;
16223 }
16224
16225 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
16226 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
16227 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
16228 X86ISD::NodeType NTOperator = is64BitFP ?
16229 X86ISD::FSETCCsd : X86ISD::FSETCCss;
16230 // FIXME: need symbolic constants for these magic numbers.
16231 // See X86ATTInstPrinter.cpp:printSSECC().
16232 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
16233 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
16234 DAG.getConstant(x86cc, MVT::i8));
16235 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
16236 OnesOrZeroesF);
16237 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
16238 DAG.getConstant(1, MVT::i32));
16239 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
16240 return OneBitOfTruth;
16241 }
16242 }
16243 }
16244 }
16245 return SDValue();
16246}
16247
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016248/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
16249/// so it can be folded inside ANDNP.
16250static bool CanFoldXORWithAllOnes(const SDNode *N) {
16251 EVT VT = N->getValueType(0);
16252
16253 // Match direct AllOnes for 128 and 256-bit vectors
16254 if (ISD::isBuildVectorAllOnes(N))
16255 return true;
16256
16257 // Look through a bit convert.
16258 if (N->getOpcode() == ISD::BITCAST)
16259 N = N->getOperand(0).getNode();
16260
16261 // Sometimes the operand may come from a insert_subvector building a 256-bit
16262 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000016263 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000016264 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
16265 SDValue V1 = N->getOperand(0);
16266 SDValue V2 = N->getOperand(1);
16267
16268 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
16269 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
16270 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
16271 ISD::isBuildVectorAllOnes(V2.getNode()))
16272 return true;
16273 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016274
16275 return false;
16276}
16277
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016278// On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
16279// register. In most cases we actually compare or select YMM-sized registers
16280// and mixing the two types creates horrible code. This method optimizes
16281// some of the transition sequences.
16282static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
16283 TargetLowering::DAGCombinerInfo &DCI,
16284 const X86Subtarget *Subtarget) {
16285 EVT VT = N->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016286 if (!VT.is256BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016287 return SDValue();
16288
16289 assert((N->getOpcode() == ISD::ANY_EXTEND ||
16290 N->getOpcode() == ISD::ZERO_EXTEND ||
16291 N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
16292
16293 SDValue Narrow = N->getOperand(0);
16294 EVT NarrowVT = Narrow->getValueType(0);
Craig Topper5a529e42013-01-18 06:44:29 +000016295 if (!NarrowVT.is128BitVector())
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016296 return SDValue();
16297
16298 if (Narrow->getOpcode() != ISD::XOR &&
16299 Narrow->getOpcode() != ISD::AND &&
16300 Narrow->getOpcode() != ISD::OR)
16301 return SDValue();
16302
16303 SDValue N0 = Narrow->getOperand(0);
16304 SDValue N1 = Narrow->getOperand(1);
16305 DebugLoc DL = Narrow->getDebugLoc();
16306
16307 // The Left side has to be a trunc.
16308 if (N0.getOpcode() != ISD::TRUNCATE)
16309 return SDValue();
16310
16311 // The type of the truncated inputs.
16312 EVT WideVT = N0->getOperand(0)->getValueType(0);
16313 if (WideVT != VT)
16314 return SDValue();
16315
16316 // The right side has to be a 'trunc' or a constant vector.
16317 bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
16318 bool RHSConst = (isSplatVector(N1.getNode()) &&
16319 isa<ConstantSDNode>(N1->getOperand(0)));
16320 if (!RHSTrunc && !RHSConst)
16321 return SDValue();
16322
16323 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16324
16325 if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
16326 return SDValue();
16327
16328 // Set N0 and N1 to hold the inputs to the new wide operation.
16329 N0 = N0->getOperand(0);
16330 if (RHSConst) {
16331 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(),
16332 N1->getOperand(0));
16333 SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
16334 N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size());
16335 } else if (RHSTrunc) {
16336 N1 = N1->getOperand(0);
16337 }
16338
16339 // Generate the wide operation.
Nadav Roteme3b24892013-01-02 17:41:03 +000016340 SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016341 unsigned Opcode = N->getOpcode();
16342 switch (Opcode) {
16343 case ISD::ANY_EXTEND:
16344 return Op;
16345 case ISD::ZERO_EXTEND: {
16346 unsigned InBits = NarrowVT.getScalarType().getSizeInBits();
16347 APInt Mask = APInt::getAllOnesValue(InBits);
16348 Mask = Mask.zext(VT.getScalarType().getSizeInBits());
16349 return DAG.getNode(ISD::AND, DL, VT,
16350 Op, DAG.getConstant(Mask, VT));
16351 }
16352 case ISD::SIGN_EXTEND:
16353 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
16354 Op, DAG.getValueType(NarrowVT));
16355 default:
16356 llvm_unreachable("Unexpected opcode");
16357 }
16358}
16359
Nate Begemanb65c1752010-12-17 22:55:37 +000016360static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
16361 TargetLowering::DAGCombinerInfo &DCI,
16362 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016363 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000016364 if (DCI.isBeforeLegalizeOps())
16365 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016366
Stuart Hastings865f0932011-06-03 23:53:54 +000016367 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16368 if (R.getNode())
16369 return R;
16370
Craig Topperb926afc2012-12-17 05:12:30 +000016371 // Create BLSI, and BLSR instructions
Craig Topperb4c94572011-10-21 06:55:01 +000016372 // BLSI is X & (-X)
16373 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000016374 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
16375 SDValue N0 = N->getOperand(0);
16376 SDValue N1 = N->getOperand(1);
16377 DebugLoc DL = N->getDebugLoc();
16378
Craig Topperb4c94572011-10-21 06:55:01 +000016379 // Check LHS for neg
16380 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
16381 isZero(N0.getOperand(0)))
16382 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
16383
16384 // Check RHS for neg
16385 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
16386 isZero(N1.getOperand(0)))
16387 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
16388
16389 // Check LHS for X-1
16390 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16391 isAllOnes(N0.getOperand(1)))
16392 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
16393
16394 // Check RHS for X-1
16395 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16396 isAllOnes(N1.getOperand(1)))
16397 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
16398
Craig Topper54a11172011-10-14 07:06:56 +000016399 return SDValue();
16400 }
16401
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016402 // Want to form ANDNP nodes:
16403 // 1) In the hopes of then easily combining them with OR and AND nodes
16404 // to form PBLEND/PSIGN.
16405 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000016406 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000016407 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016408
Nate Begemanb65c1752010-12-17 22:55:37 +000016409 SDValue N0 = N->getOperand(0);
16410 SDValue N1 = N->getOperand(1);
16411 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016412
Nate Begemanb65c1752010-12-17 22:55:37 +000016413 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016414 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016415 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
16416 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016417 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000016418
16419 // Check RHS for vnot
16420 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000016421 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
16422 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000016423 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016424
Nate Begemanb65c1752010-12-17 22:55:37 +000016425 return SDValue();
16426}
16427
Evan Cheng760d1942010-01-04 21:22:48 +000016428static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000016429 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000016430 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016431 EVT VT = N->getValueType(0);
Evan Cheng39cfeec2010-04-28 02:25:18 +000016432 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000016433 return SDValue();
16434
Stuart Hastings865f0932011-06-03 23:53:54 +000016435 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
16436 if (R.getNode())
16437 return R;
16438
Evan Cheng760d1942010-01-04 21:22:48 +000016439 SDValue N0 = N->getOperand(0);
16440 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016441
Nate Begemanb65c1752010-12-17 22:55:37 +000016442 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000016443 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000016444 if (!Subtarget->hasSSSE3() ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016445 (VT == MVT::v4i64 && !Subtarget->hasInt256()))
Craig Topper1666cb62011-11-19 07:07:26 +000016446 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016447
Craig Topper1666cb62011-11-19 07:07:26 +000016448 // Canonicalize pandn to RHS
16449 if (N0.getOpcode() == X86ISD::ANDNP)
16450 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000016451 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000016452 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
16453 SDValue Mask = N1.getOperand(0);
16454 SDValue X = N1.getOperand(1);
16455 SDValue Y;
16456 if (N0.getOperand(0) == Mask)
16457 Y = N0.getOperand(1);
16458 if (N0.getOperand(1) == Mask)
16459 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016460
Craig Topper1666cb62011-11-19 07:07:26 +000016461 // Check to see if the mask appeared in both the AND and ANDNP and
16462 if (!Y.getNode())
16463 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016464
Craig Topper1666cb62011-11-19 07:07:26 +000016465 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000016466 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000016467 if (Mask.getOpcode() == ISD::BITCAST)
16468 Mask = Mask.getOperand(0);
16469 if (X.getOpcode() == ISD::BITCAST)
16470 X = X.getOperand(0);
16471 if (Y.getOpcode() == ISD::BITCAST)
16472 Y = Y.getOperand(0);
16473
Craig Topper1666cb62011-11-19 07:07:26 +000016474 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016475
Craig Toppered2e13d2012-01-22 19:15:14 +000016476 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000016477 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
16478 // there is no psrai.b
Craig Topper1666cb62011-11-19 07:07:26 +000016479 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
Michael Liao42317cc2013-03-20 02:33:21 +000016480 unsigned SraAmt = ~0;
16481 if (Mask.getOpcode() == ISD::SRA) {
16482 SDValue Amt = Mask.getOperand(1);
16483 if (isSplatVector(Amt.getNode())) {
16484 SDValue SclrAmt = Amt->getOperand(0);
16485 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt))
16486 SraAmt = C->getZExtValue();
16487 }
16488 } else if (Mask.getOpcode() == X86ISD::VSRAI) {
16489 SDValue SraC = Mask.getOperand(1);
16490 SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
16491 }
Craig Topper1666cb62011-11-19 07:07:26 +000016492 if ((SraAmt + 1) != EltBits)
16493 return SDValue();
16494
16495 DebugLoc DL = N->getDebugLoc();
16496
16497 // Now we know we at least have a plendvb with the mask val. See if
16498 // we can form a psignb/w/d.
16499 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000016500 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
16501 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000016502 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
16503 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
16504 "Unsupported VT for PSIGN");
Nadav Rotemf8db4472013-02-24 07:09:35 +000016505 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000016506 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000016507 }
16508 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000016509 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000016510 return SDValue();
16511
16512 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
16513
16514 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
16515 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
16516 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000016517 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000016518 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000016519 }
16520 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016521
Craig Topper1666cb62011-11-19 07:07:26 +000016522 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
16523 return SDValue();
16524
Nate Begemanb65c1752010-12-17 22:55:37 +000016525 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000016526 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
16527 std::swap(N0, N1);
16528 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
16529 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000016530 if (!N0.hasOneUse() || !N1.hasOneUse())
16531 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000016532
16533 SDValue ShAmt0 = N0.getOperand(1);
16534 if (ShAmt0.getValueType() != MVT::i8)
16535 return SDValue();
16536 SDValue ShAmt1 = N1.getOperand(1);
16537 if (ShAmt1.getValueType() != MVT::i8)
16538 return SDValue();
16539 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
16540 ShAmt0 = ShAmt0.getOperand(0);
16541 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
16542 ShAmt1 = ShAmt1.getOperand(0);
16543
16544 DebugLoc DL = N->getDebugLoc();
16545 unsigned Opc = X86ISD::SHLD;
16546 SDValue Op0 = N0.getOperand(0);
16547 SDValue Op1 = N1.getOperand(0);
16548 if (ShAmt0.getOpcode() == ISD::SUB) {
16549 Opc = X86ISD::SHRD;
16550 std::swap(Op0, Op1);
16551 std::swap(ShAmt0, ShAmt1);
16552 }
16553
Evan Cheng8b1190a2010-04-28 01:18:01 +000016554 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000016555 if (ShAmt1.getOpcode() == ISD::SUB) {
16556 SDValue Sum = ShAmt1.getOperand(0);
16557 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000016558 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
16559 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
16560 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
16561 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000016562 return DAG.getNode(Opc, DL, VT,
16563 Op0, Op1,
16564 DAG.getNode(ISD::TRUNCATE, DL,
16565 MVT::i8, ShAmt0));
16566 }
16567 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
16568 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
16569 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000016570 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000016571 return DAG.getNode(Opc, DL, VT,
16572 N0.getOperand(0), N1.getOperand(0),
16573 DAG.getNode(ISD::TRUNCATE, DL,
16574 MVT::i8, ShAmt0));
16575 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000016576
Evan Cheng760d1942010-01-04 21:22:48 +000016577 return SDValue();
16578}
16579
Manman Ren92363622012-06-07 22:39:10 +000016580// Generate NEG and CMOV for integer abs.
16581static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
16582 EVT VT = N->getValueType(0);
16583
16584 // Since X86 does not have CMOV for 8-bit integer, we don't convert
16585 // 8-bit integer abs to NEG and CMOV.
16586 if (VT.isInteger() && VT.getSizeInBits() == 8)
16587 return SDValue();
16588
16589 SDValue N0 = N->getOperand(0);
16590 SDValue N1 = N->getOperand(1);
16591 DebugLoc DL = N->getDebugLoc();
16592
16593 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
16594 // and change it to SUB and CMOV.
16595 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
16596 N0.getOpcode() == ISD::ADD &&
16597 N0.getOperand(1) == N1 &&
16598 N1.getOpcode() == ISD::SRA &&
16599 N1.getOperand(0) == N0.getOperand(0))
16600 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
16601 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
16602 // Generate SUB & CMOV.
16603 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
16604 DAG.getConstant(0, VT), N0.getOperand(0));
16605
16606 SDValue Ops[] = { N0.getOperand(0), Neg,
16607 DAG.getConstant(X86::COND_GE, MVT::i8),
16608 SDValue(Neg.getNode(), 1) };
16609 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
16610 Ops, array_lengthof(Ops));
16611 }
16612 return SDValue();
16613}
16614
Craig Topper3738ccd2011-12-27 06:27:23 +000016615// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000016616static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
16617 TargetLowering::DAGCombinerInfo &DCI,
16618 const X86Subtarget *Subtarget) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000016619 EVT VT = N->getValueType(0);
Craig Topperb4c94572011-10-21 06:55:01 +000016620 if (DCI.isBeforeLegalizeOps())
16621 return SDValue();
16622
Manman Ren45d53b82012-06-08 18:58:26 +000016623 if (Subtarget->hasCMov()) {
16624 SDValue RV = performIntegerAbsCombine(N, DAG);
16625 if (RV.getNode())
16626 return RV;
16627 }
Manman Ren92363622012-06-07 22:39:10 +000016628
16629 // Try forming BMI if it is available.
16630 if (!Subtarget->hasBMI())
16631 return SDValue();
16632
Craig Topperb4c94572011-10-21 06:55:01 +000016633 if (VT != MVT::i32 && VT != MVT::i64)
16634 return SDValue();
16635
Craig Topper3738ccd2011-12-27 06:27:23 +000016636 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
16637
Craig Topperb4c94572011-10-21 06:55:01 +000016638 // Create BLSMSK instructions by finding X ^ (X-1)
16639 SDValue N0 = N->getOperand(0);
16640 SDValue N1 = N->getOperand(1);
16641 DebugLoc DL = N->getDebugLoc();
16642
16643 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
16644 isAllOnes(N0.getOperand(1)))
16645 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
16646
16647 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
16648 isAllOnes(N1.getOperand(1)))
16649 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
16650
16651 return SDValue();
16652}
16653
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016654/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
16655static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016656 TargetLowering::DAGCombinerInfo &DCI,
16657 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016658 LoadSDNode *Ld = cast<LoadSDNode>(N);
16659 EVT RegVT = Ld->getValueType(0);
16660 EVT MemVT = Ld->getMemoryVT();
16661 DebugLoc dl = Ld->getDebugLoc();
16662 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016663 unsigned RegSz = RegVT.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016664
Michael Liaod4584c92013-03-25 23:50:10 +000016665 // On Sandybridge unaligned 256bit loads are inefficient.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016666 ISD::LoadExtType Ext = Ld->getExtensionType();
Nadav Rotem48177ac2013-01-18 23:10:30 +000016667 unsigned Alignment = Ld->getAlignment();
Michael Liaod4584c92013-03-25 23:50:10 +000016668 bool IsAligned = Alignment == 0 || Alignment >= MemVT.getSizeInBits()/8;
Nadav Rotem48177ac2013-01-18 23:10:30 +000016669 if (RegVT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016670 !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) {
Nadav Rotem48177ac2013-01-18 23:10:30 +000016671 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotemba958652013-01-19 08:38:41 +000016672 if (NumElems < 2)
16673 return SDValue();
16674
Nadav Rotem48177ac2013-01-18 23:10:30 +000016675 SDValue Ptr = Ld->getBasePtr();
16676 SDValue Increment = DAG.getConstant(16, TLI.getPointerTy());
16677
16678 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
16679 NumElems/2);
16680 SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16681 Ld->getPointerInfo(), Ld->isVolatile(),
16682 Ld->isNonTemporal(), Ld->isInvariant(),
16683 Alignment);
16684 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16685 SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
16686 Ld->getPointerInfo(), Ld->isVolatile(),
16687 Ld->isNonTemporal(), Ld->isInvariant(),
Michael Liaod4584c92013-03-25 23:50:10 +000016688 std::min(16U, Alignment));
Nadav Rotem48177ac2013-01-18 23:10:30 +000016689 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
16690 Load1.getValue(1),
16691 Load2.getValue(1));
16692
16693 SDValue NewVec = DAG.getUNDEF(RegVT);
16694 NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
16695 NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
16696 return DCI.CombineTo(N, NewVec, TF, true);
16697 }
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016698
Nadav Rotemca6f2962011-09-18 19:00:23 +000016699 // If this is a vector EXT Load then attempt to optimize it using a
Benjamin Kramer17347912012-12-22 11:34:28 +000016700 // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the
16701 // expansion is still better than scalar code.
16702 // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll
16703 // emit a shuffle and a arithmetic shift.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016704 // TODO: It is possible to support ZExt by zeroing the undef values
16705 // during the shuffle phase or after the shuffle.
Benjamin Kramer17347912012-12-22 11:34:28 +000016706 if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() &&
16707 (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016708 assert(MemVT != RegVT && "Cannot extend to the same type");
16709 assert(MemVT.isVector() && "Must load a vector from memory");
16710
16711 unsigned NumElems = RegVT.getVectorNumElements();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016712 unsigned MemSz = MemVT.getSizeInBits();
16713 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016714
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016715 if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256())
16716 return SDValue();
16717
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016718 // All sizes must be a power of two.
16719 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
16720 return SDValue();
16721
16722 // Attempt to load the original value using scalar loads.
16723 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016724 MVT SclrLoadTy = MVT::i8;
16725 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16726 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16727 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016728 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016729 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016730 }
16731 }
16732
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016733 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16734 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
16735 (64 <= MemSz))
16736 SclrLoadTy = MVT::f64;
16737
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016738 // Calculate the number of scalar loads that we need to perform
16739 // in order to load our vector from memory.
16740 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016741 if (Ext == ISD::SEXTLOAD && NumLoads > 1)
16742 return SDValue();
16743
16744 unsigned loadRegZize = RegSz;
16745 if (Ext == ISD::SEXTLOAD && RegSz == 256)
16746 loadRegZize /= 2;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016747
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016748 // Represent our vector as a sequence of elements which are the
16749 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016750 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016751 loadRegZize/SclrLoadTy.getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016752
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016753 // Represent the data using the same element type that is stored in
16754 // memory. In practice, we ''widen'' MemVT.
Eric Christophere187e252013-01-31 00:50:48 +000016755 EVT WideVecVT =
16756 EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016757 loadRegZize/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016758
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016759 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
16760 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016761
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016762 // We can't shuffle using an illegal type.
16763 if (!TLI.isTypeLegal(WideVecVT))
16764 return SDValue();
16765
16766 SmallVector<SDValue, 8> Chains;
16767 SDValue Ptr = Ld->getBasePtr();
16768 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
16769 TLI.getPointerTy());
16770 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
16771
16772 for (unsigned i = 0; i < NumLoads; ++i) {
16773 // Perform a single load.
16774 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
16775 Ptr, Ld->getPointerInfo(),
16776 Ld->isVolatile(), Ld->isNonTemporal(),
16777 Ld->isInvariant(), Ld->getAlignment());
16778 Chains.push_back(ScalarLoad.getValue(1));
16779 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
16780 // another round of DAGCombining.
16781 if (i == 0)
16782 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
16783 else
16784 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
16785 ScalarLoad, DAG.getIntPtrConstant(i));
16786
16787 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16788 }
16789
16790 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16791 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016792
16793 // Bitcast the loaded value to a vector of the original element type, in
16794 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016795 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016796 unsigned SizeRatio = RegSz/MemSz;
16797
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016798 if (Ext == ISD::SEXTLOAD) {
Benjamin Kramer17347912012-12-22 11:34:28 +000016799 // If we have SSE4.1 we can directly emit a VSEXT node.
16800 if (Subtarget->hasSSE41()) {
16801 SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
16802 return DCI.CombineTo(N, Sext, TF, true);
16803 }
16804
16805 // Otherwise we'll shuffle the small elements in the high bits of the
16806 // larger type and perform an arithmetic shift. If the shift is not legal
16807 // it's better to scalarize.
16808 if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT))
16809 return SDValue();
16810
16811 // Redistribute the loaded elements into the different locations.
16812 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
16813 for (unsigned i = 0; i != NumElems; ++i)
16814 ShuffleVec[i*SizeRatio + SizeRatio-1] = i;
16815
16816 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
16817 DAG.getUNDEF(WideVecVT),
16818 &ShuffleVec[0]);
16819
16820 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16821
16822 // Build the arithmetic shift.
16823 unsigned Amt = RegVT.getVectorElementType().getSizeInBits() -
16824 MemVT.getVectorElementType().getSizeInBits();
Benjamin Kramer9fa92512013-02-04 15:19:25 +000016825 Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff,
16826 DAG.getConstant(Amt, RegVT));
Benjamin Kramer17347912012-12-22 11:34:28 +000016827
16828 return DCI.CombineTo(N, Shuff, TF, true);
Elena Demikhovsky4b977312012-12-19 07:50:20 +000016829 }
Benjamin Kramer17347912012-12-22 11:34:28 +000016830
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016831 // Redistribute the loaded elements into the different locations.
16832 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016833 for (unsigned i = 0; i != NumElems; ++i)
16834 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016835
16836 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016837 DAG.getUNDEF(WideVecVT),
16838 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016839
16840 // Bitcast to the requested type.
16841 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
16842 // Replace the original load with the new sequence
16843 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016844 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016845 }
16846
16847 return SDValue();
16848}
16849
Chris Lattner149a4e52008-02-22 02:09:43 +000016850/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000016851static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000016852 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016853 StoreSDNode *St = cast<StoreSDNode>(N);
16854 EVT VT = St->getValue().getValueType();
16855 EVT StVT = St->getMemoryVT();
16856 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000016857 SDValue StoredVal = St->getOperand(1);
16858 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16859
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016860 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000016861 // On Sandy Bridge, 256-bit memory operations are executed by two
16862 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
16863 // memory operation.
Michael Liaod4584c92013-03-25 23:50:10 +000016864 unsigned Alignment = St->getAlignment();
16865 bool IsAligned = Alignment == 0 || Alignment >= VT.getSizeInBits()/8;
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000016866 if (VT.is256BitVector() && !Subtarget->hasInt256() &&
Nadav Rotemba958652013-01-19 08:38:41 +000016867 StVT == VT && !IsAligned) {
16868 unsigned NumElems = VT.getVectorNumElements();
16869 if (NumElems < 2)
16870 return SDValue();
16871
16872 SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
16873 SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016874
16875 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
16876 SDValue Ptr0 = St->getBasePtr();
16877 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
16878
16879 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
16880 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016881 St->isNonTemporal(), Alignment);
Nadav Rotem5e742a32011-08-11 16:41:21 +000016882 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
16883 St->getPointerInfo(), St->isVolatile(),
Nadav Rotemba958652013-01-19 08:38:41 +000016884 St->isNonTemporal(),
Michael Liaod4584c92013-03-25 23:50:10 +000016885 std::min(16U, Alignment));
Nadav Rotem5e742a32011-08-11 16:41:21 +000016886 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
16887 }
Nadav Rotem614061b2011-08-10 19:30:14 +000016888
16889 // Optimize trunc store (of multiple scalars) to shuffle and store.
16890 // First, pack all of the elements in one place. Next, store to memory
16891 // in fewer chunks.
16892 if (St->isTruncatingStore() && VT.isVector()) {
16893 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16894 unsigned NumElems = VT.getVectorNumElements();
16895 assert(StVT != VT && "Cannot truncate to the same type");
16896 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
16897 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
16898
16899 // From, To sizes and ElemCount must be pow of two
16900 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016901 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000016902 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000016903 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000016904
Nadav Rotem614061b2011-08-10 19:30:14 +000016905 unsigned SizeRatio = FromSz / ToSz;
16906
16907 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
16908
16909 // Create a type on which we perform the shuffle
16910 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
16911 StVT.getScalarType(), NumElems*SizeRatio);
16912
16913 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
16914
16915 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
16916 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000016917 for (unsigned i = 0; i != NumElems; ++i)
16918 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000016919
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016920 // Can't shuffle using an illegal type.
16921 if (!TLI.isTypeLegal(WideVecVT))
16922 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000016923
16924 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000016925 DAG.getUNDEF(WideVecVT),
16926 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000016927 // At this point all of the data is stored at the bottom of the
16928 // register. We now need to save it to mem.
16929
16930 // Find the largest store unit
16931 MVT StoreType = MVT::i8;
16932 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
16933 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
16934 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016935 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000016936 StoreType = Tp;
16937 }
16938
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016939 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
16940 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
16941 (64 <= NumElems * ToSz))
16942 StoreType = MVT::f64;
16943
Nadav Rotem614061b2011-08-10 19:30:14 +000016944 // Bitcast the original vector into a vector of store-size units
16945 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000016946 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000016947 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
16948 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
16949 SmallVector<SDValue, 8> Chains;
16950 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
16951 TLI.getPointerTy());
16952 SDValue Ptr = St->getBasePtr();
16953
16954 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000016955 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000016956 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
16957 StoreType, ShuffWide,
16958 DAG.getIntPtrConstant(i));
16959 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
16960 St->getPointerInfo(), St->isVolatile(),
16961 St->isNonTemporal(), St->getAlignment());
16962 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
16963 Chains.push_back(Ch);
16964 }
16965
16966 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
16967 Chains.size());
16968 }
16969
Chris Lattner149a4e52008-02-22 02:09:43 +000016970 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
16971 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000016972 // A preferable solution to the general problem is to figure out the right
16973 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000016974
16975 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000016976 if (VT.getSizeInBits() != 64)
16977 return SDValue();
16978
Devang Patel578efa92009-06-05 21:57:13 +000016979 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +000016980 bool NoImplicitFloatOps = F->getAttributes().
16981 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000016982 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000016983 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000016984 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000016985 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000016986 isa<LoadSDNode>(St->getValue()) &&
16987 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
16988 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000016989 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016990 LoadSDNode *Ld = 0;
16991 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000016992 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000016993 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000016994 // Must be a store of a load. We currently handle two cases: the load
16995 // is a direct child, and it's under an intervening TokenFactor. It is
16996 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000016997 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000016998 Ld = cast<LoadSDNode>(St->getChain());
16999 else if (St->getValue().hasOneUse() &&
17000 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000017001 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000017002 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000017003 TokenFactorIndex = i;
17004 Ld = cast<LoadSDNode>(St->getValue());
17005 } else
17006 Ops.push_back(ChainVal->getOperand(i));
17007 }
17008 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000017009
Evan Cheng536e6672009-03-12 05:59:15 +000017010 if (!Ld || !ISD::isNormalLoad(Ld))
17011 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017012
Evan Cheng536e6672009-03-12 05:59:15 +000017013 // If this is not the MMX case, i.e. we are just turning i64 load/store
17014 // into f64 load/store, avoid the transformation if there are multiple
17015 // uses of the loaded value.
17016 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
17017 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000017018
Evan Cheng536e6672009-03-12 05:59:15 +000017019 DebugLoc LdDL = Ld->getDebugLoc();
17020 DebugLoc StDL = N->getDebugLoc();
17021 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
17022 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
17023 // pair instead.
17024 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000017025 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000017026 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
17027 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017028 Ld->isNonTemporal(), Ld->isInvariant(),
17029 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017030 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000017031 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000017032 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000017033 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000017034 Ops.size());
17035 }
Evan Cheng536e6672009-03-12 05:59:15 +000017036 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000017037 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017038 St->isVolatile(), St->isNonTemporal(),
17039 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000017040 }
Evan Cheng536e6672009-03-12 05:59:15 +000017041
17042 // Otherwise, lower to two pairs of 32-bit loads / stores.
17043 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017044 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
17045 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017046
Owen Anderson825b72b2009-08-11 20:47:22 +000017047 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017048 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017049 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017050 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000017051 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000017052 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000017053 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000017054 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000017055 MinAlign(Ld->getAlignment(), 4));
17056
17057 SDValue NewChain = LoLd.getValue(1);
17058 if (TokenFactorIndex != -1) {
17059 Ops.push_back(LoLd);
17060 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000017061 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000017062 Ops.size());
17063 }
17064
17065 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000017066 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
17067 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000017068
17069 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017070 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000017071 St->isVolatile(), St->isNonTemporal(),
17072 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000017073 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000017074 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000017075 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000017076 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000017077 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000017078 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000017079 }
Dan Gohman475871a2008-07-27 21:46:04 +000017080 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000017081}
17082
Duncan Sands17470be2011-09-22 20:15:48 +000017083/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
17084/// and return the operands for the horizontal operation in LHS and RHS. A
17085/// horizontal operation performs the binary operation on successive elements
17086/// of its first operand, then on successive elements of its second operand,
17087/// returning the resulting values in a vector. For example, if
17088/// A = < float a0, float a1, float a2, float a3 >
17089/// and
17090/// B = < float b0, float b1, float b2, float b3 >
17091/// then the result of doing a horizontal operation on A and B is
17092/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
17093/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
17094/// A horizontal-op B, for some already available A and B, and if so then LHS is
17095/// set to A, RHS to B, and the routine returns 'true'.
17096/// Note that the binary operation should have the property that if one of the
17097/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017098static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000017099 // Look for the following pattern: if
17100 // A = < float a0, float a1, float a2, float a3 >
17101 // B = < float b0, float b1, float b2, float b3 >
17102 // and
17103 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
17104 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
17105 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
17106 // which is A horizontal-op B.
17107
17108 // At least one of the operands should be a vector shuffle.
17109 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
17110 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
17111 return false;
17112
17113 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000017114
17115 assert((VT.is128BitVector() || VT.is256BitVector()) &&
17116 "Unsupported vector type for horizontal add/sub");
17117
17118 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
17119 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000017120 unsigned NumElts = VT.getVectorNumElements();
17121 unsigned NumLanes = VT.getSizeInBits()/128;
17122 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000017123 assert((NumLaneElts % 2 == 0) &&
17124 "Vector type should have an even number of elements in each lane");
17125 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000017126
17127 // View LHS in the form
17128 // LHS = VECTOR_SHUFFLE A, B, LMask
17129 // If LHS is not a shuffle then pretend it is the shuffle
17130 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
17131 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
17132 // type VT.
17133 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000017134 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017135 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17136 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
17137 A = LHS.getOperand(0);
17138 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
17139 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017140 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
17141 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017142 } else {
17143 if (LHS.getOpcode() != ISD::UNDEF)
17144 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017145 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017146 LMask[i] = i;
17147 }
17148
17149 // Likewise, view RHS in the form
17150 // RHS = VECTOR_SHUFFLE C, D, RMask
17151 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000017152 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017153 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
17154 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
17155 C = RHS.getOperand(0);
17156 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
17157 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000017158 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
17159 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000017160 } else {
17161 if (RHS.getOpcode() != ISD::UNDEF)
17162 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000017163 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000017164 RMask[i] = i;
17165 }
17166
17167 // Check that the shuffles are both shuffling the same vectors.
17168 if (!(A == C && B == D) && !(A == D && B == C))
17169 return false;
17170
17171 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
17172 if (!A.getNode() && !B.getNode())
17173 return false;
17174
17175 // If A and B occur in reverse order in RHS, then "swap" them (which means
17176 // rewriting the mask).
17177 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000017178 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000017179
17180 // At this point LHS and RHS are equivalent to
17181 // LHS = VECTOR_SHUFFLE A, B, LMask
17182 // RHS = VECTOR_SHUFFLE A, B, RMask
17183 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000017184 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000017185 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000017186
Craig Topperf8363302011-12-02 08:18:41 +000017187 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000017188 if (LIdx < 0 || RIdx < 0 ||
17189 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
17190 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000017191 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000017192
Craig Topperf8363302011-12-02 08:18:41 +000017193 // Check that successive elements are being operated on. If not, this is
17194 // not a horizontal operation.
17195 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
17196 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000017197 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000017198 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000017199 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000017200 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000017201 }
17202
17203 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
17204 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
17205 return true;
17206}
17207
17208/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
17209static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
17210 const X86Subtarget *Subtarget) {
17211 EVT VT = N->getValueType(0);
17212 SDValue LHS = N->getOperand(0);
17213 SDValue RHS = N->getOperand(1);
17214
17215 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017216 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017217 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017218 isHorizontalBinOp(LHS, RHS, true))
17219 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
17220 return SDValue();
17221}
17222
17223/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
17224static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
17225 const X86Subtarget *Subtarget) {
17226 EVT VT = N->getValueType(0);
17227 SDValue LHS = N->getOperand(0);
17228 SDValue RHS = N->getOperand(1);
17229
17230 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017231 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017232 (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000017233 isHorizontalBinOp(LHS, RHS, false))
17234 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
17235 return SDValue();
17236}
17237
Chris Lattner6cf73262008-01-25 06:14:17 +000017238/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
17239/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017240static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000017241 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
17242 // F[X]OR(0.0, x) -> x
17243 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000017244 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17245 if (C->getValueAPF().isPosZero())
17246 return N->getOperand(1);
17247 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17248 if (C->getValueAPF().isPosZero())
17249 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000017250 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017251}
17252
Nadav Rotemd60cb112012-08-19 13:06:16 +000017253/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
17254/// X86ISD::FMAX nodes.
17255static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
17256 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
17257
17258 // Only perform optimizations if UnsafeMath is used.
17259 if (!DAG.getTarget().Options.UnsafeFPMath)
17260 return SDValue();
17261
17262 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000017263 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000017264 unsigned NewOp = 0;
17265 switch (N->getOpcode()) {
17266 default: llvm_unreachable("unknown opcode");
17267 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
17268 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
17269 }
17270
17271 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
17272 N->getOperand(0), N->getOperand(1));
17273}
17274
Chris Lattneraf723b92008-01-25 05:46:26 +000017275/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000017276static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000017277 // FAND(0.0, x) -> 0.0
17278 // FAND(x, 0.0) -> 0.0
17279 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
17280 if (C->getValueAPF().isPosZero())
17281 return N->getOperand(0);
17282 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
17283 if (C->getValueAPF().isPosZero())
17284 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000017285 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000017286}
17287
Dan Gohmane5af2d32009-01-29 01:59:02 +000017288static SDValue PerformBTCombine(SDNode *N,
17289 SelectionDAG &DAG,
17290 TargetLowering::DAGCombinerInfo &DCI) {
17291 // BT ignores high bits in the bit index operand.
17292 SDValue Op1 = N->getOperand(1);
17293 if (Op1.hasOneUse()) {
17294 unsigned BitWidth = Op1.getValueSizeInBits();
17295 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
17296 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017297 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
17298 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000017299 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000017300 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
17301 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
17302 DCI.CommitTargetLoweringOpt(TLO);
17303 }
17304 return SDValue();
17305}
Chris Lattner83e6c992006-10-04 06:57:07 +000017306
Eli Friedman7a5e5552009-06-07 06:52:44 +000017307static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
17308 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017309 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000017310 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000017311 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000017312 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000017313 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000017314 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000017315 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017316 }
17317 return SDValue();
17318}
17319
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017320static SDValue PerformSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG,
17321 const X86Subtarget *Subtarget) {
17322 EVT VT = N->getValueType(0);
17323 if (!VT.isVector())
17324 return SDValue();
17325
17326 SDValue N0 = N->getOperand(0);
17327 SDValue N1 = N->getOperand(1);
17328 EVT ExtraVT = cast<VTSDNode>(N1)->getVT();
17329 DebugLoc dl = N->getDebugLoc();
17330
17331 // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the
17332 // both SSE and AVX2 since there is no sign-extended shift right
17333 // operation on a vector with 64-bit elements.
17334 //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) ->
17335 // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT)))
17336 if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND ||
17337 N0.getOpcode() == ISD::SIGN_EXTEND)) {
17338 SDValue N00 = N0.getOperand(0);
17339
17340 // EXTLOAD has a better solution on AVX2,
17341 // it may be replaced with X86ISD::VSEXT node.
17342 if (N00.getOpcode() == ISD::LOAD && Subtarget->hasInt256())
17343 if (!ISD::isNormalLoad(N00.getNode()))
17344 return SDValue();
17345
17346 if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) {
17347 SDValue Tmp = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32,
17348 N00, N1);
17349 return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp);
17350 }
17351 }
17352 return SDValue();
17353}
17354
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017355static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
17356 TargetLowering::DAGCombinerInfo &DCI,
17357 const X86Subtarget *Subtarget) {
17358 if (!DCI.isBeforeLegalizeOps())
17359 return SDValue();
17360
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017361 if (!Subtarget->hasFp256())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000017362 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017363
Nadav Rotem0c8607b2013-01-20 08:35:56 +000017364 EVT VT = N->getValueType(0);
17365 if (VT.isVector() && VT.getSizeInBits() == 256) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017366 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17367 if (R.getNode())
17368 return R;
17369 }
17370
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017371 return SDValue();
17372}
17373
Michael Liaof6c24ee2012-08-10 14:39:24 +000017374static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017375 const X86Subtarget* Subtarget) {
17376 DebugLoc dl = N->getDebugLoc();
17377 EVT VT = N->getValueType(0);
17378
Craig Topperb1bdd7d2012-08-30 06:56:15 +000017379 // Let legalize expand this if it isn't a legal type yet.
17380 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
17381 return SDValue();
17382
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017383 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000017384 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
17385 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017386 return SDValue();
17387
17388 SDValue A = N->getOperand(0);
17389 SDValue B = N->getOperand(1);
17390 SDValue C = N->getOperand(2);
17391
17392 bool NegA = (A.getOpcode() == ISD::FNEG);
17393 bool NegB = (B.getOpcode() == ISD::FNEG);
17394 bool NegC = (C.getOpcode() == ISD::FNEG);
17395
Michael Liaof6c24ee2012-08-10 14:39:24 +000017396 // Negative multiplication when NegA xor NegB
17397 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017398 if (NegA)
17399 A = A.getOperand(0);
17400 if (NegB)
17401 B = B.getOperand(0);
17402 if (NegC)
17403 C = C.getOperand(0);
17404
17405 unsigned Opcode;
17406 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000017407 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017408 else
Craig Topperbf404372012-08-31 15:40:30 +000017409 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
17410
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017411 return DAG.getNode(Opcode, dl, VT, A, B, C);
17412}
17413
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017414static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000017415 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017416 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000017417 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
17418 // (and (i32 x86isd::setcc_carry), 1)
17419 // This eliminates the zext. This transformation is necessary because
17420 // ISD::SETCC is always legalized to i8.
17421 DebugLoc dl = N->getDebugLoc();
17422 SDValue N0 = N->getOperand(0);
17423 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000017424
Evan Cheng2e489c42009-12-16 00:53:11 +000017425 if (N0.getOpcode() == ISD::AND &&
17426 N0.hasOneUse() &&
17427 N0.getOperand(0).hasOneUse()) {
17428 SDValue N00 = N0.getOperand(0);
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017429 if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
17430 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
17431 if (!C || C->getZExtValue() != 1)
17432 return SDValue();
17433 return DAG.getNode(ISD::AND, dl, VT,
17434 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
17435 N00.getOperand(0), N00.getOperand(1)),
17436 DAG.getConstant(1, VT));
17437 }
17438 }
17439
Craig Topper5a529e42013-01-18 06:44:29 +000017440 if (VT.is256BitVector()) {
Nadav Rotemd6fb53a2012-12-27 08:15:45 +000017441 SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget);
17442 if (R.getNode())
17443 return R;
Evan Cheng2e489c42009-12-16 00:53:11 +000017444 }
Craig Topperd0cf5652012-04-21 18:13:35 +000017445
Evan Cheng2e489c42009-12-16 00:53:11 +000017446 return SDValue();
17447}
17448
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017449// Optimize x == -y --> x+y == 0
17450// x != -y --> x+y != 0
17451static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
17452 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
17453 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000017454 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017455
17456 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
17457 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
17458 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
17459 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17460 LHS.getValueType(), RHS, LHS.getOperand(1));
17461 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17462 addV, DAG.getConstant(0, addV.getValueType()), CC);
17463 }
17464 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
17465 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
17466 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
17467 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
17468 RHS.getValueType(), LHS, RHS.getOperand(1));
17469 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
17470 addV, DAG.getConstant(0, addV.getValueType()), CC);
17471 }
17472 return SDValue();
17473}
17474
Eric Christophere187e252013-01-31 00:50:48 +000017475// Helper function of PerformSETCCCombine. It is to materialize "setb reg"
17476// as "sbb reg,reg", since it can be extended without zext and produces
Shuxin Yanga5526a92012-10-31 23:11:48 +000017477// an all-ones bit which is more useful than 0/1 in some cases.
17478static SDValue MaterializeSETB(DebugLoc DL, SDValue EFLAGS, SelectionDAG &DAG) {
17479 return DAG.getNode(ISD::AND, DL, MVT::i8,
17480 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
17481 DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS),
17482 DAG.getConstant(1, MVT::i8));
17483}
17484
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017485// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017486static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
17487 TargetLowering::DAGCombinerInfo &DCI,
17488 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017489 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000017490 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
17491 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017492
Shuxin Yanga5526a92012-10-31 23:11:48 +000017493 if (CC == X86::COND_A) {
Eric Christophere187e252013-01-31 00:50:48 +000017494 // Try to convert COND_A into COND_B in an attempt to facilitate
Shuxin Yanga5526a92012-10-31 23:11:48 +000017495 // materializing "setb reg".
17496 //
17497 // Do not flip "e > c", where "c" is a constant, because Cmp instruction
17498 // cannot take an immediate as its first operand.
17499 //
Eric Christophere187e252013-01-31 00:50:48 +000017500 if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
Shuxin Yanga5526a92012-10-31 23:11:48 +000017501 EFLAGS.getValueType().isInteger() &&
17502 !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
17503 SDValue NewSub = DAG.getNode(X86ISD::SUB, EFLAGS.getDebugLoc(),
17504 EFLAGS.getNode()->getVTList(),
17505 EFLAGS.getOperand(1), EFLAGS.getOperand(0));
17506 SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
17507 return MaterializeSETB(DL, NewEFLAGS, DAG);
17508 }
17509 }
17510
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017511 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
17512 // a zext and produces an all-ones bit which is more useful than 0/1 in some
17513 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000017514 if (CC == X86::COND_B)
Shuxin Yanga5526a92012-10-31 23:11:48 +000017515 return MaterializeSETB(DL, EFLAGS, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017516
Michael Liao2a33cec2012-08-10 19:58:13 +000017517 SDValue Flags;
17518
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017519 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17520 if (Flags.getNode()) {
17521 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17522 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
17523 }
17524
Michael Liao2a33cec2012-08-10 19:58:13 +000017525 return SDValue();
17526}
17527
17528// Optimize branch condition evaluation.
17529//
17530static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
17531 TargetLowering::DAGCombinerInfo &DCI,
17532 const X86Subtarget *Subtarget) {
17533 DebugLoc DL = N->getDebugLoc();
17534 SDValue Chain = N->getOperand(0);
17535 SDValue Dest = N->getOperand(1);
17536 SDValue EFLAGS = N->getOperand(3);
17537 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
17538
17539 SDValue Flags;
17540
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017541 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
17542 if (Flags.getNode()) {
17543 SDValue Cond = DAG.getConstant(CC, MVT::i8);
17544 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
17545 Flags);
17546 }
17547
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017548 return SDValue();
17549}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017550
Benjamin Kramer1396c402011-06-18 11:09:41 +000017551static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
17552 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017553 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017554 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000017555
17556 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000017557 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000017558 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000017559 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000017560 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
17561 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
17562 }
17563
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017564 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
17565 // a 32-bit target where SSE doesn't support i64->FP operations.
17566 if (Op0.getOpcode() == ISD::LOAD) {
17567 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
17568 EVT VT = Ld->getValueType(0);
17569 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
17570 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
17571 !XTLI->getSubtarget()->is64Bit() &&
17572 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000017573 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
17574 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017575 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
17576 return FILDChain;
17577 }
17578 }
17579 return SDValue();
17580}
17581
Chris Lattner23a01992010-12-20 01:37:09 +000017582// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
17583static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
17584 X86TargetLowering::DAGCombinerInfo &DCI) {
17585 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
17586 // the result is either zero or one (depending on the input carry bit).
17587 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
17588 if (X86::isZeroNode(N->getOperand(0)) &&
17589 X86::isZeroNode(N->getOperand(1)) &&
17590 // We don't have a good way to replace an EFLAGS use, so only do this when
17591 // dead right now.
17592 SDValue(N, 1).use_empty()) {
17593 DebugLoc DL = N->getDebugLoc();
17594 EVT VT = N->getValueType(0);
17595 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
17596 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
17597 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
17598 DAG.getConstant(X86::COND_B,MVT::i8),
17599 N->getOperand(2)),
17600 DAG.getConstant(1, VT));
17601 return DCI.CombineTo(N, Res1, CarryOut);
17602 }
17603
17604 return SDValue();
17605}
17606
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017607// fold (add Y, (sete X, 0)) -> adc 0, Y
17608// (add Y, (setne X, 0)) -> sbb -1, Y
17609// (sub (sete X, 0), Y) -> sbb 0, Y
17610// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017611static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017612 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000017613
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017614 // Look through ZExts.
17615 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
17616 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
17617 return SDValue();
17618
17619 SDValue SetCC = Ext.getOperand(0);
17620 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
17621 return SDValue();
17622
17623 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
17624 if (CC != X86::COND_E && CC != X86::COND_NE)
17625 return SDValue();
17626
17627 SDValue Cmp = SetCC.getOperand(1);
17628 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000017629 !X86::isZeroNode(Cmp.getOperand(1)) ||
17630 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000017631 return SDValue();
17632
17633 SDValue CmpOp0 = Cmp.getOperand(0);
17634 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
17635 DAG.getConstant(1, CmpOp0.getValueType()));
17636
17637 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
17638 if (CC == X86::COND_NE)
17639 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
17640 DL, OtherVal.getValueType(), OtherVal,
17641 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
17642 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
17643 DL, OtherVal.getValueType(), OtherVal,
17644 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
17645}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000017646
Craig Topper54f952a2011-11-19 09:02:40 +000017647/// PerformADDCombine - Do target-specific dag combines on integer adds.
17648static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
17649 const X86Subtarget *Subtarget) {
17650 EVT VT = N->getValueType(0);
17651 SDValue Op0 = N->getOperand(0);
17652 SDValue Op1 = N->getOperand(1);
17653
17654 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000017655 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017656 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000017657 isHorizontalBinOp(Op0, Op1, true))
17658 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
17659
17660 return OptimizeConditionalInDecrement(N, DAG);
17661}
17662
17663static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
17664 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017665 SDValue Op0 = N->getOperand(0);
17666 SDValue Op1 = N->getOperand(1);
17667
17668 // X86 can't encode an immediate LHS of a sub. See if we can push the
17669 // negation into a preceding instruction.
17670 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017671 // If the RHS of the sub is a XOR with one use and a constant, invert the
17672 // immediate. Then add one to the LHS of the sub so we can turn
17673 // X-Y -> X+~Y+1, saving one register.
17674 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
17675 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000017676 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017677 EVT VT = Op0.getValueType();
17678 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
17679 Op1.getOperand(0),
17680 DAG.getConstant(~XorC, VT));
17681 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000017682 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017683 }
17684 }
17685
Craig Topper54f952a2011-11-19 09:02:40 +000017686 // Try to synthesize horizontal adds from adds of shuffles.
17687 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000017688 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000017689 (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topperb72039c2011-11-30 09:10:50 +000017690 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000017691 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
17692
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000017693 return OptimizeConditionalInDecrement(N, DAG);
17694}
17695
Michael Liaod9d09602012-10-23 17:34:00 +000017696/// performVZEXTCombine - Performs build vector combines
17697static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
17698 TargetLowering::DAGCombinerInfo &DCI,
17699 const X86Subtarget *Subtarget) {
17700 // (vzext (bitcast (vzext (x)) -> (vzext x)
17701 SDValue In = N->getOperand(0);
17702 while (In.getOpcode() == ISD::BITCAST)
17703 In = In.getOperand(0);
17704
17705 if (In.getOpcode() != X86ISD::VZEXT)
17706 return SDValue();
17707
Nadav Rotemb39a5522013-02-14 18:20:48 +000017708 return DAG.getNode(X86ISD::VZEXT, N->getDebugLoc(), N->getValueType(0),
17709 In.getOperand(0));
Michael Liaod9d09602012-10-23 17:34:00 +000017710}
17711
Dan Gohman475871a2008-07-27 21:46:04 +000017712SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000017713 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000017714 SelectionDAG &DAG = DCI.DAG;
17715 switch (N->getOpcode()) {
17716 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000017717 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000017718 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000017719 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000017720 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017721 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000017722 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
17723 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000017724 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000017725 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000017726 case ISD::SHL:
17727 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000017728 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000017729 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000017730 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000017731 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000017732 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000017733 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000017734 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Duncan Sands17470be2011-09-22 20:15:48 +000017735 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
17736 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000017737 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000017738 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000017739 case X86ISD::FMIN:
17740 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000017741 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000017742 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000017743 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000017744 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000017745 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000017746 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky52981c42013-02-20 12:42:54 +000017747 case ISD::SIGN_EXTEND_INREG: return PerformSIGN_EXTEND_INREGCombine(N, DAG, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000017748 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000017749 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000017750 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000017751 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Michael Liaod9d09602012-10-23 17:34:00 +000017752 case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000017753 case X86ISD::SHUFP: // Handle all target specific shuffles
Craig Topper4aee1bb2013-01-28 06:48:25 +000017754 case X86ISD::PALIGNR:
Craig Topper34671b82011-12-06 08:21:25 +000017755 case X86ISD::UNPCKH:
17756 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000017757 case X86ISD::MOVHLPS:
17758 case X86ISD::MOVLHPS:
17759 case X86ISD::PSHUFD:
17760 case X86ISD::PSHUFHW:
17761 case X86ISD::PSHUFLW:
17762 case X86ISD::MOVSS:
17763 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000017764 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000017765 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000017766 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000017767 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000017768 }
17769
Dan Gohman475871a2008-07-27 21:46:04 +000017770 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000017771}
17772
Evan Chenge5b51ac2010-04-17 06:13:15 +000017773/// isTypeDesirableForOp - Return true if the target has native support for
17774/// the specified value type and it is 'desirable' to use the type for the
17775/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
17776/// instruction encodings are longer and some i16 instructions are slow.
17777bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
17778 if (!isTypeLegal(VT))
17779 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017780 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000017781 return true;
17782
17783 switch (Opc) {
17784 default:
17785 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000017786 case ISD::LOAD:
17787 case ISD::SIGN_EXTEND:
17788 case ISD::ZERO_EXTEND:
17789 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017790 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000017791 case ISD::SRL:
17792 case ISD::SUB:
17793 case ISD::ADD:
17794 case ISD::MUL:
17795 case ISD::AND:
17796 case ISD::OR:
17797 case ISD::XOR:
17798 return false;
17799 }
17800}
17801
17802/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000017803/// beneficial for dag combiner to promote the specified node. If true, it
17804/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000017805bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017806 EVT VT = Op.getValueType();
17807 if (VT != MVT::i16)
17808 return false;
17809
Evan Cheng4c26e932010-04-19 19:29:22 +000017810 bool Promote = false;
17811 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017812 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000017813 default: break;
17814 case ISD::LOAD: {
17815 LoadSDNode *LD = cast<LoadSDNode>(Op);
17816 // If the non-extending load has a single use and it's not live out, then it
17817 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017818 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
17819 Op.hasOneUse()*/) {
17820 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
17821 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
17822 // The only case where we'd want to promote LOAD (rather then it being
17823 // promoted as an operand is when it's only use is liveout.
17824 if (UI->getOpcode() != ISD::CopyToReg)
17825 return false;
17826 }
17827 }
Evan Cheng4c26e932010-04-19 19:29:22 +000017828 Promote = true;
17829 break;
17830 }
17831 case ISD::SIGN_EXTEND:
17832 case ISD::ZERO_EXTEND:
17833 case ISD::ANY_EXTEND:
17834 Promote = true;
17835 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017836 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000017837 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000017838 SDValue N0 = Op.getOperand(0);
17839 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000017840 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000017841 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017842 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000017843 break;
17844 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000017845 case ISD::ADD:
17846 case ISD::MUL:
17847 case ISD::AND:
17848 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000017849 case ISD::XOR:
17850 Commute = true;
17851 // fallthrough
17852 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000017853 SDValue N0 = Op.getOperand(0);
17854 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000017855 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017856 return false;
17857 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000017858 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017859 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000017860 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000017861 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000017862 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017863 }
17864 }
17865
17866 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000017867 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000017868}
17869
Evan Cheng60c07e12006-07-05 22:17:51 +000017870//===----------------------------------------------------------------------===//
17871// X86 Inline Assembly Support
17872//===----------------------------------------------------------------------===//
17873
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017874namespace {
17875 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017876 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017877 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017878
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017879 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017880 StringRef piece(*args[i]);
17881 if (!s.startswith(piece)) // Check if the piece matches.
17882 return false;
17883
17884 s = s.substr(piece.size());
17885 StringRef::size_type pos = s.find_first_not_of(" \t");
17886 if (pos == 0) // We matched a prefix.
17887 return false;
17888
17889 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017890 }
17891
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017892 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017893 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000017894 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017895}
17896
Chris Lattnerb8105652009-07-20 17:51:36 +000017897bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
17898 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000017899
17900 std::string AsmStr = IA->getAsmString();
17901
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017902 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
17903 if (!Ty || Ty->getBitWidth() % 16 != 0)
17904 return false;
17905
Chris Lattnerb8105652009-07-20 17:51:36 +000017906 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017907 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000017908 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000017909
17910 switch (AsmPieces.size()) {
17911 default: return false;
17912 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000017913 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017914 // we will turn this bswap into something that will be lowered to logical
17915 // ops instead of emitting the bswap asm. For now, we don't support 486 or
17916 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000017917 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017918 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
17919 matchAsm(AsmPieces[0], "bswapl", "$0") ||
17920 matchAsm(AsmPieces[0], "bswapq", "$0") ||
17921 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
17922 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
17923 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000017924 // No need to check constraints, nothing other than the equivalent of
17925 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000017926 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017927 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017928
Chris Lattnerb8105652009-07-20 17:51:36 +000017929 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000017930 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017931 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017932 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
17933 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000017934 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000017935 const std::string &ConstraintsStr = IA->getConstraintString();
17936 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000017937 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Dan Gohman0ef701e2010-03-04 19:58:08 +000017938 if (AsmPieces.size() == 4 &&
17939 AsmPieces[0] == "~{cc}" &&
17940 AsmPieces[1] == "~{dirflag}" &&
17941 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017942 AsmPieces[3] == "~{fpsr}")
17943 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017944 }
17945 break;
17946 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000017947 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017948 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017949 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
17950 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
17951 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017952 AsmPieces.clear();
17953 const std::string &ConstraintsStr = IA->getConstraintString();
17954 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Jakub Staszak56f58ad2013-02-18 23:18:22 +000017955 array_pod_sort(AsmPieces.begin(), AsmPieces.end());
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017956 if (AsmPieces.size() == 4 &&
17957 AsmPieces[0] == "~{cc}" &&
17958 AsmPieces[1] == "~{dirflag}" &&
17959 AsmPieces[2] == "~{flags}" &&
17960 AsmPieces[3] == "~{fpsr}")
17961 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000017962 }
Evan Cheng55d42002011-01-08 01:24:27 +000017963
17964 if (CI->getType()->isIntegerTy(64)) {
17965 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
17966 if (Constraints.size() >= 2 &&
17967 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
17968 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
17969 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000017970 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
17971 matchAsm(AsmPieces[1], "bswap", "%edx") &&
17972 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000017973 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000017974 }
17975 }
17976 break;
17977 }
17978 return false;
17979}
17980
Chris Lattnerf4dff842006-07-11 02:54:03 +000017981/// getConstraintType - Given a constraint letter, return the type of
17982/// constraint it is for this target.
17983X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000017984X86TargetLowering::getConstraintType(const std::string &Constraint) const {
17985 if (Constraint.size() == 1) {
17986 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000017987 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000017988 case 'q':
17989 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000017990 case 'f':
17991 case 't':
17992 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000017993 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000017994 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000017995 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000017996 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000017997 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000017998 case 'a':
17999 case 'b':
18000 case 'c':
18001 case 'd':
18002 case 'S':
18003 case 'D':
18004 case 'A':
18005 return C_Register;
18006 case 'I':
18007 case 'J':
18008 case 'K':
18009 case 'L':
18010 case 'M':
18011 case 'N':
18012 case 'G':
18013 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000018014 case 'e':
18015 case 'Z':
18016 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000018017 default:
18018 break;
18019 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000018020 }
Chris Lattner4234f572007-03-25 02:14:49 +000018021 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000018022}
18023
John Thompson44ab89e2010-10-29 17:29:13 +000018024/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000018025/// This object must already have been set up with the operand type
18026/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000018027TargetLowering::ConstraintWeight
18028 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000018029 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000018030 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018031 Value *CallOperandVal = info.CallOperandVal;
18032 // If we don't have a value, we can't do a match,
18033 // but allow it at the lowest weight.
18034 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000018035 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000018036 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000018037 // Look at the constraint type.
18038 switch (*constraint) {
18039 default:
John Thompson44ab89e2010-10-29 17:29:13 +000018040 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
18041 case 'R':
18042 case 'q':
18043 case 'Q':
18044 case 'a':
18045 case 'b':
18046 case 'c':
18047 case 'd':
18048 case 'S':
18049 case 'D':
18050 case 'A':
18051 if (CallOperandVal->getType()->isIntegerTy())
18052 weight = CW_SpecificReg;
18053 break;
18054 case 'f':
18055 case 't':
18056 case 'u':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018057 if (type->isFloatingPointTy())
18058 weight = CW_SpecificReg;
18059 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018060 case 'y':
Jakub Staszakc20323a2012-12-29 15:57:26 +000018061 if (type->isX86_MMXTy() && Subtarget->hasMMX())
18062 weight = CW_SpecificReg;
18063 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018064 case 'x':
18065 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000018066 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Elena Demikhovsky8564dc62012-11-29 12:44:59 +000018067 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
John Thompson44ab89e2010-10-29 17:29:13 +000018068 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018069 break;
18070 case 'I':
18071 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
18072 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000018073 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018074 }
18075 break;
John Thompson44ab89e2010-10-29 17:29:13 +000018076 case 'J':
18077 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18078 if (C->getZExtValue() <= 63)
18079 weight = CW_Constant;
18080 }
18081 break;
18082 case 'K':
18083 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18084 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
18085 weight = CW_Constant;
18086 }
18087 break;
18088 case 'L':
18089 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18090 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
18091 weight = CW_Constant;
18092 }
18093 break;
18094 case 'M':
18095 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18096 if (C->getZExtValue() <= 3)
18097 weight = CW_Constant;
18098 }
18099 break;
18100 case 'N':
18101 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18102 if (C->getZExtValue() <= 0xff)
18103 weight = CW_Constant;
18104 }
18105 break;
18106 case 'G':
18107 case 'C':
18108 if (dyn_cast<ConstantFP>(CallOperandVal)) {
18109 weight = CW_Constant;
18110 }
18111 break;
18112 case 'e':
18113 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18114 if ((C->getSExtValue() >= -0x80000000LL) &&
18115 (C->getSExtValue() <= 0x7fffffffLL))
18116 weight = CW_Constant;
18117 }
18118 break;
18119 case 'Z':
18120 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
18121 if (C->getZExtValue() <= 0xffffffff)
18122 weight = CW_Constant;
18123 }
18124 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000018125 }
18126 return weight;
18127}
18128
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018129/// LowerXConstraint - try to replace an X constraint, which matches anything,
18130/// with another that has more specific requirements based on the type of the
18131/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000018132const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000018133LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000018134 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
18135 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000018136 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000018137 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000018138 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000018139 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000018140 return "x";
18141 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018142
Chris Lattner5e764232008-04-26 23:02:14 +000018143 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000018144}
18145
Chris Lattner48884cd2007-08-25 00:47:38 +000018146/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
18147/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000018148void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000018149 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000018150 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000018151 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000018152 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000018153
Eric Christopher100c8332011-06-02 23:16:42 +000018154 // Only support length 1 constraints for now.
18155 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000018156
Eric Christopher100c8332011-06-02 23:16:42 +000018157 char ConstraintLetter = Constraint[0];
18158 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018159 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000018160 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000018161 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018162 if (C->getZExtValue() <= 31) {
18163 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018164 break;
18165 }
Devang Patel84f7fd22007-03-17 00:13:28 +000018166 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018167 return;
Evan Cheng364091e2008-09-22 23:57:37 +000018168 case 'J':
18169 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000018170 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000018171 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18172 break;
18173 }
18174 }
18175 return;
18176 case 'K':
18177 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Jakub Staszakdccd7f92012-11-06 23:52:19 +000018178 if (isInt<8>(C->getSExtValue())) {
Evan Cheng364091e2008-09-22 23:57:37 +000018179 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18180 break;
18181 }
18182 }
18183 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000018184 case 'N':
18185 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000018186 if (C->getZExtValue() <= 255) {
18187 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000018188 break;
18189 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000018190 }
Chris Lattner48884cd2007-08-25 00:47:38 +000018191 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000018192 case 'e': {
18193 // 32-bit signed value
18194 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018195 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18196 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018197 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018198 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000018199 break;
18200 }
18201 // FIXME gcc accepts some relocatable values here too, but only in certain
18202 // memory models; it's complicated.
18203 }
18204 return;
18205 }
18206 case 'Z': {
18207 // 32-bit unsigned value
18208 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000018209 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
18210 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018211 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
18212 break;
18213 }
18214 }
18215 // FIXME gcc accepts some relocatable values here too, but only in certain
18216 // memory models; it's complicated.
18217 return;
18218 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018219 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018220 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000018221 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000018222 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000018223 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000018224 break;
18225 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018226
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018227 // In any sort of PIC mode addresses need to be computed at runtime by
18228 // adding in a register or some sort of table lookup. These can't
18229 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000018230 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000018231 return;
18232
Chris Lattnerdc43a882007-05-03 16:52:29 +000018233 // If we are in non-pic codegen mode, we allow the address of a global (with
18234 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000018235 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018236 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000018237
Chris Lattner49921962009-05-08 18:23:14 +000018238 // Match either (GA), (GA+C), (GA+C1+C2), etc.
18239 while (1) {
18240 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
18241 Offset += GA->getOffset();
18242 break;
18243 } else if (Op.getOpcode() == ISD::ADD) {
18244 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18245 Offset += C->getZExtValue();
18246 Op = Op.getOperand(0);
18247 continue;
18248 }
18249 } else if (Op.getOpcode() == ISD::SUB) {
18250 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
18251 Offset += -C->getZExtValue();
18252 Op = Op.getOperand(0);
18253 continue;
18254 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018255 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018256
Chris Lattner49921962009-05-08 18:23:14 +000018257 // Otherwise, this isn't something we can handle, reject it.
18258 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000018259 }
Eric Christopherfd179292009-08-27 18:07:15 +000018260
Dan Gohman46510a72010-04-15 01:51:59 +000018261 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018262 // If we require an extra load to get this address, as in PIC mode, we
18263 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000018264 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
18265 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000018266 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000018267
Devang Patel0d881da2010-07-06 22:08:15 +000018268 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
18269 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000018270 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018271 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000018272 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018273
Gabor Greifba36cb52008-08-28 21:40:38 +000018274 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000018275 Ops.push_back(Result);
18276 return;
18277 }
Dale Johannesen1784d162010-06-25 21:55:36 +000018278 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000018279}
18280
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018281std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000018282X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000018283 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000018284 // First, see if this is a constraint that directly corresponds to an LLVM
18285 // register class.
18286 if (Constraint.size() == 1) {
18287 // GCC Constraint Letters
18288 switch (Constraint[0]) {
18289 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000018290 // TODO: Slight differences here in allocation order and leaving
18291 // RIP in the class. Do they matter any more here than they do
18292 // in the normal allocation?
18293 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
18294 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000018295 if (VT == MVT::i32 || VT == MVT::f32)
18296 return std::make_pair(0U, &X86::GR32RegClass);
18297 if (VT == MVT::i16)
18298 return std::make_pair(0U, &X86::GR16RegClass);
18299 if (VT == MVT::i8 || VT == MVT::i1)
18300 return std::make_pair(0U, &X86::GR8RegClass);
18301 if (VT == MVT::i64 || VT == MVT::f64)
18302 return std::make_pair(0U, &X86::GR64RegClass);
18303 break;
Eric Christopherd176af82011-06-29 17:23:50 +000018304 }
18305 // 32-bit fallthrough
18306 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000018307 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000018308 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
18309 if (VT == MVT::i16)
18310 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
18311 if (VT == MVT::i8 || VT == MVT::i1)
18312 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
18313 if (VT == MVT::i64)
18314 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000018315 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018316 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000018317 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018318 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018319 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018320 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018321 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000018322 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018323 return std::make_pair(0U, &X86::GR32RegClass);
18324 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018325 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000018326 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000018327 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018328 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000018329 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000018330 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000018331 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
18332 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000018333 case 'f': // FP Stack registers.
18334 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
18335 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000018336 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018337 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018338 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000018339 return std::make_pair(0U, &X86::RFP64RegClass);
18340 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000018341 case 'y': // MMX_REGS if MMX allowed.
18342 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000018343 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018344 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018345 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000018346 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000018347 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000018348 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000018349
Owen Anderson825b72b2009-08-11 20:47:22 +000018350 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000018351 default: break;
18352 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018353 case MVT::f32:
18354 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000018355 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000018356 case MVT::f64:
18357 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000018358 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018359 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000018360 case MVT::v16i8:
18361 case MVT::v8i16:
18362 case MVT::v4i32:
18363 case MVT::v2i64:
18364 case MVT::v4f32:
18365 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000018366 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000018367 // AVX types.
18368 case MVT::v32i8:
18369 case MVT::v16i16:
18370 case MVT::v8i32:
18371 case MVT::v4i64:
18372 case MVT::v8f32:
18373 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000018374 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000018375 }
Chris Lattnerad043e82007-04-09 05:11:28 +000018376 break;
18377 }
18378 }
Scott Michelfdc40a02009-02-17 22:15:04 +000018379
Chris Lattnerf76d1802006-07-31 23:26:50 +000018380 // Use the default implementation in TargetLowering to convert the register
18381 // constraint into a member of a register class.
18382 std::pair<unsigned, const TargetRegisterClass*> Res;
18383 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000018384
18385 // Not found as a standard register?
18386 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018387 // Map st(0) -> st(7) -> ST0
18388 if (Constraint.size() == 7 && Constraint[0] == '{' &&
18389 tolower(Constraint[1]) == 's' &&
18390 tolower(Constraint[2]) == 't' &&
18391 Constraint[3] == '(' &&
18392 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
18393 Constraint[5] == ')' &&
18394 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000018395
Chris Lattner56d77c72009-09-13 22:41:48 +000018396 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000018397 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018398 return Res;
18399 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018400
Chris Lattner56d77c72009-09-13 22:41:48 +000018401 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018402 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000018403 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000018404 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018405 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000018406 }
Chris Lattner56d77c72009-09-13 22:41:48 +000018407
18408 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000018409 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000018410 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000018411 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018412 return Res;
18413 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000018414
Dale Johannesen330169f2008-11-13 21:52:36 +000018415 // 'A' means EAX + EDX.
18416 if (Constraint == "A") {
18417 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000018418 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000018419 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000018420 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000018421 return Res;
18422 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018423
Chris Lattnerf76d1802006-07-31 23:26:50 +000018424 // Otherwise, check to see if this is a register class of the wrong value
18425 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
18426 // turn into {ax},{dx}.
18427 if (Res.second->hasType(VT))
18428 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018429
Chris Lattnerf76d1802006-07-31 23:26:50 +000018430 // All of the single-register GCC register classes map their values onto
18431 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
18432 // really want an 8-bit or 32-bit register, map to the appropriate register
18433 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000018434 if (Res.second == &X86::GR16RegClass) {
Eric Christopher23571f42013-02-13 06:01:05 +000018435 if (VT == MVT::i8 || VT == MVT::i1) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018436 unsigned DestReg = 0;
18437 switch (Res.first) {
18438 default: break;
18439 case X86::AX: DestReg = X86::AL; break;
18440 case X86::DX: DestReg = X86::DL; break;
18441 case X86::CX: DestReg = X86::CL; break;
18442 case X86::BX: DestReg = X86::BL; break;
18443 }
18444 if (DestReg) {
18445 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018446 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018447 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018448 } else if (VT == MVT::i32 || VT == MVT::f32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018449 unsigned DestReg = 0;
18450 switch (Res.first) {
18451 default: break;
18452 case X86::AX: DestReg = X86::EAX; break;
18453 case X86::DX: DestReg = X86::EDX; break;
18454 case X86::CX: DestReg = X86::ECX; break;
18455 case X86::BX: DestReg = X86::EBX; break;
18456 case X86::SI: DestReg = X86::ESI; break;
18457 case X86::DI: DestReg = X86::EDI; break;
18458 case X86::BP: DestReg = X86::EBP; break;
18459 case X86::SP: DestReg = X86::ESP; break;
18460 }
18461 if (DestReg) {
18462 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018463 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018464 }
Eric Christophera9bd4b42013-01-31 00:50:46 +000018465 } else if (VT == MVT::i64 || VT == MVT::f64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018466 unsigned DestReg = 0;
18467 switch (Res.first) {
18468 default: break;
18469 case X86::AX: DestReg = X86::RAX; break;
18470 case X86::DX: DestReg = X86::RDX; break;
18471 case X86::CX: DestReg = X86::RCX; break;
18472 case X86::BX: DestReg = X86::RBX; break;
18473 case X86::SI: DestReg = X86::RSI; break;
18474 case X86::DI: DestReg = X86::RDI; break;
18475 case X86::BP: DestReg = X86::RBP; break;
18476 case X86::SP: DestReg = X86::RSP; break;
18477 }
18478 if (DestReg) {
18479 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000018480 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000018481 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000018482 }
Craig Topperc9099502012-04-20 06:31:50 +000018483 } else if (Res.second == &X86::FR32RegClass ||
18484 Res.second == &X86::FR64RegClass ||
18485 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000018486 // Handle references to XMM physical registers that got mapped into the
18487 // wrong class. This can happen with constraints like {xmm0} where the
18488 // target independent register mapper will just pick the first match it can
18489 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000018490
18491 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000018492 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018493 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000018494 Res.second = &X86::FR64RegClass;
18495 else if (X86::VR128RegClass.hasType(VT))
18496 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000018497 else if (X86::VR256RegClass.hasType(VT))
18498 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000018499 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000018500
Chris Lattnerf76d1802006-07-31 23:26:50 +000018501 return Res;
18502}