blob: 0ad74c9eb943c08ecc3907165f9fea57aa43c283 [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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000018#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000045#include "llvm/ADT/VariadicFunction.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
Craig Topperb8d9da12012-09-06 06:09:01 +000088 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000089 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
90 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000091
Craig Topperb14940a2012-04-22 20:55:18 +000092 return Result;
David Greenea5f26012011-02-07 19:36:54 +000093}
94
95/// Generate a DAG to put 128-bits into a vector > 128 bits. This
96/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +000097/// simple superregister reference. Idx is an index in the 128 bits
98/// we want. It need not be aligned to a 128-bit bounday. That makes
99/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000100static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
101 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000102 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000103 // Inserting UNDEF is Result
104 if (Vec.getOpcode() == ISD::UNDEF)
105 return Result;
106
Craig Topperb14940a2012-04-22 20:55:18 +0000107 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000108 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000109
Craig Topperb14940a2012-04-22 20:55:18 +0000110 EVT ElVT = VT.getVectorElementType();
111 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000112
Craig Topperb14940a2012-04-22 20:55:18 +0000113 // Insert the relevant 128 bits.
114 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000115
Craig Topperb14940a2012-04-22 20:55:18 +0000116 // This is the index of the first element of the 128-bit chunk
117 // we want.
118 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
119 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb8d9da12012-09-06 06:09:01 +0000121 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000122 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
123 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000124}
125
Craig Topper4c7972d2012-04-22 18:15:59 +0000126/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
127/// instructions. This is used because creating CONCAT_VECTOR nodes of
128/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
129/// large BUILD_VECTORS.
130static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
131 unsigned NumElems, SelectionDAG &DAG,
132 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000133 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
134 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000135}
136
Chris Lattnerf0144122009-07-28 03:13:23 +0000137static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000138 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000140
Evan Cheng2bffee22011-02-01 01:14:13 +0000141 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000142 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000143 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000144 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000145 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000146
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000147 if (Subtarget->isTargetLinux())
148 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000149 if (Subtarget->isTargetELF())
150 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000151 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000152 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000153 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000154}
155
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000156X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000158 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000159 X86ScalarSSEf64 = Subtarget->hasSSE2();
160 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +0000161 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000162
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000163 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000164 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000165
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000167 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000168
169 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000170 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000171 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
172 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000173
Eric Christopherde5e1012011-03-11 01:05:58 +0000174 // For 64-bit since we have so many registers use the ILP scheduler, for
175 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000176 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000177 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000178 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000179 else if (Subtarget->is64Bit())
180 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000181 else
182 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000183 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000184
Preston Gurd2e2efd92012-09-04 18:22:17 +0000185 // Bypass i32 with i8 on Atom when compiling with O2
186 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default)
187 addBypassSlowDivType(Type::getInt32Ty(getGlobalContext()), Type::getInt8Ty(getGlobalContext()));
188
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000189 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000190 // Setup Windows compiler runtime calls.
191 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000192 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000193 setLibcallName(RTLIB::SREM_I64, "_allrem");
194 setLibcallName(RTLIB::UREM_I64, "_aullrem");
195 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
200 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000201
202 // The _ftol2 runtime function has an unusual calling conv, which
203 // is modeled by a special pseudo-instruction.
204 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
205 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
206 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
207 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000208 }
209
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000210 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000211 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 setUseUnderscoreSetJmp(false);
213 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000214 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 // MS runtime is weird: it exports _setjmp, but longjmp!
216 setUseUnderscoreSetJmp(true);
217 setUseUnderscoreLongJmp(false);
218 } else {
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(true);
221 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000222
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000224 addRegisterClass(MVT::i8, &X86::GR8RegClass);
225 addRegisterClass(MVT::i16, &X86::GR16RegClass);
226 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000228 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000231
Scott Michelfdc40a02009-02-17 22:15:04 +0000232 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000234 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
238 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000239
240 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
243 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000247
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000248 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
249 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
252 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000253
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000256 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000257 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000258 // We have an algorithm for SSE2->double, and we turn this into a
259 // 64-bit FILD followed by conditional FADD for other targets.
260 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000261 // We have an algorithm for SSE2, and we turn this into a 64-bit
262 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000263 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000264 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265
266 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
267 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
269 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000270
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000271 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000272 // SSE has no i16 to fp conversion, only i32
273 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000281 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
283 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000284 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000285
Dale Johannesen73328d12007-09-19 23:55:34 +0000286 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
287 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
289 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000290
Evan Cheng02568ff2006-01-30 22:13:22 +0000291 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
292 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
294 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000295
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000296 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000298 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000300 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000303 }
304
305 // Handle FP_TO_UINT by promoting the destination to a larger signed
306 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
309 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310
Evan Cheng25ab6902006-09-08 06:48:29 +0000311 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
313 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000314 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000315 // Since AVX is a superset of SSE3, only check for SSE here.
316 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 // Expand FP_TO_UINT into a select.
318 // FIXME: We would like to use a Custom expander here eventually to do
319 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000322 // With SSE3 we can use fisttpll to convert to a signed i64; without
323 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000325 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000326
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000327 if (isTargetFTOL()) {
328 // Use the _ftol2 runtime function, which has a pseudo-instruction
329 // to handle its weird calling convention.
330 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
331 }
332
Chris Lattner399610a2006-12-05 18:22:22 +0000333 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000334 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
336 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000337 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000338 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000339 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000340 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000341 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000342 }
Chris Lattner21f66852005-12-23 05:15:23 +0000343
Dan Gohmanb00ee212008-02-18 19:34:53 +0000344 // Scalar integer divide and remainder are lowered to use operations that
345 // produce two results, to match the available instructions. This exposes
346 // the two-result form to trivial CSE, which is able to combine x/y and x%y
347 // into a single instruction.
348 //
349 // Scalar integer multiply-high is also lowered to use two-result
350 // operations, to match the available instructions. However, plain multiply
351 // (low) operations are left as Legal, as there are single-result
352 // instructions for this in x86. Using the two-result multiply instructions
353 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000354 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000355 MVT VT = IntVTs[i];
356 setOperationAction(ISD::MULHS, VT, Expand);
357 setOperationAction(ISD::MULHU, VT, Expand);
358 setOperationAction(ISD::SDIV, VT, Expand);
359 setOperationAction(ISD::UDIV, VT, Expand);
360 setOperationAction(ISD::SREM, VT, Expand);
361 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000362
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000363 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000364 setOperationAction(ISD::ADDC, VT, Custom);
365 setOperationAction(ISD::ADDE, VT, Custom);
366 setOperationAction(ISD::SUBC, VT, Custom);
367 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000368 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000369
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
371 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
372 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
373 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000374 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
378 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
379 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
380 setOperationAction(ISD::FREM , MVT::f32 , Expand);
381 setOperationAction(ISD::FREM , MVT::f64 , Expand);
382 setOperationAction(ISD::FREM , MVT::f80 , Expand);
383 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000384
Chandler Carruth77821022011-12-24 12:12:34 +0000385 // Promote the i8 variants and force them on up to i32 which has a shorter
386 // encoding.
387 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
388 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
389 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
390 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000391 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000392 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
393 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
394 if (Subtarget->is64Bit())
395 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000396 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000397 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
398 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
399 if (Subtarget->is64Bit())
400 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
401 }
Craig Topper37f21672011-10-11 06:44:02 +0000402
403 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000404 // When promoting the i8 variants, force them to i32 for a shorter
405 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000406 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000407 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
408 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
409 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
411 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
412 if (Subtarget->is64Bit())
413 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000414 } else {
415 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000418 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
420 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
421 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000422 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000423 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
424 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000425 }
426
Benjamin Kramer1292c222010-12-04 20:32:23 +0000427 if (Subtarget->hasPOPCNT()) {
428 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
429 } else {
430 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
431 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
432 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
433 if (Subtarget->is64Bit())
434 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
435 }
436
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
438 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000439
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000440 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000441 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000442 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000443 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000444 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
448 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
449 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000450 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
454 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000457 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000458 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000460
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000461 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
463 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
464 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
465 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000466 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
468 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000469 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000470 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
472 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
473 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
474 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000475 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000476 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000477 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
479 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
480 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000481 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
483 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
484 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000485 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000486
Craig Topper1accb7e2012-01-10 06:54:16 +0000487 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000489
Eric Christopher9a9d2752010-07-22 02:48:34 +0000490 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000491 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000492
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000493 // On X86 and X86-64, atomic operations are lowered to locked instructions.
494 // Locked instructions, in turn, have implicit fence semantics (all memory
495 // operations are flushed before issuing the locked instruction, and they
496 // are not buffered), so we can fold away the common pattern of
497 // fence-atomic-fence.
498 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000499
Mon P Wang63307c32008-05-05 19:05:59 +0000500 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000501 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000502 MVT VT = IntVTs[i];
503 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000505 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000506 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000507
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000508 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000509 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
513 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
514 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
515 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
516 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000517 }
518
Eli Friedman43f51ae2011-08-26 21:21:21 +0000519 if (Subtarget->hasCmpxchg16b()) {
520 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
521 }
522
Evan Cheng3c992d22006-03-07 02:02:57 +0000523 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000524 if (!Subtarget->isTargetDarwin() &&
525 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000526 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000528 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000529
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
531 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
532 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
533 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000534 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000535 setExceptionPointerRegister(X86::RAX);
536 setExceptionSelectorRegister(X86::RDX);
537 } else {
538 setExceptionPointerRegister(X86::EAX);
539 setExceptionSelectorRegister(X86::EDX);
540 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
542 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000543
Duncan Sands4a544a72011-09-06 13:37:06 +0000544 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
545 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000546
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000548
Nate Begemanacc398c2006-01-25 18:21:52 +0000549 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::VASTART , MVT::Other, Custom);
551 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000552 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::VAARG , MVT::Other, Custom);
554 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000555 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 setOperationAction(ISD::VAARG , MVT::Other, Expand);
557 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000558 }
Evan Chengae642192007-03-02 23:16:35 +0000559
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
561 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000562
563 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
564 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
565 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000566 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000567 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
568 MVT::i64 : MVT::i32, Custom);
569 else
570 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
571 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000572
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000573 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000574 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000575 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000576 addRegisterClass(MVT::f32, &X86::FR32RegClass);
577 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000578
Evan Cheng223547a2006-01-31 22:28:30 +0000579 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FABS , MVT::f64, Custom);
581 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000582
583 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::FNEG , MVT::f64, Custom);
585 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000586
Evan Cheng68c47cb2007-01-05 07:55:56 +0000587 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
589 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000590
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000591 // Lower this to FGETSIGNx86 plus an AND.
592 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
593 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
594
Evan Chengd25e9e82006-02-02 00:28:23 +0000595 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FSIN , MVT::f64, Expand);
597 setOperationAction(ISD::FCOS , MVT::f64, Expand);
598 setOperationAction(ISD::FSIN , MVT::f32, Expand);
599 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000600
Chris Lattnera54aa942006-01-29 06:26:08 +0000601 // Expand FP immediates into loads from the stack, except for the special
602 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 addLegalFPImmediate(APFloat(+0.0)); // xorpd
604 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000605 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000606 // Use SSE for f32, x87 for f64.
607 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000608 addRegisterClass(MVT::f32, &X86::FR32RegClass);
609 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610
611 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613
614 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618
619 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f32, Expand);
625 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626
Nate Begemane1795842008-02-14 08:57:00 +0000627 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628 addLegalFPImmediate(APFloat(+0.0f)); // xorps
629 addLegalFPImmediate(APFloat(+0.0)); // FLD0
630 addLegalFPImmediate(APFloat(+1.0)); // FLD1
631 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
632 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
633
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000634 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
636 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000637 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000638 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000640 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000641 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
642 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000643
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
645 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
646 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
647 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000648
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000649 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
651 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000652 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000653 addLegalFPImmediate(APFloat(+0.0)); // FLD0
654 addLegalFPImmediate(APFloat(+1.0)); // FLD1
655 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
656 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000657 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
658 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
659 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
660 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000661 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000662
Cameron Zwarich33390842011-07-08 21:39:21 +0000663 // We don't support FMA.
664 setOperationAction(ISD::FMA, MVT::f64, Expand);
665 setOperationAction(ISD::FMA, MVT::f32, Expand);
666
Dale Johannesen59a58732007-08-05 18:49:15 +0000667 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000668 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000669 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
671 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000672 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000673 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000674 addLegalFPImmediate(TmpFlt); // FLD0
675 TmpFlt.changeSign();
676 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000677
678 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000679 APFloat TmpFlt2(+1.0);
680 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
681 &ignored);
682 addLegalFPImmediate(TmpFlt2); // FLD1
683 TmpFlt2.changeSign();
684 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
685 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000686
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000687 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
689 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000690 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000691
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000692 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
693 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
694 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
695 setOperationAction(ISD::FRINT, MVT::f80, Expand);
696 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000697 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000698 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000699
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000700 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
702 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
703 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000704
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FLOG, MVT::f80, Expand);
706 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
707 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
708 setOperationAction(ISD::FEXP, MVT::f80, Expand);
709 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000710
Mon P Wangf007a8b2008-11-06 05:31:54 +0000711 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000712 // (for widening) or expand (for scalarization). Then we will selectively
713 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000714 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
715 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000732 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
733 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000734 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000738 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000742 setOperationAction(ISD::FFLOOR, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
749 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000750 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000752 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
757 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
758 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000759 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
765 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
766 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
768 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000769 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000770 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
771 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
772 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
773 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000774 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000775 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
776 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000777 setTruncStoreAction((MVT::SimpleValueType)VT,
778 (MVT::SimpleValueType)InnerVT, Expand);
779 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
780 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
781 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000782 }
783
Evan Chengc7ce29b2009-02-13 22:36:38 +0000784 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
785 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000786 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000787 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000789 }
790
Dale Johannesen0488fb62010-09-30 23:57:10 +0000791 // MMX-sized vectors (other than x86mmx) are expected to be expanded
792 // into smaller operations.
793 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
794 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
795 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
796 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
797 setOperationAction(ISD::AND, MVT::v8i8, Expand);
798 setOperationAction(ISD::AND, MVT::v4i16, Expand);
799 setOperationAction(ISD::AND, MVT::v2i32, Expand);
800 setOperationAction(ISD::AND, MVT::v1i64, Expand);
801 setOperationAction(ISD::OR, MVT::v8i8, Expand);
802 setOperationAction(ISD::OR, MVT::v4i16, Expand);
803 setOperationAction(ISD::OR, MVT::v2i32, Expand);
804 setOperationAction(ISD::OR, MVT::v1i64, Expand);
805 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
806 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
807 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
808 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
809 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
810 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
811 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
812 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
813 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
814 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
815 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
816 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
817 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000818 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
819 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
820 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
821 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000822
Craig Topper1accb7e2012-01-10 06:54:16 +0000823 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000824 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000825
Owen Anderson825b72b2009-08-11 20:47:22 +0000826 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
827 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
828 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
829 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
831 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000832 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
834 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
835 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
836 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
837 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000838 }
839
Craig Topper1accb7e2012-01-10 06:54:16 +0000840 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000841 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000842
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000843 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
844 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000845 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
846 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
847 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
848 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000849
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
851 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
852 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
853 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
854 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
855 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
856 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
857 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
858 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
859 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
860 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
861 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
862 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
863 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
864 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
865 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000866 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000867
Nadav Rotem354efd82011-09-18 14:57:03 +0000868 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000869 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
870 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
871 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000872
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
874 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
876 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
877 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000878
Evan Cheng2c3ae372006-04-12 21:21:57 +0000879 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000880 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000881 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000882 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000883 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000884 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000885 // Do not attempt to custom lower non-128-bit vectors
886 if (!VT.is128BitVector())
887 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000888 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
889 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
890 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000891 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000892
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
894 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
895 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
896 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
898 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000899
Nate Begemancdd1eec2008-02-12 22:51:28 +0000900 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
902 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000903 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000904
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000905 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000906 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000907 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000908
909 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000910 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000911 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000912
Craig Topper0d1f1762012-08-12 00:34:56 +0000913 setOperationAction(ISD::AND, VT, Promote);
914 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
915 setOperationAction(ISD::OR, VT, Promote);
916 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
917 setOperationAction(ISD::XOR, VT, Promote);
918 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
919 setOperationAction(ISD::LOAD, VT, Promote);
920 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
921 setOperationAction(ISD::SELECT, VT, Promote);
922 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000923 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000924
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000926
Evan Cheng2c3ae372006-04-12 21:21:57 +0000927 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
929 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
930 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
931 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000932
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
934 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000935 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000936
Craig Topperd0a31172012-01-10 06:37:29 +0000937 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000938 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
939 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
940 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
941 setOperationAction(ISD::FRINT, MVT::f32, Legal);
942 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
943 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
944 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
945 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
946 setOperationAction(ISD::FRINT, MVT::f64, Legal);
947 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
948
Craig Topper12fb5c62012-09-08 17:42:27 +0000949 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
950 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
951
Nate Begeman14d12ca2008-02-11 04:19:36 +0000952 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000954
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000955 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
956 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
957 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
958 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
959 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000960
Nate Begeman14d12ca2008-02-11 04:19:36 +0000961 // i8 and i16 vectors are custom , because the source register and source
962 // source memory operand types are not the same width. f32 vectors are
963 // custom since the immediate controlling the insert encodes additional
964 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000965 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
966 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
967 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
968 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000969
Owen Anderson825b72b2009-08-11 20:47:22 +0000970 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
971 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
972 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
973 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000974
Pete Coopera77214a2011-11-14 19:38:42 +0000975 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000976 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000977 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000978 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
979 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000980 }
981 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000982
Craig Topper1accb7e2012-01-10 06:54:16 +0000983 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000984 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000985 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000986
Nadav Rotem43012222011-05-11 08:12:09 +0000987 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000988 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000989
Nadav Rotem43012222011-05-11 08:12:09 +0000990 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000991 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000992
993 if (Subtarget->hasAVX2()) {
994 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
995 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
996
997 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
998 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
999
1000 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1001 } else {
1002 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1003 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1004
1005 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1006 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1007
1008 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1009 }
Nadav Rotem43012222011-05-11 08:12:09 +00001010 }
1011
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001012 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001013 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1014 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1015 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1016 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1017 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1018 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001019
Owen Anderson825b72b2009-08-11 20:47:22 +00001020 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1022 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001023
Owen Anderson825b72b2009-08-11 20:47:22 +00001024 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1025 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1026 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1027 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1028 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001029 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001030 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001031 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001032
Owen Anderson825b72b2009-08-11 20:47:22 +00001033 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1034 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1035 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1036 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1037 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001038 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001039 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001040 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001041
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001042 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1043 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001044 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001045
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001046 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1047 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1048
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001049 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1050 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1051
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001052 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001053 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001054
Duncan Sands28b77e92011-09-06 19:07:46 +00001055 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1056 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1057 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1058 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001059
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001060 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1061 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1062 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1063
Craig Topperaaa643c2011-11-09 07:28:55 +00001064 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1065 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1066 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1067 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001068
Craig Topperbf404372012-08-31 15:40:30 +00001069 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001070 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1071 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1072 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1073 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1074 setOperationAction(ISD::FMA, MVT::f32, Custom);
1075 setOperationAction(ISD::FMA, MVT::f64, Custom);
1076 }
Craig Topper880ef452012-08-11 22:34:26 +00001077
Craig Topperaaa643c2011-11-09 07:28:55 +00001078 if (Subtarget->hasAVX2()) {
1079 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1080 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1081 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1082 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001083
Craig Topperaaa643c2011-11-09 07:28:55 +00001084 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1085 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1086 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1087 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001088
Craig Topperaaa643c2011-11-09 07:28:55 +00001089 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1090 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1091 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001092 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001093
1094 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001095
1096 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1097 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1098
1099 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1100 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1101
1102 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001103 } else {
1104 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1105 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1106 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1107 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1108
1109 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1110 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1111 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1112 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1113
1114 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1115 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1116 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1117 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001118
1119 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1120 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1121
1122 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1123 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1124
1125 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001126 }
Craig Topper13894fa2011-08-24 06:14:18 +00001127
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001128 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001129 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1130 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001131 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001132
1133 // Extract subvector is special because the value type
1134 // (result) is 128-bit but the source is 256-bit wide.
1135 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001136 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001137
1138 // Do not attempt to custom lower other non-256-bit vectors
1139 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001140 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001141
Craig Topper0d1f1762012-08-12 00:34:56 +00001142 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1143 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1144 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1145 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1146 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1147 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1148 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001149 }
1150
David Greene54d8eba2011-01-27 22:38:56 +00001151 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001152 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001153 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001154
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001155 // Do not attempt to promote non-256-bit vectors
1156 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001157 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001158
Craig Topper0d1f1762012-08-12 00:34:56 +00001159 setOperationAction(ISD::AND, VT, Promote);
1160 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1161 setOperationAction(ISD::OR, VT, Promote);
1162 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1163 setOperationAction(ISD::XOR, VT, Promote);
1164 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1165 setOperationAction(ISD::LOAD, VT, Promote);
1166 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1167 setOperationAction(ISD::SELECT, VT, Promote);
1168 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001169 }
David Greene9b9838d2009-06-29 16:47:10 +00001170 }
1171
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001172 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1173 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001174 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1175 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001176 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1177 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001178 }
1179
Evan Cheng6be2c582006-04-05 23:38:46 +00001180 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001181 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001182 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001183
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001184
Eli Friedman962f5492010-06-02 19:35:46 +00001185 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1186 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001187 //
Eli Friedman962f5492010-06-02 19:35:46 +00001188 // FIXME: We really should do custom legalization for addition and
1189 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1190 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001191 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1192 // Add/Sub/Mul with overflow operations are custom lowered.
1193 MVT VT = IntVTs[i];
1194 setOperationAction(ISD::SADDO, VT, Custom);
1195 setOperationAction(ISD::UADDO, VT, Custom);
1196 setOperationAction(ISD::SSUBO, VT, Custom);
1197 setOperationAction(ISD::USUBO, VT, Custom);
1198 setOperationAction(ISD::SMULO, VT, Custom);
1199 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001200 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001201
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001202 // There are no 8-bit 3-address imul/mul instructions
1203 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1204 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001205
Evan Chengd54f2d52009-03-31 19:38:51 +00001206 if (!Subtarget->is64Bit()) {
1207 // These libcalls are not available in 32-bit.
1208 setLibcallName(RTLIB::SHL_I128, 0);
1209 setLibcallName(RTLIB::SRL_I128, 0);
1210 setLibcallName(RTLIB::SRA_I128, 0);
1211 }
1212
Evan Cheng206ee9d2006-07-07 08:33:52 +00001213 // We have target-specific dag combine patterns for the following nodes:
1214 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001215 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001216 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001217 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001218 setTargetDAGCombine(ISD::SHL);
1219 setTargetDAGCombine(ISD::SRA);
1220 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001221 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001222 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001223 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001224 setTargetDAGCombine(ISD::FADD);
1225 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001226 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001227 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001228 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001229 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001230 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001231 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001232 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001233 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001234 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001235 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001236 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001237 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001238 if (Subtarget->is64Bit())
1239 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001240 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001241
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001242 computeRegisterProperties();
1243
Evan Cheng05219282011-01-06 06:52:41 +00001244 // On Darwin, -Os means optimize for size without hurting performance,
1245 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001246 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001247 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001248 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001249 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1250 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1251 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001252 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001253 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001254
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001255 // Predictable cmov don't hurt on atom because it's in-order.
1256 predictableSelectIsExpensive = !Subtarget->isAtom();
1257
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001258 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001259}
1260
Scott Michel5b8f82e2008-03-10 15:42:14 +00001261
Duncan Sands28b77e92011-09-06 19:07:46 +00001262EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1263 if (!VT.isVector()) return MVT::i8;
1264 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001265}
1266
1267
Evan Cheng29286502008-01-23 23:17:41 +00001268/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1269/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001270static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001271 if (MaxAlign == 16)
1272 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001273 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001274 if (VTy->getBitWidth() == 128)
1275 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001276 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001277 unsigned EltAlign = 0;
1278 getMaxByValAlign(ATy->getElementType(), EltAlign);
1279 if (EltAlign > MaxAlign)
1280 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001281 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001282 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1283 unsigned EltAlign = 0;
1284 getMaxByValAlign(STy->getElementType(i), EltAlign);
1285 if (EltAlign > MaxAlign)
1286 MaxAlign = EltAlign;
1287 if (MaxAlign == 16)
1288 break;
1289 }
1290 }
Evan Cheng29286502008-01-23 23:17:41 +00001291}
1292
1293/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1294/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001295/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1296/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001297unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001298 if (Subtarget->is64Bit()) {
1299 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001300 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001301 if (TyAlign > 8)
1302 return TyAlign;
1303 return 8;
1304 }
1305
Evan Cheng29286502008-01-23 23:17:41 +00001306 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001307 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001308 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001309 return Align;
1310}
Chris Lattner2b02a442007-02-25 08:29:00 +00001311
Evan Chengf0df0312008-05-15 08:39:06 +00001312/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001313/// and store operations as a result of memset, memcpy, and memmove
1314/// lowering. If DstAlign is zero that means it's safe to destination
1315/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1316/// means there isn't a need to check it against alignment requirement,
1317/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001318/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001319/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1320/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1321/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001322/// It returns EVT::Other if the type should be determined using generic
1323/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001324EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001325X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1326 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001327 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001328 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001329 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001330 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1331 // linux. This is because the stack realignment code can't handle certain
1332 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001333 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001334 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001335 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001336 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001337 (Subtarget->isUnalignedMemAccessFast() ||
1338 ((DstAlign == 0 || DstAlign >= 16) &&
1339 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001340 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001341 if (Subtarget->getStackAlignment() >= 32) {
1342 if (Subtarget->hasAVX2())
1343 return MVT::v8i32;
1344 if (Subtarget->hasAVX())
1345 return MVT::v8f32;
1346 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001347 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001348 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001349 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001350 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001351 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001352 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001353 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001354 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001355 // Do not use f64 to lower memcpy if source is string constant. It's
1356 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001357 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001358 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001359 }
Evan Chengf0df0312008-05-15 08:39:06 +00001360 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001361 return MVT::i64;
1362 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001363}
1364
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001365/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1366/// current function. The returned value is a member of the
1367/// MachineJumpTableInfo::JTEntryKind enum.
1368unsigned X86TargetLowering::getJumpTableEncoding() const {
1369 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1370 // symbol.
1371 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1372 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001373 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001374
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001375 // Otherwise, use the normal jump table encoding heuristics.
1376 return TargetLowering::getJumpTableEncoding();
1377}
1378
Chris Lattnerc64daab2010-01-26 05:02:42 +00001379const MCExpr *
1380X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1381 const MachineBasicBlock *MBB,
1382 unsigned uid,MCContext &Ctx) const{
1383 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1384 Subtarget->isPICStyleGOT());
1385 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1386 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001387 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1388 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001389}
1390
Evan Chengcc415862007-11-09 01:32:10 +00001391/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1392/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001393SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001394 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001395 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001396 // This doesn't have DebugLoc associated with it, but is not really the
1397 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001398 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001399 return Table;
1400}
1401
Chris Lattner589c6f62010-01-26 06:28:43 +00001402/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1403/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1404/// MCExpr.
1405const MCExpr *X86TargetLowering::
1406getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1407 MCContext &Ctx) const {
1408 // X86-64 uses RIP relative addressing based on the jump table label.
1409 if (Subtarget->isPICStyleRIPRel())
1410 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1411
1412 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001413 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001414}
1415
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001416// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001417std::pair<const TargetRegisterClass*, uint8_t>
1418X86TargetLowering::findRepresentativeClass(EVT VT) const{
1419 const TargetRegisterClass *RRC = 0;
1420 uint8_t Cost = 1;
1421 switch (VT.getSimpleVT().SimpleTy) {
1422 default:
1423 return TargetLowering::findRepresentativeClass(VT);
1424 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001425 RRC = Subtarget->is64Bit() ?
1426 (const TargetRegisterClass*)&X86::GR64RegClass :
1427 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001428 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001429 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001430 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001431 break;
1432 case MVT::f32: case MVT::f64:
1433 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1434 case MVT::v4f32: case MVT::v2f64:
1435 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1436 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001437 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001438 break;
1439 }
1440 return std::make_pair(RRC, Cost);
1441}
1442
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001443bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1444 unsigned &Offset) const {
1445 if (!Subtarget->isTargetLinux())
1446 return false;
1447
1448 if (Subtarget->is64Bit()) {
1449 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1450 Offset = 0x28;
1451 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1452 AddressSpace = 256;
1453 else
1454 AddressSpace = 257;
1455 } else {
1456 // %gs:0x14 on i386
1457 Offset = 0x14;
1458 AddressSpace = 256;
1459 }
1460 return true;
1461}
1462
1463
Chris Lattner2b02a442007-02-25 08:29:00 +00001464//===----------------------------------------------------------------------===//
1465// Return Value Calling Convention Implementation
1466//===----------------------------------------------------------------------===//
1467
Chris Lattner59ed56b2007-02-28 04:55:35 +00001468#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001469
Michael J. Spencerec38de22010-10-10 22:04:20 +00001470bool
Eric Christopher471e4222011-06-08 23:55:35 +00001471X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001472 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001473 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001474 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001475 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001476 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001477 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001478 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001479}
1480
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481SDValue
1482X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001483 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001484 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001485 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001486 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001487 MachineFunction &MF = DAG.getMachineFunction();
1488 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001489
Chris Lattner9774c912007-02-27 05:28:59 +00001490 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001491 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001492 RVLocs, *DAG.getContext());
1493 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001494
Evan Chengdcea1632010-02-04 02:40:39 +00001495 // Add the regs to the liveout set for the function.
1496 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1497 for (unsigned i = 0; i != RVLocs.size(); ++i)
1498 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1499 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001500
Dan Gohman475871a2008-07-27 21:46:04 +00001501 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001502
Dan Gohman475871a2008-07-27 21:46:04 +00001503 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001504 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1505 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001506 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1507 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001508
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001509 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001510 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1511 CCValAssign &VA = RVLocs[i];
1512 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001513 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001514 EVT ValVT = ValToCopy.getValueType();
1515
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001516 // Promote values to the appropriate types
1517 if (VA.getLocInfo() == CCValAssign::SExt)
1518 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1519 else if (VA.getLocInfo() == CCValAssign::ZExt)
1520 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1521 else if (VA.getLocInfo() == CCValAssign::AExt)
1522 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1523 else if (VA.getLocInfo() == CCValAssign::BCvt)
1524 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1525
Dale Johannesenc4510512010-09-24 19:05:48 +00001526 // If this is x86-64, and we disabled SSE, we can't return FP values,
1527 // or SSE or MMX vectors.
1528 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1529 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001530 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001531 report_fatal_error("SSE register return with SSE disabled");
1532 }
1533 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1534 // llvm-gcc has never done it right and no one has noticed, so this
1535 // should be OK for now.
1536 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001537 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001538 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001539
Chris Lattner447ff682008-03-11 03:23:40 +00001540 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1541 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001542 if (VA.getLocReg() == X86::ST0 ||
1543 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001544 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1545 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001546 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001547 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001548 RetOps.push_back(ValToCopy);
1549 // Don't emit a copytoreg.
1550 continue;
1551 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001552
Evan Cheng242b38b2009-02-23 09:03:22 +00001553 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1554 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001555 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001556 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001557 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001558 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001559 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1560 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001561 // If we don't have SSE2 available, convert to v4f32 so the generated
1562 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001563 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001564 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001565 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001566 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001567 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001568
Dale Johannesendd64c412009-02-04 00:33:20 +00001569 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001570 Flag = Chain.getValue(1);
1571 }
Dan Gohman61a92132008-04-21 23:59:07 +00001572
1573 // The x86-64 ABI for returning structs by value requires that we copy
1574 // the sret argument into %rax for the return. We saved the argument into
1575 // a virtual register in the entry block, so now we copy the value out
1576 // and into %rax.
1577 if (Subtarget->is64Bit() &&
1578 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1579 MachineFunction &MF = DAG.getMachineFunction();
1580 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1581 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001582 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001583 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001584 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001585
Dale Johannesendd64c412009-02-04 00:33:20 +00001586 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001587 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001588
1589 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001590 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001591 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001592
Chris Lattner447ff682008-03-11 03:23:40 +00001593 RetOps[0] = Chain; // Update chain.
1594
1595 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001596 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001597 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001598
1599 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001600 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001601}
1602
Evan Chengbf010eb2012-04-10 01:51:00 +00001603bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001604 if (N->getNumValues() != 1)
1605 return false;
1606 if (!N->hasNUsesOfValue(1, 0))
1607 return false;
1608
Evan Chengbf010eb2012-04-10 01:51:00 +00001609 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001610 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001611 if (Copy->getOpcode() == ISD::CopyToReg) {
1612 // If the copy has a glue operand, we conservatively assume it isn't safe to
1613 // perform a tail call.
1614 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1615 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001616 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001617 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001618 return false;
1619
Evan Cheng1bf891a2010-12-01 22:59:46 +00001620 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001621 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001622 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001623 if (UI->getOpcode() != X86ISD::RET_FLAG)
1624 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001625 HasRet = true;
1626 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001627
Evan Chengbf010eb2012-04-10 01:51:00 +00001628 if (!HasRet)
1629 return false;
1630
1631 Chain = TCChain;
1632 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001633}
1634
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001635EVT
1636X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001637 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001638 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001639 // TODO: Is this also valid on 32-bit?
1640 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001641 ReturnMVT = MVT::i8;
1642 else
1643 ReturnMVT = MVT::i32;
1644
1645 EVT MinVT = getRegisterType(Context, ReturnMVT);
1646 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001647}
1648
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649/// LowerCallResult - Lower the result values of a call into the
1650/// appropriate copies out of appropriate physical registers.
1651///
1652SDValue
1653X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001654 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001655 const SmallVectorImpl<ISD::InputArg> &Ins,
1656 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001657 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001658
Chris Lattnere32bbf62007-02-28 07:09:55 +00001659 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001660 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001661 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001662 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001663 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001664 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001665
Chris Lattner3085e152007-02-25 08:59:22 +00001666 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001667 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001668 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001669 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001670
Torok Edwin3f142c32009-02-01 18:15:56 +00001671 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001672 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001673 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001674 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001675 }
1676
Evan Cheng79fb3b42009-02-20 20:43:02 +00001677 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001678
1679 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001680 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001681 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001682 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001683 // instead.
1684 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1685 // If we prefer to use the value in xmm registers, copy it out as f80 and
1686 // use a truncate to move it from fp stack reg to xmm reg.
1687 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001688 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001689 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1690 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001691 Val = Chain.getValue(0);
1692
1693 // Round the f80 to the right size, which also moves it to the appropriate
1694 // xmm register.
1695 if (CopyVT != VA.getValVT())
1696 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1697 // This truncation won't change the value.
1698 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001699 } else {
1700 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1701 CopyVT, InFlag).getValue(1);
1702 Val = Chain.getValue(0);
1703 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001704 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001705 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001706 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001707
Dan Gohman98ca4f22009-08-05 01:29:28 +00001708 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001709}
1710
1711
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001712//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001713// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001714//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001715// StdCall calling convention seems to be standard for many Windows' API
1716// routines and around. It differs from C calling convention just a little:
1717// callee should clean up the stack, not caller. Symbols should be also
1718// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001719// For info on fast calling convention see Fast Calling Convention (tail call)
1720// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001721
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001723/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001724enum StructReturnType {
1725 NotStructReturn,
1726 RegStructReturn,
1727 StackStructReturn
1728};
1729static StructReturnType
1730callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001731 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001732 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001733
Rafael Espindola1cee7102012-07-25 13:41:10 +00001734 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1735 if (!Flags.isSRet())
1736 return NotStructReturn;
1737 if (Flags.isInReg())
1738 return RegStructReturn;
1739 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001740}
1741
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001742/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001743/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001744static StructReturnType
1745argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001746 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001747 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001748
Rafael Espindola1cee7102012-07-25 13:41:10 +00001749 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1750 if (!Flags.isSRet())
1751 return NotStructReturn;
1752 if (Flags.isInReg())
1753 return RegStructReturn;
1754 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001755}
1756
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001757/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1758/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001759/// the specific parameter attribute. The copy will be passed as a byval
1760/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001761static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001762CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001763 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1764 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001765 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001766
Dale Johannesendd64c412009-02-04 00:33:20 +00001767 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001768 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001769 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001770}
1771
Chris Lattner29689432010-03-11 00:22:57 +00001772/// IsTailCallConvention - Return true if the calling convention is one that
1773/// supports tail call optimization.
1774static bool IsTailCallConvention(CallingConv::ID CC) {
1775 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1776}
1777
Evan Cheng485fafc2011-03-21 01:19:09 +00001778bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001779 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001780 return false;
1781
1782 CallSite CS(CI);
1783 CallingConv::ID CalleeCC = CS.getCallingConv();
1784 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1785 return false;
1786
1787 return true;
1788}
1789
Evan Cheng0c439eb2010-01-27 00:07:07 +00001790/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1791/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001792static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1793 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001794 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001795}
1796
Dan Gohman98ca4f22009-08-05 01:29:28 +00001797SDValue
1798X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001799 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001800 const SmallVectorImpl<ISD::InputArg> &Ins,
1801 DebugLoc dl, SelectionDAG &DAG,
1802 const CCValAssign &VA,
1803 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001804 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001805 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001806 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001807 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1808 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001809 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001810 EVT ValVT;
1811
1812 // If value is passed by pointer we have address passed instead of the value
1813 // itself.
1814 if (VA.getLocInfo() == CCValAssign::Indirect)
1815 ValVT = VA.getLocVT();
1816 else
1817 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001818
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001819 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001820 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001821 // In case of tail call optimization mark all arguments mutable. Since they
1822 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001823 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001824 unsigned Bytes = Flags.getByValSize();
1825 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1826 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001827 return DAG.getFrameIndex(FI, getPointerTy());
1828 } else {
1829 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001830 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001831 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1832 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001833 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001834 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001835 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001836}
1837
Dan Gohman475871a2008-07-27 21:46:04 +00001838SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001839X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001840 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001841 bool isVarArg,
1842 const SmallVectorImpl<ISD::InputArg> &Ins,
1843 DebugLoc dl,
1844 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001845 SmallVectorImpl<SDValue> &InVals)
1846 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001847 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001848 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001849
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 const Function* Fn = MF.getFunction();
1851 if (Fn->hasExternalLinkage() &&
1852 Subtarget->isTargetCygMing() &&
1853 Fn->getName() == "main")
1854 FuncInfo->setForceFramePointer(true);
1855
Evan Cheng1bc78042006-04-26 01:20:17 +00001856 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001857 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001858 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001859 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001860
Chris Lattner29689432010-03-11 00:22:57 +00001861 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1862 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001863
Chris Lattner638402b2007-02-28 07:00:42 +00001864 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001865 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001866 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001867 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001868
1869 // Allocate shadow area for Win64
1870 if (IsWin64) {
1871 CCInfo.AllocateStack(32, 8);
1872 }
1873
Duncan Sands45907662010-10-31 13:21:44 +00001874 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001875
Chris Lattnerf39f7712007-02-28 05:46:49 +00001876 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001877 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001878 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1879 CCValAssign &VA = ArgLocs[i];
1880 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1881 // places.
1882 assert(VA.getValNo() != LastVal &&
1883 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001884 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001885 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001886
Chris Lattnerf39f7712007-02-28 05:46:49 +00001887 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001888 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001889 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001890 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001891 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001892 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001893 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001895 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001897 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001898 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001899 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001900 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001901 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001902 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001903 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001904 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001905 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001906
Devang Patel68e6bee2011-02-21 23:21:26 +00001907 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001908 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001909
Chris Lattnerf39f7712007-02-28 05:46:49 +00001910 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1911 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1912 // right size.
1913 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001914 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001915 DAG.getValueType(VA.getValVT()));
1916 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001917 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001918 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001919 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001920 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001921
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001922 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001923 // Handle MMX values passed in XMM regs.
1924 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001925 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1926 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001927 } else
1928 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001929 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001930 } else {
1931 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001932 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001933 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001934
1935 // If value is passed via pointer - do a load.
1936 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001937 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001938 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001939
Dan Gohman98ca4f22009-08-05 01:29:28 +00001940 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001941 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001942
Dan Gohman61a92132008-04-21 23:59:07 +00001943 // The x86-64 ABI for returning structs by value requires that we copy
1944 // the sret argument into %rax for the return. Save the argument into
1945 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001946 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001947 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1948 unsigned Reg = FuncInfo->getSRetReturnReg();
1949 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001950 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001951 FuncInfo->setSRetReturnReg(Reg);
1952 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001953 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001955 }
1956
Chris Lattnerf39f7712007-02-28 05:46:49 +00001957 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001958 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001959 if (FuncIsMadeTailCallSafe(CallConv,
1960 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001961 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001962
Evan Cheng1bc78042006-04-26 01:20:17 +00001963 // If the function takes variable number of arguments, make a frame index for
1964 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001965 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001966 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1967 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001968 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001969 }
1970 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001971 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1972
1973 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001974 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001975 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001976 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001977 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001978 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1979 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001980 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001981 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1982 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1983 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001984 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001985 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001986
1987 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001988 // The XMM registers which might contain var arg parameters are shadowed
1989 // in their paired GPR. So we only need to save the GPR to their home
1990 // slots.
1991 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001992 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001993 } else {
1994 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1995 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001996
Chad Rosier30450e82011-12-22 22:35:21 +00001997 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
1998 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001999 }
2000 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2001 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002002
Devang Patel578efa92009-06-05 21:57:13 +00002003 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002004 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002005 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002006 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2007 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002008 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002009 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002010 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002011 // Kernel mode asks for SSE to be disabled, so don't push them
2012 // on the stack.
2013 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002014
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002015 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002016 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002017 // Get to the caller-allocated home save location. Add 8 to account
2018 // for the return address.
2019 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002020 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002021 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002022 // Fixup to set vararg frame on shadow area (4 x i64).
2023 if (NumIntRegs < 4)
2024 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002025 } else {
2026 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002027 // registers, then we must store them to their spots on the stack so
2028 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002029 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2030 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2031 FuncInfo->setRegSaveFrameIndex(
2032 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002033 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002034 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002035
Gordon Henriksen86737662008-01-05 16:56:59 +00002036 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002038 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2039 getPointerTy());
2040 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002041 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002042 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2043 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002044 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002045 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002046 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002047 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002048 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002049 MachinePointerInfo::getFixedStack(
2050 FuncInfo->getRegSaveFrameIndex(), Offset),
2051 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002052 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002053 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002054 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002055
Dan Gohmanface41a2009-08-16 21:24:25 +00002056 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2057 // Now store the XMM (fp + vector) parameter registers.
2058 SmallVector<SDValue, 11> SaveXMMOps;
2059 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002060
Craig Topperc9099502012-04-20 06:31:50 +00002061 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002062 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2063 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002064
Dan Gohman1e93df62010-04-17 14:41:14 +00002065 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2066 FuncInfo->getRegSaveFrameIndex()));
2067 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2068 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002069
Dan Gohmanface41a2009-08-16 21:24:25 +00002070 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002071 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002072 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002073 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2074 SaveXMMOps.push_back(Val);
2075 }
2076 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2077 MVT::Other,
2078 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002079 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002080
2081 if (!MemOps.empty())
2082 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2083 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002084 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002085 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002086
Gordon Henriksen86737662008-01-05 16:56:59 +00002087 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002088 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2089 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002090 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002091 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002092 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002093 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002094 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002095 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002096 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002097 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002098
Gordon Henriksen86737662008-01-05 16:56:59 +00002099 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002100 // RegSaveFrameIndex is X86-64 only.
2101 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002102 if (CallConv == CallingConv::X86_FastCall ||
2103 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002104 // fastcc functions can't have varargs.
2105 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002106 }
Evan Cheng25caf632006-05-23 21:06:34 +00002107
Rafael Espindola76927d752011-08-30 19:39:58 +00002108 FuncInfo->setArgumentStackSize(StackSize);
2109
Dan Gohman98ca4f22009-08-05 01:29:28 +00002110 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002111}
2112
Dan Gohman475871a2008-07-27 21:46:04 +00002113SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002114X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2115 SDValue StackPtr, SDValue Arg,
2116 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002117 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002118 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002119 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002120 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002121 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002122 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002123 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002124
2125 return DAG.getStore(Chain, dl, Arg, PtrOff,
2126 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002127 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002128}
2129
Bill Wendling64e87322009-01-16 19:25:27 +00002130/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002131/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002132SDValue
2133X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002134 SDValue &OutRetAddr, SDValue Chain,
2135 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002136 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002137 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002138 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002139 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002140
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002141 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002142 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002143 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002144 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002145}
2146
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002147/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002148/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002149static SDValue
2150EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002151 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002152 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002153 // Store the return address to the appropriate stack slot.
2154 if (!FPDiff) return Chain;
2155 // Calculate the new stack slot for the return address.
2156 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002157 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002158 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002159 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002160 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002161 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002162 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002163 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002164 return Chain;
2165}
2166
Dan Gohman98ca4f22009-08-05 01:29:28 +00002167SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002168X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002169 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002170 SelectionDAG &DAG = CLI.DAG;
2171 DebugLoc &dl = CLI.DL;
2172 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2173 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2174 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2175 SDValue Chain = CLI.Chain;
2176 SDValue Callee = CLI.Callee;
2177 CallingConv::ID CallConv = CLI.CallConv;
2178 bool &isTailCall = CLI.IsTailCall;
2179 bool isVarArg = CLI.IsVarArg;
2180
Dan Gohman98ca4f22009-08-05 01:29:28 +00002181 MachineFunction &MF = DAG.getMachineFunction();
2182 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002183 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002184 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002185 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002186 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187
Nick Lewycky22de16d2012-01-19 00:34:10 +00002188 if (MF.getTarget().Options.DisableTailCalls)
2189 isTailCall = false;
2190
Evan Cheng5f941932010-02-05 02:21:12 +00002191 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002192 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002193 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002194 isVarArg, SR != NotStructReturn,
2195 MF.getFunction()->hasStructRetAttr(),
2196 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002197
2198 // Sibcalls are automatically detected tailcalls which do not require
2199 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002200 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002201 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002202
2203 if (isTailCall)
2204 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002205 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002206
Chris Lattner29689432010-03-11 00:22:57 +00002207 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2208 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002209
Chris Lattner638402b2007-02-28 07:00:42 +00002210 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002211 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002212 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002213 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002214
2215 // Allocate shadow area for Win64
2216 if (IsWin64) {
2217 CCInfo.AllocateStack(32, 8);
2218 }
2219
Duncan Sands45907662010-10-31 13:21:44 +00002220 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002221
Chris Lattner423c5f42007-02-28 05:31:48 +00002222 // Get a count of how many bytes are to be pushed on the stack.
2223 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002224 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002225 // This is a sibcall. The memory operands are available in caller's
2226 // own caller's stack.
2227 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002228 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2229 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002230 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002231
Gordon Henriksen86737662008-01-05 16:56:59 +00002232 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002233 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002234 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002235 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002236 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2237 FPDiff = NumBytesCallerPushed - NumBytes;
2238
2239 // Set the delta of movement of the returnaddr stackslot.
2240 // But only set if delta is greater than previous delta.
2241 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2242 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2243 }
2244
Evan Chengf22f9b32010-02-06 03:28:46 +00002245 if (!IsSibcall)
2246 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002247
Dan Gohman475871a2008-07-27 21:46:04 +00002248 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002249 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002250 if (isTailCall && FPDiff)
2251 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2252 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002253
Dan Gohman475871a2008-07-27 21:46:04 +00002254 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2255 SmallVector<SDValue, 8> MemOpChains;
2256 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002257
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002258 // Walk the register/memloc assignments, inserting copies/loads. In the case
2259 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002260 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2261 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002262 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002263 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002264 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002265 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002266
Chris Lattner423c5f42007-02-28 05:31:48 +00002267 // Promote the value if needed.
2268 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002269 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002270 case CCValAssign::Full: break;
2271 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002272 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002273 break;
2274 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002275 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002276 break;
2277 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002278 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002279 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002280 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002281 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2282 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002283 } else
2284 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2285 break;
2286 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002287 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002288 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002289 case CCValAssign::Indirect: {
2290 // Store the argument.
2291 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002292 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002293 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002294 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002295 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002296 Arg = SpillSlot;
2297 break;
2298 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002299 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002300
Chris Lattner423c5f42007-02-28 05:31:48 +00002301 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002302 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2303 if (isVarArg && IsWin64) {
2304 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2305 // shadow reg if callee is a varargs function.
2306 unsigned ShadowReg = 0;
2307 switch (VA.getLocReg()) {
2308 case X86::XMM0: ShadowReg = X86::RCX; break;
2309 case X86::XMM1: ShadowReg = X86::RDX; break;
2310 case X86::XMM2: ShadowReg = X86::R8; break;
2311 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002312 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002313 if (ShadowReg)
2314 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002315 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002316 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002317 assert(VA.isMemLoc());
2318 if (StackPtr.getNode() == 0)
2319 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2320 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2321 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002322 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002323 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002324
Evan Cheng32fe1032006-05-25 00:59:30 +00002325 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002326 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002327 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002328
Chris Lattner88e1fd52009-07-09 04:24:46 +00002329 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002330 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2331 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002332 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002333 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2334 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002335 } else {
2336 // If we are tail calling and generating PIC/GOT style code load the
2337 // address of the callee into ECX. The value in ecx is used as target of
2338 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2339 // for tail calls on PIC/GOT architectures. Normally we would just put the
2340 // address of GOT into ebx and then call target@PLT. But for tail calls
2341 // ebx would be restored (since ebx is callee saved) before jumping to the
2342 // target@PLT.
2343
2344 // Note: The actual moving to ECX is done further down.
2345 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2346 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2347 !G->getGlobal()->hasProtectedVisibility())
2348 Callee = LowerGlobalAddress(Callee, DAG);
2349 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002350 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002351 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002352 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002353
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002354 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002355 // From AMD64 ABI document:
2356 // For calls that may call functions that use varargs or stdargs
2357 // (prototype-less calls or calls to functions containing ellipsis (...) in
2358 // the declaration) %al is used as hidden argument to specify the number
2359 // of SSE registers used. The contents of %al do not need to match exactly
2360 // the number of registers, but must be an ubound on the number of SSE
2361 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002362
Gordon Henriksen86737662008-01-05 16:56:59 +00002363 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002364 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002365 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2366 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2367 };
2368 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002369 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002370 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002371
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002372 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2373 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002374 }
2375
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002376 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002377 if (isTailCall) {
2378 // Force all the incoming stack arguments to be loaded from the stack
2379 // before any new outgoing arguments are stored to the stack, because the
2380 // outgoing stack slots may alias the incoming argument stack slots, and
2381 // the alias isn't otherwise explicit. This is slightly more conservative
2382 // than necessary, because it means that each store effectively depends
2383 // on every argument instead of just those arguments it would clobber.
2384 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2385
Dan Gohman475871a2008-07-27 21:46:04 +00002386 SmallVector<SDValue, 8> MemOpChains2;
2387 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002388 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002389 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002390 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2391 CCValAssign &VA = ArgLocs[i];
2392 if (VA.isRegLoc())
2393 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002394 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002395 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002396 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002397 // Create frame index.
2398 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002399 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002400 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002401 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002402
Duncan Sands276dcbd2008-03-21 09:14:45 +00002403 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002404 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002405 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002406 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002407 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002408 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002409 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002410
Dan Gohman98ca4f22009-08-05 01:29:28 +00002411 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2412 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002413 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002414 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002415 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002416 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002417 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002418 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002419 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002420 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002421 }
2422 }
2423
2424 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002425 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002426 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002427
2428 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002429 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002430 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002431 }
2432
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002433 // Build a sequence of copy-to-reg nodes chained together with token chain
2434 // and flag operands which copy the outgoing args into registers.
2435 SDValue InFlag;
2436 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2437 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2438 RegsToPass[i].second, InFlag);
2439 InFlag = Chain.getValue(1);
2440 }
2441
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002442 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2443 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2444 // In the 64-bit large code model, we have to make all calls
2445 // through a register, since the call instruction's 32-bit
2446 // pc-relative offset may not be large enough to hold the whole
2447 // address.
2448 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002449 // If the callee is a GlobalAddress node (quite common, every direct call
2450 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2451 // it.
2452
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002453 // We should use extra load for direct calls to dllimported functions in
2454 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002455 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002456 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002457 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002458 bool ExtraLoad = false;
2459 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002460
Chris Lattner48a7d022009-07-09 05:02:21 +00002461 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2462 // external symbols most go through the PLT in PIC mode. If the symbol
2463 // has hidden or protected visibility, or if it is static or local, then
2464 // we don't need to use the PLT - we can directly call it.
2465 if (Subtarget->isTargetELF() &&
2466 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002467 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002468 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002469 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002470 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002471 (!Subtarget->getTargetTriple().isMacOSX() ||
2472 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002473 // PC-relative references to external symbols should go through $stub,
2474 // unless we're building with the leopard linker or later, which
2475 // automatically synthesizes these stubs.
2476 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002477 } else if (Subtarget->isPICStyleRIPRel() &&
2478 isa<Function>(GV) &&
2479 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2480 // If the function is marked as non-lazy, generate an indirect call
2481 // which loads from the GOT directly. This avoids runtime overhead
2482 // at the cost of eager binding (and one extra byte of encoding).
2483 OpFlags = X86II::MO_GOTPCREL;
2484 WrapperKind = X86ISD::WrapperRIP;
2485 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002486 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002487
Devang Patel0d881da2010-07-06 22:08:15 +00002488 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002489 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002490
2491 // Add a wrapper if needed.
2492 if (WrapperKind != ISD::DELETED_NODE)
2493 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2494 // Add extra indirection if needed.
2495 if (ExtraLoad)
2496 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2497 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002498 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002499 }
Bill Wendling056292f2008-09-16 21:48:12 +00002500 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002501 unsigned char OpFlags = 0;
2502
Evan Cheng1bf891a2010-12-01 22:59:46 +00002503 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2504 // external symbols should go through the PLT.
2505 if (Subtarget->isTargetELF() &&
2506 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2507 OpFlags = X86II::MO_PLT;
2508 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002509 (!Subtarget->getTargetTriple().isMacOSX() ||
2510 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002511 // PC-relative references to external symbols should go through $stub,
2512 // unless we're building with the leopard linker or later, which
2513 // automatically synthesizes these stubs.
2514 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002515 }
Eric Christopherfd179292009-08-27 18:07:15 +00002516
Chris Lattner48a7d022009-07-09 05:02:21 +00002517 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2518 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002519 }
2520
Chris Lattnerd96d0722007-02-25 06:40:16 +00002521 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002522 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002523 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002524
Evan Chengf22f9b32010-02-06 03:28:46 +00002525 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002526 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2527 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002528 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002529 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002530
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002531 Ops.push_back(Chain);
2532 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002533
Dan Gohman98ca4f22009-08-05 01:29:28 +00002534 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002535 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002536
Gordon Henriksen86737662008-01-05 16:56:59 +00002537 // Add argument registers to the end of the list so that they are known live
2538 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002539 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2540 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2541 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002542
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002543 // Add a register mask operand representing the call-preserved registers.
2544 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2545 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2546 assert(Mask && "Missing call preserved mask for calling convention");
2547 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002548
Gabor Greifba36cb52008-08-28 21:40:38 +00002549 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002550 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002551
Dan Gohman98ca4f22009-08-05 01:29:28 +00002552 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002553 // We used to do:
2554 //// If this is the first return lowered for this function, add the regs
2555 //// to the liveout set for the function.
2556 // This isn't right, although it's probably harmless on x86; liveouts
2557 // should be computed from returns not tail calls. Consider a void
2558 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002559 return DAG.getNode(X86ISD::TC_RETURN, dl,
2560 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002561 }
2562
Dale Johannesenace16102009-02-03 19:33:06 +00002563 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002564 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002565
Chris Lattner2d297092006-05-23 18:50:38 +00002566 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002567 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002568 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2569 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002570 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002571 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002572 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002573 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002574 // pops the hidden struct pointer, so we have to push it back.
2575 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002576 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002577 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002578 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002579 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002580
Gordon Henriksenae636f82008-01-03 16:47:34 +00002581 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002582 if (!IsSibcall) {
2583 Chain = DAG.getCALLSEQ_END(Chain,
2584 DAG.getIntPtrConstant(NumBytes, true),
2585 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2586 true),
2587 InFlag);
2588 InFlag = Chain.getValue(1);
2589 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002590
Chris Lattner3085e152007-02-25 08:59:22 +00002591 // Handle result values, copying them out of physregs into vregs that we
2592 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002593 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2594 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002595}
2596
Evan Cheng25ab6902006-09-08 06:48:29 +00002597
2598//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002599// Fast Calling Convention (tail call) implementation
2600//===----------------------------------------------------------------------===//
2601
2602// Like std call, callee cleans arguments, convention except that ECX is
2603// reserved for storing the tail called function address. Only 2 registers are
2604// free for argument passing (inreg). Tail call optimization is performed
2605// provided:
2606// * tailcallopt is enabled
2607// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002608// On X86_64 architecture with GOT-style position independent code only local
2609// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002610// To keep the stack aligned according to platform abi the function
2611// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2612// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002613// If a tail called function callee has more arguments than the caller the
2614// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002615// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002616// original REtADDR, but before the saved framepointer or the spilled registers
2617// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2618// stack layout:
2619// arg1
2620// arg2
2621// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002622// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002623// move area ]
2624// (possible EBP)
2625// ESI
2626// EDI
2627// local1 ..
2628
2629/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2630/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002631unsigned
2632X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2633 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002634 MachineFunction &MF = DAG.getMachineFunction();
2635 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002636 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002637 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002638 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002639 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002640 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002641 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2642 // Number smaller than 12 so just add the difference.
2643 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2644 } else {
2645 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002646 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002647 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002648 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002649 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002650}
2651
Evan Cheng5f941932010-02-05 02:21:12 +00002652/// MatchingStackOffset - Return true if the given stack call argument is
2653/// already available in the same position (relatively) of the caller's
2654/// incoming argument stack.
2655static
2656bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2657 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2658 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002659 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2660 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002661 if (Arg.getOpcode() == ISD::CopyFromReg) {
2662 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002663 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002664 return false;
2665 MachineInstr *Def = MRI->getVRegDef(VR);
2666 if (!Def)
2667 return false;
2668 if (!Flags.isByVal()) {
2669 if (!TII->isLoadFromStackSlot(Def, FI))
2670 return false;
2671 } else {
2672 unsigned Opcode = Def->getOpcode();
2673 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2674 Def->getOperand(1).isFI()) {
2675 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002676 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002677 } else
2678 return false;
2679 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002680 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2681 if (Flags.isByVal())
2682 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002683 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002684 // define @foo(%struct.X* %A) {
2685 // tail call @bar(%struct.X* byval %A)
2686 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002687 return false;
2688 SDValue Ptr = Ld->getBasePtr();
2689 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2690 if (!FINode)
2691 return false;
2692 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002693 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002694 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002695 FI = FINode->getIndex();
2696 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002697 } else
2698 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002699
Evan Cheng4cae1332010-03-05 08:38:04 +00002700 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002701 if (!MFI->isFixedObjectIndex(FI))
2702 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002703 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002704}
2705
Dan Gohman98ca4f22009-08-05 01:29:28 +00002706/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2707/// for tail call optimization. Targets which want to do tail call
2708/// optimization should implement this function.
2709bool
2710X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002711 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002712 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002713 bool isCalleeStructRet,
2714 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002715 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002716 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002717 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002718 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002719 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002720 CalleeCC != CallingConv::C)
2721 return false;
2722
Evan Cheng7096ae42010-01-29 06:45:59 +00002723 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002724 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002725 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002726 CallingConv::ID CallerCC = CallerF->getCallingConv();
2727 bool CCMatch = CallerCC == CalleeCC;
2728
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002729 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002730 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002731 return true;
2732 return false;
2733 }
2734
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002735 // Look for obvious safe cases to perform tail call optimization that do not
2736 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002737
Evan Cheng2c12cb42010-03-26 16:26:03 +00002738 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2739 // emit a special epilogue.
2740 if (RegInfo->needsStackRealignment(MF))
2741 return false;
2742
Evan Chenga375d472010-03-15 18:54:48 +00002743 // Also avoid sibcall optimization if either caller or callee uses struct
2744 // return semantics.
2745 if (isCalleeStructRet || isCallerStructRet)
2746 return false;
2747
Chad Rosier2416da32011-06-24 21:15:36 +00002748 // An stdcall caller is expected to clean up its arguments; the callee
2749 // isn't going to do that.
2750 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2751 return false;
2752
Chad Rosier871f6642011-05-18 19:59:50 +00002753 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002754 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002755 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002756
2757 // Optimizing for varargs on Win64 is unlikely to be safe without
2758 // additional testing.
2759 if (Subtarget->isTargetWin64())
2760 return false;
2761
Chad Rosier871f6642011-05-18 19:59:50 +00002762 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002763 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002764 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002765
Chad Rosier871f6642011-05-18 19:59:50 +00002766 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2767 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2768 if (!ArgLocs[i].isRegLoc())
2769 return false;
2770 }
2771
Chad Rosier30450e82011-12-22 22:35:21 +00002772 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2773 // stack. Therefore, if it's not used by the call it is not safe to optimize
2774 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002775 bool Unused = false;
2776 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2777 if (!Ins[i].Used) {
2778 Unused = true;
2779 break;
2780 }
2781 }
2782 if (Unused) {
2783 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002784 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002785 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002786 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002787 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002788 CCValAssign &VA = RVLocs[i];
2789 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2790 return false;
2791 }
2792 }
2793
Evan Cheng13617962010-04-30 01:12:32 +00002794 // If the calling conventions do not match, then we'd better make sure the
2795 // results are returned in the same way as what the caller expects.
2796 if (!CCMatch) {
2797 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002798 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002799 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002800 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2801
2802 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002803 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002804 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002805 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2806
2807 if (RVLocs1.size() != RVLocs2.size())
2808 return false;
2809 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2810 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2811 return false;
2812 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2813 return false;
2814 if (RVLocs1[i].isRegLoc()) {
2815 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2816 return false;
2817 } else {
2818 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2819 return false;
2820 }
2821 }
2822 }
2823
Evan Chenga6bff982010-01-30 01:22:00 +00002824 // If the callee takes no arguments then go on to check the results of the
2825 // call.
2826 if (!Outs.empty()) {
2827 // Check if stack adjustment is needed. For now, do not do this if any
2828 // argument is passed on the stack.
2829 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002830 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002831 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002832
2833 // Allocate shadow area for Win64
2834 if (Subtarget->isTargetWin64()) {
2835 CCInfo.AllocateStack(32, 8);
2836 }
2837
Duncan Sands45907662010-10-31 13:21:44 +00002838 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002839 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002840 MachineFunction &MF = DAG.getMachineFunction();
2841 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2842 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002843
2844 // Check if the arguments are already laid out in the right way as
2845 // the caller's fixed stack objects.
2846 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002847 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2848 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002849 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002850 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2851 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002852 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002853 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002854 if (VA.getLocInfo() == CCValAssign::Indirect)
2855 return false;
2856 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002857 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2858 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002859 return false;
2860 }
2861 }
2862 }
Evan Cheng9c044672010-05-29 01:35:22 +00002863
2864 // If the tailcall address may be in a register, then make sure it's
2865 // possible to register allocate for it. In 32-bit, the call address can
2866 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002867 // callee-saved registers are restored. These happen to be the same
2868 // registers used to pass 'inreg' arguments so watch out for those.
2869 if (!Subtarget->is64Bit() &&
2870 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002871 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002872 unsigned NumInRegs = 0;
2873 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2874 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002875 if (!VA.isRegLoc())
2876 continue;
2877 unsigned Reg = VA.getLocReg();
2878 switch (Reg) {
2879 default: break;
2880 case X86::EAX: case X86::EDX: case X86::ECX:
2881 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002882 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002883 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002884 }
2885 }
2886 }
Evan Chenga6bff982010-01-30 01:22:00 +00002887 }
Evan Chengb1712452010-01-27 06:25:16 +00002888
Evan Cheng86809cc2010-02-03 03:28:02 +00002889 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002890}
2891
Dan Gohman3df24e62008-09-03 23:12:08 +00002892FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002893X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2894 const TargetLibraryInfo *libInfo) const {
2895 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002896}
2897
2898
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002899//===----------------------------------------------------------------------===//
2900// Other Lowering Hooks
2901//===----------------------------------------------------------------------===//
2902
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002903static bool MayFoldLoad(SDValue Op) {
2904 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2905}
2906
2907static bool MayFoldIntoStore(SDValue Op) {
2908 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2909}
2910
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002911static bool isTargetShuffle(unsigned Opcode) {
2912 switch(Opcode) {
2913 default: return false;
2914 case X86ISD::PSHUFD:
2915 case X86ISD::PSHUFHW:
2916 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002917 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002918 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002919 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002920 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002921 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002922 case X86ISD::MOVLPS:
2923 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002924 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002925 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002926 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002927 case X86ISD::MOVSS:
2928 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002929 case X86ISD::UNPCKL:
2930 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002931 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002932 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002933 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002934 return true;
2935 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002936}
2937
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002938static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002939 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002940 switch(Opc) {
2941 default: llvm_unreachable("Unknown x86 shuffle node");
2942 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002943 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002944 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002945 return DAG.getNode(Opc, dl, VT, V1);
2946 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002947}
2948
2949static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002950 SDValue V1, unsigned TargetMask,
2951 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002952 switch(Opc) {
2953 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002954 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002955 case X86ISD::PSHUFHW:
2956 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002957 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002958 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002959 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2960 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002961}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002962
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002963static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002964 SDValue V1, SDValue V2, unsigned TargetMask,
2965 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002966 switch(Opc) {
2967 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002968 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002969 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002970 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002971 return DAG.getNode(Opc, dl, VT, V1, V2,
2972 DAG.getConstant(TargetMask, MVT::i8));
2973 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002974}
2975
2976static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2977 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2978 switch(Opc) {
2979 default: llvm_unreachable("Unknown x86 shuffle node");
2980 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002981 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002982 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002983 case X86ISD::MOVLPS:
2984 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002985 case X86ISD::MOVSS:
2986 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002987 case X86ISD::UNPCKL:
2988 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002989 return DAG.getNode(Opc, dl, VT, V1, V2);
2990 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002991}
2992
Dan Gohmand858e902010-04-17 15:26:15 +00002993SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002994 MachineFunction &MF = DAG.getMachineFunction();
2995 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2996 int ReturnAddrIndex = FuncInfo->getRAIndex();
2997
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002998 if (ReturnAddrIndex == 0) {
2999 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00003000 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00003001 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003002 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003003 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003004 }
3005
Evan Cheng25ab6902006-09-08 06:48:29 +00003006 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003007}
3008
3009
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003010bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3011 bool hasSymbolicDisplacement) {
3012 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003013 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003014 return false;
3015
3016 // If we don't have a symbolic displacement - we don't have any extra
3017 // restrictions.
3018 if (!hasSymbolicDisplacement)
3019 return true;
3020
3021 // FIXME: Some tweaks might be needed for medium code model.
3022 if (M != CodeModel::Small && M != CodeModel::Kernel)
3023 return false;
3024
3025 // For small code model we assume that latest object is 16MB before end of 31
3026 // bits boundary. We may also accept pretty large negative constants knowing
3027 // that all objects are in the positive half of address space.
3028 if (M == CodeModel::Small && Offset < 16*1024*1024)
3029 return true;
3030
3031 // For kernel code model we know that all object resist in the negative half
3032 // of 32bits address space. We may not accept negative offsets, since they may
3033 // be just off and we may accept pretty large positive ones.
3034 if (M == CodeModel::Kernel && Offset > 0)
3035 return true;
3036
3037 return false;
3038}
3039
Evan Chengef41ff62011-06-23 17:54:54 +00003040/// isCalleePop - Determines whether the callee is required to pop its
3041/// own arguments. Callee pop is necessary to support tail calls.
3042bool X86::isCalleePop(CallingConv::ID CallingConv,
3043 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3044 if (IsVarArg)
3045 return false;
3046
3047 switch (CallingConv) {
3048 default:
3049 return false;
3050 case CallingConv::X86_StdCall:
3051 return !is64Bit;
3052 case CallingConv::X86_FastCall:
3053 return !is64Bit;
3054 case CallingConv::X86_ThisCall:
3055 return !is64Bit;
3056 case CallingConv::Fast:
3057 return TailCallOpt;
3058 case CallingConv::GHC:
3059 return TailCallOpt;
3060 }
3061}
3062
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003063/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3064/// specific condition code, returning the condition code and the LHS/RHS of the
3065/// comparison to make.
3066static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3067 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003068 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003069 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3070 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3071 // X > -1 -> X == 0, jump !sign.
3072 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003073 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003074 }
3075 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003076 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003077 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003078 }
3079 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003080 // X < 1 -> X <= 0
3081 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003082 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003083 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003084 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003085
Evan Chengd9558e02006-01-06 00:43:03 +00003086 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003087 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003088 case ISD::SETEQ: return X86::COND_E;
3089 case ISD::SETGT: return X86::COND_G;
3090 case ISD::SETGE: return X86::COND_GE;
3091 case ISD::SETLT: return X86::COND_L;
3092 case ISD::SETLE: return X86::COND_LE;
3093 case ISD::SETNE: return X86::COND_NE;
3094 case ISD::SETULT: return X86::COND_B;
3095 case ISD::SETUGT: return X86::COND_A;
3096 case ISD::SETULE: return X86::COND_BE;
3097 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003098 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003099 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003100
Chris Lattner4c78e022008-12-23 23:42:27 +00003101 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003102
Chris Lattner4c78e022008-12-23 23:42:27 +00003103 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003104 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3105 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003106 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3107 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003108 }
3109
Chris Lattner4c78e022008-12-23 23:42:27 +00003110 switch (SetCCOpcode) {
3111 default: break;
3112 case ISD::SETOLT:
3113 case ISD::SETOLE:
3114 case ISD::SETUGT:
3115 case ISD::SETUGE:
3116 std::swap(LHS, RHS);
3117 break;
3118 }
3119
3120 // On a floating point condition, the flags are set as follows:
3121 // ZF PF CF op
3122 // 0 | 0 | 0 | X > Y
3123 // 0 | 0 | 1 | X < Y
3124 // 1 | 0 | 0 | X == Y
3125 // 1 | 1 | 1 | unordered
3126 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003127 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003128 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003129 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003130 case ISD::SETOLT: // flipped
3131 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003132 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003133 case ISD::SETOLE: // flipped
3134 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003135 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003136 case ISD::SETUGT: // flipped
3137 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003138 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003139 case ISD::SETUGE: // flipped
3140 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003141 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003142 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003143 case ISD::SETNE: return X86::COND_NE;
3144 case ISD::SETUO: return X86::COND_P;
3145 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003146 case ISD::SETOEQ:
3147 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003148 }
Evan Chengd9558e02006-01-06 00:43:03 +00003149}
3150
Evan Cheng4a460802006-01-11 00:33:36 +00003151/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3152/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003153/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003154static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003155 switch (X86CC) {
3156 default:
3157 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003158 case X86::COND_B:
3159 case X86::COND_BE:
3160 case X86::COND_E:
3161 case X86::COND_P:
3162 case X86::COND_A:
3163 case X86::COND_AE:
3164 case X86::COND_NE:
3165 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003166 return true;
3167 }
3168}
3169
Evan Chengeb2f9692009-10-27 19:56:55 +00003170/// isFPImmLegal - Returns true if the target can instruction select the
3171/// specified FP immediate natively. If false, the legalizer will
3172/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003173bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003174 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3175 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3176 return true;
3177 }
3178 return false;
3179}
3180
Nate Begeman9008ca62009-04-27 18:41:29 +00003181/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3182/// the specified range (L, H].
3183static bool isUndefOrInRange(int Val, int Low, int Hi) {
3184 return (Val < 0) || (Val >= Low && Val < Hi);
3185}
3186
3187/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3188/// specified value.
3189static bool isUndefOrEqual(int Val, int CmpVal) {
3190 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003191 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003193}
3194
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003195/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003196/// from position Pos and ending in Pos+Size, falls within the specified
3197/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003198static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003199 unsigned Pos, unsigned Size, int Low) {
3200 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003201 if (!isUndefOrEqual(Mask[i], Low))
3202 return false;
3203 return true;
3204}
3205
Nate Begeman9008ca62009-04-27 18:41:29 +00003206/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3207/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3208/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003209static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003210 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003212 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 return (Mask[0] < 2 && Mask[1] < 2);
3214 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003215}
3216
Nate Begeman9008ca62009-04-27 18:41:29 +00003217/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3218/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003219static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3220 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003221 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003222
Nate Begeman9008ca62009-04-27 18:41:29 +00003223 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003224 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3225 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003226
Evan Cheng506d3df2006-03-29 23:07:14 +00003227 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003228 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003229 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003230 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003231
Craig Toppera9a568a2012-05-02 08:03:44 +00003232 if (VT == MVT::v16i16) {
3233 // Lower quadword copied in order or undef.
3234 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3235 return false;
3236
3237 // Upper quadword shuffled.
3238 for (unsigned i = 12; i != 16; ++i)
3239 if (!isUndefOrInRange(Mask[i], 12, 16))
3240 return false;
3241 }
3242
Evan Cheng506d3df2006-03-29 23:07:14 +00003243 return true;
3244}
3245
Nate Begeman9008ca62009-04-27 18:41:29 +00003246/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3247/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003248static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3249 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003250 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003251
Rafael Espindola15684b22009-04-24 12:40:33 +00003252 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003253 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3254 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003255
Rafael Espindola15684b22009-04-24 12:40:33 +00003256 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003257 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003258 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003259 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003260
Craig Toppera9a568a2012-05-02 08:03:44 +00003261 if (VT == MVT::v16i16) {
3262 // Upper quadword copied in order.
3263 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3264 return false;
3265
3266 // Lower quadword shuffled.
3267 for (unsigned i = 8; i != 12; ++i)
3268 if (!isUndefOrInRange(Mask[i], 8, 12))
3269 return false;
3270 }
3271
Rafael Espindola15684b22009-04-24 12:40:33 +00003272 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003273}
3274
Nate Begemana09008b2009-10-19 02:17:23 +00003275/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3276/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003277static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3278 const X86Subtarget *Subtarget) {
3279 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3280 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003281 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003282
Craig Topper0e2037b2012-01-20 05:53:00 +00003283 unsigned NumElts = VT.getVectorNumElements();
3284 unsigned NumLanes = VT.getSizeInBits()/128;
3285 unsigned NumLaneElts = NumElts/NumLanes;
3286
3287 // Do not handle 64-bit element shuffles with palignr.
3288 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003289 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003290
Craig Topper0e2037b2012-01-20 05:53:00 +00003291 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3292 unsigned i;
3293 for (i = 0; i != NumLaneElts; ++i) {
3294 if (Mask[i+l] >= 0)
3295 break;
3296 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003297
Craig Topper0e2037b2012-01-20 05:53:00 +00003298 // Lane is all undef, go to next lane
3299 if (i == NumLaneElts)
3300 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003301
Craig Topper0e2037b2012-01-20 05:53:00 +00003302 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003303
Craig Topper0e2037b2012-01-20 05:53:00 +00003304 // Make sure its in this lane in one of the sources
3305 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3306 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003307 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003308
3309 // If not lane 0, then we must match lane 0
3310 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3311 return false;
3312
3313 // Correct second source to be contiguous with first source
3314 if (Start >= (int)NumElts)
3315 Start -= NumElts - NumLaneElts;
3316
3317 // Make sure we're shifting in the right direction.
3318 if (Start <= (int)(i+l))
3319 return false;
3320
3321 Start -= i;
3322
3323 // Check the rest of the elements to see if they are consecutive.
3324 for (++i; i != NumLaneElts; ++i) {
3325 int Idx = Mask[i+l];
3326
3327 // Make sure its in this lane
3328 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3329 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3330 return false;
3331
3332 // If not lane 0, then we must match lane 0
3333 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3334 return false;
3335
3336 if (Idx >= (int)NumElts)
3337 Idx -= NumElts - NumLaneElts;
3338
3339 if (!isUndefOrEqual(Idx, Start+i))
3340 return false;
3341
3342 }
Nate Begemana09008b2009-10-19 02:17:23 +00003343 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003344
Nate Begemana09008b2009-10-19 02:17:23 +00003345 return true;
3346}
3347
Craig Topper1a7700a2012-01-19 08:19:12 +00003348/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3349/// the two vector operands have swapped position.
3350static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3351 unsigned NumElems) {
3352 for (unsigned i = 0; i != NumElems; ++i) {
3353 int idx = Mask[i];
3354 if (idx < 0)
3355 continue;
3356 else if (idx < (int)NumElems)
3357 Mask[i] = idx + NumElems;
3358 else
3359 Mask[i] = idx - NumElems;
3360 }
3361}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003362
Craig Topper1a7700a2012-01-19 08:19:12 +00003363/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3364/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3365/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3366/// reverse of what x86 shuffles want.
3367static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3368 bool Commuted = false) {
3369 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003370 return false;
3371
Craig Topper1a7700a2012-01-19 08:19:12 +00003372 unsigned NumElems = VT.getVectorNumElements();
3373 unsigned NumLanes = VT.getSizeInBits()/128;
3374 unsigned NumLaneElems = NumElems/NumLanes;
3375
3376 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003377 return false;
3378
3379 // VSHUFPSY divides the resulting vector into 4 chunks.
3380 // The sources are also splitted into 4 chunks, and each destination
3381 // chunk must come from a different source chunk.
3382 //
3383 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3384 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3385 //
3386 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3387 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3388 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003389 // VSHUFPDY divides the resulting vector into 4 chunks.
3390 // The sources are also splitted into 4 chunks, and each destination
3391 // chunk must come from a different source chunk.
3392 //
3393 // SRC1 => X3 X2 X1 X0
3394 // SRC2 => Y3 Y2 Y1 Y0
3395 //
3396 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3397 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003398 unsigned HalfLaneElems = NumLaneElems/2;
3399 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3400 for (unsigned i = 0; i != NumLaneElems; ++i) {
3401 int Idx = Mask[i+l];
3402 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3403 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3404 return false;
3405 // For VSHUFPSY, the mask of the second half must be the same as the
3406 // first but with the appropriate offsets. This works in the same way as
3407 // VPERMILPS works with masks.
3408 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3409 continue;
3410 if (!isUndefOrEqual(Idx, Mask[i]+l))
3411 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003412 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003413 }
3414
3415 return true;
3416}
3417
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003418/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3419/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003420static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003421 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003422 return false;
3423
Craig Topper7a9a28b2012-08-12 02:23:29 +00003424 unsigned NumElems = VT.getVectorNumElements();
3425
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003426 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003427 return false;
3428
Evan Cheng2064a2b2006-03-28 06:50:32 +00003429 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003430 return isUndefOrEqual(Mask[0], 6) &&
3431 isUndefOrEqual(Mask[1], 7) &&
3432 isUndefOrEqual(Mask[2], 2) &&
3433 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003434}
3435
Nate Begeman0b10b912009-11-07 23:17:15 +00003436/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3437/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3438/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003439static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003440 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003441 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003442
Craig Topper7a9a28b2012-08-12 02:23:29 +00003443 unsigned NumElems = VT.getVectorNumElements();
3444
Nate Begeman0b10b912009-11-07 23:17:15 +00003445 if (NumElems != 4)
3446 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003447
Craig Topperdd637ae2012-02-19 05:41:45 +00003448 return isUndefOrEqual(Mask[0], 2) &&
3449 isUndefOrEqual(Mask[1], 3) &&
3450 isUndefOrEqual(Mask[2], 2) &&
3451 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003452}
3453
Evan Cheng5ced1d82006-04-06 23:23:56 +00003454/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3455/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003456static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003457 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003458 return false;
3459
Craig Topperdd637ae2012-02-19 05:41:45 +00003460 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003461
Evan Cheng5ced1d82006-04-06 23:23:56 +00003462 if (NumElems != 2 && NumElems != 4)
3463 return false;
3464
Chad Rosier238ae312012-04-30 17:47:15 +00003465 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003466 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003467 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003468
Chad Rosier238ae312012-04-30 17:47:15 +00003469 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003470 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003471 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003472
3473 return true;
3474}
3475
Nate Begeman0b10b912009-11-07 23:17:15 +00003476/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3477/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003478static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003479 if (!VT.is128BitVector())
3480 return false;
3481
Craig Topperdd637ae2012-02-19 05:41:45 +00003482 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003483
Craig Topper7a9a28b2012-08-12 02:23:29 +00003484 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003485 return false;
3486
Chad Rosier238ae312012-04-30 17:47:15 +00003487 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003488 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003489 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003490
Chad Rosier238ae312012-04-30 17:47:15 +00003491 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3492 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003493 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003494
3495 return true;
3496}
3497
Elena Demikhovsky15963732012-06-26 08:04:10 +00003498//
3499// Some special combinations that can be optimized.
3500//
3501static
3502SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3503 SelectionDAG &DAG) {
3504 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003505 DebugLoc dl = SVOp->getDebugLoc();
3506
3507 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3508 return SDValue();
3509
3510 ArrayRef<int> Mask = SVOp->getMask();
3511
3512 // These are the special masks that may be optimized.
3513 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3514 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3515 bool MatchEvenMask = true;
3516 bool MatchOddMask = true;
3517 for (int i=0; i<8; ++i) {
3518 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3519 MatchEvenMask = false;
3520 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3521 MatchOddMask = false;
3522 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003523
Elena Demikhovsky32510202012-09-04 12:49:02 +00003524 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003525 return SDValue();
Elena Demikhovsky32510202012-09-04 12:49:02 +00003526
Elena Demikhovsky15963732012-06-26 08:04:10 +00003527 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3528
Elena Demikhovsky32510202012-09-04 12:49:02 +00003529 SDValue Op0 = SVOp->getOperand(0);
3530 SDValue Op1 = SVOp->getOperand(1);
3531
3532 if (MatchEvenMask) {
3533 // Shift the second operand right to 32 bits.
3534 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3535 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3536 } else {
3537 // Shift the first operand left to 32 bits.
3538 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3539 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3540 }
3541 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3542 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003543}
3544
Evan Cheng0038e592006-03-28 00:39:58 +00003545/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3546/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003547static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003548 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003549 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003550
3551 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3552 "Unsupported vector type for unpckh");
3553
Craig Topper6347e862011-11-21 06:57:39 +00003554 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003555 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003556 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003557
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003558 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3559 // independently on 128-bit lanes.
3560 unsigned NumLanes = VT.getSizeInBits()/128;
3561 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003562
Craig Topper94438ba2011-12-16 08:06:31 +00003563 for (unsigned l = 0; l != NumLanes; ++l) {
3564 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3565 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003566 i += 2, ++j) {
3567 int BitI = Mask[i];
3568 int BitI1 = Mask[i+1];
3569 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003570 return false;
David Greenea20244d2011-03-02 17:23:43 +00003571 if (V2IsSplat) {
3572 if (!isUndefOrEqual(BitI1, NumElts))
3573 return false;
3574 } else {
3575 if (!isUndefOrEqual(BitI1, j + NumElts))
3576 return false;
3577 }
Evan Cheng39623da2006-04-20 08:58:49 +00003578 }
Evan Cheng0038e592006-03-28 00:39:58 +00003579 }
David Greenea20244d2011-03-02 17:23:43 +00003580
Evan Cheng0038e592006-03-28 00:39:58 +00003581 return true;
3582}
3583
Evan Cheng4fcb9222006-03-28 02:43:26 +00003584/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3585/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003586static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003587 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003588 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003589
3590 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3591 "Unsupported vector type for unpckh");
3592
Craig Topper6347e862011-11-21 06:57:39 +00003593 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003594 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003595 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003596
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003597 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3598 // independently on 128-bit lanes.
3599 unsigned NumLanes = VT.getSizeInBits()/128;
3600 unsigned NumLaneElts = NumElts/NumLanes;
3601
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003602 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003603 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3604 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003605 int BitI = Mask[i];
3606 int BitI1 = Mask[i+1];
3607 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003608 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003609 if (V2IsSplat) {
3610 if (isUndefOrEqual(BitI1, NumElts))
3611 return false;
3612 } else {
3613 if (!isUndefOrEqual(BitI1, j+NumElts))
3614 return false;
3615 }
Evan Cheng39623da2006-04-20 08:58:49 +00003616 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003617 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003618 return true;
3619}
3620
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003621/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3622/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3623/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003624static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003625 bool HasAVX2) {
3626 unsigned NumElts = VT.getVectorNumElements();
3627
3628 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3629 "Unsupported vector type for unpckh");
3630
3631 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3632 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003633 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003634
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003635 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3636 // FIXME: Need a better way to get rid of this, there's no latency difference
3637 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3638 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003639 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003640 return false;
3641
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003642 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3643 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003644 unsigned NumLanes = VT.getSizeInBits()/128;
3645 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003646
Craig Topper94438ba2011-12-16 08:06:31 +00003647 for (unsigned l = 0; l != NumLanes; ++l) {
3648 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3649 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003650 i += 2, ++j) {
3651 int BitI = Mask[i];
3652 int BitI1 = Mask[i+1];
3653
3654 if (!isUndefOrEqual(BitI, j))
3655 return false;
3656 if (!isUndefOrEqual(BitI1, j))
3657 return false;
3658 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003659 }
David Greenea20244d2011-03-02 17:23:43 +00003660
Rafael Espindola15684b22009-04-24 12:40:33 +00003661 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003662}
3663
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003664/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3665/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3666/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003667static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003668 unsigned NumElts = VT.getVectorNumElements();
3669
3670 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3671 "Unsupported vector type for unpckh");
3672
3673 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3674 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003675 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003676
Craig Topper94438ba2011-12-16 08:06:31 +00003677 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3678 // independently on 128-bit lanes.
3679 unsigned NumLanes = VT.getSizeInBits()/128;
3680 unsigned NumLaneElts = NumElts/NumLanes;
3681
3682 for (unsigned l = 0; l != NumLanes; ++l) {
3683 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3684 i != (l+1)*NumLaneElts; i += 2, ++j) {
3685 int BitI = Mask[i];
3686 int BitI1 = Mask[i+1];
3687 if (!isUndefOrEqual(BitI, j))
3688 return false;
3689 if (!isUndefOrEqual(BitI1, j))
3690 return false;
3691 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003692 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003693 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003694}
3695
Evan Cheng017dcc62006-04-21 01:05:10 +00003696/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3697/// specifies a shuffle of elements that is suitable for input to MOVSS,
3698/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003699static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003700 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003701 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003702 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003703 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003704
Craig Topperc612d792012-01-02 09:17:37 +00003705 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003706
Nate Begeman9008ca62009-04-27 18:41:29 +00003707 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003708 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003709
Craig Topperc612d792012-01-02 09:17:37 +00003710 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003711 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003712 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003713
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003714 return true;
3715}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003716
Craig Topper70b883b2011-11-28 10:14:51 +00003717/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003718/// as permutations between 128-bit chunks or halves. As an example: this
3719/// shuffle bellow:
3720/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3721/// The first half comes from the second half of V1 and the second half from the
3722/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003723static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003724 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003725 return false;
3726
3727 // The shuffle result is divided into half A and half B. In total the two
3728 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3729 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003730 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003731 bool MatchA = false, MatchB = false;
3732
3733 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003734 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003735 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3736 MatchA = true;
3737 break;
3738 }
3739 }
3740
3741 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003742 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003743 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3744 MatchB = true;
3745 break;
3746 }
3747 }
3748
3749 return MatchA && MatchB;
3750}
3751
Craig Topper70b883b2011-11-28 10:14:51 +00003752/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3753/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003754static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003755 EVT VT = SVOp->getValueType(0);
3756
Craig Topperc612d792012-01-02 09:17:37 +00003757 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003758
Craig Topperc612d792012-01-02 09:17:37 +00003759 unsigned FstHalf = 0, SndHalf = 0;
3760 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003761 if (SVOp->getMaskElt(i) > 0) {
3762 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3763 break;
3764 }
3765 }
Craig Topperc612d792012-01-02 09:17:37 +00003766 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003767 if (SVOp->getMaskElt(i) > 0) {
3768 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3769 break;
3770 }
3771 }
3772
3773 return (FstHalf | (SndHalf << 4));
3774}
3775
Craig Topper70b883b2011-11-28 10:14:51 +00003776/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003777/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3778/// Note that VPERMIL mask matching is different depending whether theunderlying
3779/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3780/// to the same elements of the low, but to the higher half of the source.
3781/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003782/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003783static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003784 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003785 return false;
3786
Craig Topperc612d792012-01-02 09:17:37 +00003787 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003788 // Only match 256-bit with 32/64-bit types
3789 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003790 return false;
3791
Craig Topperc612d792012-01-02 09:17:37 +00003792 unsigned NumLanes = VT.getSizeInBits()/128;
3793 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003794 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003795 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003796 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003797 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003798 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003799 continue;
3800 // VPERMILPS handling
3801 if (Mask[i] < 0)
3802 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003803 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003804 return false;
3805 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003806 }
3807
3808 return true;
3809}
3810
Craig Topper5aaffa82012-02-19 02:53:47 +00003811/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003812/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003813/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003814static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003815 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003816 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003817 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003818
3819 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003820 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003821 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003822
Nate Begeman9008ca62009-04-27 18:41:29 +00003823 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003824 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003825
Craig Topperc612d792012-01-02 09:17:37 +00003826 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003827 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3828 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3829 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003830 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003831
Evan Cheng39623da2006-04-20 08:58:49 +00003832 return true;
3833}
3834
Evan Chengd9539472006-04-14 21:59:03 +00003835/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3836/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003837/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003838static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003839 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003840 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003841 return false;
3842
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003843 unsigned NumElems = VT.getVectorNumElements();
3844
3845 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3846 (VT.getSizeInBits() == 256 && NumElems != 8))
3847 return false;
3848
3849 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003850 for (unsigned i = 0; i != NumElems; i += 2)
3851 if (!isUndefOrEqual(Mask[i], i+1) ||
3852 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003853 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003854
3855 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003856}
3857
3858/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3859/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003860/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003861static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003862 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003863 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003864 return false;
3865
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003866 unsigned NumElems = VT.getVectorNumElements();
3867
3868 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3869 (VT.getSizeInBits() == 256 && NumElems != 8))
3870 return false;
3871
3872 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003873 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003874 if (!isUndefOrEqual(Mask[i], i) ||
3875 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003876 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003877
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003878 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003879}
3880
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003881/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3882/// specifies a shuffle of elements that is suitable for input to 256-bit
3883/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003884static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003885 if (!HasAVX || !VT.is256BitVector())
3886 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003887
Craig Topper7a9a28b2012-08-12 02:23:29 +00003888 unsigned NumElts = VT.getVectorNumElements();
3889 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003890 return false;
3891
Craig Topperc612d792012-01-02 09:17:37 +00003892 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003893 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003894 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003895 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003896 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003897 return false;
3898 return true;
3899}
3900
Evan Cheng0b457f02008-09-25 20:50:48 +00003901/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003902/// specifies a shuffle of elements that is suitable for input to 128-bit
3903/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003904static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003905 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003906 return false;
3907
Craig Topperc612d792012-01-02 09:17:37 +00003908 unsigned e = VT.getVectorNumElements() / 2;
3909 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003910 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003911 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003912 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003913 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003914 return false;
3915 return true;
3916}
3917
David Greenec38a03e2011-02-03 15:50:00 +00003918/// isVEXTRACTF128Index - Return true if the specified
3919/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3920/// suitable for input to VEXTRACTF128.
3921bool X86::isVEXTRACTF128Index(SDNode *N) {
3922 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3923 return false;
3924
3925 // The index should be aligned on a 128-bit boundary.
3926 uint64_t Index =
3927 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3928
3929 unsigned VL = N->getValueType(0).getVectorNumElements();
3930 unsigned VBits = N->getValueType(0).getSizeInBits();
3931 unsigned ElSize = VBits / VL;
3932 bool Result = (Index * ElSize) % 128 == 0;
3933
3934 return Result;
3935}
3936
David Greeneccacdc12011-02-04 16:08:29 +00003937/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3938/// operand specifies a subvector insert that is suitable for input to
3939/// VINSERTF128.
3940bool X86::isVINSERTF128Index(SDNode *N) {
3941 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3942 return false;
3943
3944 // The index should be aligned on a 128-bit boundary.
3945 uint64_t Index =
3946 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3947
3948 unsigned VL = N->getValueType(0).getVectorNumElements();
3949 unsigned VBits = N->getValueType(0).getSizeInBits();
3950 unsigned ElSize = VBits / VL;
3951 bool Result = (Index * ElSize) % 128 == 0;
3952
3953 return Result;
3954}
3955
Evan Cheng63d33002006-03-22 08:01:21 +00003956/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003957/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003958/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003959static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003960 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003961
Craig Topper1a7700a2012-01-19 08:19:12 +00003962 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3963 "Unsupported vector type for PSHUF/SHUFP");
3964
3965 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3966 // independently on 128-bit lanes.
3967 unsigned NumElts = VT.getVectorNumElements();
3968 unsigned NumLanes = VT.getSizeInBits()/128;
3969 unsigned NumLaneElts = NumElts/NumLanes;
3970
3971 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3972 "Only supports 2 or 4 elements per lane");
3973
3974 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003975 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003976 for (unsigned i = 0; i != NumElts; ++i) {
3977 int Elt = N->getMaskElt(i);
3978 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003979 Elt &= NumLaneElts - 1;
3980 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003981 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003982 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003983
Evan Cheng63d33002006-03-22 08:01:21 +00003984 return Mask;
3985}
3986
Evan Cheng506d3df2006-03-29 23:07:14 +00003987/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003988/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00003989static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00003990 EVT VT = N->getValueType(0);
3991
3992 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
3993 "Unsupported vector type for PSHUFHW");
3994
3995 unsigned NumElts = VT.getVectorNumElements();
3996
Evan Cheng506d3df2006-03-29 23:07:14 +00003997 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00003998 for (unsigned l = 0; l != NumElts; l += 8) {
3999 // 8 nodes per lane, but we only care about the last 4.
4000 for (unsigned i = 0; i < 4; ++i) {
4001 int Elt = N->getMaskElt(l+i+4);
4002 if (Elt < 0) continue;
4003 Elt &= 0x3; // only 2-bits.
4004 Mask |= Elt << (i * 2);
4005 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004006 }
Craig Topper6b28d352012-05-03 07:12:59 +00004007
Evan Cheng506d3df2006-03-29 23:07:14 +00004008 return Mask;
4009}
4010
4011/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004012/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004013static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004014 EVT VT = N->getValueType(0);
4015
4016 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4017 "Unsupported vector type for PSHUFHW");
4018
4019 unsigned NumElts = VT.getVectorNumElements();
4020
Evan Cheng506d3df2006-03-29 23:07:14 +00004021 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004022 for (unsigned l = 0; l != NumElts; l += 8) {
4023 // 8 nodes per lane, but we only care about the first 4.
4024 for (unsigned i = 0; i < 4; ++i) {
4025 int Elt = N->getMaskElt(l+i);
4026 if (Elt < 0) continue;
4027 Elt &= 0x3; // only 2-bits
4028 Mask |= Elt << (i * 2);
4029 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004030 }
Craig Topper6b28d352012-05-03 07:12:59 +00004031
Evan Cheng506d3df2006-03-29 23:07:14 +00004032 return Mask;
4033}
4034
Nate Begemana09008b2009-10-19 02:17:23 +00004035/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4036/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004037static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4038 EVT VT = SVOp->getValueType(0);
4039 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004040
Craig Topper0e2037b2012-01-20 05:53:00 +00004041 unsigned NumElts = VT.getVectorNumElements();
4042 unsigned NumLanes = VT.getSizeInBits()/128;
4043 unsigned NumLaneElts = NumElts/NumLanes;
4044
4045 int Val = 0;
4046 unsigned i;
4047 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004048 Val = SVOp->getMaskElt(i);
4049 if (Val >= 0)
4050 break;
4051 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004052 if (Val >= (int)NumElts)
4053 Val -= NumElts - NumLaneElts;
4054
Eli Friedman63f8dde2011-07-25 21:36:45 +00004055 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004056 return (Val - i) * EltSize;
4057}
4058
David Greenec38a03e2011-02-03 15:50:00 +00004059/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4060/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4061/// instructions.
4062unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4063 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4064 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4065
4066 uint64_t Index =
4067 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4068
4069 EVT VecVT = N->getOperand(0).getValueType();
4070 EVT ElVT = VecVT.getVectorElementType();
4071
4072 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004073 return Index / NumElemsPerChunk;
4074}
4075
David Greeneccacdc12011-02-04 16:08:29 +00004076/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4077/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4078/// instructions.
4079unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4080 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4081 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4082
4083 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004084 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004085
4086 EVT VecVT = N->getValueType(0);
4087 EVT ElVT = VecVT.getVectorElementType();
4088
4089 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004090 return Index / NumElemsPerChunk;
4091}
4092
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004093/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4094/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4095/// Handles 256-bit.
4096static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4097 EVT VT = N->getValueType(0);
4098
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004099 unsigned NumElts = VT.getVectorNumElements();
4100
Craig Topper095c5282012-04-15 23:48:57 +00004101 assert((VT.is256BitVector() && NumElts == 4) &&
4102 "Unsupported vector type for VPERMQ/VPERMPD");
4103
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004104 unsigned Mask = 0;
4105 for (unsigned i = 0; i != NumElts; ++i) {
4106 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004107 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004108 continue;
4109 Mask |= Elt << (i*2);
4110 }
4111
4112 return Mask;
4113}
Evan Cheng37b73872009-07-30 08:33:02 +00004114/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4115/// constant +0.0.
4116bool X86::isZeroNode(SDValue Elt) {
4117 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004118 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004119 (isa<ConstantFPSDNode>(Elt) &&
4120 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4121}
4122
Nate Begeman9008ca62009-04-27 18:41:29 +00004123/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4124/// their permute mask.
4125static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4126 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004127 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004128 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004129 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004130
Nate Begeman5a5ca152009-04-29 05:20:52 +00004131 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004132 int Idx = SVOp->getMaskElt(i);
4133 if (Idx >= 0) {
4134 if (Idx < (int)NumElems)
4135 Idx += NumElems;
4136 else
4137 Idx -= NumElems;
4138 }
4139 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004140 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004141 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4142 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004143}
4144
Evan Cheng533a0aa2006-04-19 20:35:22 +00004145/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4146/// match movhlps. The lower half elements should come from upper half of
4147/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004148/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004149static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004150 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004151 return false;
4152 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004153 return false;
4154 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004155 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004156 return false;
4157 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004158 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004159 return false;
4160 return true;
4161}
4162
Evan Cheng5ced1d82006-04-06 23:23:56 +00004163/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004164/// is promoted to a vector. It also returns the LoadSDNode by reference if
4165/// required.
4166static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004167 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4168 return false;
4169 N = N->getOperand(0).getNode();
4170 if (!ISD::isNON_EXTLoad(N))
4171 return false;
4172 if (LD)
4173 *LD = cast<LoadSDNode>(N);
4174 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004175}
4176
Dan Gohman65fd6562011-11-03 21:49:52 +00004177// Test whether the given value is a vector value which will be legalized
4178// into a load.
4179static bool WillBeConstantPoolLoad(SDNode *N) {
4180 if (N->getOpcode() != ISD::BUILD_VECTOR)
4181 return false;
4182
4183 // Check for any non-constant elements.
4184 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4185 switch (N->getOperand(i).getNode()->getOpcode()) {
4186 case ISD::UNDEF:
4187 case ISD::ConstantFP:
4188 case ISD::Constant:
4189 break;
4190 default:
4191 return false;
4192 }
4193
4194 // Vectors of all-zeros and all-ones are materialized with special
4195 // instructions rather than being loaded.
4196 return !ISD::isBuildVectorAllZeros(N) &&
4197 !ISD::isBuildVectorAllOnes(N);
4198}
4199
Evan Cheng533a0aa2006-04-19 20:35:22 +00004200/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4201/// match movlp{s|d}. The lower half elements should come from lower half of
4202/// V1 (and in order), and the upper half elements should come from the upper
4203/// half of V2 (and in order). And since V1 will become the source of the
4204/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004205static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004206 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004207 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004208 return false;
4209
Evan Cheng466685d2006-10-09 20:57:25 +00004210 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004211 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004212 // Is V2 is a vector load, don't do this transformation. We will try to use
4213 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004214 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004215 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004216
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004217 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004218
Evan Cheng533a0aa2006-04-19 20:35:22 +00004219 if (NumElems != 2 && NumElems != 4)
4220 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004221 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004222 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004223 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004224 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004225 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004226 return false;
4227 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004228}
4229
Evan Cheng39623da2006-04-20 08:58:49 +00004230/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4231/// all the same.
4232static bool isSplatVector(SDNode *N) {
4233 if (N->getOpcode() != ISD::BUILD_VECTOR)
4234 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004235
Dan Gohman475871a2008-07-27 21:46:04 +00004236 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004237 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4238 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004239 return false;
4240 return true;
4241}
4242
Evan Cheng213d2cf2007-05-17 18:45:50 +00004243/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004244/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004245/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004246static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004247 SDValue V1 = N->getOperand(0);
4248 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004249 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4250 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004251 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004252 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004253 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004254 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4255 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004256 if (Opc != ISD::BUILD_VECTOR ||
4257 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004258 return false;
4259 } else if (Idx >= 0) {
4260 unsigned Opc = V1.getOpcode();
4261 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4262 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004263 if (Opc != ISD::BUILD_VECTOR ||
4264 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004265 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004266 }
4267 }
4268 return true;
4269}
4270
4271/// getZeroVector - Returns a vector of specified type with all zero elements.
4272///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004273static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004274 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004275 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004276 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004277
Dale Johannesen0488fb62010-09-30 23:57:10 +00004278 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004279 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004280 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004281 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004282 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004283 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4284 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4285 } else { // SSE1
4286 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4287 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4288 }
Craig Topper9d352402012-04-23 07:24:41 +00004289 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004290 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004291 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4292 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4293 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4294 } else {
4295 // 256-bit logic and arithmetic instructions in AVX are all
4296 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4297 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4298 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4299 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4300 }
Craig Topper9d352402012-04-23 07:24:41 +00004301 } else
4302 llvm_unreachable("Unexpected vector type");
4303
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004304 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004305}
4306
Chris Lattner8a594482007-11-25 00:24:49 +00004307/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004308/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4309/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4310/// Then bitcast to their original type, ensuring they get CSE'd.
4311static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4312 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004313 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004314 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004315
Owen Anderson825b72b2009-08-11 20:47:22 +00004316 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004317 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004318 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004319 if (HasAVX2) { // AVX2
4320 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4321 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4322 } else { // AVX
4323 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004324 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004325 }
Craig Topper9d352402012-04-23 07:24:41 +00004326 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004327 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004328 } else
4329 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004330
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004331 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004332}
4333
Evan Cheng39623da2006-04-20 08:58:49 +00004334/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4335/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004336static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004337 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004338 if (Mask[i] > (int)NumElems) {
4339 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004340 }
Evan Cheng39623da2006-04-20 08:58:49 +00004341 }
Evan Cheng39623da2006-04-20 08:58:49 +00004342}
4343
Evan Cheng017dcc62006-04-21 01:05:10 +00004344/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4345/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004346static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004347 SDValue V2) {
4348 unsigned NumElems = VT.getVectorNumElements();
4349 SmallVector<int, 8> Mask;
4350 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004351 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004352 Mask.push_back(i);
4353 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004354}
4355
Nate Begeman9008ca62009-04-27 18:41:29 +00004356/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004357static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004358 SDValue V2) {
4359 unsigned NumElems = VT.getVectorNumElements();
4360 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004361 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004362 Mask.push_back(i);
4363 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004364 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004366}
4367
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004368/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004369static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 SDValue V2) {
4371 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004372 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004373 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 Mask.push_back(i + Half);
4375 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004376 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004377 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004378}
4379
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004380// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004381// a generic shuffle instruction because the target has no such instructions.
4382// Generate shuffles which repeat i16 and i8 several times until they can be
4383// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004384static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004385 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004387 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004388
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 while (NumElems > 4) {
4390 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004391 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004393 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004394 EltNo -= NumElems/2;
4395 }
4396 NumElems >>= 1;
4397 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004398 return V;
4399}
Eric Christopherfd179292009-08-27 18:07:15 +00004400
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004401/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4402static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4403 EVT VT = V.getValueType();
4404 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004405 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004406
Craig Topper9d352402012-04-23 07:24:41 +00004407 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004408 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004409 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004410 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4411 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004412 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004413 // To use VPERMILPS to splat scalars, the second half of indicies must
4414 // refer to the higher part, which is a duplication of the lower one,
4415 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004416 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4417 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004418
4419 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4420 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4421 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004422 } else
4423 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004424
4425 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4426}
4427
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004428/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004429static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4430 EVT SrcVT = SV->getValueType(0);
4431 SDValue V1 = SV->getOperand(0);
4432 DebugLoc dl = SV->getDebugLoc();
4433
4434 int EltNo = SV->getSplatIndex();
4435 int NumElems = SrcVT.getVectorNumElements();
4436 unsigned Size = SrcVT.getSizeInBits();
4437
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004438 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4439 "Unknown how to promote splat for type");
4440
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004441 // Extract the 128-bit part containing the splat element and update
4442 // the splat element index when it refers to the higher register.
4443 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004444 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4445 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004446 EltNo -= NumElems/2;
4447 }
4448
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004449 // All i16 and i8 vector types can't be used directly by a generic shuffle
4450 // instruction because the target has no such instruction. Generate shuffles
4451 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004452 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004453 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004454 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004455 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004456
4457 // Recreate the 256-bit vector and place the same 128-bit vector
4458 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004459 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004460 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004461 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004462 }
4463
4464 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004465}
4466
Evan Chengba05f722006-04-21 23:03:30 +00004467/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004468/// vector of zero or undef vector. This produces a shuffle where the low
4469/// element of V2 is swizzled into the zero/undef vector, landing at element
4470/// 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 +00004471static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004472 bool IsZero,
4473 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004474 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004475 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004476 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004477 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 unsigned NumElems = VT.getVectorNumElements();
4479 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004480 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004481 // If this is the insertion idx, put the low elt of V2 here.
4482 MaskVec.push_back(i == Idx ? NumElems : i);
4483 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004484}
4485
Craig Toppera1ffc682012-03-20 06:42:26 +00004486/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4487/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004488/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004489static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004490 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004491 unsigned NumElems = VT.getVectorNumElements();
4492 SDValue ImmN;
4493
Craig Topper89f4e662012-03-20 07:17:59 +00004494 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004495 switch(N->getOpcode()) {
4496 case X86ISD::SHUFP:
4497 ImmN = N->getOperand(N->getNumOperands()-1);
4498 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4499 break;
4500 case X86ISD::UNPCKH:
4501 DecodeUNPCKHMask(VT, Mask);
4502 break;
4503 case X86ISD::UNPCKL:
4504 DecodeUNPCKLMask(VT, Mask);
4505 break;
4506 case X86ISD::MOVHLPS:
4507 DecodeMOVHLPSMask(NumElems, Mask);
4508 break;
4509 case X86ISD::MOVLHPS:
4510 DecodeMOVLHPSMask(NumElems, Mask);
4511 break;
4512 case X86ISD::PSHUFD:
4513 case X86ISD::VPERMILP:
4514 ImmN = N->getOperand(N->getNumOperands()-1);
4515 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004516 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004517 break;
4518 case X86ISD::PSHUFHW:
4519 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004520 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004521 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004522 break;
4523 case X86ISD::PSHUFLW:
4524 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004525 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004526 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004527 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004528 case X86ISD::VPERMI:
4529 ImmN = N->getOperand(N->getNumOperands()-1);
4530 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4531 IsUnary = true;
4532 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004533 case X86ISD::MOVSS:
4534 case X86ISD::MOVSD: {
4535 // The index 0 always comes from the first element of the second source,
4536 // this is why MOVSS and MOVSD are used in the first place. The other
4537 // elements come from the other positions of the first source vector
4538 Mask.push_back(NumElems);
4539 for (unsigned i = 1; i != NumElems; ++i) {
4540 Mask.push_back(i);
4541 }
4542 break;
4543 }
4544 case X86ISD::VPERM2X128:
4545 ImmN = N->getOperand(N->getNumOperands()-1);
4546 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004547 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004548 break;
4549 case X86ISD::MOVDDUP:
4550 case X86ISD::MOVLHPD:
4551 case X86ISD::MOVLPD:
4552 case X86ISD::MOVLPS:
4553 case X86ISD::MOVSHDUP:
4554 case X86ISD::MOVSLDUP:
4555 case X86ISD::PALIGN:
4556 // Not yet implemented
4557 return false;
4558 default: llvm_unreachable("unknown target shuffle node");
4559 }
4560
4561 return true;
4562}
4563
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004564/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4565/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004566static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004567 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004568 if (Depth == 6)
4569 return SDValue(); // Limit search depth.
4570
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004571 SDValue V = SDValue(N, 0);
4572 EVT VT = V.getValueType();
4573 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004574
4575 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4576 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004577 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004578
Craig Topper3d092db2012-03-21 02:14:01 +00004579 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004580 return DAG.getUNDEF(VT.getVectorElementType());
4581
Craig Topperd156dc12012-02-06 07:17:51 +00004582 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004583 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4584 : SV->getOperand(1);
4585 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004586 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004587
4588 // Recurse into target specific vector shuffles to find scalars.
4589 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004590 MVT ShufVT = V.getValueType().getSimpleVT();
4591 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004592 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004593 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004594 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004595
Craig Topperd978c542012-05-06 19:46:21 +00004596 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004597 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004598
Craig Topper3d092db2012-03-21 02:14:01 +00004599 int Elt = ShuffleMask[Index];
4600 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004601 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004602
Craig Topper3d092db2012-03-21 02:14:01 +00004603 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004604 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004605 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004606 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004607 }
4608
4609 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004610 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004611 V = V.getOperand(0);
4612 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004613 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004614
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004615 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004616 return SDValue();
4617 }
4618
4619 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4620 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004621 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004622
4623 if (V.getOpcode() == ISD::BUILD_VECTOR)
4624 return V.getOperand(Index);
4625
4626 return SDValue();
4627}
4628
4629/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4630/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004631/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004632static
Craig Topper3d092db2012-03-21 02:14:01 +00004633unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004634 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004635 unsigned i;
4636 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004637 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004638 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004639 if (!(Elt.getNode() &&
4640 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4641 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004642 }
4643
4644 return i;
4645}
4646
Craig Topper3d092db2012-03-21 02:14:01 +00004647/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4648/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4650static
Craig Topper3d092db2012-03-21 02:14:01 +00004651bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4652 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4653 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004654 bool SeenV1 = false;
4655 bool SeenV2 = false;
4656
Craig Topper3d092db2012-03-21 02:14:01 +00004657 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004658 int Idx = SVOp->getMaskElt(i);
4659 // Ignore undef indicies
4660 if (Idx < 0)
4661 continue;
4662
Craig Topper3d092db2012-03-21 02:14:01 +00004663 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004664 SeenV1 = true;
4665 else
4666 SeenV2 = true;
4667
4668 // Only accept consecutive elements from the same vector
4669 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4670 return false;
4671 }
4672
4673 OpNum = SeenV1 ? 0 : 1;
4674 return true;
4675}
4676
4677/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4678/// logical left shift of a vector.
4679static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4680 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4681 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4682 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4683 false /* check zeros from right */, DAG);
4684 unsigned OpSrc;
4685
4686 if (!NumZeros)
4687 return false;
4688
4689 // Considering the elements in the mask that are not consecutive zeros,
4690 // check if they consecutively come from only one of the source vectors.
4691 //
4692 // V1 = {X, A, B, C} 0
4693 // \ \ \ /
4694 // vector_shuffle V1, V2 <1, 2, 3, X>
4695 //
4696 if (!isShuffleMaskConsecutive(SVOp,
4697 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004698 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004699 NumZeros, // Where to start looking in the src vector
4700 NumElems, // Number of elements in vector
4701 OpSrc)) // Which source operand ?
4702 return false;
4703
4704 isLeft = false;
4705 ShAmt = NumZeros;
4706 ShVal = SVOp->getOperand(OpSrc);
4707 return true;
4708}
4709
4710/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4711/// logical left shift of a vector.
4712static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4713 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4714 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4715 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4716 true /* check zeros from left */, DAG);
4717 unsigned OpSrc;
4718
4719 if (!NumZeros)
4720 return false;
4721
4722 // Considering the elements in the mask that are not consecutive zeros,
4723 // check if they consecutively come from only one of the source vectors.
4724 //
4725 // 0 { A, B, X, X } = V2
4726 // / \ / /
4727 // vector_shuffle V1, V2 <X, X, 4, 5>
4728 //
4729 if (!isShuffleMaskConsecutive(SVOp,
4730 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004731 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004732 0, // Where to start looking in the src vector
4733 NumElems, // Number of elements in vector
4734 OpSrc)) // Which source operand ?
4735 return false;
4736
4737 isLeft = true;
4738 ShAmt = NumZeros;
4739 ShVal = SVOp->getOperand(OpSrc);
4740 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004741}
4742
4743/// isVectorShift - Returns true if the shuffle can be implemented as a
4744/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004745static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004746 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004747 // Although the logic below support any bitwidth size, there are no
4748 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004749 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004750 return false;
4751
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004752 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4753 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4754 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004755
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004756 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004757}
4758
Evan Chengc78d3b42006-04-24 18:01:45 +00004759/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4760///
Dan Gohman475871a2008-07-27 21:46:04 +00004761static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004762 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004763 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004764 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004765 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004766 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004767 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004768
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004769 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004770 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004771 bool First = true;
4772 for (unsigned i = 0; i < 16; ++i) {
4773 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4774 if (ThisIsNonZero && First) {
4775 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004776 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004777 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004778 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004779 First = false;
4780 }
4781
4782 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004783 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004784 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4785 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004786 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004787 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004788 }
4789 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4791 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4792 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004793 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004794 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004795 } else
4796 ThisElt = LastElt;
4797
Gabor Greifba36cb52008-08-28 21:40:38 +00004798 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004799 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004800 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004801 }
4802 }
4803
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004804 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004805}
4806
Bill Wendlinga348c562007-03-22 18:42:45 +00004807/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004808///
Dan Gohman475871a2008-07-27 21:46:04 +00004809static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004810 unsigned NumNonZero, unsigned NumZero,
4811 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004812 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004813 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004814 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004815 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004816
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004817 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004818 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004819 bool First = true;
4820 for (unsigned i = 0; i < 8; ++i) {
4821 bool isNonZero = (NonZeros & (1 << i)) != 0;
4822 if (isNonZero) {
4823 if (First) {
4824 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004825 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004826 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004828 First = false;
4829 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004830 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004831 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004832 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004833 }
4834 }
4835
4836 return V;
4837}
4838
Evan Chengf26ffe92008-05-29 08:22:04 +00004839/// getVShift - Return a vector logical shift node.
4840///
Owen Andersone50ed302009-08-10 22:56:29 +00004841static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004842 unsigned NumBits, SelectionDAG &DAG,
4843 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004844 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004845 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004846 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004847 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4848 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004849 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004850 DAG.getConstant(NumBits,
4851 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004852}
4853
Dan Gohman475871a2008-07-27 21:46:04 +00004854SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004855X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004856 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004857
Evan Chengc3630942009-12-09 21:00:30 +00004858 // Check if the scalar load can be widened into a vector load. And if
4859 // the address is "base + cst" see if the cst can be "absorbed" into
4860 // the shuffle mask.
4861 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4862 SDValue Ptr = LD->getBasePtr();
4863 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4864 return SDValue();
4865 EVT PVT = LD->getValueType(0);
4866 if (PVT != MVT::i32 && PVT != MVT::f32)
4867 return SDValue();
4868
4869 int FI = -1;
4870 int64_t Offset = 0;
4871 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4872 FI = FINode->getIndex();
4873 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004874 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004875 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4876 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4877 Offset = Ptr.getConstantOperandVal(1);
4878 Ptr = Ptr.getOperand(0);
4879 } else {
4880 return SDValue();
4881 }
4882
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004883 // FIXME: 256-bit vector instructions don't require a strict alignment,
4884 // improve this code to support it better.
4885 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004886 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004887 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004888 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004889 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004890 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004891 // Can't change the alignment. FIXME: It's possible to compute
4892 // the exact stack offset and reference FI + adjust offset instead.
4893 // If someone *really* cares about this. That's the way to implement it.
4894 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004895 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004896 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004897 }
4898 }
4899
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004900 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004901 // Ptr + (Offset & ~15).
4902 if (Offset < 0)
4903 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004904 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004905 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004906 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004907 if (StartOffset)
4908 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4909 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4910
4911 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004912 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004913
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004914 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4915 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004916 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004917 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004918
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004919 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004920 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004921 Mask.push_back(EltNo);
4922
Craig Toppercc3000632012-01-30 07:50:31 +00004923 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004924 }
4925
4926 return SDValue();
4927}
4928
Michael J. Spencerec38de22010-10-10 22:04:20 +00004929/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4930/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004931/// load which has the same value as a build_vector whose operands are 'elts'.
4932///
4933/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004934///
Nate Begeman1449f292010-03-24 22:19:06 +00004935/// FIXME: we'd also like to handle the case where the last elements are zero
4936/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4937/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004938static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004939 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004940 EVT EltVT = VT.getVectorElementType();
4941 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004942
Nate Begemanfdea31a2010-03-24 20:49:50 +00004943 LoadSDNode *LDBase = NULL;
4944 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004945
Nate Begeman1449f292010-03-24 22:19:06 +00004946 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004947 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004948 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004949 for (unsigned i = 0; i < NumElems; ++i) {
4950 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004951
Nate Begemanfdea31a2010-03-24 20:49:50 +00004952 if (!Elt.getNode() ||
4953 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4954 return SDValue();
4955 if (!LDBase) {
4956 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4957 return SDValue();
4958 LDBase = cast<LoadSDNode>(Elt.getNode());
4959 LastLoadedElt = i;
4960 continue;
4961 }
4962 if (Elt.getOpcode() == ISD::UNDEF)
4963 continue;
4964
4965 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4966 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4967 return SDValue();
4968 LastLoadedElt = i;
4969 }
Nate Begeman1449f292010-03-24 22:19:06 +00004970
4971 // If we have found an entire vector of loads and undefs, then return a large
4972 // load of the entire vector width starting at the base pointer. If we found
4973 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004974 if (LastLoadedElt == NumElems - 1) {
4975 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004976 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004977 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004978 LDBase->isVolatile(), LDBase->isNonTemporal(),
4979 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004980 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004981 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004982 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004983 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00004984 }
4985 if (NumElems == 4 && LastLoadedElt == 1 &&
4986 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004987 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4988 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004989 SDValue ResNode =
4990 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4991 LDBase->getPointerInfo(),
4992 LDBase->getAlignment(),
4993 false/*isVolatile*/, true/*ReadMem*/,
4994 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00004995
4996 // Make sure the newly-created LOAD is in the same position as LDBase in
4997 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
4998 // update uses of LDBase's output chain to use the TokenFactor.
4999 if (LDBase->hasAnyUseOfValue(1)) {
5000 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5001 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5002 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5003 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5004 SDValue(ResNode.getNode(), 1));
5005 }
5006
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005007 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005008 }
5009 return SDValue();
5010}
5011
Nadav Rotem9d68b062012-04-08 12:54:54 +00005012/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5013/// to generate a splat value for the following cases:
5014/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005015/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005016/// a scalar load, or a constant.
5017/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005018/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005019SDValue
5020X86TargetLowering::LowerVectorBroadcast(SDValue &Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005021 if (!Subtarget->hasAVX())
5022 return SDValue();
5023
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005024 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005025 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005026
Craig Topper5da8a802012-05-04 05:49:51 +00005027 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5028 "Unsupported vector type for broadcast.");
5029
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005030 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005031 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005032
Nadav Rotem9d68b062012-04-08 12:54:54 +00005033 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005034 default:
5035 // Unknown pattern found.
5036 return SDValue();
5037
5038 case ISD::BUILD_VECTOR: {
5039 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005040 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005041 return SDValue();
5042
Nadav Rotem9d68b062012-04-08 12:54:54 +00005043 Ld = Op.getOperand(0);
5044 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5045 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005046
5047 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005048 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005049 // Constants may have multiple users.
5050 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005051 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005052 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005053 }
5054
5055 case ISD::VECTOR_SHUFFLE: {
5056 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5057
5058 // Shuffles must have a splat mask where the first element is
5059 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005060 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005061 return SDValue();
5062
5063 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005064 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005065 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5066
5067 if (!Subtarget->hasAVX2())
5068 return SDValue();
5069
5070 // Use the register form of the broadcast instruction available on AVX2.
5071 if (VT.is256BitVector())
5072 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5073 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5074 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005075
5076 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005077 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005078 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005079
5080 // The scalar_to_vector node and the suspected
5081 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005082 // Constants may have multiple users.
5083 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005084 return SDValue();
5085 break;
5086 }
5087 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005088
Craig Topper7a9a28b2012-08-12 02:23:29 +00005089 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005090
5091 // Handle the broadcasting a single constant scalar from the constant pool
5092 // into a vector. On Sandybridge it is still better to load a constant vector
5093 // from the constant pool and not to broadcast it from a scalar.
5094 if (ConstSplatVal && Subtarget->hasAVX2()) {
5095 EVT CVT = Ld.getValueType();
5096 assert(!CVT.isVector() && "Must not broadcast a vector type");
5097 unsigned ScalarSize = CVT.getSizeInBits();
5098
Craig Topper5da8a802012-05-04 05:49:51 +00005099 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005100 const Constant *C = 0;
5101 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5102 C = CI->getConstantIntValue();
5103 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5104 C = CF->getConstantFPValue();
5105
5106 assert(C && "Invalid constant type");
5107
Nadav Rotem154819d2012-04-09 07:45:58 +00005108 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005109 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005110 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005111 MachinePointerInfo::getConstantPool(),
5112 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005113
Nadav Rotem9d68b062012-04-08 12:54:54 +00005114 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5115 }
5116 }
5117
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005118 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005119 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5120
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005121 // Handle AVX2 in-register broadcasts.
5122 if (!IsLoad && Subtarget->hasAVX2() &&
5123 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5124 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5125
5126 // The scalar source must be a normal load.
5127 if (!IsLoad)
5128 return SDValue();
5129
Craig Topper5da8a802012-05-04 05:49:51 +00005130 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005131 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005132
Craig Toppera9376332012-01-10 08:23:59 +00005133 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005134 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005135 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005136 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005137 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005138 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005139
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005140 // Unsupported broadcast.
5141 return SDValue();
5142}
5143
Michael Liao7091b242012-08-14 21:24:47 +00005144// LowerVectorFpExtend - Recognize the scalarized FP_EXTEND from v2f32 to v2f64
5145// and convert it into X86ISD::VFPEXT due to the current ISD::FP_EXTEND has the
5146// constraint of matching input/output vector elements.
5147SDValue
5148X86TargetLowering::LowerVectorFpExtend(SDValue &Op, SelectionDAG &DAG) const {
5149 DebugLoc DL = Op.getDebugLoc();
5150 SDNode *N = Op.getNode();
5151 EVT VT = Op.getValueType();
5152 unsigned NumElts = Op.getNumOperands();
5153
5154 // Check supported types and sub-targets.
5155 //
5156 // Only v2f32 -> v2f64 needs special handling.
5157 if (VT != MVT::v2f64 || !Subtarget->hasSSE2())
5158 return SDValue();
5159
5160 SDValue VecIn;
5161 EVT VecInVT;
5162 SmallVector<int, 8> Mask;
5163 EVT SrcVT = MVT::Other;
5164
5165 // Check the patterns could be translated into X86vfpext.
5166 for (unsigned i = 0; i < NumElts; ++i) {
5167 SDValue In = N->getOperand(i);
5168 unsigned Opcode = In.getOpcode();
5169
5170 // Skip if the element is undefined.
5171 if (Opcode == ISD::UNDEF) {
5172 Mask.push_back(-1);
5173 continue;
5174 }
5175
5176 // Quit if one of the elements is not defined from 'fpext'.
5177 if (Opcode != ISD::FP_EXTEND)
5178 return SDValue();
5179
5180 // Check how the source of 'fpext' is defined.
5181 SDValue L2In = In.getOperand(0);
5182 EVT L2InVT = L2In.getValueType();
5183
5184 // Check the original type
5185 if (SrcVT == MVT::Other)
5186 SrcVT = L2InVT;
5187 else if (SrcVT != L2InVT) // Quit if non-homogenous typed.
5188 return SDValue();
5189
5190 // Check whether the value being 'fpext'ed is extracted from the same
5191 // source.
5192 Opcode = L2In.getOpcode();
5193
5194 // Quit if it's not extracted with a constant index.
5195 if (Opcode != ISD::EXTRACT_VECTOR_ELT ||
5196 !isa<ConstantSDNode>(L2In.getOperand(1)))
5197 return SDValue();
5198
5199 SDValue ExtractedFromVec = L2In.getOperand(0);
5200
5201 if (VecIn.getNode() == 0) {
5202 VecIn = ExtractedFromVec;
5203 VecInVT = ExtractedFromVec.getValueType();
5204 } else if (VecIn != ExtractedFromVec) // Quit if built from more than 1 vec.
5205 return SDValue();
5206
5207 Mask.push_back(cast<ConstantSDNode>(L2In.getOperand(1))->getZExtValue());
5208 }
5209
Michael Liao24438b82012-08-20 17:59:18 +00005210 // Quit if all operands of BUILD_VECTOR are undefined.
5211 if (!VecIn.getNode())
5212 return SDValue();
5213
Michael Liao7091b242012-08-14 21:24:47 +00005214 // Fill the remaining mask as undef.
5215 for (unsigned i = NumElts; i < VecInVT.getVectorNumElements(); ++i)
5216 Mask.push_back(-1);
5217
5218 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
5219 DAG.getVectorShuffle(VecInVT, DL,
5220 VecIn, DAG.getUNDEF(VecInVT),
5221 &Mask[0]));
5222}
5223
Evan Chengc3630942009-12-09 21:00:30 +00005224SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005225X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005226 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005227
David Greenef125a292011-02-08 19:04:41 +00005228 EVT VT = Op.getValueType();
5229 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005230 unsigned NumElems = Op.getNumOperands();
5231
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005232 // Vectors containing all zeros can be matched by pxor and xorps later
5233 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5234 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5235 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005236 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005237 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005238
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005239 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005240 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005241
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005242 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005243 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5244 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005245 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005246 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005247 return Op;
5248
Craig Topper07a27622012-01-22 03:07:48 +00005249 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005250 }
5251
Nadav Rotem154819d2012-04-09 07:45:58 +00005252 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005253 if (Broadcast.getNode())
5254 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005255
Michael Liao7091b242012-08-14 21:24:47 +00005256 SDValue FpExt = LowerVectorFpExtend(Op, DAG);
5257 if (FpExt.getNode())
5258 return FpExt;
5259
Owen Andersone50ed302009-08-10 22:56:29 +00005260 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005261
Evan Cheng0db9fe62006-04-25 20:13:52 +00005262 unsigned NumZero = 0;
5263 unsigned NumNonZero = 0;
5264 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005265 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005266 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005267 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005268 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005269 if (Elt.getOpcode() == ISD::UNDEF)
5270 continue;
5271 Values.insert(Elt);
5272 if (Elt.getOpcode() != ISD::Constant &&
5273 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005274 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005275 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005276 NumZero++;
5277 else {
5278 NonZeros |= (1 << i);
5279 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005280 }
5281 }
5282
Chris Lattner97a2a562010-08-26 05:24:29 +00005283 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5284 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005285 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005286
Chris Lattner67f453a2008-03-09 05:42:06 +00005287 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005288 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005289 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005290 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005291
Chris Lattner62098042008-03-09 01:05:04 +00005292 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5293 // the value are obviously zero, truncate the value to i32 and do the
5294 // insertion that way. Only do this if the value is non-constant or if the
5295 // value is a constant being inserted into element 0. It is cheaper to do
5296 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005297 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005298 (!IsAllConstants || Idx == 0)) {
5299 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005300 // Handle SSE only.
5301 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5302 EVT VecVT = MVT::v4i32;
5303 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005304
Chris Lattner62098042008-03-09 01:05:04 +00005305 // Truncate the value (which may itself be a constant) to i32, and
5306 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005307 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005308 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005309 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005310
Chris Lattner62098042008-03-09 01:05:04 +00005311 // Now we have our 32-bit value zero extended in the low element of
5312 // a vector. If Idx != 0, swizzle it into place.
5313 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005314 SmallVector<int, 4> Mask;
5315 Mask.push_back(Idx);
5316 for (unsigned i = 1; i != VecElts; ++i)
5317 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005318 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005319 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005320 }
Craig Topper07a27622012-01-22 03:07:48 +00005321 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005322 }
5323 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005324
Chris Lattner19f79692008-03-08 22:59:52 +00005325 // If we have a constant or non-constant insertion into the low element of
5326 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5327 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005328 // depending on what the source datatype is.
5329 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005330 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005331 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005332
5333 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005334 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005335 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005336 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005337 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5338 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005339 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005340 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005341 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5342 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005343 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005344 }
5345
5346 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005347 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005348 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005349 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005350 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005351 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005352 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005353 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005354 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005355 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005356 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005357 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005358 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005359
5360 // Is it a vector logical left shift?
5361 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005362 X86::isZeroNode(Op.getOperand(0)) &&
5363 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005364 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005365 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005366 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005367 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005368 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005369 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005370
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005371 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005372 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005373
Chris Lattner19f79692008-03-08 22:59:52 +00005374 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5375 // is a non-constant being inserted into an element other than the low one,
5376 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5377 // movd/movss) to move this into the low element, then shuffle it into
5378 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005379 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005380 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005381
Evan Cheng0db9fe62006-04-25 20:13:52 +00005382 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005383 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005384 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005385 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005386 MaskVec.push_back(i == Idx ? 0 : 1);
5387 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005388 }
5389 }
5390
Chris Lattner67f453a2008-03-09 05:42:06 +00005391 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005392 if (Values.size() == 1) {
5393 if (EVTBits == 32) {
5394 // Instead of a shuffle like this:
5395 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5396 // Check if it's possible to issue this instead.
5397 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5398 unsigned Idx = CountTrailingZeros_32(NonZeros);
5399 SDValue Item = Op.getOperand(Idx);
5400 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5401 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5402 }
Dan Gohman475871a2008-07-27 21:46:04 +00005403 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005404 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005405
Dan Gohmana3941172007-07-24 22:55:08 +00005406 // A vector full of immediates; various special cases are already
5407 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005408 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005409 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005410
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005411 // For AVX-length vectors, build the individual 128-bit pieces and use
5412 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005413 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005414 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005415 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005416 V.push_back(Op.getOperand(i));
5417
5418 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5419
5420 // Build both the lower and upper subvector.
5421 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5422 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5423 NumElems/2);
5424
5425 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005426 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005427 }
5428
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005429 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005430 if (EVTBits == 64) {
5431 if (NumNonZero == 1) {
5432 // One half is zero or undef.
5433 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005434 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005435 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005436 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005437 }
Dan Gohman475871a2008-07-27 21:46:04 +00005438 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005439 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005440
5441 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005442 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005443 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005444 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005445 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005446 }
5447
Bill Wendling826f36f2007-03-28 00:57:11 +00005448 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005449 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005450 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005451 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005452 }
5453
5454 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005455 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005456 if (NumElems == 4 && NumZero > 0) {
5457 for (unsigned i = 0; i < 4; ++i) {
5458 bool isZero = !(NonZeros & (1 << i));
5459 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005460 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005461 else
Dale Johannesenace16102009-02-03 19:33:06 +00005462 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005463 }
5464
5465 for (unsigned i = 0; i < 2; ++i) {
5466 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5467 default: break;
5468 case 0:
5469 V[i] = V[i*2]; // Must be a zero vector.
5470 break;
5471 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005472 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005473 break;
5474 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005475 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005476 break;
5477 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005478 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005479 break;
5480 }
5481 }
5482
Benjamin Kramer9c683542012-01-30 15:16:21 +00005483 bool Reverse1 = (NonZeros & 0x3) == 2;
5484 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5485 int MaskVec[] = {
5486 Reverse1 ? 1 : 0,
5487 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005488 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5489 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005490 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005491 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005492 }
5493
Craig Topper7a9a28b2012-08-12 02:23:29 +00005494 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005495 // Check for a build vector of consecutive loads.
5496 for (unsigned i = 0; i < NumElems; ++i)
5497 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005498
Nate Begemanfdea31a2010-03-24 20:49:50 +00005499 // Check for elements which are consecutive loads.
5500 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5501 if (LD.getNode())
5502 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005503
5504 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005505 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005506 SDValue Result;
5507 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5508 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5509 else
5510 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005511
Chris Lattner24faf612010-08-28 17:59:08 +00005512 for (unsigned i = 1; i < NumElems; ++i) {
5513 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5514 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005515 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005516 }
5517 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005518 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005519
Chris Lattner6e80e442010-08-28 17:15:43 +00005520 // Otherwise, expand into a number of unpckl*, start by extending each of
5521 // our (non-undef) elements to the full vector width with the element in the
5522 // bottom slot of the vector (which generates no code for SSE).
5523 for (unsigned i = 0; i < NumElems; ++i) {
5524 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5525 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5526 else
5527 V[i] = DAG.getUNDEF(VT);
5528 }
5529
5530 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005531 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5532 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5533 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005534 unsigned EltStride = NumElems >> 1;
5535 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005536 for (unsigned i = 0; i < EltStride; ++i) {
5537 // If V[i+EltStride] is undef and this is the first round of mixing,
5538 // then it is safe to just drop this shuffle: V[i] is already in the
5539 // right place, the one element (since it's the first round) being
5540 // inserted as undef can be dropped. This isn't safe for successive
5541 // rounds because they will permute elements within both vectors.
5542 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5543 EltStride == NumElems/2)
5544 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005545
Chris Lattner6e80e442010-08-28 17:15:43 +00005546 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005547 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005548 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005549 }
5550 return V[0];
5551 }
Dan Gohman475871a2008-07-27 21:46:04 +00005552 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005553}
5554
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005555// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5556// to create 256-bit vectors from two other 128-bit ones.
5557static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5558 DebugLoc dl = Op.getDebugLoc();
5559 EVT ResVT = Op.getValueType();
5560
Craig Topper7a9a28b2012-08-12 02:23:29 +00005561 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005562
5563 SDValue V1 = Op.getOperand(0);
5564 SDValue V2 = Op.getOperand(1);
5565 unsigned NumElems = ResVT.getVectorNumElements();
5566
Craig Topper4c7972d2012-04-22 18:15:59 +00005567 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005568}
5569
5570SDValue
5571X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005572 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005573
5574 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5575 // from two other 128-bit ones.
5576 return LowerAVXCONCAT_VECTORS(Op, DAG);
5577}
5578
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005579// Try to lower a shuffle node into a simple blend instruction.
Craig Topper1842ba02012-04-23 06:38:28 +00005580static SDValue LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005581 const X86Subtarget *Subtarget,
Nadav Rotem91794872012-04-11 11:05:21 +00005582 SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005583 SDValue V1 = SVOp->getOperand(0);
5584 SDValue V2 = SVOp->getOperand(1);
5585 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005586 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005587 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005588
Nadav Roteme6113782012-04-11 06:40:27 +00005589 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005590 return SDValue();
5591
Craig Topper1842ba02012-04-23 06:38:28 +00005592 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005593 MVT OpTy;
5594
Craig Topper708e44f2012-04-23 07:36:33 +00005595 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005596 default: return SDValue();
5597 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005598 ISDNo = X86ISD::BLENDPW;
5599 OpTy = MVT::v8i16;
5600 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005601 case MVT::v4i32:
5602 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005603 ISDNo = X86ISD::BLENDPS;
5604 OpTy = MVT::v4f32;
5605 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005606 case MVT::v2i64:
5607 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005608 ISDNo = X86ISD::BLENDPD;
5609 OpTy = MVT::v2f64;
5610 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005611 case MVT::v8i32:
5612 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005613 if (!Subtarget->hasAVX())
5614 return SDValue();
5615 ISDNo = X86ISD::BLENDPS;
5616 OpTy = MVT::v8f32;
5617 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005618 case MVT::v4i64:
5619 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005620 if (!Subtarget->hasAVX())
5621 return SDValue();
5622 ISDNo = X86ISD::BLENDPD;
5623 OpTy = MVT::v4f64;
5624 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005625 }
5626 assert(ISDNo && "Invalid Op Number");
5627
5628 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005629
Craig Topper1842ba02012-04-23 06:38:28 +00005630 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005631 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005632 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005633 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005634 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005635 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005636 else
5637 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005638 }
5639
Nadav Roteme6113782012-04-11 06:40:27 +00005640 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5641 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5642 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5643 DAG.getConstant(MaskVals, MVT::i32));
5644 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005645}
5646
Nate Begemanb9a47b82009-02-23 08:49:38 +00005647// v8i16 shuffles - Prefer shuffles in the following order:
5648// 1. [all] pshuflw, pshufhw, optional move
5649// 2. [ssse3] 1 x pshufb
5650// 3. [ssse3] 2 x pshufb + 1 x por
5651// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005652SDValue
5653X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5654 SelectionDAG &DAG) const {
5655 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005656 SDValue V1 = SVOp->getOperand(0);
5657 SDValue V2 = SVOp->getOperand(1);
5658 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005659 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005660
Nate Begemanb9a47b82009-02-23 08:49:38 +00005661 // Determine if more than 1 of the words in each of the low and high quadwords
5662 // of the result come from the same quadword of one of the two inputs. Undef
5663 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005664 unsigned LoQuad[] = { 0, 0, 0, 0 };
5665 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005666 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005667 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005668 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005669 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005670 MaskVals.push_back(EltIdx);
5671 if (EltIdx < 0) {
5672 ++Quad[0];
5673 ++Quad[1];
5674 ++Quad[2];
5675 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005676 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 }
5678 ++Quad[EltIdx / 4];
5679 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005680 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005681
Nate Begemanb9a47b82009-02-23 08:49:38 +00005682 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005683 unsigned MaxQuad = 1;
5684 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 if (LoQuad[i] > MaxQuad) {
5686 BestLoQuad = i;
5687 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005688 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005689 }
5690
Nate Begemanb9a47b82009-02-23 08:49:38 +00005691 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005692 MaxQuad = 1;
5693 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005694 if (HiQuad[i] > MaxQuad) {
5695 BestHiQuad = i;
5696 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005697 }
5698 }
5699
Nate Begemanb9a47b82009-02-23 08:49:38 +00005700 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005701 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702 // single pshufb instruction is necessary. If There are more than 2 input
5703 // quads, disable the next transformation since it does not help SSSE3.
5704 bool V1Used = InputQuads[0] || InputQuads[1];
5705 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005706 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005708 BestLoQuad = InputQuads[0] ? 0 : 1;
5709 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005710 }
5711 if (InputQuads.count() > 2) {
5712 BestLoQuad = -1;
5713 BestHiQuad = -1;
5714 }
5715 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005716
Nate Begemanb9a47b82009-02-23 08:49:38 +00005717 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5718 // the shuffle mask. If a quad is scored as -1, that means that it contains
5719 // words from all 4 input quadwords.
5720 SDValue NewV;
5721 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005722 int MaskV[] = {
5723 BestLoQuad < 0 ? 0 : BestLoQuad,
5724 BestHiQuad < 0 ? 1 : BestHiQuad
5725 };
Eric Christopherfd179292009-08-27 18:07:15 +00005726 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005727 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5728 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5729 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005730
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5732 // source words for the shuffle, to aid later transformations.
5733 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005734 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005735 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005737 if (idx != (int)i)
5738 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005739 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005740 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005741 AllWordsInNewV = false;
5742 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005743 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005744
Nate Begemanb9a47b82009-02-23 08:49:38 +00005745 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5746 if (AllWordsInNewV) {
5747 for (int i = 0; i != 8; ++i) {
5748 int idx = MaskVals[i];
5749 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005750 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005751 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 if ((idx != i) && idx < 4)
5753 pshufhw = false;
5754 if ((idx != i) && idx > 3)
5755 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005756 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005757 V1 = NewV;
5758 V2Used = false;
5759 BestLoQuad = 0;
5760 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005761 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005762
Nate Begemanb9a47b82009-02-23 08:49:38 +00005763 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5764 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005765 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005766 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5767 unsigned TargetMask = 0;
5768 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005769 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005770 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5771 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5772 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005773 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005774 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005775 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005776 }
Eric Christopherfd179292009-08-27 18:07:15 +00005777
Nate Begemanb9a47b82009-02-23 08:49:38 +00005778 // If we have SSSE3, and all words of the result are from 1 input vector,
5779 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5780 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005781 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005782 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005783
Nate Begemanb9a47b82009-02-23 08:49:38 +00005784 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005785 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005786 // mask, and elements that come from V1 in the V2 mask, so that the two
5787 // results can be OR'd together.
5788 bool TwoInputs = V1Used && V2Used;
5789 for (unsigned i = 0; i != 8; ++i) {
5790 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005791 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5792 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5793 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5794 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005796 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005797 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005798 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005799 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005801 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005802
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 // Calculate the shuffle mask for the second input, shuffle it, and
5804 // OR it with the first shuffled input.
5805 pshufbMask.clear();
5806 for (unsigned i = 0; i != 8; ++i) {
5807 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005808 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5809 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5810 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5811 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005812 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005813 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005814 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005815 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005816 MVT::v16i8, &pshufbMask[0], 16));
5817 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005818 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005819 }
5820
5821 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5822 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005823 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005825 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005826 for (int i = 0; i != 4; ++i) {
5827 int idx = MaskVals[i];
5828 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 InOrder.set(i);
5830 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005831 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005832 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005833 }
5834 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005835 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005836 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005837
Craig Topperdd637ae2012-02-19 05:41:45 +00005838 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5839 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005840 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005841 NewV.getOperand(0),
5842 getShufflePSHUFLWImmediate(SVOp), DAG);
5843 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005844 }
Eric Christopherfd179292009-08-27 18:07:15 +00005845
Nate Begemanb9a47b82009-02-23 08:49:38 +00005846 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5847 // and update MaskVals with the new element order.
5848 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005849 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005850 for (unsigned i = 4; i != 8; ++i) {
5851 int idx = MaskVals[i];
5852 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005853 InOrder.set(i);
5854 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005855 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005856 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005857 }
5858 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005859 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005860 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005861
Craig Topperdd637ae2012-02-19 05:41:45 +00005862 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5863 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005864 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005865 NewV.getOperand(0),
5866 getShufflePSHUFHWImmediate(SVOp), DAG);
5867 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005868 }
Eric Christopherfd179292009-08-27 18:07:15 +00005869
Nate Begemanb9a47b82009-02-23 08:49:38 +00005870 // In case BestHi & BestLo were both -1, which means each quadword has a word
5871 // from each of the four input quadwords, calculate the InOrder bitvector now
5872 // before falling through to the insert/extract cleanup.
5873 if (BestLoQuad == -1 && BestHiQuad == -1) {
5874 NewV = V1;
5875 for (int i = 0; i != 8; ++i)
5876 if (MaskVals[i] < 0 || MaskVals[i] == i)
5877 InOrder.set(i);
5878 }
Eric Christopherfd179292009-08-27 18:07:15 +00005879
Nate Begemanb9a47b82009-02-23 08:49:38 +00005880 // The other elements are put in the right place using pextrw and pinsrw.
5881 for (unsigned i = 0; i != 8; ++i) {
5882 if (InOrder[i])
5883 continue;
5884 int EltIdx = MaskVals[i];
5885 if (EltIdx < 0)
5886 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005887 SDValue ExtOp = (EltIdx < 8) ?
5888 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5889 DAG.getIntPtrConstant(EltIdx)) :
5890 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005891 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005892 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005893 DAG.getIntPtrConstant(i));
5894 }
5895 return NewV;
5896}
5897
5898// v16i8 shuffles - Prefer shuffles in the following order:
5899// 1. [ssse3] 1 x pshufb
5900// 2. [ssse3] 2 x pshufb + 1 x por
5901// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5902static
Nate Begeman9008ca62009-04-27 18:41:29 +00005903SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005904 SelectionDAG &DAG,
5905 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005906 SDValue V1 = SVOp->getOperand(0);
5907 SDValue V2 = SVOp->getOperand(1);
5908 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005909 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005910
Nate Begemanb9a47b82009-02-23 08:49:38 +00005911 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005912 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005913 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005914
Nate Begemanb9a47b82009-02-23 08:49:38 +00005915 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005916 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005917 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005918
Nate Begemanb9a47b82009-02-23 08:49:38 +00005919 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005920 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005921 //
5922 // Otherwise, we have elements from both input vectors, and must zero out
5923 // elements that come from V2 in the first mask, and V1 in the second mask
5924 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 for (unsigned i = 0; i != 16; ++i) {
5926 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005927 if (EltIdx < 0 || EltIdx >= 16)
5928 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005929 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005930 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005932 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005933 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005934
5935 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5936 // the 2nd operand if it's undefined or zero.
5937 if (V2.getOpcode() == ISD::UNDEF ||
5938 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005939 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005940
Nate Begemanb9a47b82009-02-23 08:49:38 +00005941 // Calculate the shuffle mask for the second input, shuffle it, and
5942 // OR it with the first shuffled input.
5943 pshufbMask.clear();
5944 for (unsigned i = 0; i != 16; ++i) {
5945 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005946 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005947 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005948 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005949 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005950 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005951 MVT::v16i8, &pshufbMask[0], 16));
5952 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005953 }
Eric Christopherfd179292009-08-27 18:07:15 +00005954
Nate Begemanb9a47b82009-02-23 08:49:38 +00005955 // No SSSE3 - Calculate in place words and then fix all out of place words
5956 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5957 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005958 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5959 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005960 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005961 for (int i = 0; i != 8; ++i) {
5962 int Elt0 = MaskVals[i*2];
5963 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005964
Nate Begemanb9a47b82009-02-23 08:49:38 +00005965 // This word of the result is all undef, skip it.
5966 if (Elt0 < 0 && Elt1 < 0)
5967 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005968
Nate Begemanb9a47b82009-02-23 08:49:38 +00005969 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005970 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005971 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005972
Nate Begemanb9a47b82009-02-23 08:49:38 +00005973 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5974 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5975 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005976
5977 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5978 // using a single extract together, load it and store it.
5979 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005980 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005981 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005982 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005983 DAG.getIntPtrConstant(i));
5984 continue;
5985 }
5986
Nate Begemanb9a47b82009-02-23 08:49:38 +00005987 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005988 // source byte is not also odd, shift the extracted word left 8 bits
5989 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005990 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005991 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005992 DAG.getIntPtrConstant(Elt1 / 2));
5993 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005994 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005995 DAG.getConstant(8,
5996 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005997 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005998 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5999 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006000 }
6001 // If Elt0 is defined, extract it from the appropriate source. If the
6002 // source byte is not also even, shift the extracted word right 8 bits. If
6003 // Elt1 was also defined, OR the extracted values together before
6004 // inserting them in the result.
6005 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006006 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006007 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6008 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006009 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006010 DAG.getConstant(8,
6011 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006012 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006013 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6014 DAG.getConstant(0x00FF, MVT::i16));
6015 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006016 : InsElt0;
6017 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006018 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006019 DAG.getIntPtrConstant(i));
6020 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006021 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006022}
6023
Elena Demikhovsky41789462012-09-06 12:42:01 +00006024// v32i8 shuffles - Translate to VPSHUFB if possible.
6025static
6026SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
6027 SelectionDAG &DAG,
6028 const X86TargetLowering &TLI) {
6029 EVT VT = SVOp->getValueType(0);
6030 SDValue V1 = SVOp->getOperand(0);
6031 SDValue V2 = SVOp->getOperand(1);
6032 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006033 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006034
6035 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006036 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6037 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006038
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006039 // VPSHUFB may be generated if
6040 // (1) one of input vector is undefined or zeroinitializer.
6041 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6042 // And (2) the mask indexes don't cross the 128-bit lane.
6043 if (VT != MVT::v32i8 || !TLI.getSubtarget()->hasAVX2() ||
6044 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006045 return SDValue();
6046
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006047 if (V1IsAllZero && !V2IsAllZero) {
6048 CommuteVectorShuffleMask(MaskVals, 32);
6049 V1 = V2;
6050 }
6051 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006052 for (unsigned i = 0; i != 32; i++) {
6053 int EltIdx = MaskVals[i];
6054 if (EltIdx < 0 || EltIdx >= 32)
6055 EltIdx = 0x80;
6056 else {
6057 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6058 // Cross lane is not allowed.
6059 return SDValue();
6060 EltIdx &= 0xf;
6061 }
6062 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6063 }
6064 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6065 DAG.getNode(ISD::BUILD_VECTOR, dl,
6066 MVT::v32i8, &pshufbMask[0], 32));
6067}
6068
Evan Cheng7a831ce2007-12-15 03:00:47 +00006069/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006070/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006071/// done when every pair / quad of shuffle mask elements point to elements in
6072/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006073/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006074static
Nate Begeman9008ca62009-04-27 18:41:29 +00006075SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006076 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006077 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006078 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006079 MVT NewVT;
6080 unsigned Scale;
6081 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006082 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006083 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6084 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6085 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6086 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6087 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6088 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006089 }
6090
Nate Begeman9008ca62009-04-27 18:41:29 +00006091 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006092 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006093 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006094 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006095 int EltIdx = SVOp->getMaskElt(i+j);
6096 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006097 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006098 if (StartIdx < 0)
6099 StartIdx = (EltIdx / Scale);
6100 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006101 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006102 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006103 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006104 }
6105
Craig Topper11ac1f82012-05-04 04:08:44 +00006106 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6107 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006108 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006109}
6110
Evan Chengd880b972008-05-09 21:53:03 +00006111/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006112///
Owen Andersone50ed302009-08-10 22:56:29 +00006113static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006114 SDValue SrcOp, SelectionDAG &DAG,
6115 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006116 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006117 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006118 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006119 LD = dyn_cast<LoadSDNode>(SrcOp);
6120 if (!LD) {
6121 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6122 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006123 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006124 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006125 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006126 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006127 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006128 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006129 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006130 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006131 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6132 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6133 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006134 SrcOp.getOperand(0)
6135 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006136 }
6137 }
6138 }
6139
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006140 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006141 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006142 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006143 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006144}
6145
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006146/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6147/// which could not be matched by any known target speficic shuffle
6148static SDValue
6149LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006150
6151 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6152 if (NewOp.getNode())
6153 return NewOp;
6154
Craig Topper8f35c132012-01-20 09:29:03 +00006155 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006156
Craig Topper8f35c132012-01-20 09:29:03 +00006157 unsigned NumElems = VT.getVectorNumElements();
6158 unsigned NumLaneElems = NumElems / 2;
6159
Craig Topper8f35c132012-01-20 09:29:03 +00006160 DebugLoc dl = SVOp->getDebugLoc();
6161 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006162 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006163 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006164
Craig Topper9a2b6e12012-04-06 07:45:23 +00006165 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006166 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006167 // Build a shuffle mask for the output, discovering on the fly which
6168 // input vectors to use as shuffle operands (recorded in InputUsed).
6169 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006170 // out with UseBuildVector set.
6171 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006172 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006173 unsigned LaneStart = l * NumLaneElems;
6174 for (unsigned i = 0; i != NumLaneElems; ++i) {
6175 // The mask element. This indexes into the input.
6176 int Idx = SVOp->getMaskElt(i+LaneStart);
6177 if (Idx < 0) {
6178 // the mask element does not index into any input vector.
6179 Mask.push_back(-1);
6180 continue;
6181 }
Craig Topper8f35c132012-01-20 09:29:03 +00006182
Craig Topper9a2b6e12012-04-06 07:45:23 +00006183 // The input vector this mask element indexes into.
6184 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006185
Craig Topper9a2b6e12012-04-06 07:45:23 +00006186 // Turn the index into an offset from the start of the input vector.
6187 Idx -= Input * NumLaneElems;
6188
6189 // Find or create a shuffle vector operand to hold this input.
6190 unsigned OpNo;
6191 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6192 if (InputUsed[OpNo] == Input)
6193 // This input vector is already an operand.
6194 break;
6195 if (InputUsed[OpNo] < 0) {
6196 // Create a new operand for this input vector.
6197 InputUsed[OpNo] = Input;
6198 break;
6199 }
6200 }
6201
6202 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006203 // More than two input vectors used! Give up on trying to create a
6204 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6205 UseBuildVector = true;
6206 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006207 }
6208
6209 // Add the mask index for the new shuffle vector.
6210 Mask.push_back(Idx + OpNo * NumLaneElems);
6211 }
6212
Craig Topper8ae97ba2012-05-21 06:40:16 +00006213 if (UseBuildVector) {
6214 SmallVector<SDValue, 16> SVOps;
6215 for (unsigned i = 0; i != NumLaneElems; ++i) {
6216 // The mask element. This indexes into the input.
6217 int Idx = SVOp->getMaskElt(i+LaneStart);
6218 if (Idx < 0) {
6219 SVOps.push_back(DAG.getUNDEF(EltVT));
6220 continue;
6221 }
6222
6223 // The input vector this mask element indexes into.
6224 int Input = Idx / NumElems;
6225
6226 // Turn the index into an offset from the start of the input vector.
6227 Idx -= Input * NumElems;
6228
6229 // Extract the vector element by hand.
6230 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6231 SVOp->getOperand(Input),
6232 DAG.getIntPtrConstant(Idx)));
6233 }
6234
6235 // Construct the output using a BUILD_VECTOR.
6236 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6237 SVOps.size());
6238 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006239 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006240 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006241 } else {
6242 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006243 (InputUsed[0] % 2) * NumLaneElems,
6244 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006245 // If only one input was used, use an undefined vector for the other.
6246 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6247 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006248 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006249 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006250 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006251 }
6252
6253 Mask.clear();
6254 }
Craig Topper8f35c132012-01-20 09:29:03 +00006255
6256 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006257 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006258}
6259
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006260/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6261/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006262static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006263LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006264 SDValue V1 = SVOp->getOperand(0);
6265 SDValue V2 = SVOp->getOperand(1);
6266 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006267 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006268
Craig Topper7a9a28b2012-08-12 02:23:29 +00006269 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006270
Benjamin Kramer9c683542012-01-30 15:16:21 +00006271 std::pair<int, int> Locs[4];
6272 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006273 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006274
Evan Chengace3c172008-07-22 21:13:36 +00006275 unsigned NumHi = 0;
6276 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006277 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006278 int Idx = PermMask[i];
6279 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006280 Locs[i] = std::make_pair(-1, -1);
6281 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006282 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6283 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006284 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006285 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006286 NumLo++;
6287 } else {
6288 Locs[i] = std::make_pair(1, NumHi);
6289 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006290 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006291 NumHi++;
6292 }
6293 }
6294 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006295
Evan Chengace3c172008-07-22 21:13:36 +00006296 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006297 // If no more than two elements come from either vector. This can be
6298 // implemented with two shuffles. First shuffle gather the elements.
6299 // The second shuffle, which takes the first shuffle as both of its
6300 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006301 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006302
Benjamin Kramer9c683542012-01-30 15:16:21 +00006303 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006304
Benjamin Kramer9c683542012-01-30 15:16:21 +00006305 for (unsigned i = 0; i != 4; ++i)
6306 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006307 unsigned Idx = (i < 2) ? 0 : 4;
6308 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006309 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006310 }
Evan Chengace3c172008-07-22 21:13:36 +00006311
Nate Begeman9008ca62009-04-27 18:41:29 +00006312 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006313 }
6314
6315 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006316 // Otherwise, we must have three elements from one vector, call it X, and
6317 // one element from the other, call it Y. First, use a shufps to build an
6318 // intermediate vector with the one element from Y and the element from X
6319 // that will be in the same half in the final destination (the indexes don't
6320 // matter). Then, use a shufps to build the final vector, taking the half
6321 // containing the element from Y from the intermediate, and the other half
6322 // from X.
6323 if (NumHi == 3) {
6324 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006325 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006326 std::swap(V1, V2);
6327 }
6328
6329 // Find the element from V2.
6330 unsigned HiIndex;
6331 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006332 int Val = PermMask[HiIndex];
6333 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006334 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006335 if (Val >= 4)
6336 break;
6337 }
6338
Nate Begeman9008ca62009-04-27 18:41:29 +00006339 Mask1[0] = PermMask[HiIndex];
6340 Mask1[1] = -1;
6341 Mask1[2] = PermMask[HiIndex^1];
6342 Mask1[3] = -1;
6343 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006344
6345 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006346 Mask1[0] = PermMask[0];
6347 Mask1[1] = PermMask[1];
6348 Mask1[2] = HiIndex & 1 ? 6 : 4;
6349 Mask1[3] = HiIndex & 1 ? 4 : 6;
6350 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006351 }
Craig Topper69947b92012-04-23 06:57:04 +00006352
6353 Mask1[0] = HiIndex & 1 ? 2 : 0;
6354 Mask1[1] = HiIndex & 1 ? 0 : 2;
6355 Mask1[2] = PermMask[2];
6356 Mask1[3] = PermMask[3];
6357 if (Mask1[2] >= 0)
6358 Mask1[2] += 4;
6359 if (Mask1[3] >= 0)
6360 Mask1[3] += 4;
6361 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006362 }
6363
6364 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006365 int LoMask[] = { -1, -1, -1, -1 };
6366 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006367
Benjamin Kramer9c683542012-01-30 15:16:21 +00006368 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006369 unsigned MaskIdx = 0;
6370 unsigned LoIdx = 0;
6371 unsigned HiIdx = 2;
6372 for (unsigned i = 0; i != 4; ++i) {
6373 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006374 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006375 MaskIdx = 1;
6376 LoIdx = 0;
6377 HiIdx = 2;
6378 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006379 int Idx = PermMask[i];
6380 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006381 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006382 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006383 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006384 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006385 LoIdx++;
6386 } else {
6387 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006388 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006389 HiIdx++;
6390 }
6391 }
6392
Nate Begeman9008ca62009-04-27 18:41:29 +00006393 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6394 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006395 int MaskOps[] = { -1, -1, -1, -1 };
6396 for (unsigned i = 0; i != 4; ++i)
6397 if (Locs[i].first != -1)
6398 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006399 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006400}
6401
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006402static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006403 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006404 V = V.getOperand(0);
6405 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6406 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006407 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6408 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6409 // BUILD_VECTOR (load), undef
6410 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006411 if (MayFoldLoad(V))
6412 return true;
6413 return false;
6414}
6415
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006416// FIXME: the version above should always be used. Since there's
6417// a bug where several vector shuffles can't be folded because the
6418// DAG is not updated during lowering and a node claims to have two
6419// uses while it only has one, use this version, and let isel match
6420// another instruction if the load really happens to have more than
6421// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006422// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006423static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006424 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006425 V = V.getOperand(0);
6426 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6427 V = V.getOperand(0);
6428 if (ISD::isNormalLoad(V.getNode()))
6429 return true;
6430 return false;
6431}
6432
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006433static
Evan Cheng835580f2010-10-07 20:50:20 +00006434SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6435 EVT VT = Op.getValueType();
6436
6437 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006438 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6439 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006440 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6441 V1, DAG));
6442}
6443
6444static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006445SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006446 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006447 SDValue V1 = Op.getOperand(0);
6448 SDValue V2 = Op.getOperand(1);
6449 EVT VT = Op.getValueType();
6450
6451 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6452
Craig Topper1accb7e2012-01-10 06:54:16 +00006453 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006454 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6455
Evan Cheng0899f5c2011-08-31 02:05:24 +00006456 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6457 return DAG.getNode(ISD::BITCAST, dl, VT,
6458 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6459 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6460 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006461}
6462
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006463static
6464SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6465 SDValue V1 = Op.getOperand(0);
6466 SDValue V2 = Op.getOperand(1);
6467 EVT VT = Op.getValueType();
6468
6469 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6470 "unsupported shuffle type");
6471
6472 if (V2.getOpcode() == ISD::UNDEF)
6473 V2 = V1;
6474
6475 // v4i32 or v4f32
6476 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6477}
6478
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006479static
Craig Topper1accb7e2012-01-10 06:54:16 +00006480SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006481 SDValue V1 = Op.getOperand(0);
6482 SDValue V2 = Op.getOperand(1);
6483 EVT VT = Op.getValueType();
6484 unsigned NumElems = VT.getVectorNumElements();
6485
6486 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6487 // operand of these instructions is only memory, so check if there's a
6488 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6489 // same masks.
6490 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006491
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006492 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006493 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006494 CanFoldLoad = true;
6495
6496 // When V1 is a load, it can be folded later into a store in isel, example:
6497 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6498 // turns into:
6499 // (MOVLPSmr addr:$src1, VR128:$src2)
6500 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006501 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006502 CanFoldLoad = true;
6503
Dan Gohman65fd6562011-11-03 21:49:52 +00006504 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006505 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006506 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006507 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6508
6509 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006510 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006511 if (SVOp->getMaskElt(1) != -1)
6512 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006513 }
6514
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006515 // movl and movlp will both match v2i64, but v2i64 is never matched by
6516 // movl earlier because we make it strict to avoid messing with the movlp load
6517 // folding logic (see the code above getMOVLP call). Match it here then,
6518 // this is horrible, but will stay like this until we move all shuffle
6519 // matching to x86 specific nodes. Note that for the 1st condition all
6520 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006521 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006522 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6523 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006524 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006525 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006526 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006527 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006528
6529 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6530
6531 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006532 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006533 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006534}
6535
Nadav Rotem154819d2012-04-09 07:45:58 +00006536SDValue
6537X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006538 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6539 EVT VT = Op.getValueType();
6540 DebugLoc dl = Op.getDebugLoc();
6541 SDValue V1 = Op.getOperand(0);
6542 SDValue V2 = Op.getOperand(1);
6543
6544 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006545 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006546
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006547 // Handle splat operations
6548 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006549 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006550 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006551
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006552 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006553 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006554 if (Broadcast.getNode())
6555 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006556
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006557 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006558 if ((Size == 128 && NumElem <= 4) ||
6559 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006560 return SDValue();
6561
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006562 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006563 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006564 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006565
6566 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6567 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006568 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6569 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006570 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6571 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006572 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006573 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006574 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006575 // FIXME: Figure out a cleaner way to do this.
6576 // Try to make use of movq to zero out the top part.
6577 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6578 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6579 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006580 EVT NewVT = NewOp.getValueType();
6581 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6582 NewVT, true, false))
6583 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006584 DAG, Subtarget, dl);
6585 }
6586 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6587 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006588 if (NewOp.getNode()) {
6589 EVT NewVT = NewOp.getValueType();
6590 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6591 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6592 DAG, Subtarget, dl);
6593 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006594 }
6595 }
6596 return SDValue();
6597}
6598
Dan Gohman475871a2008-07-27 21:46:04 +00006599SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006600X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006601 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006602 SDValue V1 = Op.getOperand(0);
6603 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006604 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006605 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006606 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006607 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006608 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006609 bool V1IsSplat = false;
6610 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006611 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006612 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006613 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006614 MachineFunction &MF = DAG.getMachineFunction();
6615 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006616
Craig Topper3426a3e2011-11-14 06:46:21 +00006617 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006618
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006619 if (V1IsUndef && V2IsUndef)
6620 return DAG.getUNDEF(VT);
6621
6622 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006623
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006624 // Vector shuffle lowering takes 3 steps:
6625 //
6626 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6627 // narrowing and commutation of operands should be handled.
6628 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6629 // shuffle nodes.
6630 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6631 // so the shuffle can be broken into other shuffles and the legalizer can
6632 // try the lowering again.
6633 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006634 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006635 // be matched during isel, all of them must be converted to a target specific
6636 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006637
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006638 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6639 // narrowing and commutation of operands should be handled. The actual code
6640 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006641 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006642 if (NewOp.getNode())
6643 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006644
Craig Topper5aaffa82012-02-19 02:53:47 +00006645 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6646
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006647 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6648 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006649 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006650 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006651 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006652 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006653
Craig Topperdd637ae2012-02-19 05:41:45 +00006654 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006655 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006656 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006657
Craig Topperdd637ae2012-02-19 05:41:45 +00006658 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006659 return getMOVHighToLow(Op, dl, DAG);
6660
6661 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006662 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006663 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006664 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006665
Craig Topper5aaffa82012-02-19 02:53:47 +00006666 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006667 // The actual implementation will match the mask in the if above and then
6668 // during isel it can match several different instructions, not only pshufd
6669 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006670 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6671 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006672
Craig Topper5aaffa82012-02-19 02:53:47 +00006673 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006674
Craig Topperdbd98a42012-02-07 06:28:42 +00006675 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6676 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6677
Craig Topper1accb7e2012-01-10 06:54:16 +00006678 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006679 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6680
Craig Topperb3982da2011-12-31 23:50:21 +00006681 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006682 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006683 }
Eric Christopherfd179292009-08-27 18:07:15 +00006684
Evan Chengf26ffe92008-05-29 08:22:04 +00006685 // Check if this can be converted into a logical shift.
6686 bool isLeft = false;
6687 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006688 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006689 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006690 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006691 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006692 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006693 EVT EltVT = VT.getVectorElementType();
6694 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006695 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006696 }
Eric Christopherfd179292009-08-27 18:07:15 +00006697
Craig Topper5aaffa82012-02-19 02:53:47 +00006698 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006699 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006700 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006701 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006702 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006703 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6704
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006705 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006706 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6707 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006708 }
Eric Christopherfd179292009-08-27 18:07:15 +00006709
Nate Begeman9008ca62009-04-27 18:41:29 +00006710 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006711 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006712 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006713
Craig Topperdd637ae2012-02-19 05:41:45 +00006714 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006715 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006716
Craig Topperdd637ae2012-02-19 05:41:45 +00006717 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006718 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006719
Craig Topperdd637ae2012-02-19 05:41:45 +00006720 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006721 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006722
Craig Topperdd637ae2012-02-19 05:41:45 +00006723 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006724 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006725
Craig Topperdd637ae2012-02-19 05:41:45 +00006726 if (ShouldXformToMOVHLPS(M, VT) ||
6727 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006728 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006729
Evan Chengf26ffe92008-05-29 08:22:04 +00006730 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006731 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006732 EVT EltVT = VT.getVectorElementType();
6733 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006734 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006735 }
Eric Christopherfd179292009-08-27 18:07:15 +00006736
Evan Cheng9eca5e82006-10-25 21:49:50 +00006737 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006738 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6739 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006740 V1IsSplat = isSplatVector(V1.getNode());
6741 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006742
Chris Lattner8a594482007-11-25 00:24:49 +00006743 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006744 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6745 CommuteVectorShuffleMask(M, NumElems);
6746 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006747 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006748 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006749 }
6750
Craig Topperbeabc6c2011-12-05 06:56:46 +00006751 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006752 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006753 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006754 return V1;
6755 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6756 // the instruction selector will not match, so get a canonical MOVL with
6757 // swapped operands to undo the commute.
6758 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006759 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006760
Craig Topperbeabc6c2011-12-05 06:56:46 +00006761 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006762 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006763
Craig Topperbeabc6c2011-12-05 06:56:46 +00006764 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006765 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006766
Evan Cheng9bbbb982006-10-25 20:48:19 +00006767 if (V2IsSplat) {
6768 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006769 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006770 // new vector_shuffle with the corrected mask.p
6771 SmallVector<int, 8> NewMask(M.begin(), M.end());
6772 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006773 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006774 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006775 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006776 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006777 }
6778
Evan Cheng9eca5e82006-10-25 21:49:50 +00006779 if (Commuted) {
6780 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006781 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006782 CommuteVectorShuffleMask(M, NumElems);
6783 std::swap(V1, V2);
6784 std::swap(V1IsSplat, V2IsSplat);
6785 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006786
Craig Topper39a9e482012-02-11 06:24:48 +00006787 if (isUNPCKLMask(M, VT, HasAVX2))
6788 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006789
Craig Topper39a9e482012-02-11 06:24:48 +00006790 if (isUNPCKHMask(M, VT, HasAVX2))
6791 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006792 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006793
Nate Begeman9008ca62009-04-27 18:41:29 +00006794 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006795 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006796 return CommuteVectorShuffle(SVOp, DAG);
6797
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006798 // The checks below are all present in isShuffleMaskLegal, but they are
6799 // inlined here right now to enable us to directly emit target specific
6800 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006801
Craig Topper0e2037b2012-01-20 05:53:00 +00006802 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006803 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006804 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006805 DAG);
6806
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006807 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6808 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006809 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006810 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006811 }
6812
Craig Toppera9a568a2012-05-02 08:03:44 +00006813 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006814 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006815 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006816 DAG);
6817
Craig Toppera9a568a2012-05-02 08:03:44 +00006818 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006819 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006820 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006821 DAG);
6822
Craig Topper1a7700a2012-01-19 08:19:12 +00006823 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006824 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006825 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006826
Craig Topper94438ba2011-12-16 08:06:31 +00006827 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006828 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006829 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006830 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006831
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006832 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006833 // Generate target specific nodes for 128 or 256-bit shuffles only
6834 // supported in the AVX instruction set.
6835 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006836
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006837 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006838 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006839 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6840
Craig Topper70b883b2011-11-28 10:14:51 +00006841 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006842 if (isVPERMILPMask(M, VT, HasAVX)) {
6843 if (HasAVX2 && VT == MVT::v8i32)
6844 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006845 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006846 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006847 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006848 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006849
Craig Topper70b883b2011-11-28 10:14:51 +00006850 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006851 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006852 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006853 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006854
Craig Topper1842ba02012-04-23 06:38:28 +00006855 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006856 if (BlendOp.getNode())
6857 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006858
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006859 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006860 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006861 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006862 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006863 }
Craig Topper92040742012-04-16 06:43:40 +00006864 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6865 &permclMask[0], 8);
6866 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006867 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006868 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006869 }
Craig Topper095c5282012-04-15 23:48:57 +00006870
Craig Topper8325c112012-04-16 00:41:45 +00006871 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6872 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006873 getShuffleCLImmediate(SVOp), DAG);
6874
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006875
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006876 //===--------------------------------------------------------------------===//
6877 // Since no target specific shuffle was selected for this generic one,
6878 // lower it into other known shuffles. FIXME: this isn't true yet, but
6879 // this is the plan.
6880 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006881
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006882 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6883 if (VT == MVT::v8i16) {
6884 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6885 if (NewOp.getNode())
6886 return NewOp;
6887 }
6888
6889 if (VT == MVT::v16i8) {
6890 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6891 if (NewOp.getNode())
6892 return NewOp;
6893 }
6894
Elena Demikhovsky41789462012-09-06 12:42:01 +00006895 if (VT == MVT::v32i8) {
6896 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, DAG, *this);
6897 if (NewOp.getNode())
6898 return NewOp;
6899 }
6900
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006901 // Handle all 128-bit wide vectors with 4 elements, and match them with
6902 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006903 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006904 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6905
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006906 // Handle general 256-bit shuffles
6907 if (VT.is256BitVector())
6908 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6909
Dan Gohman475871a2008-07-27 21:46:04 +00006910 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006911}
6912
Dan Gohman475871a2008-07-27 21:46:04 +00006913SDValue
6914X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006915 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006916 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006917 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006918
Craig Topper7a9a28b2012-08-12 02:23:29 +00006919 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006920 return SDValue();
6921
Duncan Sands83ec4b62008-06-06 12:08:01 +00006922 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006923 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006924 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006925 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006926 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006927 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006928 }
6929
6930 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006931 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6932 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6933 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006934 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6935 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006936 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006937 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006938 Op.getOperand(0)),
6939 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006941 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006943 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006944 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006945 }
6946
6947 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006948 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6949 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006950 // result has a single use which is a store or a bitcast to i32. And in
6951 // the case of a store, it's not worth it if the index is a constant 0,
6952 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006953 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006954 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006955 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006956 if ((User->getOpcode() != ISD::STORE ||
6957 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6958 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006959 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006960 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006961 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006962 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006963 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006964 Op.getOperand(0)),
6965 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006966 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006967 }
6968
6969 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006970 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006971 if (isa<ConstantSDNode>(Op.getOperand(1)))
6972 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006973 }
Dan Gohman475871a2008-07-27 21:46:04 +00006974 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006975}
6976
6977
Dan Gohman475871a2008-07-27 21:46:04 +00006978SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006979X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6980 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006981 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006982 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006983
David Greene74a579d2011-02-10 16:57:36 +00006984 SDValue Vec = Op.getOperand(0);
6985 EVT VecVT = Vec.getValueType();
6986
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006987 // If this is a 256-bit vector result, first extract the 128-bit vector and
6988 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006989 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00006990 DebugLoc dl = Op.getNode()->getDebugLoc();
6991 unsigned NumElems = VecVT.getVectorNumElements();
6992 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006993 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6994
6995 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006996 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006997
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006998 if (IdxVal >= NumElems/2)
6999 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007000 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007001 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007002 }
7003
Craig Topper7a9a28b2012-08-12 02:23:29 +00007004 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007005
Craig Topperd0a31172012-01-10 06:37:29 +00007006 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007007 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007008 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007009 return Res;
7010 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007011
Owen Andersone50ed302009-08-10 22:56:29 +00007012 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007013 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007014 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007015 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007016 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007017 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007018 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007019 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7020 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007021 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007022 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007023 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007024 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007025 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007026 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007027 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007028 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007029 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007030 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007031 }
7032
7033 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007034 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007035 if (Idx == 0)
7036 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007037
Evan Cheng0db9fe62006-04-25 20:13:52 +00007038 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007039 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007040 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007041 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007042 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007043 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007044 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007045 }
7046
7047 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007048 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7049 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7050 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007051 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007052 if (Idx == 0)
7053 return Op;
7054
7055 // UNPCKHPD the element to the lowest double word, then movsd.
7056 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7057 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007058 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007059 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007060 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007061 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007062 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007063 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007064 }
7065
Dan Gohman475871a2008-07-27 21:46:04 +00007066 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007067}
7068
Dan Gohman475871a2008-07-27 21:46:04 +00007069SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007070X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7071 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007072 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007073 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007074 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007075
Dan Gohman475871a2008-07-27 21:46:04 +00007076 SDValue N0 = Op.getOperand(0);
7077 SDValue N1 = Op.getOperand(1);
7078 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007079
Craig Topper7a9a28b2012-08-12 02:23:29 +00007080 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007081 return SDValue();
7082
Dan Gohman8a55ce42009-09-23 21:02:20 +00007083 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007084 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007085 unsigned Opc;
7086 if (VT == MVT::v8i16)
7087 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007088 else if (VT == MVT::v16i8)
7089 Opc = X86ISD::PINSRB;
7090 else
7091 Opc = X86ISD::PINSRB;
7092
Nate Begeman14d12ca2008-02-11 04:19:36 +00007093 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7094 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007095 if (N1.getValueType() != MVT::i32)
7096 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7097 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007098 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007099 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007100 }
7101
7102 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007103 // Bits [7:6] of the constant are the source select. This will always be
7104 // zero here. The DAG Combiner may combine an extract_elt index into these
7105 // bits. For example (insert (extract, 3), 2) could be matched by putting
7106 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007107 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007108 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007109 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007110 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007111 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007112 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007114 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007115 }
7116
7117 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007118 // PINSR* works with constant index.
7119 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007120 }
Dan Gohman475871a2008-07-27 21:46:04 +00007121 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007122}
7123
Dan Gohman475871a2008-07-27 21:46:04 +00007124SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007125X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007126 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007127 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007128
David Greene6b381262011-02-09 15:32:06 +00007129 DebugLoc dl = Op.getDebugLoc();
7130 SDValue N0 = Op.getOperand(0);
7131 SDValue N1 = Op.getOperand(1);
7132 SDValue N2 = Op.getOperand(2);
7133
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007134 // If this is a 256-bit vector result, first extract the 128-bit vector,
7135 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007136 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007137 if (!isa<ConstantSDNode>(N2))
7138 return SDValue();
7139
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007140 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007141 unsigned NumElems = VT.getVectorNumElements();
7142 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007143 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007144
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007145 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007146 bool Upper = IdxVal >= NumElems/2;
7147 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7148 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007149
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007150 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007151 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007152 }
7153
Craig Topperd0a31172012-01-10 06:37:29 +00007154 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007155 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7156
Dan Gohman8a55ce42009-09-23 21:02:20 +00007157 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007158 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007159
Dan Gohman8a55ce42009-09-23 21:02:20 +00007160 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007161 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7162 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007163 if (N1.getValueType() != MVT::i32)
7164 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7165 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007166 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007167 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007168 }
Dan Gohman475871a2008-07-27 21:46:04 +00007169 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007170}
7171
Dan Gohman475871a2008-07-27 21:46:04 +00007172SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007173X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007174 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007175 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007176 EVT OpVT = Op.getValueType();
7177
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007178 // If this is a 256-bit vector result, first insert into a 128-bit
7179 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007180 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007181 // Insert into a 128-bit vector.
7182 EVT VT128 = EVT::getVectorVT(*Context,
7183 OpVT.getVectorElementType(),
7184 OpVT.getVectorNumElements() / 2);
7185
7186 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7187
7188 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007189 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007190 }
7191
Craig Topperd77d2fe2012-04-29 20:22:05 +00007192 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007193 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007194 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007195
Owen Anderson825b72b2009-08-11 20:47:22 +00007196 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007197 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007198 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007199 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007200}
7201
David Greene91585092011-01-26 15:38:49 +00007202// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7203// a simple subregister reference or explicit instructions to grab
7204// upper bits of a vector.
7205SDValue
7206X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7207 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007208 DebugLoc dl = Op.getNode()->getDebugLoc();
7209 SDValue Vec = Op.getNode()->getOperand(0);
7210 SDValue Idx = Op.getNode()->getOperand(1);
7211
Craig Topper7a9a28b2012-08-12 02:23:29 +00007212 if (Op.getNode()->getValueType(0).is128BitVector() &&
7213 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007214 isa<ConstantSDNode>(Idx)) {
7215 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7216 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007217 }
David Greene91585092011-01-26 15:38:49 +00007218 }
7219 return SDValue();
7220}
7221
David Greenecfe33c42011-01-26 19:13:22 +00007222// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7223// simple superregister reference or explicit instructions to insert
7224// the upper bits of a vector.
7225SDValue
7226X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7227 if (Subtarget->hasAVX()) {
7228 DebugLoc dl = Op.getNode()->getDebugLoc();
7229 SDValue Vec = Op.getNode()->getOperand(0);
7230 SDValue SubVec = Op.getNode()->getOperand(1);
7231 SDValue Idx = Op.getNode()->getOperand(2);
7232
Craig Topper7a9a28b2012-08-12 02:23:29 +00007233 if (Op.getNode()->getValueType(0).is256BitVector() &&
7234 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007235 isa<ConstantSDNode>(Idx)) {
7236 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7237 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007238 }
7239 }
7240 return SDValue();
7241}
7242
Bill Wendling056292f2008-09-16 21:48:12 +00007243// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7244// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7245// one of the above mentioned nodes. It has to be wrapped because otherwise
7246// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7247// be used to form addressing mode. These wrapped nodes will be selected
7248// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007249SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007250X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007251 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007252
Chris Lattner41621a22009-06-26 19:22:52 +00007253 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7254 // global base reg.
7255 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007256 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007257 CodeModel::Model M = getTargetMachine().getCodeModel();
7258
Chris Lattner4f066492009-07-11 20:29:19 +00007259 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007260 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007261 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007262 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007263 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007264 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007265 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007266
Evan Cheng1606e8e2009-03-13 07:51:59 +00007267 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007268 CP->getAlignment(),
7269 CP->getOffset(), OpFlag);
7270 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007271 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007272 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007273 if (OpFlag) {
7274 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007275 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007276 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007277 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007278 }
7279
7280 return Result;
7281}
7282
Dan Gohmand858e902010-04-17 15:26:15 +00007283SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007284 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007285
Chris Lattner18c59872009-06-27 04:16:01 +00007286 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7287 // global base reg.
7288 unsigned char OpFlag = 0;
7289 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007290 CodeModel::Model M = getTargetMachine().getCodeModel();
7291
Chris Lattner4f066492009-07-11 20:29:19 +00007292 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007293 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007294 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007295 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007296 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007297 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007298 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007299
Chris Lattner18c59872009-06-27 04:16:01 +00007300 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7301 OpFlag);
7302 DebugLoc DL = JT->getDebugLoc();
7303 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007304
Chris Lattner18c59872009-06-27 04:16:01 +00007305 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007306 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007307 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7308 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007309 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007310 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007311
Chris Lattner18c59872009-06-27 04:16:01 +00007312 return Result;
7313}
7314
7315SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007316X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007317 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007318
Chris Lattner18c59872009-06-27 04:16:01 +00007319 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7320 // global base reg.
7321 unsigned char OpFlag = 0;
7322 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007323 CodeModel::Model M = getTargetMachine().getCodeModel();
7324
Chris Lattner4f066492009-07-11 20:29:19 +00007325 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007326 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7327 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7328 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007329 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007330 } else if (Subtarget->isPICStyleGOT()) {
7331 OpFlag = X86II::MO_GOT;
7332 } else if (Subtarget->isPICStyleStubPIC()) {
7333 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7334 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7335 OpFlag = X86II::MO_DARWIN_NONLAZY;
7336 }
Eric Christopherfd179292009-08-27 18:07:15 +00007337
Chris Lattner18c59872009-06-27 04:16:01 +00007338 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007339
Chris Lattner18c59872009-06-27 04:16:01 +00007340 DebugLoc DL = Op.getDebugLoc();
7341 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007342
7343
Chris Lattner18c59872009-06-27 04:16:01 +00007344 // With PIC, the address is actually $g + Offset.
7345 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007346 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007347 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7348 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007349 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007350 Result);
7351 }
Eric Christopherfd179292009-08-27 18:07:15 +00007352
Eli Friedman586272d2011-08-11 01:48:05 +00007353 // For symbols that require a load from a stub to get the address, emit the
7354 // load.
7355 if (isGlobalStubReference(OpFlag))
7356 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007357 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007358
Chris Lattner18c59872009-06-27 04:16:01 +00007359 return Result;
7360}
7361
Dan Gohman475871a2008-07-27 21:46:04 +00007362SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007363X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007364 // Create the TargetBlockAddressAddress node.
7365 unsigned char OpFlags =
7366 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007367 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007368 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007369 DebugLoc dl = Op.getDebugLoc();
7370 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7371 /*isTarget=*/true, OpFlags);
7372
Dan Gohmanf705adb2009-10-30 01:28:02 +00007373 if (Subtarget->isPICStyleRIPRel() &&
7374 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007375 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7376 else
7377 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007378
Dan Gohman29cbade2009-11-20 23:18:13 +00007379 // With PIC, the address is actually $g + Offset.
7380 if (isGlobalRelativeToPICBase(OpFlags)) {
7381 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7382 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7383 Result);
7384 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007385
7386 return Result;
7387}
7388
7389SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007390X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007391 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007392 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007393 // Create the TargetGlobalAddress node, folding in the constant
7394 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007395 unsigned char OpFlags =
7396 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007397 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007398 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007399 if (OpFlags == X86II::MO_NO_FLAG &&
7400 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007401 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007402 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007403 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007404 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007405 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007406 }
Eric Christopherfd179292009-08-27 18:07:15 +00007407
Chris Lattner4f066492009-07-11 20:29:19 +00007408 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007409 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007410 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7411 else
7412 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007413
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007414 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007415 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007416 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7417 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007418 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007419 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007420
Chris Lattner36c25012009-07-10 07:34:39 +00007421 // For globals that require a load from a stub to get the address, emit the
7422 // load.
7423 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007424 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007425 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007426
Dan Gohman6520e202008-10-18 02:06:02 +00007427 // If there was a non-zero offset that we didn't fold, create an explicit
7428 // addition for it.
7429 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007430 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007431 DAG.getConstant(Offset, getPointerTy()));
7432
Evan Cheng0db9fe62006-04-25 20:13:52 +00007433 return Result;
7434}
7435
Evan Chengda43bcf2008-09-24 00:05:32 +00007436SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007437X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007438 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007439 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007440 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007441}
7442
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007443static SDValue
7444GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007445 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007446 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007447 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007448 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007449 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007450 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007451 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007452 GA->getOffset(),
7453 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007454
7455 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7456 : X86ISD::TLSADDR;
7457
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007458 if (InFlag) {
7459 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007460 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007461 } else {
7462 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007463 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007464 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007465
7466 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007467 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007468
Rafael Espindola15f1b662009-04-24 12:59:40 +00007469 SDValue Flag = Chain.getValue(1);
7470 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007471}
7472
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007473// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007474static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007475LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007476 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007477 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007478 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7479 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007480 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007481 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007482 InFlag = Chain.getValue(1);
7483
Chris Lattnerb903bed2009-06-26 21:20:29 +00007484 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007485}
7486
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007487// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007488static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007489LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007490 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007491 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7492 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007493}
7494
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007495static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7496 SelectionDAG &DAG,
7497 const EVT PtrVT,
7498 bool is64Bit) {
7499 DebugLoc dl = GA->getDebugLoc();
7500
7501 // Get the start address of the TLS block for this module.
7502 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7503 .getInfo<X86MachineFunctionInfo>();
7504 MFI->incNumLocalDynamicTLSAccesses();
7505
7506 SDValue Base;
7507 if (is64Bit) {
7508 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7509 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7510 } else {
7511 SDValue InFlag;
7512 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7513 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7514 InFlag = Chain.getValue(1);
7515 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7516 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7517 }
7518
7519 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7520 // of Base.
7521
7522 // Build x@dtpoff.
7523 unsigned char OperandFlags = X86II::MO_DTPOFF;
7524 unsigned WrapperKind = X86ISD::Wrapper;
7525 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7526 GA->getValueType(0),
7527 GA->getOffset(), OperandFlags);
7528 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7529
7530 // Add x@dtpoff with the base.
7531 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7532}
7533
Hans Wennborg228756c2012-05-11 10:11:01 +00007534// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007535static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007536 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007537 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007538 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007539
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007540 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7541 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7542 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007543
Michael J. Spencerec38de22010-10-10 22:04:20 +00007544 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007545 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007546 MachinePointerInfo(Ptr),
7547 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007548
Chris Lattnerb903bed2009-06-26 21:20:29 +00007549 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007550 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7551 // initialexec.
7552 unsigned WrapperKind = X86ISD::Wrapper;
7553 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007554 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007555 } else if (model == TLSModel::InitialExec) {
7556 if (is64Bit) {
7557 OperandFlags = X86II::MO_GOTTPOFF;
7558 WrapperKind = X86ISD::WrapperRIP;
7559 } else {
7560 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7561 }
Chris Lattner18c59872009-06-27 04:16:01 +00007562 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007563 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007564 }
Eric Christopherfd179292009-08-27 18:07:15 +00007565
Hans Wennborg228756c2012-05-11 10:11:01 +00007566 // emit "addl x@ntpoff,%eax" (local exec)
7567 // or "addl x@indntpoff,%eax" (initial exec)
7568 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007569 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007570 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007571 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007572 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007573
Hans Wennborg228756c2012-05-11 10:11:01 +00007574 if (model == TLSModel::InitialExec) {
7575 if (isPIC && !is64Bit) {
7576 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7577 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7578 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007579 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007580
7581 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7582 MachinePointerInfo::getGOT(), false, false, false,
7583 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007584 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007585
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007586 // The address of the thread local variable is the add of the thread
7587 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007588 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007589}
7590
Dan Gohman475871a2008-07-27 21:46:04 +00007591SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007592X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007593
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007594 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007595 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007596
Eric Christopher30ef0e52010-06-03 04:07:48 +00007597 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007598 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007599
Eric Christopher30ef0e52010-06-03 04:07:48 +00007600 switch (model) {
7601 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007602 if (Subtarget->is64Bit())
7603 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7604 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007605 case TLSModel::LocalDynamic:
7606 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7607 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007608 case TLSModel::InitialExec:
7609 case TLSModel::LocalExec:
7610 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007611 Subtarget->is64Bit(),
7612 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007613 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007614 llvm_unreachable("Unknown TLS model.");
7615 }
7616
7617 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007618 // Darwin only has one model of TLS. Lower to that.
7619 unsigned char OpFlag = 0;
7620 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7621 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007622
Eric Christopher30ef0e52010-06-03 04:07:48 +00007623 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7624 // global base reg.
7625 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7626 !Subtarget->is64Bit();
7627 if (PIC32)
7628 OpFlag = X86II::MO_TLVP_PIC_BASE;
7629 else
7630 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007631 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007632 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007633 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007634 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007635 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007636
Eric Christopher30ef0e52010-06-03 04:07:48 +00007637 // With PIC32, the address is actually $g + Offset.
7638 if (PIC32)
7639 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7640 DAG.getNode(X86ISD::GlobalBaseReg,
7641 DebugLoc(), getPointerTy()),
7642 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007643
Eric Christopher30ef0e52010-06-03 04:07:48 +00007644 // Lowering the machine isd will make sure everything is in the right
7645 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007646 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007647 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007648 SDValue Args[] = { Chain, Offset };
7649 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007650
Eric Christopher30ef0e52010-06-03 04:07:48 +00007651 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7652 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7653 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007654
Eric Christopher30ef0e52010-06-03 04:07:48 +00007655 // And our return value (tls address) is in the standard call return value
7656 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007657 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007658 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7659 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007660 }
7661
7662 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007663 // Just use the implicit TLS architecture
7664 // Need to generate someting similar to:
7665 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7666 // ; from TEB
7667 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7668 // mov rcx, qword [rdx+rcx*8]
7669 // mov eax, .tls$:tlsvar
7670 // [rax+rcx] contains the address
7671 // Windows 64bit: gs:0x58
7672 // Windows 32bit: fs:__tls_array
7673
7674 // If GV is an alias then use the aliasee for determining
7675 // thread-localness.
7676 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7677 GV = GA->resolveAliasedGlobal(false);
7678 DebugLoc dl = GA->getDebugLoc();
7679 SDValue Chain = DAG.getEntryNode();
7680
7681 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7682 // %gs:0x58 (64-bit).
7683 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7684 ? Type::getInt8PtrTy(*DAG.getContext(),
7685 256)
7686 : Type::getInt32PtrTy(*DAG.getContext(),
7687 257));
7688
7689 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7690 Subtarget->is64Bit()
7691 ? DAG.getIntPtrConstant(0x58)
7692 : DAG.getExternalSymbol("_tls_array",
7693 getPointerTy()),
7694 MachinePointerInfo(Ptr),
7695 false, false, false, 0);
7696
7697 // Load the _tls_index variable
7698 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7699 if (Subtarget->is64Bit())
7700 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7701 IDX, MachinePointerInfo(), MVT::i32,
7702 false, false, 0);
7703 else
7704 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7705 false, false, false, 0);
7706
7707 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007708 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007709 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7710
7711 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7712 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7713 false, false, false, 0);
7714
7715 // Get the offset of start of .tls section
7716 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7717 GA->getValueType(0),
7718 GA->getOffset(), X86II::MO_SECREL);
7719 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7720
7721 // The address of the thread local variable is the add of the thread
7722 // pointer with the offset of the variable.
7723 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007724 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007725
David Blaikie4d6ccb52012-01-20 21:51:11 +00007726 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007727}
7728
Evan Cheng0db9fe62006-04-25 20:13:52 +00007729
Chad Rosierb90d2a92012-01-03 23:19:12 +00007730/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7731/// and take a 2 x i32 value to shift plus a shift amount.
7732SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007733 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007734 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007735 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007736 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007737 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007738 SDValue ShOpLo = Op.getOperand(0);
7739 SDValue ShOpHi = Op.getOperand(1);
7740 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007741 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007742 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007743 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007744
Dan Gohman475871a2008-07-27 21:46:04 +00007745 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007746 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007747 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7748 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007749 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007750 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7751 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007752 }
Evan Chenge3413162006-01-09 18:33:28 +00007753
Owen Anderson825b72b2009-08-11 20:47:22 +00007754 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7755 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007756 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007757 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007758
Dan Gohman475871a2008-07-27 21:46:04 +00007759 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007760 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007761 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7762 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007763
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007764 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007765 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7766 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007767 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007768 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7769 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007770 }
7771
Dan Gohman475871a2008-07-27 21:46:04 +00007772 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007773 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007774}
Evan Chenga3195e82006-01-12 22:54:21 +00007775
Dan Gohmand858e902010-04-17 15:26:15 +00007776SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7777 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007778 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007779
Dale Johannesen0488fb62010-09-30 23:57:10 +00007780 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007781 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007782
Owen Anderson825b72b2009-08-11 20:47:22 +00007783 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007784 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007785
Eli Friedman36df4992009-05-27 00:47:34 +00007786 // These are really Legal; return the operand so the caller accepts it as
7787 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007788 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007789 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007790 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007791 Subtarget->is64Bit()) {
7792 return Op;
7793 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007794
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007795 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007796 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007797 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007798 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007799 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007800 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007801 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007802 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007803 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007804 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7805}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007806
Owen Andersone50ed302009-08-10 22:56:29 +00007807SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007808 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007809 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007810 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007811 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007812 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007813 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007814 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007815 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007816 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007817 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007818
Chris Lattner492a43e2010-09-22 01:28:21 +00007819 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007820
Stuart Hastings84be9582011-06-02 15:57:11 +00007821 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7822 MachineMemOperand *MMO;
7823 if (FI) {
7824 int SSFI = FI->getIndex();
7825 MMO =
7826 DAG.getMachineFunction()
7827 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7828 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7829 } else {
7830 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7831 StackSlot = StackSlot.getOperand(1);
7832 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007833 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007834 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7835 X86ISD::FILD, DL,
7836 Tys, Ops, array_lengthof(Ops),
7837 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007838
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007839 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007840 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007841 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007842
7843 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7844 // shouldn't be necessary except that RFP cannot be live across
7845 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007846 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007847 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7848 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007849 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007850 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007851 SDValue Ops[] = {
7852 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7853 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007854 MachineMemOperand *MMO =
7855 DAG.getMachineFunction()
7856 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007857 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007858
Chris Lattner492a43e2010-09-22 01:28:21 +00007859 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7860 Ops, array_lengthof(Ops),
7861 Op.getValueType(), MMO);
7862 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007863 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007864 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007865 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007866
Evan Cheng0db9fe62006-04-25 20:13:52 +00007867 return Result;
7868}
7869
Bill Wendling8b8a6362009-01-17 03:56:04 +00007870// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007871SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7872 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007873 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007874 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007875 movq %rax, %xmm0
7876 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7877 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7878 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007879 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007880 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007881 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007882 addpd %xmm1, %xmm0
7883 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007884 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007885
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007886 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007887 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007888
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007889 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007890 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7891 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007892 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007893
Chris Lattner97484792012-01-25 09:56:22 +00007894 SmallVector<Constant*,2> CV1;
7895 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007896 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007897 CV1.push_back(
7898 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7899 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007900 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007901
Bill Wendling397ae212012-01-05 02:13:20 +00007902 // Load the 64-bit value into an XMM register.
7903 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7904 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007905 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007906 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007907 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007908 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7909 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7910 CLod0);
7911
Owen Anderson825b72b2009-08-11 20:47:22 +00007912 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007913 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007914 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007915 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007916 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007917 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007918
Craig Topperd0a31172012-01-10 06:37:29 +00007919 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007920 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7921 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7922 } else {
7923 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7924 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7925 S2F, 0x4E, DAG);
7926 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7927 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7928 Sub);
7929 }
7930
7931 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007932 DAG.getIntPtrConstant(0));
7933}
7934
Bill Wendling8b8a6362009-01-17 03:56:04 +00007935// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007936SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7937 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007938 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007939 // FP constant to bias correct the final result.
7940 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007941 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007942
7943 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007944 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007945 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007946
Eli Friedmanf3704762011-08-29 21:15:46 +00007947 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007948 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007949
Owen Anderson825b72b2009-08-11 20:47:22 +00007950 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007951 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007952 DAG.getIntPtrConstant(0));
7953
7954 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007955 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007956 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007957 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007958 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007959 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007960 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007961 MVT::v2f64, Bias)));
7962 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007963 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007964 DAG.getIntPtrConstant(0));
7965
7966 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007967 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007968
7969 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007970 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007971
Craig Topper69947b92012-04-23 06:57:04 +00007972 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007973 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007974 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007975 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007976 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007977
7978 // Handle final rounding.
7979 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007980}
7981
Dan Gohmand858e902010-04-17 15:26:15 +00007982SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7983 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007984 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007985 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007986
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007987 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007988 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7989 // the optimization here.
7990 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007991 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007992
Owen Andersone50ed302009-08-10 22:56:29 +00007993 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007994 EVT DstVT = Op.getValueType();
7995 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007996 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007997 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007998 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007999 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008000 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008001
8002 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008003 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008004 if (SrcVT == MVT::i32) {
8005 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8006 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8007 getPointerTy(), StackSlot, WordOff);
8008 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008009 StackSlot, MachinePointerInfo(),
8010 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008011 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008012 OffsetSlot, MachinePointerInfo(),
8013 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008014 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8015 return Fild;
8016 }
8017
8018 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8019 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008020 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008021 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008022 // For i64 source, we need to add the appropriate power of 2 if the input
8023 // was negative. This is the same as the optimization in
8024 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8025 // we must be careful to do the computation in x87 extended precision, not
8026 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008027 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8028 MachineMemOperand *MMO =
8029 DAG.getMachineFunction()
8030 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8031 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008032
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008033 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8034 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008035 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8036 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008037
8038 APInt FF(32, 0x5F800000ULL);
8039
8040 // Check whether the sign bit is set.
8041 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8042 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8043 ISD::SETLT);
8044
8045 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8046 SDValue FudgePtr = DAG.getConstantPool(
8047 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8048 getPointerTy());
8049
8050 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8051 SDValue Zero = DAG.getIntPtrConstant(0);
8052 SDValue Four = DAG.getIntPtrConstant(4);
8053 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8054 Zero, Four);
8055 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8056
8057 // Load the value out, extending it from f32 to f80.
8058 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008059 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008060 FudgePtr, MachinePointerInfo::getConstantPool(),
8061 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008062 // Extend everything to 80 bits to force it to be done on x87.
8063 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8064 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008065}
8066
Dan Gohman475871a2008-07-27 21:46:04 +00008067std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008068FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008069 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008070
Owen Andersone50ed302009-08-10 22:56:29 +00008071 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008072
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008073 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008074 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8075 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008076 }
8077
Owen Anderson825b72b2009-08-11 20:47:22 +00008078 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8079 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008080 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008081
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008082 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008084 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008085 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008086 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008087 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008088 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008089 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008090
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008091 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8092 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008093 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008094 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008095 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008096 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008097
Evan Cheng0db9fe62006-04-25 20:13:52 +00008098 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008099 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8100 Opc = X86ISD::WIN_FTOL;
8101 else
8102 switch (DstTy.getSimpleVT().SimpleTy) {
8103 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8104 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8105 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8106 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8107 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008108
Dan Gohman475871a2008-07-27 21:46:04 +00008109 SDValue Chain = DAG.getEntryNode();
8110 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008111 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008112 // FIXME This causes a redundant load/store if the SSE-class value is already
8113 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008114 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008115 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008116 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008117 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008118 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008119 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008120 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008121 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008122 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008123
Chris Lattner492a43e2010-09-22 01:28:21 +00008124 MachineMemOperand *MMO =
8125 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8126 MachineMemOperand::MOLoad, MemSize, MemSize);
8127 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8128 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008129 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008130 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008131 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8132 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008133
Chris Lattner07290932010-09-22 01:05:16 +00008134 MachineMemOperand *MMO =
8135 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8136 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008137
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008138 if (Opc != X86ISD::WIN_FTOL) {
8139 // Build the FP_TO_INT*_IN_MEM
8140 SDValue Ops[] = { Chain, Value, StackSlot };
8141 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8142 Ops, 3, DstTy, MMO);
8143 return std::make_pair(FIST, StackSlot);
8144 } else {
8145 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8146 DAG.getVTList(MVT::Other, MVT::Glue),
8147 Chain, Value);
8148 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8149 MVT::i32, ftol.getValue(1));
8150 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8151 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008152 SDValue Ops[] = { eax, edx };
8153 SDValue pair = IsReplace
8154 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8155 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008156 return std::make_pair(pair, SDValue());
8157 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008158}
8159
Dan Gohmand858e902010-04-17 15:26:15 +00008160SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8161 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008162 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008163 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008164
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008165 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8166 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008167 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008168 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8169 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008170
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008171 if (StackSlot.getNode())
8172 // Load the result.
8173 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8174 FIST, StackSlot, MachinePointerInfo(),
8175 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008176
8177 // The node is the result.
8178 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008179}
8180
Dan Gohmand858e902010-04-17 15:26:15 +00008181SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8182 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008183 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8184 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008185 SDValue FIST = Vals.first, StackSlot = Vals.second;
8186 assert(FIST.getNode() && "Unexpected failure");
8187
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008188 if (StackSlot.getNode())
8189 // Load the result.
8190 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8191 FIST, StackSlot, MachinePointerInfo(),
8192 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008193
8194 // The node is the result.
8195 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008196}
8197
Craig Topper43620672012-09-08 07:31:51 +00008198SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008199 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008200 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008201 EVT VT = Op.getValueType();
8202 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008203 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8204 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008205 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008206 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008207 }
Craig Topper43620672012-09-08 07:31:51 +00008208 Constant *C;
8209 if (EltVT == MVT::f64)
8210 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8211 else
8212 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8213 C = ConstantVector::getSplat(NumElts, C);
8214 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8215 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008216 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008217 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008218 false, false, false, Alignment);
8219 if (VT.isVector()) {
8220 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8221 return DAG.getNode(ISD::BITCAST, dl, VT,
8222 DAG.getNode(ISD::AND, dl, ANDVT,
8223 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8224 Op.getOperand(0)),
8225 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8226 }
Dale Johannesenace16102009-02-03 19:33:06 +00008227 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008228}
8229
Dan Gohmand858e902010-04-17 15:26:15 +00008230SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008231 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008232 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008233 EVT VT = Op.getValueType();
8234 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008235 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8236 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008237 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008238 NumElts = VT.getVectorNumElements();
8239 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008240 Constant *C;
8241 if (EltVT == MVT::f64)
8242 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8243 else
8244 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8245 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008246 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8247 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008248 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008249 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008250 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008251 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008252 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008253 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008254 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008255 DAG.getNode(ISD::BITCAST, dl, XORVT,
8256 Op.getOperand(0)),
8257 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008258 }
Craig Topper69947b92012-04-23 06:57:04 +00008259
8260 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008261}
8262
Dan Gohmand858e902010-04-17 15:26:15 +00008263SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008264 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008265 SDValue Op0 = Op.getOperand(0);
8266 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008267 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008268 EVT VT = Op.getValueType();
8269 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008270
8271 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008272 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008273 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008274 SrcVT = VT;
8275 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008276 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008277 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008278 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008279 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008280 }
8281
8282 // At this point the operands and the result should have the same
8283 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008284
Evan Cheng68c47cb2007-01-05 07:55:56 +00008285 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008286 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008287 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008288 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8289 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008290 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008291 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8292 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8293 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8294 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008295 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008296 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008297 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008298 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008299 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008300 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008301 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008302
8303 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008304 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008305 // Op0 is MVT::f32, Op1 is MVT::f64.
8306 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8307 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8308 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008309 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008310 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008311 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008312 }
8313
Evan Cheng73d6cf12007-01-05 21:37:56 +00008314 // Clear first operand sign bit.
8315 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008316 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008317 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8318 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008319 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008320 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8321 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8322 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8323 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008324 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008325 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008326 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008327 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008328 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008329 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008330 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008331
8332 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008333 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008334}
8335
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008336SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8337 SDValue N0 = Op.getOperand(0);
8338 DebugLoc dl = Op.getDebugLoc();
8339 EVT VT = Op.getValueType();
8340
8341 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8342 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8343 DAG.getConstant(1, VT));
8344 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8345}
8346
Dan Gohman076aee32009-03-04 19:44:21 +00008347/// Emit nodes that will be selected as "test Op0,Op0", or something
8348/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008349SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008350 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008351 DebugLoc dl = Op.getDebugLoc();
8352
Dan Gohman31125812009-03-07 01:58:32 +00008353 // CF and OF aren't always set the way we want. Determine which
8354 // of these we need.
8355 bool NeedCF = false;
8356 bool NeedOF = false;
8357 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008358 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008359 case X86::COND_A: case X86::COND_AE:
8360 case X86::COND_B: case X86::COND_BE:
8361 NeedCF = true;
8362 break;
8363 case X86::COND_G: case X86::COND_GE:
8364 case X86::COND_L: case X86::COND_LE:
8365 case X86::COND_O: case X86::COND_NO:
8366 NeedOF = true;
8367 break;
Dan Gohman31125812009-03-07 01:58:32 +00008368 }
8369
Dan Gohman076aee32009-03-04 19:44:21 +00008370 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008371 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8372 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008373 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8374 // Emit a CMP with 0, which is the TEST pattern.
8375 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8376 DAG.getConstant(0, Op.getValueType()));
8377
8378 unsigned Opcode = 0;
8379 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008380
8381 // Truncate operations may prevent the merge of the SETCC instruction
8382 // and the arithmetic intruction before it. Attempt to truncate the operands
8383 // of the arithmetic instruction and use a reduced bit-width instruction.
8384 bool NeedTruncation = false;
8385 SDValue ArithOp = Op;
8386 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8387 SDValue Arith = Op->getOperand(0);
8388 // Both the trunc and the arithmetic op need to have one user each.
8389 if (Arith->hasOneUse())
8390 switch (Arith.getOpcode()) {
8391 default: break;
8392 case ISD::ADD:
8393 case ISD::SUB:
8394 case ISD::AND:
8395 case ISD::OR:
8396 case ISD::XOR: {
8397 NeedTruncation = true;
8398 ArithOp = Arith;
8399 }
8400 }
8401 }
8402
8403 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8404 // which may be the result of a CAST. We use the variable 'Op', which is the
8405 // non-casted variable when we check for possible users.
8406 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008407 case ISD::ADD:
8408 // Due to an isel shortcoming, be conservative if this add is likely to be
8409 // selected as part of a load-modify-store instruction. When the root node
8410 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8411 // uses of other nodes in the match, such as the ADD in this case. This
8412 // leads to the ADD being left around and reselected, with the result being
8413 // two adds in the output. Alas, even if none our users are stores, that
8414 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8415 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8416 // climbing the DAG back to the root, and it doesn't seem to be worth the
8417 // effort.
8418 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008419 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8420 if (UI->getOpcode() != ISD::CopyToReg &&
8421 UI->getOpcode() != ISD::SETCC &&
8422 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008423 goto default_case;
8424
8425 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008426 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008427 // An add of one will be selected as an INC.
8428 if (C->getAPIntValue() == 1) {
8429 Opcode = X86ISD::INC;
8430 NumOperands = 1;
8431 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008432 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008433
8434 // An add of negative one (subtract of one) will be selected as a DEC.
8435 if (C->getAPIntValue().isAllOnesValue()) {
8436 Opcode = X86ISD::DEC;
8437 NumOperands = 1;
8438 break;
8439 }
Dan Gohman076aee32009-03-04 19:44:21 +00008440 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008441
8442 // Otherwise use a regular EFLAGS-setting add.
8443 Opcode = X86ISD::ADD;
8444 NumOperands = 2;
8445 break;
8446 case ISD::AND: {
8447 // If the primary and result isn't used, don't bother using X86ISD::AND,
8448 // because a TEST instruction will be better.
8449 bool NonFlagUse = false;
8450 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8451 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8452 SDNode *User = *UI;
8453 unsigned UOpNo = UI.getOperandNo();
8454 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8455 // Look pass truncate.
8456 UOpNo = User->use_begin().getOperandNo();
8457 User = *User->use_begin();
8458 }
8459
8460 if (User->getOpcode() != ISD::BRCOND &&
8461 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008462 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008463 NonFlagUse = true;
8464 break;
8465 }
Dan Gohman076aee32009-03-04 19:44:21 +00008466 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008467
8468 if (!NonFlagUse)
8469 break;
8470 }
8471 // FALL THROUGH
8472 case ISD::SUB:
8473 case ISD::OR:
8474 case ISD::XOR:
8475 // Due to the ISEL shortcoming noted above, be conservative if this op is
8476 // likely to be selected as part of a load-modify-store instruction.
8477 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8478 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8479 if (UI->getOpcode() == ISD::STORE)
8480 goto default_case;
8481
8482 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008483 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008484 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008485 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008486 case ISD::OR: Opcode = X86ISD::OR; break;
8487 case ISD::XOR: Opcode = X86ISD::XOR; break;
8488 case ISD::AND: Opcode = X86ISD::AND; break;
8489 }
8490
8491 NumOperands = 2;
8492 break;
8493 case X86ISD::ADD:
8494 case X86ISD::SUB:
8495 case X86ISD::INC:
8496 case X86ISD::DEC:
8497 case X86ISD::OR:
8498 case X86ISD::XOR:
8499 case X86ISD::AND:
8500 return SDValue(Op.getNode(), 1);
8501 default:
8502 default_case:
8503 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008504 }
8505
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008506 // If we found that truncation is beneficial, perform the truncation and
8507 // update 'Op'.
8508 if (NeedTruncation) {
8509 EVT VT = Op.getValueType();
8510 SDValue WideVal = Op->getOperand(0);
8511 EVT WideVT = WideVal.getValueType();
8512 unsigned ConvertedOp = 0;
8513 // Use a target machine opcode to prevent further DAGCombine
8514 // optimizations that may separate the arithmetic operations
8515 // from the setcc node.
8516 switch (WideVal.getOpcode()) {
8517 default: break;
8518 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8519 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8520 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8521 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8522 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8523 }
8524
8525 if (ConvertedOp) {
8526 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8527 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8528 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8529 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8530 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8531 }
8532 }
8533 }
8534
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008535 if (Opcode == 0)
8536 // Emit a CMP with 0, which is the TEST pattern.
8537 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8538 DAG.getConstant(0, Op.getValueType()));
8539
8540 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8541 SmallVector<SDValue, 4> Ops;
8542 for (unsigned i = 0; i != NumOperands; ++i)
8543 Ops.push_back(Op.getOperand(i));
8544
8545 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8546 DAG.ReplaceAllUsesWith(Op, New);
8547 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008548}
8549
8550/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8551/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008552SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008553 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008554 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8555 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008556 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008557
8558 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008559 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8560 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8561 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8562 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8563 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8564 Op0, Op1);
8565 return SDValue(Sub.getNode(), 1);
8566 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008567 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008568}
8569
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008570/// Convert a comparison if required by the subtarget.
8571SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8572 SelectionDAG &DAG) const {
8573 // If the subtarget does not support the FUCOMI instruction, floating-point
8574 // comparisons have to be converted.
8575 if (Subtarget->hasCMov() ||
8576 Cmp.getOpcode() != X86ISD::CMP ||
8577 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8578 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8579 return Cmp;
8580
8581 // The instruction selector will select an FUCOM instruction instead of
8582 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8583 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8584 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8585 DebugLoc dl = Cmp.getDebugLoc();
8586 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8587 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8588 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8589 DAG.getConstant(8, MVT::i8));
8590 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8591 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8592}
8593
Evan Chengd40d03e2010-01-06 19:38:29 +00008594/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8595/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008596SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8597 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008598 SDValue Op0 = And.getOperand(0);
8599 SDValue Op1 = And.getOperand(1);
8600 if (Op0.getOpcode() == ISD::TRUNCATE)
8601 Op0 = Op0.getOperand(0);
8602 if (Op1.getOpcode() == ISD::TRUNCATE)
8603 Op1 = Op1.getOperand(0);
8604
Evan Chengd40d03e2010-01-06 19:38:29 +00008605 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008606 if (Op1.getOpcode() == ISD::SHL)
8607 std::swap(Op0, Op1);
8608 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008609 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8610 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008611 // If we looked past a truncate, check that it's only truncating away
8612 // known zeros.
8613 unsigned BitWidth = Op0.getValueSizeInBits();
8614 unsigned AndBitWidth = And.getValueSizeInBits();
8615 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008616 APInt Zeros, Ones;
8617 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008618 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8619 return SDValue();
8620 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008621 LHS = Op1;
8622 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008623 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008624 } else if (Op1.getOpcode() == ISD::Constant) {
8625 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008626 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008627 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008628
8629 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008630 LHS = AndLHS.getOperand(0);
8631 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008632 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008633
8634 // Use BT if the immediate can't be encoded in a TEST instruction.
8635 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8636 LHS = AndLHS;
8637 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8638 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008639 }
Evan Cheng0488db92007-09-25 01:57:46 +00008640
Evan Chengd40d03e2010-01-06 19:38:29 +00008641 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008642 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008643 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008644 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008645 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008646 // Also promote i16 to i32 for performance / code size reason.
8647 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008648 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008649 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008650
Evan Chengd40d03e2010-01-06 19:38:29 +00008651 // If the operand types disagree, extend the shift amount to match. Since
8652 // BT ignores high bits (like shifts) we can use anyextend.
8653 if (LHS.getValueType() != RHS.getValueType())
8654 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008655
Evan Chengd40d03e2010-01-06 19:38:29 +00008656 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8657 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8658 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8659 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008660 }
8661
Evan Cheng54de3ea2010-01-05 06:52:31 +00008662 return SDValue();
8663}
8664
Dan Gohmand858e902010-04-17 15:26:15 +00008665SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008666
8667 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8668
Evan Cheng54de3ea2010-01-05 06:52:31 +00008669 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8670 SDValue Op0 = Op.getOperand(0);
8671 SDValue Op1 = Op.getOperand(1);
8672 DebugLoc dl = Op.getDebugLoc();
8673 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8674
8675 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008676 // Lower (X & (1 << N)) == 0 to BT(X, N).
8677 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8678 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008679 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008680 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008681 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008682 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8683 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8684 if (NewSetCC.getNode())
8685 return NewSetCC;
8686 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008687
Chris Lattner481eebc2010-12-19 21:23:48 +00008688 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8689 // these.
8690 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008691 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008692 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8693 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008694
Chris Lattner481eebc2010-12-19 21:23:48 +00008695 // If the input is a setcc, then reuse the input setcc or use a new one with
8696 // the inverted condition.
8697 if (Op0.getOpcode() == X86ISD::SETCC) {
8698 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8699 bool Invert = (CC == ISD::SETNE) ^
8700 cast<ConstantSDNode>(Op1)->isNullValue();
8701 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008702
Evan Cheng2c755ba2010-02-27 07:36:59 +00008703 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008704 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8705 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8706 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008707 }
8708
Evan Chenge5b51ac2010-04-17 06:13:15 +00008709 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008710 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008711 if (X86CC == X86::COND_INVALID)
8712 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008713
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008714 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008715 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008716 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008717 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008718}
8719
Craig Topper89af15e2011-09-18 08:03:58 +00008720// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008721// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008722static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008723 EVT VT = Op.getValueType();
8724
Craig Topper7a9a28b2012-08-12 02:23:29 +00008725 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008726 "Unsupported value type for operation");
8727
Craig Topper66ddd152012-04-27 22:54:43 +00008728 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008729 DebugLoc dl = Op.getDebugLoc();
8730 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008731
8732 // Extract the LHS vectors
8733 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008734 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8735 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008736
8737 // Extract the RHS vectors
8738 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008739 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8740 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008741
8742 // Issue the operation on the smaller types and concatenate the result back
8743 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8744 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8745 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8746 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8747 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8748}
8749
8750
Dan Gohmand858e902010-04-17 15:26:15 +00008751SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008752 SDValue Cond;
8753 SDValue Op0 = Op.getOperand(0);
8754 SDValue Op1 = Op.getOperand(1);
8755 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008756 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008757 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8758 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008759 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008760
8761 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008762#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008763 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008764 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8765#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008766
Craig Topper523908d2012-08-13 02:34:03 +00008767 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008768 bool Swap = false;
8769
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008770 // SSE Condition code mapping:
8771 // 0 - EQ
8772 // 1 - LT
8773 // 2 - LE
8774 // 3 - UNORD
8775 // 4 - NEQ
8776 // 5 - NLT
8777 // 6 - NLE
8778 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008779 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008780 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008781 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008782 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008783 case ISD::SETOGT:
8784 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008785 case ISD::SETLT:
8786 case ISD::SETOLT: SSECC = 1; break;
8787 case ISD::SETOGE:
8788 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008789 case ISD::SETLE:
8790 case ISD::SETOLE: SSECC = 2; break;
8791 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008792 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008793 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008794 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008795 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008796 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008797 case ISD::SETUGT: SSECC = 6; break;
8798 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008799 case ISD::SETUEQ:
8800 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008801 }
8802 if (Swap)
8803 std::swap(Op0, Op1);
8804
Nate Begemanfb8ead02008-07-25 19:05:58 +00008805 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008806 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008807 unsigned CC0, CC1;
8808 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008809 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008810 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8811 } else {
8812 assert(SetCCOpcode == ISD::SETONE);
8813 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008814 }
Craig Topper523908d2012-08-13 02:34:03 +00008815
8816 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8817 DAG.getConstant(CC0, MVT::i8));
8818 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8819 DAG.getConstant(CC1, MVT::i8));
8820 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008821 }
8822 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008823 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8824 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008825 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008826
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008827 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008828 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008829 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008830
Nate Begeman30a0de92008-07-17 16:51:19 +00008831 // We are handling one of the integer comparisons here. Since SSE only has
8832 // GT and EQ comparisons for integer, swapping operands and multiple
8833 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008834 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008835 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008836
Nate Begeman30a0de92008-07-17 16:51:19 +00008837 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008838 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008839 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008840 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008841 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008842 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008843 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008844 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008845 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008846 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008847 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008848 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008849 }
8850 if (Swap)
8851 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008852
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008853 // Check that the operation in question is available (most are plain SSE2,
8854 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008855 if (VT == MVT::v2i64) {
8856 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8857 return SDValue();
8858 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8859 return SDValue();
8860 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008861
Nate Begeman30a0de92008-07-17 16:51:19 +00008862 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8863 // bits of the inputs before performing those operations.
8864 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008865 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008866 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8867 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008868 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008869 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8870 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008871 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8872 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008873 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008874
Dale Johannesenace16102009-02-03 19:33:06 +00008875 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008876
8877 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008878 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008879 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008880
Nate Begeman30a0de92008-07-17 16:51:19 +00008881 return Result;
8882}
Evan Cheng0488db92007-09-25 01:57:46 +00008883
Evan Cheng370e5342008-12-03 08:38:43 +00008884// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008885static bool isX86LogicalCmp(SDValue Op) {
8886 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008887 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8888 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008889 return true;
8890 if (Op.getResNo() == 1 &&
8891 (Opc == X86ISD::ADD ||
8892 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008893 Opc == X86ISD::ADC ||
8894 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008895 Opc == X86ISD::SMUL ||
8896 Opc == X86ISD::UMUL ||
8897 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008898 Opc == X86ISD::DEC ||
8899 Opc == X86ISD::OR ||
8900 Opc == X86ISD::XOR ||
8901 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008902 return true;
8903
Chris Lattner9637d5b2010-12-05 07:49:54 +00008904 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8905 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008906
Dan Gohman076aee32009-03-04 19:44:21 +00008907 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008908}
8909
Chris Lattnera2b56002010-12-05 01:23:24 +00008910static bool isZero(SDValue V) {
8911 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8912 return C && C->isNullValue();
8913}
8914
Chris Lattner96908b12010-12-05 02:00:51 +00008915static bool isAllOnes(SDValue V) {
8916 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8917 return C && C->isAllOnesValue();
8918}
8919
Evan Chengb64dd5f2012-08-07 22:21:00 +00008920static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
8921 if (V.getOpcode() != ISD::TRUNCATE)
8922 return false;
8923
8924 SDValue VOp0 = V.getOperand(0);
8925 unsigned InBits = VOp0.getValueSizeInBits();
8926 unsigned Bits = V.getValueSizeInBits();
8927 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
8928}
8929
Dan Gohmand858e902010-04-17 15:26:15 +00008930SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008931 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008932 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008933 SDValue Op1 = Op.getOperand(1);
8934 SDValue Op2 = Op.getOperand(2);
8935 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008936 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008937
Dan Gohman1a492952009-10-20 16:22:37 +00008938 if (Cond.getOpcode() == ISD::SETCC) {
8939 SDValue NewCond = LowerSETCC(Cond, DAG);
8940 if (NewCond.getNode())
8941 Cond = NewCond;
8942 }
Evan Cheng734503b2006-09-11 02:19:56 +00008943
Chris Lattnera2b56002010-12-05 01:23:24 +00008944 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008945 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008946 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008947 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008948 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008949 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8950 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008951 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008952
Chris Lattnera2b56002010-12-05 01:23:24 +00008953 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008954
8955 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008956 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8957 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008958
8959 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00008960 // Apply further optimizations for special cases
8961 // (select (x != 0), -1, 0) -> neg & sbb
8962 // (select (x == 0), 0, -1) -> neg & sbb
8963 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00008964 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00008965 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
8966 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00008967 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
8968 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00008969 CmpOp0);
8970 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8971 DAG.getConstant(X86::COND_B, MVT::i8),
8972 SDValue(Neg.getNode(), 1));
8973 return Res;
8974 }
8975
Chris Lattnera2b56002010-12-05 01:23:24 +00008976 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8977 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008978 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008979
Chris Lattner96908b12010-12-05 02:00:51 +00008980 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008981 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8982 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008983
Chris Lattner96908b12010-12-05 02:00:51 +00008984 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8985 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008986
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008987 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008988 if (N2C == 0 || !N2C->isNullValue())
8989 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8990 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008991 }
8992 }
8993
Chris Lattnera2b56002010-12-05 01:23:24 +00008994 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008995 if (Cond.getOpcode() == ISD::AND &&
8996 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8997 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008998 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008999 Cond = Cond.getOperand(0);
9000 }
9001
Evan Cheng3f41d662007-10-08 22:16:29 +00009002 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9003 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009004 unsigned CondOpcode = Cond.getOpcode();
9005 if (CondOpcode == X86ISD::SETCC ||
9006 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009007 CC = Cond.getOperand(0);
9008
Dan Gohman475871a2008-07-27 21:46:04 +00009009 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009010 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009011 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009012
Evan Cheng3f41d662007-10-08 22:16:29 +00009013 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009014 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009015 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009016 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009017
Chris Lattnerd1980a52009-03-12 06:52:53 +00009018 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9019 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009020 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009021 addTest = false;
9022 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009023 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9024 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9025 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9026 Cond.getOperand(0).getValueType() != MVT::i8)) {
9027 SDValue LHS = Cond.getOperand(0);
9028 SDValue RHS = Cond.getOperand(1);
9029 unsigned X86Opcode;
9030 unsigned X86Cond;
9031 SDVTList VTs;
9032 switch (CondOpcode) {
9033 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9034 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9035 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9036 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9037 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9038 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9039 default: llvm_unreachable("unexpected overflowing operator");
9040 }
9041 if (CondOpcode == ISD::UMULO)
9042 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9043 MVT::i32);
9044 else
9045 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9046
9047 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9048
9049 if (CondOpcode == ISD::UMULO)
9050 Cond = X86Op.getValue(2);
9051 else
9052 Cond = X86Op.getValue(1);
9053
9054 CC = DAG.getConstant(X86Cond, MVT::i8);
9055 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009056 }
9057
9058 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009059 // Look pass the truncate if the high bits are known zero.
9060 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9061 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009062
9063 // We know the result of AND is compared against zero. Try to match
9064 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009065 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009066 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009067 if (NewSetCC.getNode()) {
9068 CC = NewSetCC.getOperand(0);
9069 Cond = NewSetCC.getOperand(1);
9070 addTest = false;
9071 }
9072 }
9073 }
9074
9075 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009076 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009077 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009078 }
9079
Benjamin Kramere915ff32010-12-22 23:09:28 +00009080 // a < b ? -1 : 0 -> RES = ~setcc_carry
9081 // a < b ? 0 : -1 -> RES = setcc_carry
9082 // a >= b ? -1 : 0 -> RES = setcc_carry
9083 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009084 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009085 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009086 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9087
9088 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9089 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9090 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9091 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9092 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9093 return DAG.getNOT(DL, Res, Res.getValueType());
9094 return Res;
9095 }
9096 }
9097
Evan Cheng0488db92007-09-25 01:57:46 +00009098 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9099 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009100 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009101 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009102 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009103}
9104
Evan Cheng370e5342008-12-03 08:38:43 +00009105// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9106// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9107// from the AND / OR.
9108static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9109 Opc = Op.getOpcode();
9110 if (Opc != ISD::OR && Opc != ISD::AND)
9111 return false;
9112 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9113 Op.getOperand(0).hasOneUse() &&
9114 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9115 Op.getOperand(1).hasOneUse());
9116}
9117
Evan Cheng961d6d42009-02-02 08:19:07 +00009118// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9119// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009120static bool isXor1OfSetCC(SDValue Op) {
9121 if (Op.getOpcode() != ISD::XOR)
9122 return false;
9123 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9124 if (N1C && N1C->getAPIntValue() == 1) {
9125 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9126 Op.getOperand(0).hasOneUse();
9127 }
9128 return false;
9129}
9130
Dan Gohmand858e902010-04-17 15:26:15 +00009131SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009132 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009133 SDValue Chain = Op.getOperand(0);
9134 SDValue Cond = Op.getOperand(1);
9135 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009136 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009137 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009138 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009139
Dan Gohman1a492952009-10-20 16:22:37 +00009140 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009141 // Check for setcc([su]{add,sub,mul}o == 0).
9142 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9143 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9144 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9145 Cond.getOperand(0).getResNo() == 1 &&
9146 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9147 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9148 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9149 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9150 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9151 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9152 Inverted = true;
9153 Cond = Cond.getOperand(0);
9154 } else {
9155 SDValue NewCond = LowerSETCC(Cond, DAG);
9156 if (NewCond.getNode())
9157 Cond = NewCond;
9158 }
Dan Gohman1a492952009-10-20 16:22:37 +00009159 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009160#if 0
9161 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009162 else if (Cond.getOpcode() == X86ISD::ADD ||
9163 Cond.getOpcode() == X86ISD::SUB ||
9164 Cond.getOpcode() == X86ISD::SMUL ||
9165 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009166 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009167#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009168
Evan Chengad9c0a32009-12-15 00:53:42 +00009169 // Look pass (and (setcc_carry (cmp ...)), 1).
9170 if (Cond.getOpcode() == ISD::AND &&
9171 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9172 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009173 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009174 Cond = Cond.getOperand(0);
9175 }
9176
Evan Cheng3f41d662007-10-08 22:16:29 +00009177 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9178 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009179 unsigned CondOpcode = Cond.getOpcode();
9180 if (CondOpcode == X86ISD::SETCC ||
9181 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009182 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009183
Dan Gohman475871a2008-07-27 21:46:04 +00009184 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009185 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009186 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009187 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009188 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009189 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009190 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009191 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009192 default: break;
9193 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009194 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009195 // These can only come from an arithmetic instruction with overflow,
9196 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009197 Cond = Cond.getNode()->getOperand(1);
9198 addTest = false;
9199 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009200 }
Evan Cheng0488db92007-09-25 01:57:46 +00009201 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009202 }
9203 CondOpcode = Cond.getOpcode();
9204 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9205 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9206 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9207 Cond.getOperand(0).getValueType() != MVT::i8)) {
9208 SDValue LHS = Cond.getOperand(0);
9209 SDValue RHS = Cond.getOperand(1);
9210 unsigned X86Opcode;
9211 unsigned X86Cond;
9212 SDVTList VTs;
9213 switch (CondOpcode) {
9214 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9215 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9216 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9217 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9218 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9219 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9220 default: llvm_unreachable("unexpected overflowing operator");
9221 }
9222 if (Inverted)
9223 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9224 if (CondOpcode == ISD::UMULO)
9225 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9226 MVT::i32);
9227 else
9228 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9229
9230 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9231
9232 if (CondOpcode == ISD::UMULO)
9233 Cond = X86Op.getValue(2);
9234 else
9235 Cond = X86Op.getValue(1);
9236
9237 CC = DAG.getConstant(X86Cond, MVT::i8);
9238 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009239 } else {
9240 unsigned CondOpc;
9241 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9242 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009243 if (CondOpc == ISD::OR) {
9244 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9245 // two branches instead of an explicit OR instruction with a
9246 // separate test.
9247 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009248 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009249 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009250 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009251 Chain, Dest, CC, Cmp);
9252 CC = Cond.getOperand(1).getOperand(0);
9253 Cond = Cmp;
9254 addTest = false;
9255 }
9256 } else { // ISD::AND
9257 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9258 // two branches instead of an explicit AND instruction with a
9259 // separate test. However, we only do this if this block doesn't
9260 // have a fall-through edge, because this requires an explicit
9261 // jmp when the condition is false.
9262 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009263 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009264 Op.getNode()->hasOneUse()) {
9265 X86::CondCode CCode =
9266 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9267 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009268 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009269 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009270 // Look for an unconditional branch following this conditional branch.
9271 // We need this because we need to reverse the successors in order
9272 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009273 if (User->getOpcode() == ISD::BR) {
9274 SDValue FalseBB = User->getOperand(1);
9275 SDNode *NewBR =
9276 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009277 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009278 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009279 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009280
Dale Johannesene4d209d2009-02-03 20:21:25 +00009281 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009282 Chain, Dest, CC, Cmp);
9283 X86::CondCode CCode =
9284 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9285 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009286 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009287 Cond = Cmp;
9288 addTest = false;
9289 }
9290 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009291 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009292 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9293 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9294 // It should be transformed during dag combiner except when the condition
9295 // is set by a arithmetics with overflow node.
9296 X86::CondCode CCode =
9297 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9298 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009299 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009300 Cond = Cond.getOperand(0).getOperand(1);
9301 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009302 } else if (Cond.getOpcode() == ISD::SETCC &&
9303 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9304 // For FCMP_OEQ, we can emit
9305 // two branches instead of an explicit AND instruction with a
9306 // separate test. However, we only do this if this block doesn't
9307 // have a fall-through edge, because this requires an explicit
9308 // jmp when the condition is false.
9309 if (Op.getNode()->hasOneUse()) {
9310 SDNode *User = *Op.getNode()->use_begin();
9311 // Look for an unconditional branch following this conditional branch.
9312 // We need this because we need to reverse the successors in order
9313 // to implement FCMP_OEQ.
9314 if (User->getOpcode() == ISD::BR) {
9315 SDValue FalseBB = User->getOperand(1);
9316 SDNode *NewBR =
9317 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9318 assert(NewBR == User);
9319 (void)NewBR;
9320 Dest = FalseBB;
9321
9322 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9323 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009324 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009325 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9326 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9327 Chain, Dest, CC, Cmp);
9328 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9329 Cond = Cmp;
9330 addTest = false;
9331 }
9332 }
9333 } else if (Cond.getOpcode() == ISD::SETCC &&
9334 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9335 // For FCMP_UNE, we can emit
9336 // two branches instead of an explicit AND instruction with a
9337 // separate test. However, we only do this if this block doesn't
9338 // have a fall-through edge, because this requires an explicit
9339 // jmp when the condition is false.
9340 if (Op.getNode()->hasOneUse()) {
9341 SDNode *User = *Op.getNode()->use_begin();
9342 // Look for an unconditional branch following this conditional branch.
9343 // We need this because we need to reverse the successors in order
9344 // to implement FCMP_UNE.
9345 if (User->getOpcode() == ISD::BR) {
9346 SDValue FalseBB = User->getOperand(1);
9347 SDNode *NewBR =
9348 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9349 assert(NewBR == User);
9350 (void)NewBR;
9351
9352 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9353 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009354 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009355 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9356 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9357 Chain, Dest, CC, Cmp);
9358 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9359 Cond = Cmp;
9360 addTest = false;
9361 Dest = FalseBB;
9362 }
9363 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009364 }
Evan Cheng0488db92007-09-25 01:57:46 +00009365 }
9366
9367 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009368 // Look pass the truncate if the high bits are known zero.
9369 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9370 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009371
9372 // We know the result of AND is compared against zero. Try to match
9373 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009374 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009375 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9376 if (NewSetCC.getNode()) {
9377 CC = NewSetCC.getOperand(0);
9378 Cond = NewSetCC.getOperand(1);
9379 addTest = false;
9380 }
9381 }
9382 }
9383
9384 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009385 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009386 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009387 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009388 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009389 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009390 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009391}
9392
Anton Korobeynikove060b532007-04-17 19:34:00 +00009393
9394// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9395// Calls to _alloca is needed to probe the stack when allocating more than 4k
9396// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9397// that the guard pages used by the OS virtual memory manager are allocated in
9398// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009399SDValue
9400X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009401 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009402 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009403 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009404 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009405 "are being used");
9406 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009407 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009408
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009409 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009410 SDValue Chain = Op.getOperand(0);
9411 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009412 // FIXME: Ensure alignment here
9413
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009414 bool Is64Bit = Subtarget->is64Bit();
9415 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009416
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009417 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009418 MachineFunction &MF = DAG.getMachineFunction();
9419 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009420
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009421 if (Is64Bit) {
9422 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009423 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009424 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009425
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009426 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009427 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009428 if (I->hasNestAttr())
9429 report_fatal_error("Cannot use segmented stacks with functions that "
9430 "have nested arguments.");
9431 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009432
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009433 const TargetRegisterClass *AddrRegClass =
9434 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9435 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9436 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9437 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9438 DAG.getRegister(Vreg, SPTy));
9439 SDValue Ops1[2] = { Value, Chain };
9440 return DAG.getMergeValues(Ops1, 2, dl);
9441 } else {
9442 SDValue Flag;
9443 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009444
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009445 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9446 Flag = Chain.getValue(1);
9447 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009448
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009449 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9450 Flag = Chain.getValue(1);
9451
9452 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9453
9454 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9455 return DAG.getMergeValues(Ops1, 2, dl);
9456 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009457}
9458
Dan Gohmand858e902010-04-17 15:26:15 +00009459SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009460 MachineFunction &MF = DAG.getMachineFunction();
9461 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9462
Dan Gohman69de1932008-02-06 22:27:42 +00009463 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009464 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009465
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009466 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009467 // vastart just stores the address of the VarArgsFrameIndex slot into the
9468 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009469 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9470 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009471 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9472 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009473 }
9474
9475 // __va_list_tag:
9476 // gp_offset (0 - 6 * 8)
9477 // fp_offset (48 - 48 + 8 * 16)
9478 // overflow_arg_area (point to parameters coming in memory).
9479 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009480 SmallVector<SDValue, 8> MemOps;
9481 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009482 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009483 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009484 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9485 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009486 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009487 MemOps.push_back(Store);
9488
9489 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009490 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009491 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009492 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009493 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9494 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009495 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009496 MemOps.push_back(Store);
9497
9498 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009499 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009500 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009501 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9502 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009503 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9504 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009505 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009506 MemOps.push_back(Store);
9507
9508 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009509 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009510 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009511 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9512 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009513 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9514 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009515 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009516 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009517 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009518}
9519
Dan Gohmand858e902010-04-17 15:26:15 +00009520SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009521 assert(Subtarget->is64Bit() &&
9522 "LowerVAARG only handles 64-bit va_arg!");
9523 assert((Subtarget->isTargetLinux() ||
9524 Subtarget->isTargetDarwin()) &&
9525 "Unhandled target in LowerVAARG");
9526 assert(Op.getNode()->getNumOperands() == 4);
9527 SDValue Chain = Op.getOperand(0);
9528 SDValue SrcPtr = Op.getOperand(1);
9529 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9530 unsigned Align = Op.getConstantOperandVal(3);
9531 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009532
Dan Gohman320afb82010-10-12 18:00:49 +00009533 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009534 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009535 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9536 uint8_t ArgMode;
9537
9538 // Decide which area this value should be read from.
9539 // TODO: Implement the AMD64 ABI in its entirety. This simple
9540 // selection mechanism works only for the basic types.
9541 if (ArgVT == MVT::f80) {
9542 llvm_unreachable("va_arg for f80 not yet implemented");
9543 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9544 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9545 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9546 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9547 } else {
9548 llvm_unreachable("Unhandled argument type in LowerVAARG");
9549 }
9550
9551 if (ArgMode == 2) {
9552 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009553 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009554 !(DAG.getMachineFunction()
9555 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009556 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009557 }
9558
9559 // Insert VAARG_64 node into the DAG
9560 // VAARG_64 returns two values: Variable Argument Address, Chain
9561 SmallVector<SDValue, 11> InstOps;
9562 InstOps.push_back(Chain);
9563 InstOps.push_back(SrcPtr);
9564 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9565 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9566 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9567 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9568 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9569 VTs, &InstOps[0], InstOps.size(),
9570 MVT::i64,
9571 MachinePointerInfo(SV),
9572 /*Align=*/0,
9573 /*Volatile=*/false,
9574 /*ReadMem=*/true,
9575 /*WriteMem=*/true);
9576 Chain = VAARG.getValue(1);
9577
9578 // Load the next argument and return it
9579 return DAG.getLoad(ArgVT, dl,
9580 Chain,
9581 VAARG,
9582 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009583 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009584}
9585
Dan Gohmand858e902010-04-17 15:26:15 +00009586SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009587 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009588 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009589 SDValue Chain = Op.getOperand(0);
9590 SDValue DstPtr = Op.getOperand(1);
9591 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009592 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9593 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009594 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009595
Chris Lattnere72f2022010-09-21 05:40:29 +00009596 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009597 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009598 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009599 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009600}
9601
Craig Topper80e46362012-01-23 06:16:53 +00009602// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9603// may or may not be a constant. Takes immediate version of shift as input.
9604static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9605 SDValue SrcOp, SDValue ShAmt,
9606 SelectionDAG &DAG) {
9607 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9608
9609 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009610 // Constant may be a TargetConstant. Use a regular constant.
9611 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009612 switch (Opc) {
9613 default: llvm_unreachable("Unknown target vector shift node");
9614 case X86ISD::VSHLI:
9615 case X86ISD::VSRLI:
9616 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009617 return DAG.getNode(Opc, dl, VT, SrcOp,
9618 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009619 }
9620 }
9621
9622 // Change opcode to non-immediate version
9623 switch (Opc) {
9624 default: llvm_unreachable("Unknown target vector shift node");
9625 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9626 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9627 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9628 }
9629
9630 // Need to build a vector containing shift amount
9631 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9632 SDValue ShOps[4];
9633 ShOps[0] = ShAmt;
9634 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009635 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009636 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009637
9638 // The return type has to be a 128-bit type with the same element
9639 // type as the input type.
9640 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9641 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9642
9643 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009644 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9645}
9646
Dan Gohman475871a2008-07-27 21:46:04 +00009647SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009648X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009649 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009650 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009651 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009652 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009653 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009654 case Intrinsic::x86_sse_comieq_ss:
9655 case Intrinsic::x86_sse_comilt_ss:
9656 case Intrinsic::x86_sse_comile_ss:
9657 case Intrinsic::x86_sse_comigt_ss:
9658 case Intrinsic::x86_sse_comige_ss:
9659 case Intrinsic::x86_sse_comineq_ss:
9660 case Intrinsic::x86_sse_ucomieq_ss:
9661 case Intrinsic::x86_sse_ucomilt_ss:
9662 case Intrinsic::x86_sse_ucomile_ss:
9663 case Intrinsic::x86_sse_ucomigt_ss:
9664 case Intrinsic::x86_sse_ucomige_ss:
9665 case Intrinsic::x86_sse_ucomineq_ss:
9666 case Intrinsic::x86_sse2_comieq_sd:
9667 case Intrinsic::x86_sse2_comilt_sd:
9668 case Intrinsic::x86_sse2_comile_sd:
9669 case Intrinsic::x86_sse2_comigt_sd:
9670 case Intrinsic::x86_sse2_comige_sd:
9671 case Intrinsic::x86_sse2_comineq_sd:
9672 case Intrinsic::x86_sse2_ucomieq_sd:
9673 case Intrinsic::x86_sse2_ucomilt_sd:
9674 case Intrinsic::x86_sse2_ucomile_sd:
9675 case Intrinsic::x86_sse2_ucomigt_sd:
9676 case Intrinsic::x86_sse2_ucomige_sd:
9677 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009678 unsigned Opc;
9679 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009680 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009681 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009682 case Intrinsic::x86_sse_comieq_ss:
9683 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009684 Opc = X86ISD::COMI;
9685 CC = ISD::SETEQ;
9686 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009687 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009688 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009689 Opc = X86ISD::COMI;
9690 CC = ISD::SETLT;
9691 break;
9692 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009693 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009694 Opc = X86ISD::COMI;
9695 CC = ISD::SETLE;
9696 break;
9697 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009698 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009699 Opc = X86ISD::COMI;
9700 CC = ISD::SETGT;
9701 break;
9702 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009703 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009704 Opc = X86ISD::COMI;
9705 CC = ISD::SETGE;
9706 break;
9707 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009708 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009709 Opc = X86ISD::COMI;
9710 CC = ISD::SETNE;
9711 break;
9712 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009713 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009714 Opc = X86ISD::UCOMI;
9715 CC = ISD::SETEQ;
9716 break;
9717 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009718 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009719 Opc = X86ISD::UCOMI;
9720 CC = ISD::SETLT;
9721 break;
9722 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009723 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009724 Opc = X86ISD::UCOMI;
9725 CC = ISD::SETLE;
9726 break;
9727 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009728 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009729 Opc = X86ISD::UCOMI;
9730 CC = ISD::SETGT;
9731 break;
9732 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009733 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009734 Opc = X86ISD::UCOMI;
9735 CC = ISD::SETGE;
9736 break;
9737 case Intrinsic::x86_sse_ucomineq_ss:
9738 case Intrinsic::x86_sse2_ucomineq_sd:
9739 Opc = X86ISD::UCOMI;
9740 CC = ISD::SETNE;
9741 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009742 }
Evan Cheng734503b2006-09-11 02:19:56 +00009743
Dan Gohman475871a2008-07-27 21:46:04 +00009744 SDValue LHS = Op.getOperand(1);
9745 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009746 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009747 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009748 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9749 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9750 DAG.getConstant(X86CC, MVT::i8), Cond);
9751 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009752 }
Craig Topper6d688152012-08-14 07:43:25 +00009753
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009754 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009755 case Intrinsic::x86_sse2_pmulu_dq:
9756 case Intrinsic::x86_avx2_pmulu_dq:
9757 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9758 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009759
9760 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009761 case Intrinsic::x86_sse3_hadd_ps:
9762 case Intrinsic::x86_sse3_hadd_pd:
9763 case Intrinsic::x86_avx_hadd_ps_256:
9764 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009765 case Intrinsic::x86_sse3_hsub_ps:
9766 case Intrinsic::x86_sse3_hsub_pd:
9767 case Intrinsic::x86_avx_hsub_ps_256:
9768 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009769 case Intrinsic::x86_ssse3_phadd_w_128:
9770 case Intrinsic::x86_ssse3_phadd_d_128:
9771 case Intrinsic::x86_avx2_phadd_w:
9772 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009773 case Intrinsic::x86_ssse3_phsub_w_128:
9774 case Intrinsic::x86_ssse3_phsub_d_128:
9775 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009776 case Intrinsic::x86_avx2_phsub_d: {
9777 unsigned Opcode;
9778 switch (IntNo) {
9779 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9780 case Intrinsic::x86_sse3_hadd_ps:
9781 case Intrinsic::x86_sse3_hadd_pd:
9782 case Intrinsic::x86_avx_hadd_ps_256:
9783 case Intrinsic::x86_avx_hadd_pd_256:
9784 Opcode = X86ISD::FHADD;
9785 break;
9786 case Intrinsic::x86_sse3_hsub_ps:
9787 case Intrinsic::x86_sse3_hsub_pd:
9788 case Intrinsic::x86_avx_hsub_ps_256:
9789 case Intrinsic::x86_avx_hsub_pd_256:
9790 Opcode = X86ISD::FHSUB;
9791 break;
9792 case Intrinsic::x86_ssse3_phadd_w_128:
9793 case Intrinsic::x86_ssse3_phadd_d_128:
9794 case Intrinsic::x86_avx2_phadd_w:
9795 case Intrinsic::x86_avx2_phadd_d:
9796 Opcode = X86ISD::HADD;
9797 break;
9798 case Intrinsic::x86_ssse3_phsub_w_128:
9799 case Intrinsic::x86_ssse3_phsub_d_128:
9800 case Intrinsic::x86_avx2_phsub_w:
9801 case Intrinsic::x86_avx2_phsub_d:
9802 Opcode = X86ISD::HSUB;
9803 break;
9804 }
9805 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009806 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009807 }
9808
9809 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009810 case Intrinsic::x86_avx2_psllv_d:
9811 case Intrinsic::x86_avx2_psllv_q:
9812 case Intrinsic::x86_avx2_psllv_d_256:
9813 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009814 case Intrinsic::x86_avx2_psrlv_d:
9815 case Intrinsic::x86_avx2_psrlv_q:
9816 case Intrinsic::x86_avx2_psrlv_d_256:
9817 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009818 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009819 case Intrinsic::x86_avx2_psrav_d_256: {
9820 unsigned Opcode;
9821 switch (IntNo) {
9822 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9823 case Intrinsic::x86_avx2_psllv_d:
9824 case Intrinsic::x86_avx2_psllv_q:
9825 case Intrinsic::x86_avx2_psllv_d_256:
9826 case Intrinsic::x86_avx2_psllv_q_256:
9827 Opcode = ISD::SHL;
9828 break;
9829 case Intrinsic::x86_avx2_psrlv_d:
9830 case Intrinsic::x86_avx2_psrlv_q:
9831 case Intrinsic::x86_avx2_psrlv_d_256:
9832 case Intrinsic::x86_avx2_psrlv_q_256:
9833 Opcode = ISD::SRL;
9834 break;
9835 case Intrinsic::x86_avx2_psrav_d:
9836 case Intrinsic::x86_avx2_psrav_d_256:
9837 Opcode = ISD::SRA;
9838 break;
9839 }
9840 return DAG.getNode(Opcode, dl, Op.getValueType(),
9841 Op.getOperand(1), Op.getOperand(2));
9842 }
9843
Craig Topper969ba282012-01-25 06:43:11 +00009844 case Intrinsic::x86_ssse3_pshuf_b_128:
9845 case Intrinsic::x86_avx2_pshuf_b:
9846 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9847 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009848
Craig Topper969ba282012-01-25 06:43:11 +00009849 case Intrinsic::x86_ssse3_psign_b_128:
9850 case Intrinsic::x86_ssse3_psign_w_128:
9851 case Intrinsic::x86_ssse3_psign_d_128:
9852 case Intrinsic::x86_avx2_psign_b:
9853 case Intrinsic::x86_avx2_psign_w:
9854 case Intrinsic::x86_avx2_psign_d:
9855 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9856 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009857
Craig Toppere566cd02012-01-26 07:18:03 +00009858 case Intrinsic::x86_sse41_insertps:
9859 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9860 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009861
Craig Toppere566cd02012-01-26 07:18:03 +00009862 case Intrinsic::x86_avx_vperm2f128_ps_256:
9863 case Intrinsic::x86_avx_vperm2f128_pd_256:
9864 case Intrinsic::x86_avx_vperm2f128_si_256:
9865 case Intrinsic::x86_avx2_vperm2i128:
9866 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9867 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009868
Craig Topperffa6c402012-04-16 07:13:00 +00009869 case Intrinsic::x86_avx2_permd:
9870 case Intrinsic::x86_avx2_permps:
9871 // Operands intentionally swapped. Mask is last operand to intrinsic,
9872 // but second operand for node/intruction.
9873 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9874 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009875
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009876 // ptest and testp intrinsics. The intrinsic these come from are designed to
9877 // return an integer value, not just an instruction so lower it to the ptest
9878 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009879 case Intrinsic::x86_sse41_ptestz:
9880 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009881 case Intrinsic::x86_sse41_ptestnzc:
9882 case Intrinsic::x86_avx_ptestz_256:
9883 case Intrinsic::x86_avx_ptestc_256:
9884 case Intrinsic::x86_avx_ptestnzc_256:
9885 case Intrinsic::x86_avx_vtestz_ps:
9886 case Intrinsic::x86_avx_vtestc_ps:
9887 case Intrinsic::x86_avx_vtestnzc_ps:
9888 case Intrinsic::x86_avx_vtestz_pd:
9889 case Intrinsic::x86_avx_vtestc_pd:
9890 case Intrinsic::x86_avx_vtestnzc_pd:
9891 case Intrinsic::x86_avx_vtestz_ps_256:
9892 case Intrinsic::x86_avx_vtestc_ps_256:
9893 case Intrinsic::x86_avx_vtestnzc_ps_256:
9894 case Intrinsic::x86_avx_vtestz_pd_256:
9895 case Intrinsic::x86_avx_vtestc_pd_256:
9896 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9897 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +00009898 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +00009899 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009900 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009901 case Intrinsic::x86_avx_vtestz_ps:
9902 case Intrinsic::x86_avx_vtestz_pd:
9903 case Intrinsic::x86_avx_vtestz_ps_256:
9904 case Intrinsic::x86_avx_vtestz_pd_256:
9905 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009906 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009907 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009908 // ZF = 1
9909 X86CC = X86::COND_E;
9910 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009911 case Intrinsic::x86_avx_vtestc_ps:
9912 case Intrinsic::x86_avx_vtestc_pd:
9913 case Intrinsic::x86_avx_vtestc_ps_256:
9914 case Intrinsic::x86_avx_vtestc_pd_256:
9915 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009916 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009917 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009918 // CF = 1
9919 X86CC = X86::COND_B;
9920 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009921 case Intrinsic::x86_avx_vtestnzc_ps:
9922 case Intrinsic::x86_avx_vtestnzc_pd:
9923 case Intrinsic::x86_avx_vtestnzc_ps_256:
9924 case Intrinsic::x86_avx_vtestnzc_pd_256:
9925 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009926 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009927 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009928 // ZF and CF = 0
9929 X86CC = X86::COND_A;
9930 break;
9931 }
Eric Christopherfd179292009-08-27 18:07:15 +00009932
Eric Christopher71c67532009-07-29 00:28:05 +00009933 SDValue LHS = Op.getOperand(1);
9934 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009935 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9936 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009937 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9938 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9939 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009940 }
Evan Cheng5759f972008-05-04 09:15:50 +00009941
Craig Topper80e46362012-01-23 06:16:53 +00009942 // SSE/AVX shift intrinsics
9943 case Intrinsic::x86_sse2_psll_w:
9944 case Intrinsic::x86_sse2_psll_d:
9945 case Intrinsic::x86_sse2_psll_q:
9946 case Intrinsic::x86_avx2_psll_w:
9947 case Intrinsic::x86_avx2_psll_d:
9948 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +00009949 case Intrinsic::x86_sse2_psrl_w:
9950 case Intrinsic::x86_sse2_psrl_d:
9951 case Intrinsic::x86_sse2_psrl_q:
9952 case Intrinsic::x86_avx2_psrl_w:
9953 case Intrinsic::x86_avx2_psrl_d:
9954 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +00009955 case Intrinsic::x86_sse2_psra_w:
9956 case Intrinsic::x86_sse2_psra_d:
9957 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +00009958 case Intrinsic::x86_avx2_psra_d: {
9959 unsigned Opcode;
9960 switch (IntNo) {
9961 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9962 case Intrinsic::x86_sse2_psll_w:
9963 case Intrinsic::x86_sse2_psll_d:
9964 case Intrinsic::x86_sse2_psll_q:
9965 case Intrinsic::x86_avx2_psll_w:
9966 case Intrinsic::x86_avx2_psll_d:
9967 case Intrinsic::x86_avx2_psll_q:
9968 Opcode = X86ISD::VSHL;
9969 break;
9970 case Intrinsic::x86_sse2_psrl_w:
9971 case Intrinsic::x86_sse2_psrl_d:
9972 case Intrinsic::x86_sse2_psrl_q:
9973 case Intrinsic::x86_avx2_psrl_w:
9974 case Intrinsic::x86_avx2_psrl_d:
9975 case Intrinsic::x86_avx2_psrl_q:
9976 Opcode = X86ISD::VSRL;
9977 break;
9978 case Intrinsic::x86_sse2_psra_w:
9979 case Intrinsic::x86_sse2_psra_d:
9980 case Intrinsic::x86_avx2_psra_w:
9981 case Intrinsic::x86_avx2_psra_d:
9982 Opcode = X86ISD::VSRA;
9983 break;
9984 }
9985 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +00009986 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009987 }
9988
9989 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +00009990 case Intrinsic::x86_sse2_pslli_w:
9991 case Intrinsic::x86_sse2_pslli_d:
9992 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009993 case Intrinsic::x86_avx2_pslli_w:
9994 case Intrinsic::x86_avx2_pslli_d:
9995 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009996 case Intrinsic::x86_sse2_psrli_w:
9997 case Intrinsic::x86_sse2_psrli_d:
9998 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009999 case Intrinsic::x86_avx2_psrli_w:
10000 case Intrinsic::x86_avx2_psrli_d:
10001 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010002 case Intrinsic::x86_sse2_psrai_w:
10003 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010004 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010005 case Intrinsic::x86_avx2_psrai_d: {
10006 unsigned Opcode;
10007 switch (IntNo) {
10008 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10009 case Intrinsic::x86_sse2_pslli_w:
10010 case Intrinsic::x86_sse2_pslli_d:
10011 case Intrinsic::x86_sse2_pslli_q:
10012 case Intrinsic::x86_avx2_pslli_w:
10013 case Intrinsic::x86_avx2_pslli_d:
10014 case Intrinsic::x86_avx2_pslli_q:
10015 Opcode = X86ISD::VSHLI;
10016 break;
10017 case Intrinsic::x86_sse2_psrli_w:
10018 case Intrinsic::x86_sse2_psrli_d:
10019 case Intrinsic::x86_sse2_psrli_q:
10020 case Intrinsic::x86_avx2_psrli_w:
10021 case Intrinsic::x86_avx2_psrli_d:
10022 case Intrinsic::x86_avx2_psrli_q:
10023 Opcode = X86ISD::VSRLI;
10024 break;
10025 case Intrinsic::x86_sse2_psrai_w:
10026 case Intrinsic::x86_sse2_psrai_d:
10027 case Intrinsic::x86_avx2_psrai_w:
10028 case Intrinsic::x86_avx2_psrai_d:
10029 Opcode = X86ISD::VSRAI;
10030 break;
10031 }
10032 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010033 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010034 }
10035
Craig Topper4feb6472012-08-06 06:22:36 +000010036 case Intrinsic::x86_sse42_pcmpistria128:
10037 case Intrinsic::x86_sse42_pcmpestria128:
10038 case Intrinsic::x86_sse42_pcmpistric128:
10039 case Intrinsic::x86_sse42_pcmpestric128:
10040 case Intrinsic::x86_sse42_pcmpistrio128:
10041 case Intrinsic::x86_sse42_pcmpestrio128:
10042 case Intrinsic::x86_sse42_pcmpistris128:
10043 case Intrinsic::x86_sse42_pcmpestris128:
10044 case Intrinsic::x86_sse42_pcmpistriz128:
10045 case Intrinsic::x86_sse42_pcmpestriz128: {
10046 unsigned Opcode;
10047 unsigned X86CC;
10048 switch (IntNo) {
10049 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10050 case Intrinsic::x86_sse42_pcmpistria128:
10051 Opcode = X86ISD::PCMPISTRI;
10052 X86CC = X86::COND_A;
10053 break;
10054 case Intrinsic::x86_sse42_pcmpestria128:
10055 Opcode = X86ISD::PCMPESTRI;
10056 X86CC = X86::COND_A;
10057 break;
10058 case Intrinsic::x86_sse42_pcmpistric128:
10059 Opcode = X86ISD::PCMPISTRI;
10060 X86CC = X86::COND_B;
10061 break;
10062 case Intrinsic::x86_sse42_pcmpestric128:
10063 Opcode = X86ISD::PCMPESTRI;
10064 X86CC = X86::COND_B;
10065 break;
10066 case Intrinsic::x86_sse42_pcmpistrio128:
10067 Opcode = X86ISD::PCMPISTRI;
10068 X86CC = X86::COND_O;
10069 break;
10070 case Intrinsic::x86_sse42_pcmpestrio128:
10071 Opcode = X86ISD::PCMPESTRI;
10072 X86CC = X86::COND_O;
10073 break;
10074 case Intrinsic::x86_sse42_pcmpistris128:
10075 Opcode = X86ISD::PCMPISTRI;
10076 X86CC = X86::COND_S;
10077 break;
10078 case Intrinsic::x86_sse42_pcmpestris128:
10079 Opcode = X86ISD::PCMPESTRI;
10080 X86CC = X86::COND_S;
10081 break;
10082 case Intrinsic::x86_sse42_pcmpistriz128:
10083 Opcode = X86ISD::PCMPISTRI;
10084 X86CC = X86::COND_E;
10085 break;
10086 case Intrinsic::x86_sse42_pcmpestriz128:
10087 Opcode = X86ISD::PCMPESTRI;
10088 X86CC = X86::COND_E;
10089 break;
10090 }
10091 SmallVector<SDValue, 5> NewOps;
10092 NewOps.append(Op->op_begin()+1, Op->op_end());
10093 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10094 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10095 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10096 DAG.getConstant(X86CC, MVT::i8),
10097 SDValue(PCMP.getNode(), 1));
10098 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10099 }
Craig Topper6d688152012-08-14 07:43:25 +000010100
Craig Topper4feb6472012-08-06 06:22:36 +000010101 case Intrinsic::x86_sse42_pcmpistri128:
10102 case Intrinsic::x86_sse42_pcmpestri128: {
10103 unsigned Opcode;
10104 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10105 Opcode = X86ISD::PCMPISTRI;
10106 else
10107 Opcode = X86ISD::PCMPESTRI;
10108
10109 SmallVector<SDValue, 5> NewOps;
10110 NewOps.append(Op->op_begin()+1, Op->op_end());
10111 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10112 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10113 }
Craig Topper0e292372012-08-24 04:03:22 +000010114 case Intrinsic::x86_fma_vfmadd_ps:
10115 case Intrinsic::x86_fma_vfmadd_pd:
10116 case Intrinsic::x86_fma_vfmsub_ps:
10117 case Intrinsic::x86_fma_vfmsub_pd:
10118 case Intrinsic::x86_fma_vfnmadd_ps:
10119 case Intrinsic::x86_fma_vfnmadd_pd:
10120 case Intrinsic::x86_fma_vfnmsub_ps:
10121 case Intrinsic::x86_fma_vfnmsub_pd:
10122 case Intrinsic::x86_fma_vfmaddsub_ps:
10123 case Intrinsic::x86_fma_vfmaddsub_pd:
10124 case Intrinsic::x86_fma_vfmsubadd_ps:
10125 case Intrinsic::x86_fma_vfmsubadd_pd:
10126 case Intrinsic::x86_fma_vfmadd_ps_256:
10127 case Intrinsic::x86_fma_vfmadd_pd_256:
10128 case Intrinsic::x86_fma_vfmsub_ps_256:
10129 case Intrinsic::x86_fma_vfmsub_pd_256:
10130 case Intrinsic::x86_fma_vfnmadd_ps_256:
10131 case Intrinsic::x86_fma_vfnmadd_pd_256:
10132 case Intrinsic::x86_fma_vfnmsub_ps_256:
10133 case Intrinsic::x86_fma_vfnmsub_pd_256:
10134 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10135 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10136 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10137 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010138 unsigned Opc;
10139 switch (IntNo) {
10140 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10141 case Intrinsic::x86_fma_vfmadd_ps:
10142 case Intrinsic::x86_fma_vfmadd_pd:
10143 case Intrinsic::x86_fma_vfmadd_ps_256:
10144 case Intrinsic::x86_fma_vfmadd_pd_256:
10145 Opc = X86ISD::FMADD;
10146 break;
10147 case Intrinsic::x86_fma_vfmsub_ps:
10148 case Intrinsic::x86_fma_vfmsub_pd:
10149 case Intrinsic::x86_fma_vfmsub_ps_256:
10150 case Intrinsic::x86_fma_vfmsub_pd_256:
10151 Opc = X86ISD::FMSUB;
10152 break;
10153 case Intrinsic::x86_fma_vfnmadd_ps:
10154 case Intrinsic::x86_fma_vfnmadd_pd:
10155 case Intrinsic::x86_fma_vfnmadd_ps_256:
10156 case Intrinsic::x86_fma_vfnmadd_pd_256:
10157 Opc = X86ISD::FNMADD;
10158 break;
10159 case Intrinsic::x86_fma_vfnmsub_ps:
10160 case Intrinsic::x86_fma_vfnmsub_pd:
10161 case Intrinsic::x86_fma_vfnmsub_ps_256:
10162 case Intrinsic::x86_fma_vfnmsub_pd_256:
10163 Opc = X86ISD::FNMSUB;
10164 break;
10165 case Intrinsic::x86_fma_vfmaddsub_ps:
10166 case Intrinsic::x86_fma_vfmaddsub_pd:
10167 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10168 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10169 Opc = X86ISD::FMADDSUB;
10170 break;
10171 case Intrinsic::x86_fma_vfmsubadd_ps:
10172 case Intrinsic::x86_fma_vfmsubadd_pd:
10173 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10174 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10175 Opc = X86ISD::FMSUBADD;
10176 break;
10177 }
10178
10179 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10180 Op.getOperand(2), Op.getOperand(3));
10181 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010182 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010183}
Evan Cheng72261582005-12-20 06:22:03 +000010184
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010185SDValue
10186X86TargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const {
10187 DebugLoc dl = Op.getDebugLoc();
10188 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10189 switch (IntNo) {
10190 default: return SDValue(); // Don't custom lower most intrinsics.
10191
10192 // RDRAND intrinsics.
10193 case Intrinsic::x86_rdrand_16:
10194 case Intrinsic::x86_rdrand_32:
10195 case Intrinsic::x86_rdrand_64: {
10196 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010197 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10198 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010199
10200 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10201 // return the value from Rand, which is always 0, casted to i32.
10202 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10203 DAG.getConstant(1, Op->getValueType(1)),
10204 DAG.getConstant(X86::COND_B, MVT::i32),
10205 SDValue(Result.getNode(), 1) };
10206 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10207 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10208 Ops, 4);
10209
10210 // Return { result, isValid, chain }.
10211 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010212 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010213 }
10214 }
10215}
10216
Dan Gohmand858e902010-04-17 15:26:15 +000010217SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10218 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010219 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10220 MFI->setReturnAddressIsTaken(true);
10221
Bill Wendling64e87322009-01-16 19:25:27 +000010222 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010223 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010224
10225 if (Depth > 0) {
10226 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10227 SDValue Offset =
10228 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010229 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010230 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010231 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010232 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010233 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010234 }
10235
10236 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010237 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010238 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010239 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010240}
10241
Dan Gohmand858e902010-04-17 15:26:15 +000010242SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010243 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10244 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010245
Owen Andersone50ed302009-08-10 22:56:29 +000010246 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010247 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010248 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10249 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010250 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010251 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010252 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10253 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010254 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010255 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010256}
10257
Dan Gohman475871a2008-07-27 21:46:04 +000010258SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010259 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010260 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010261}
10262
Dan Gohmand858e902010-04-17 15:26:15 +000010263SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010264 SDValue Chain = Op.getOperand(0);
10265 SDValue Offset = Op.getOperand(1);
10266 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010267 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010268
Dan Gohmand8816272010-08-11 18:14:00 +000010269 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10270 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10271 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010272 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010273
Dan Gohmand8816272010-08-11 18:14:00 +000010274 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
10275 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010276 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010277 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10278 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010279 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010280
Dale Johannesene4d209d2009-02-03 20:21:25 +000010281 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010282 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010283 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010284}
10285
Duncan Sands4a544a72011-09-06 13:37:06 +000010286SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
10287 SelectionDAG &DAG) const {
10288 return Op.getOperand(0);
10289}
10290
10291SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10292 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010293 SDValue Root = Op.getOperand(0);
10294 SDValue Trmp = Op.getOperand(1); // trampoline
10295 SDValue FPtr = Op.getOperand(2); // nested function
10296 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010297 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010298
Dan Gohman69de1932008-02-06 22:27:42 +000010299 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010300
10301 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010302 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010303
10304 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010305 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10306 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010307
Evan Cheng0e6a0522011-07-18 20:57:22 +000010308 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10309 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010310
10311 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10312
10313 // Load the pointer to the nested function into R11.
10314 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010315 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010316 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010317 Addr, MachinePointerInfo(TrmpAddr),
10318 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010319
Owen Anderson825b72b2009-08-11 20:47:22 +000010320 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10321 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010322 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10323 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010324 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010325
10326 // Load the 'nest' parameter value into R10.
10327 // R10 is specified in X86CallingConv.td
10328 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010329 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10330 DAG.getConstant(10, MVT::i64));
10331 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010332 Addr, MachinePointerInfo(TrmpAddr, 10),
10333 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010334
Owen Anderson825b72b2009-08-11 20:47:22 +000010335 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10336 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010337 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10338 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010339 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010340
10341 // Jump to the nested function.
10342 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010343 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10344 DAG.getConstant(20, MVT::i64));
10345 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010346 Addr, MachinePointerInfo(TrmpAddr, 20),
10347 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010348
10349 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010350 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10351 DAG.getConstant(22, MVT::i64));
10352 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010353 MachinePointerInfo(TrmpAddr, 22),
10354 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010355
Duncan Sands4a544a72011-09-06 13:37:06 +000010356 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010357 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010358 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010359 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010360 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010361 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010362
10363 switch (CC) {
10364 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010365 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010366 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010367 case CallingConv::X86_StdCall: {
10368 // Pass 'nest' parameter in ECX.
10369 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010370 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010371
10372 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010373 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010374 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010375
Chris Lattner58d74912008-03-12 17:45:29 +000010376 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010377 unsigned InRegCount = 0;
10378 unsigned Idx = 1;
10379
10380 for (FunctionType::param_iterator I = FTy->param_begin(),
10381 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010382 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010383 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010384 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010385
10386 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010387 report_fatal_error("Nest register in use - reduce number of inreg"
10388 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010389 }
10390 }
10391 break;
10392 }
10393 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010394 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010395 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010396 // Pass 'nest' parameter in EAX.
10397 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010398 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010399 break;
10400 }
10401
Dan Gohman475871a2008-07-27 21:46:04 +000010402 SDValue OutChains[4];
10403 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010404
Owen Anderson825b72b2009-08-11 20:47:22 +000010405 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10406 DAG.getConstant(10, MVT::i32));
10407 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010408
Chris Lattnera62fe662010-02-05 19:20:30 +000010409 // This is storing the opcode for MOV32ri.
10410 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010411 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010412 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010413 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010414 Trmp, MachinePointerInfo(TrmpAddr),
10415 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010416
Owen Anderson825b72b2009-08-11 20:47:22 +000010417 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10418 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010419 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10420 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010421 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010422
Chris Lattnera62fe662010-02-05 19:20:30 +000010423 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010424 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10425 DAG.getConstant(5, MVT::i32));
10426 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010427 MachinePointerInfo(TrmpAddr, 5),
10428 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010429
Owen Anderson825b72b2009-08-11 20:47:22 +000010430 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10431 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010432 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10433 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010434 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010435
Duncan Sands4a544a72011-09-06 13:37:06 +000010436 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010437 }
10438}
10439
Dan Gohmand858e902010-04-17 15:26:15 +000010440SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10441 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010442 /*
10443 The rounding mode is in bits 11:10 of FPSR, and has the following
10444 settings:
10445 00 Round to nearest
10446 01 Round to -inf
10447 10 Round to +inf
10448 11 Round to 0
10449
10450 FLT_ROUNDS, on the other hand, expects the following:
10451 -1 Undefined
10452 0 Round to 0
10453 1 Round to nearest
10454 2 Round to +inf
10455 3 Round to -inf
10456
10457 To perform the conversion, we do:
10458 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10459 */
10460
10461 MachineFunction &MF = DAG.getMachineFunction();
10462 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010463 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010464 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010465 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010466 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010467
10468 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010469 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010470 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010471
Michael J. Spencerec38de22010-10-10 22:04:20 +000010472
Chris Lattner2156b792010-09-22 01:11:26 +000010473 MachineMemOperand *MMO =
10474 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10475 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010476
Chris Lattner2156b792010-09-22 01:11:26 +000010477 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10478 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10479 DAG.getVTList(MVT::Other),
10480 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010481
10482 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010483 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010484 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010485
10486 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010487 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010488 DAG.getNode(ISD::SRL, DL, MVT::i16,
10489 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010490 CWD, DAG.getConstant(0x800, MVT::i16)),
10491 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010492 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010493 DAG.getNode(ISD::SRL, DL, MVT::i16,
10494 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010495 CWD, DAG.getConstant(0x400, MVT::i16)),
10496 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010497
Dan Gohman475871a2008-07-27 21:46:04 +000010498 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010499 DAG.getNode(ISD::AND, DL, MVT::i16,
10500 DAG.getNode(ISD::ADD, DL, MVT::i16,
10501 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010502 DAG.getConstant(1, MVT::i16)),
10503 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010504
10505
Duncan Sands83ec4b62008-06-06 12:08:01 +000010506 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010507 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010508}
10509
Dan Gohmand858e902010-04-17 15:26:15 +000010510SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010511 EVT VT = Op.getValueType();
10512 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010513 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010514 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010515
10516 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010517 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010518 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010519 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010520 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010521 }
Evan Cheng18efe262007-12-14 02:13:44 +000010522
Evan Cheng152804e2007-12-14 08:30:15 +000010523 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010524 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010525 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010526
10527 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010528 SDValue Ops[] = {
10529 Op,
10530 DAG.getConstant(NumBits+NumBits-1, OpVT),
10531 DAG.getConstant(X86::COND_E, MVT::i8),
10532 Op.getValue(1)
10533 };
10534 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010535
10536 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010537 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010538
Owen Anderson825b72b2009-08-11 20:47:22 +000010539 if (VT == MVT::i8)
10540 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010541 return Op;
10542}
10543
Chandler Carruthacc068e2011-12-24 10:55:54 +000010544SDValue X86TargetLowering::LowerCTLZ_ZERO_UNDEF(SDValue Op,
10545 SelectionDAG &DAG) const {
10546 EVT VT = Op.getValueType();
10547 EVT OpVT = VT;
10548 unsigned NumBits = VT.getSizeInBits();
10549 DebugLoc dl = Op.getDebugLoc();
10550
10551 Op = Op.getOperand(0);
10552 if (VT == MVT::i8) {
10553 // Zero extend to i32 since there is not an i8 bsr.
10554 OpVT = MVT::i32;
10555 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10556 }
10557
10558 // Issue a bsr (scan bits in reverse).
10559 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10560 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10561
10562 // And xor with NumBits-1.
10563 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10564
10565 if (VT == MVT::i8)
10566 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10567 return Op;
10568}
10569
Dan Gohmand858e902010-04-17 15:26:15 +000010570SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010571 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010572 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010573 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010574 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010575
10576 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010577 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010578 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010579
10580 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010581 SDValue Ops[] = {
10582 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010583 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010584 DAG.getConstant(X86::COND_E, MVT::i8),
10585 Op.getValue(1)
10586 };
Chandler Carruth77821022011-12-24 12:12:34 +000010587 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010588}
10589
Craig Topper13894fa2011-08-24 06:14:18 +000010590// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10591// ones, and then concatenate the result back.
10592static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010593 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010594
Craig Topper7a9a28b2012-08-12 02:23:29 +000010595 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010596 "Unsupported value type for operation");
10597
Craig Topper66ddd152012-04-27 22:54:43 +000010598 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010599 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010600
10601 // Extract the LHS vectors
10602 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010603 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10604 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010605
10606 // Extract the RHS vectors
10607 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010608 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10609 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010610
10611 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10612 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10613
10614 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10615 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10616 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10617}
10618
10619SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010620 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010621 Op.getValueType().isInteger() &&
10622 "Only handle AVX 256-bit vector integer operation");
10623 return Lower256IntArith(Op, DAG);
10624}
10625
10626SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010627 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010628 Op.getValueType().isInteger() &&
10629 "Only handle AVX 256-bit vector integer operation");
10630 return Lower256IntArith(Op, DAG);
10631}
10632
10633SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10634 EVT VT = Op.getValueType();
10635
10636 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010637 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010638 return Lower256IntArith(Op, DAG);
10639
Craig Topper5b209e82012-02-05 03:14:49 +000010640 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10641 "Only know how to lower V2I64/V4I64 multiply");
10642
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010643 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010644
Craig Topper5b209e82012-02-05 03:14:49 +000010645 // Ahi = psrlqi(a, 32);
10646 // Bhi = psrlqi(b, 32);
10647 //
10648 // AloBlo = pmuludq(a, b);
10649 // AloBhi = pmuludq(a, Bhi);
10650 // AhiBlo = pmuludq(Ahi, b);
10651
10652 // AloBhi = psllqi(AloBhi, 32);
10653 // AhiBlo = psllqi(AhiBlo, 32);
10654 // return AloBlo + AloBhi + AhiBlo;
10655
Craig Topperaaa643c2011-11-09 07:28:55 +000010656 SDValue A = Op.getOperand(0);
10657 SDValue B = Op.getOperand(1);
10658
Craig Topper5b209e82012-02-05 03:14:49 +000010659 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010660
Craig Topper5b209e82012-02-05 03:14:49 +000010661 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10662 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010663
Craig Topper5b209e82012-02-05 03:14:49 +000010664 // Bit cast to 32-bit vectors for MULUDQ
10665 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10666 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10667 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10668 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10669 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010670
Craig Topper5b209e82012-02-05 03:14:49 +000010671 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10672 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10673 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010674
Craig Topper5b209e82012-02-05 03:14:49 +000010675 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10676 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010677
Dale Johannesene4d209d2009-02-03 20:21:25 +000010678 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010679 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010680}
10681
Nadav Rotem43012222011-05-11 08:12:09 +000010682SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10683
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010684 EVT VT = Op.getValueType();
10685 DebugLoc dl = Op.getDebugLoc();
10686 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010687 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010688 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010689
Craig Topper1accb7e2012-01-10 06:54:16 +000010690 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010691 return SDValue();
10692
Nadav Rotem43012222011-05-11 08:12:09 +000010693 // Optimize shl/srl/sra with constant shift amount.
10694 if (isSplatVector(Amt.getNode())) {
10695 SDValue SclrAmt = Amt->getOperand(0);
10696 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10697 uint64_t ShiftAmt = C->getZExtValue();
10698
Craig Toppered2e13d2012-01-22 19:15:14 +000010699 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10700 (Subtarget->hasAVX2() &&
10701 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10702 if (Op.getOpcode() == ISD::SHL)
10703 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10704 DAG.getConstant(ShiftAmt, MVT::i32));
10705 if (Op.getOpcode() == ISD::SRL)
10706 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10707 DAG.getConstant(ShiftAmt, MVT::i32));
10708 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10709 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10710 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010711 }
10712
Craig Toppered2e13d2012-01-22 19:15:14 +000010713 if (VT == MVT::v16i8) {
10714 if (Op.getOpcode() == ISD::SHL) {
10715 // Make a large shift.
10716 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10717 DAG.getConstant(ShiftAmt, MVT::i32));
10718 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10719 // Zero out the rightmost bits.
10720 SmallVector<SDValue, 16> V(16,
10721 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10722 MVT::i8));
10723 return DAG.getNode(ISD::AND, dl, VT, SHL,
10724 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010725 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010726 if (Op.getOpcode() == ISD::SRL) {
10727 // Make a large shift.
10728 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10729 DAG.getConstant(ShiftAmt, MVT::i32));
10730 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10731 // Zero out the leftmost bits.
10732 SmallVector<SDValue, 16> V(16,
10733 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10734 MVT::i8));
10735 return DAG.getNode(ISD::AND, dl, VT, SRL,
10736 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10737 }
10738 if (Op.getOpcode() == ISD::SRA) {
10739 if (ShiftAmt == 7) {
10740 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010741 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010742 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010743 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010744
Craig Toppered2e13d2012-01-22 19:15:14 +000010745 // R s>> a === ((R u>> a) ^ m) - m
10746 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10747 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10748 MVT::i8));
10749 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10750 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10751 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10752 return Res;
10753 }
Craig Topper731dfd02012-04-23 03:42:40 +000010754 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010755 }
Craig Topper46154eb2011-11-11 07:39:23 +000010756
Craig Topper0d86d462011-11-20 00:12:05 +000010757 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10758 if (Op.getOpcode() == ISD::SHL) {
10759 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010760 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10761 DAG.getConstant(ShiftAmt, MVT::i32));
10762 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010763 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010764 SmallVector<SDValue, 32> V(32,
10765 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10766 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010767 return DAG.getNode(ISD::AND, dl, VT, SHL,
10768 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010769 }
Craig Topper0d86d462011-11-20 00:12:05 +000010770 if (Op.getOpcode() == ISD::SRL) {
10771 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010772 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10773 DAG.getConstant(ShiftAmt, MVT::i32));
10774 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010775 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010776 SmallVector<SDValue, 32> V(32,
10777 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10778 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010779 return DAG.getNode(ISD::AND, dl, VT, SRL,
10780 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10781 }
10782 if (Op.getOpcode() == ISD::SRA) {
10783 if (ShiftAmt == 7) {
10784 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010785 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010786 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010787 }
10788
10789 // R s>> a === ((R u>> a) ^ m) - m
10790 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10791 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10792 MVT::i8));
10793 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10794 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10795 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10796 return Res;
10797 }
Craig Topper731dfd02012-04-23 03:42:40 +000010798 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010799 }
Nadav Rotem43012222011-05-11 08:12:09 +000010800 }
10801 }
10802
10803 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010804 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010805 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10806 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010807
Chris Lattner7302d802012-02-06 21:56:39 +000010808 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10809 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010810 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10811 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010812 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010813 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010814
10815 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010816 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010817 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10818 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10819 }
Nadav Rotem43012222011-05-11 08:12:09 +000010820 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010821 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010822
Nate Begeman51409212010-07-28 00:21:48 +000010823 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010824 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10825 DAG.getConstant(5, MVT::i32));
10826 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010827
Lang Hames8b99c1e2011-12-17 01:08:46 +000010828 // Turn 'a' into a mask suitable for VSELECT
10829 SDValue VSelM = DAG.getConstant(0x80, VT);
10830 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010831 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010832
Lang Hames8b99c1e2011-12-17 01:08:46 +000010833 SDValue CM1 = DAG.getConstant(0x0f, VT);
10834 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010835
Lang Hames8b99c1e2011-12-17 01:08:46 +000010836 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10837 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010838 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10839 DAG.getConstant(4, MVT::i32), DAG);
10840 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010841 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10842
Nate Begeman51409212010-07-28 00:21:48 +000010843 // a += a
10844 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010845 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010846 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010847
Lang Hames8b99c1e2011-12-17 01:08:46 +000010848 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10849 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010850 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10851 DAG.getConstant(2, MVT::i32), DAG);
10852 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010853 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10854
Nate Begeman51409212010-07-28 00:21:48 +000010855 // a += a
10856 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010857 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010858 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010859
Lang Hames8b99c1e2011-12-17 01:08:46 +000010860 // return VSELECT(r, r+r, a);
10861 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010862 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010863 return R;
10864 }
Craig Topper46154eb2011-11-11 07:39:23 +000010865
10866 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010867 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010868 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010869 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10870 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10871
10872 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010873 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10874 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010875
10876 // Recreate the shift amount vectors
10877 SDValue Amt1, Amt2;
10878 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10879 // Constant shift amount
10880 SmallVector<SDValue, 4> Amt1Csts;
10881 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010882 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010883 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010884 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010885 Amt2Csts.push_back(Amt->getOperand(i));
10886
10887 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10888 &Amt1Csts[0], NumElems/2);
10889 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10890 &Amt2Csts[0], NumElems/2);
10891 } else {
10892 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000010893 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
10894 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010895 }
10896
10897 // Issue new vector shifts for the smaller types
10898 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10899 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10900
10901 // Concatenate the result back
10902 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10903 }
10904
Nate Begeman51409212010-07-28 00:21:48 +000010905 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010906}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010907
Dan Gohmand858e902010-04-17 15:26:15 +000010908SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010909 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10910 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010911 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10912 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010913 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010914 SDValue LHS = N->getOperand(0);
10915 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010916 unsigned BaseOp = 0;
10917 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010918 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010919 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010920 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010921 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010922 // A subtract of one will be selected as a INC. Note that INC doesn't
10923 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010924 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10925 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010926 BaseOp = X86ISD::INC;
10927 Cond = X86::COND_O;
10928 break;
10929 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010930 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010931 Cond = X86::COND_O;
10932 break;
10933 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010934 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010935 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010936 break;
10937 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010938 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10939 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010940 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10941 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010942 BaseOp = X86ISD::DEC;
10943 Cond = X86::COND_O;
10944 break;
10945 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010946 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010947 Cond = X86::COND_O;
10948 break;
10949 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010950 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010951 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010952 break;
10953 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010954 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010955 Cond = X86::COND_O;
10956 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010957 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10958 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10959 MVT::i32);
10960 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010961
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010962 SDValue SetCC =
10963 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10964 DAG.getConstant(X86::COND_O, MVT::i32),
10965 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010966
Dan Gohman6e5fda22011-07-22 18:45:15 +000010967 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010968 }
Bill Wendling74c37652008-12-09 22:08:41 +000010969 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010970
Bill Wendling61edeb52008-12-02 01:06:39 +000010971 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010972 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010973 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010974
Bill Wendling61edeb52008-12-02 01:06:39 +000010975 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010976 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10977 DAG.getConstant(Cond, MVT::i32),
10978 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010979
Dan Gohman6e5fda22011-07-22 18:45:15 +000010980 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010981}
10982
Chad Rosier30450e82011-12-22 22:35:21 +000010983SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10984 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010985 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010986 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10987 EVT VT = Op.getValueType();
10988
Craig Toppered2e13d2012-01-22 19:15:14 +000010989 if (!Subtarget->hasSSE2() || !VT.isVector())
10990 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010991
Craig Toppered2e13d2012-01-22 19:15:14 +000010992 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10993 ExtraVT.getScalarType().getSizeInBits();
10994 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10995
10996 switch (VT.getSimpleVT().SimpleTy) {
10997 default: return SDValue();
10998 case MVT::v8i32:
10999 case MVT::v16i16:
11000 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011001 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011002 if (!Subtarget->hasAVX2()) {
11003 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011004 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011005
Craig Toppered2e13d2012-01-22 19:15:14 +000011006 // Extract the LHS vectors
11007 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011008 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11009 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011010
Craig Toppered2e13d2012-01-22 19:15:14 +000011011 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11012 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011013
Craig Toppered2e13d2012-01-22 19:15:14 +000011014 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011015 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011016 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11017 ExtraNumElems/2);
11018 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011019
Craig Toppered2e13d2012-01-22 19:15:14 +000011020 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11021 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011022
Craig Toppered2e13d2012-01-22 19:15:14 +000011023 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
11024 }
11025 // fall through
11026 case MVT::v4i32:
11027 case MVT::v8i16: {
11028 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11029 Op.getOperand(0), ShAmt, DAG);
11030 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011031 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011032 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011033}
11034
11035
Eric Christopher9a9d2752010-07-22 02:48:34 +000011036SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
11037 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011038
Eric Christopher77ed1352011-07-08 00:04:56 +000011039 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11040 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011041 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011042 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011043 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011044 SDValue Ops[] = {
11045 DAG.getRegister(X86::ESP, MVT::i32), // Base
11046 DAG.getTargetConstant(1, MVT::i8), // Scale
11047 DAG.getRegister(0, MVT::i32), // Index
11048 DAG.getTargetConstant(0, MVT::i32), // Disp
11049 DAG.getRegister(0, MVT::i32), // Segment.
11050 Zero,
11051 Chain
11052 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011053 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011054 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11055 array_lengthof(Ops));
11056 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011057 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011058
Eric Christopher9a9d2752010-07-22 02:48:34 +000011059 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011060 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011061 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011062
Chris Lattner132929a2010-08-14 17:26:09 +000011063 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11064 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11065 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11066 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011067
Chris Lattner132929a2010-08-14 17:26:09 +000011068 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11069 if (!Op1 && !Op2 && !Op3 && Op4)
11070 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011071
Chris Lattner132929a2010-08-14 17:26:09 +000011072 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11073 if (Op1 && !Op2 && !Op3 && !Op4)
11074 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011075
11076 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011077 // (MFENCE)>;
11078 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011079}
11080
Eli Friedman14648462011-07-27 22:21:52 +000011081SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
11082 SelectionDAG &DAG) const {
11083 DebugLoc dl = Op.getDebugLoc();
11084 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11085 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11086 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11087 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11088
11089 // The only fence that needs an instruction is a sequentially-consistent
11090 // cross-thread fence.
11091 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11092 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11093 // no-sse2). There isn't any reason to disable it if the target processor
11094 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011095 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011096 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11097
11098 SDValue Chain = Op.getOperand(0);
11099 SDValue Zero = DAG.getConstant(0, MVT::i32);
11100 SDValue Ops[] = {
11101 DAG.getRegister(X86::ESP, MVT::i32), // Base
11102 DAG.getTargetConstant(1, MVT::i8), // Scale
11103 DAG.getRegister(0, MVT::i32), // Index
11104 DAG.getTargetConstant(0, MVT::i32), // Disp
11105 DAG.getRegister(0, MVT::i32), // Segment.
11106 Zero,
11107 Chain
11108 };
11109 SDNode *Res =
11110 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11111 array_lengthof(Ops));
11112 return SDValue(Res, 0);
11113 }
11114
11115 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11116 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11117}
11118
11119
Dan Gohmand858e902010-04-17 15:26:15 +000011120SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000011121 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011122 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011123 unsigned Reg = 0;
11124 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011125 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011126 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011127 case MVT::i8: Reg = X86::AL; size = 1; break;
11128 case MVT::i16: Reg = X86::AX; size = 2; break;
11129 case MVT::i32: Reg = X86::EAX; size = 4; break;
11130 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011131 assert(Subtarget->is64Bit() && "Node not type legal!");
11132 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011133 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011134 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011135 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011136 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011137 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011138 Op.getOperand(1),
11139 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011140 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011141 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011142 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011143 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11144 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11145 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011146 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011147 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011148 return cpOut;
11149}
11150
Duncan Sands1607f052008-12-01 11:39:25 +000011151SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000011152 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000011153 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011154 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011155 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011156 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011157 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011158 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11159 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011160 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011161 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11162 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011163 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011164 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011165 rdx.getValue(1)
11166 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011167 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011168}
11169
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011170SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000011171 SelectionDAG &DAG) const {
11172 EVT SrcVT = Op.getOperand(0).getValueType();
11173 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011174 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011175 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011176 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011177 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011178 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011179 // i64 <=> MMX conversions are Legal.
11180 if (SrcVT==MVT::i64 && DstVT.isVector())
11181 return Op;
11182 if (DstVT==MVT::i64 && SrcVT.isVector())
11183 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011184 // MMX <=> MMX conversions are Legal.
11185 if (SrcVT.isVector() && DstVT.isVector())
11186 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011187 // All other conversions need to be expanded.
11188 return SDValue();
11189}
Chris Lattner5b856542010-12-20 00:59:46 +000011190
Dan Gohmand858e902010-04-17 15:26:15 +000011191SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011192 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011193 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011194 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011195 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011196 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011197 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011198 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011199 Node->getOperand(0),
11200 Node->getOperand(1), negOp,
11201 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011202 cast<AtomicSDNode>(Node)->getAlignment(),
11203 cast<AtomicSDNode>(Node)->getOrdering(),
11204 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011205}
11206
Eli Friedman327236c2011-08-24 20:50:09 +000011207static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11208 SDNode *Node = Op.getNode();
11209 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011210 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011211
11212 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011213 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11214 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11215 // (The only way to get a 16-byte store is cmpxchg16b)
11216 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11217 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11218 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011219 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11220 cast<AtomicSDNode>(Node)->getMemoryVT(),
11221 Node->getOperand(0),
11222 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011223 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011224 cast<AtomicSDNode>(Node)->getOrdering(),
11225 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011226 return Swap.getValue(1);
11227 }
11228 // Other atomic stores have a simple pattern.
11229 return Op;
11230}
11231
Chris Lattner5b856542010-12-20 00:59:46 +000011232static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11233 EVT VT = Op.getNode()->getValueType(0);
11234
11235 // Let legalize expand this if it isn't a legal type yet.
11236 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11237 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011238
Chris Lattner5b856542010-12-20 00:59:46 +000011239 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011240
Chris Lattner5b856542010-12-20 00:59:46 +000011241 unsigned Opc;
11242 bool ExtraOp = false;
11243 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011244 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011245 case ISD::ADDC: Opc = X86ISD::ADD; break;
11246 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11247 case ISD::SUBC: Opc = X86ISD::SUB; break;
11248 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11249 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011250
Chris Lattner5b856542010-12-20 00:59:46 +000011251 if (!ExtraOp)
11252 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11253 Op.getOperand(1));
11254 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11255 Op.getOperand(1), Op.getOperand(2));
11256}
11257
Evan Cheng0db9fe62006-04-25 20:13:52 +000011258/// LowerOperation - Provide custom lowering hooks for some operations.
11259///
Dan Gohmand858e902010-04-17 15:26:15 +000011260SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011261 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011262 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011263 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000011264 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000011265 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011266 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
11267 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011268 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011269 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011270 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011271 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11272 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11273 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000011274 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000011275 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011276 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11277 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11278 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011279 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011280 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011281 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011282 case ISD::SHL_PARTS:
11283 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011284 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011285 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011286 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011287 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011288 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011289 case ISD::FABS: return LowerFABS(Op, DAG);
11290 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011291 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011292 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011293 case ISD::SETCC: return LowerSETCC(Op, DAG);
11294 case ISD::SELECT: return LowerSELECT(Op, DAG);
11295 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011296 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011297 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011298 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000011299 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011300 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011301 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011302 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11303 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011304 case ISD::FRAME_TO_ARGS_OFFSET:
11305 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011306 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011307 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011308 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11309 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011310 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011311 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011312 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011313 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011314 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011315 case ISD::SRA:
11316 case ISD::SRL:
11317 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011318 case ISD::SADDO:
11319 case ISD::UADDO:
11320 case ISD::SSUBO:
11321 case ISD::USUBO:
11322 case ISD::SMULO:
11323 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000011324 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011325 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011326 case ISD::ADDC:
11327 case ISD::ADDE:
11328 case ISD::SUBC:
11329 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011330 case ISD::ADD: return LowerADD(Op, DAG);
11331 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011332 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011333}
11334
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011335static void ReplaceATOMIC_LOAD(SDNode *Node,
11336 SmallVectorImpl<SDValue> &Results,
11337 SelectionDAG &DAG) {
11338 DebugLoc dl = Node->getDebugLoc();
11339 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11340
11341 // Convert wide load -> cmpxchg8b/cmpxchg16b
11342 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11343 // (The only way to get a 16-byte load is cmpxchg16b)
11344 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011345 SDValue Zero = DAG.getConstant(0, VT);
11346 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011347 Node->getOperand(0),
11348 Node->getOperand(1), Zero, Zero,
11349 cast<AtomicSDNode>(Node)->getMemOperand(),
11350 cast<AtomicSDNode>(Node)->getOrdering(),
11351 cast<AtomicSDNode>(Node)->getSynchScope());
11352 Results.push_back(Swap.getValue(0));
11353 Results.push_back(Swap.getValue(1));
11354}
11355
Craig Topperc0878702012-08-17 06:55:11 +000011356static void
Duncan Sands1607f052008-12-01 11:39:25 +000011357ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011358 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011359 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011360 assert (Node->getValueType(0) == MVT::i64 &&
11361 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011362
11363 SDValue Chain = Node->getOperand(0);
11364 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011365 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011366 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011367 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011368 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011369 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011370 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011371 SDValue Result =
11372 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11373 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011374 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011375 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011376 Results.push_back(Result.getValue(2));
11377}
11378
Duncan Sands126d9072008-07-04 11:47:58 +000011379/// ReplaceNodeResults - Replace a node with an illegal result type
11380/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011381void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11382 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011383 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011384 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011385 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011386 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011387 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011388 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011389 case ISD::ADDC:
11390 case ISD::ADDE:
11391 case ISD::SUBC:
11392 case ISD::SUBE:
11393 // We don't want to expand or promote these.
11394 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011395 case ISD::FP_TO_SINT:
11396 case ISD::FP_TO_UINT: {
11397 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11398
11399 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11400 return;
11401
Eli Friedman948e95a2009-05-23 09:59:16 +000011402 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011403 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011404 SDValue FIST = Vals.first, StackSlot = Vals.second;
11405 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011406 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011407 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011408 if (StackSlot.getNode() != 0)
11409 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11410 MachinePointerInfo(),
11411 false, false, false, 0));
11412 else
11413 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011414 }
11415 return;
11416 }
11417 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011418 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011419 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011420 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011421 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011422 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011423 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011424 eax.getValue(2));
11425 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11426 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011427 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011428 Results.push_back(edx.getValue(1));
11429 return;
11430 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011431 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011432 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011433 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011434 bool Regs64bit = T == MVT::i128;
11435 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011436 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011437 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11438 DAG.getConstant(0, HalfT));
11439 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11440 DAG.getConstant(1, HalfT));
11441 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11442 Regs64bit ? X86::RAX : X86::EAX,
11443 cpInL, SDValue());
11444 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11445 Regs64bit ? X86::RDX : X86::EDX,
11446 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011447 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011448 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11449 DAG.getConstant(0, HalfT));
11450 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11451 DAG.getConstant(1, HalfT));
11452 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11453 Regs64bit ? X86::RBX : X86::EBX,
11454 swapInL, cpInH.getValue(1));
11455 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011456 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011457 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011458 SDValue Ops[] = { swapInH.getValue(0),
11459 N->getOperand(1),
11460 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011461 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011462 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011463 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11464 X86ISD::LCMPXCHG8_DAG;
11465 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011466 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011467 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11468 Regs64bit ? X86::RAX : X86::EAX,
11469 HalfT, Result.getValue(1));
11470 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11471 Regs64bit ? X86::RDX : X86::EDX,
11472 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011473 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011474 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011475 Results.push_back(cpOutH.getValue(1));
11476 return;
11477 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011478 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011479 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011480 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011481 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011482 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011483 case ISD::ATOMIC_LOAD_XOR:
Craig Topperc0878702012-08-17 06:55:11 +000011484 case ISD::ATOMIC_SWAP: {
11485 unsigned Opc;
11486 switch (N->getOpcode()) {
11487 default: llvm_unreachable("Unexpected opcode");
11488 case ISD::ATOMIC_LOAD_ADD:
11489 Opc = X86ISD::ATOMADD64_DAG;
11490 break;
11491 case ISD::ATOMIC_LOAD_AND:
11492 Opc = X86ISD::ATOMAND64_DAG;
11493 break;
11494 case ISD::ATOMIC_LOAD_NAND:
11495 Opc = X86ISD::ATOMNAND64_DAG;
11496 break;
11497 case ISD::ATOMIC_LOAD_OR:
11498 Opc = X86ISD::ATOMOR64_DAG;
11499 break;
11500 case ISD::ATOMIC_LOAD_SUB:
11501 Opc = X86ISD::ATOMSUB64_DAG;
11502 break;
11503 case ISD::ATOMIC_LOAD_XOR:
11504 Opc = X86ISD::ATOMXOR64_DAG;
11505 break;
11506 case ISD::ATOMIC_SWAP:
11507 Opc = X86ISD::ATOMSWAP64_DAG;
11508 break;
11509 }
11510 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011511 return;
Craig Topperc0878702012-08-17 06:55:11 +000011512 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011513 case ISD::ATOMIC_LOAD:
11514 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011515 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011516}
11517
Evan Cheng72261582005-12-20 06:22:03 +000011518const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11519 switch (Opcode) {
11520 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011521 case X86ISD::BSF: return "X86ISD::BSF";
11522 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011523 case X86ISD::SHLD: return "X86ISD::SHLD";
11524 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011525 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011526 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011527 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011528 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011529 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011530 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011531 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11532 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11533 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011534 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011535 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011536 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011537 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011538 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011539 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011540 case X86ISD::COMI: return "X86ISD::COMI";
11541 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011542 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011543 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011544 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11545 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011546 case X86ISD::CMOV: return "X86ISD::CMOV";
11547 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011548 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011549 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11550 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011551 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011552 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011553 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011554 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011555 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011556 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11557 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011558 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011559 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011560 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011561 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011562 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011563 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11564 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11565 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011566 case X86ISD::HADD: return "X86ISD::HADD";
11567 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011568 case X86ISD::FHADD: return "X86ISD::FHADD";
11569 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011570 case X86ISD::FMAX: return "X86ISD::FMAX";
11571 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011572 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11573 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011574 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11575 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011576 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011577 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011578 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011579 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011580 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011581 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011582 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011583 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11584 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011585 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11586 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11587 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11588 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11589 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11590 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011591 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011592 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011593 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011594 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Craig Toppered2e13d2012-01-22 19:15:14 +000011595 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11596 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011597 case X86ISD::VSHL: return "X86ISD::VSHL";
11598 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011599 case X86ISD::VSRA: return "X86ISD::VSRA";
11600 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11601 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11602 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011603 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011604 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11605 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011606 case X86ISD::ADD: return "X86ISD::ADD";
11607 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011608 case X86ISD::ADC: return "X86ISD::ADC";
11609 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011610 case X86ISD::SMUL: return "X86ISD::SMUL";
11611 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011612 case X86ISD::INC: return "X86ISD::INC";
11613 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011614 case X86ISD::OR: return "X86ISD::OR";
11615 case X86ISD::XOR: return "X86ISD::XOR";
11616 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011617 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011618 case X86ISD::BLSI: return "X86ISD::BLSI";
11619 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11620 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011621 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011622 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011623 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011624 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11625 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11626 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011627 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011628 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011629 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011630 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011631 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011632 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11633 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011634 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11635 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11636 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011637 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11638 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011639 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11640 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011641 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011642 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011643 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011644 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11645 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011646 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011647 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011648 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011649 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011650 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011651 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011652 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011653 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011654 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011655 case X86ISD::FMADD: return "X86ISD::FMADD";
11656 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11657 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11658 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11659 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11660 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011661 }
11662}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011663
Chris Lattnerc9addb72007-03-30 23:15:24 +000011664// isLegalAddressingMode - Return true if the addressing mode represented
11665// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011666bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011667 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011668 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011669 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011670 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011671
Chris Lattnerc9addb72007-03-30 23:15:24 +000011672 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011673 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011674 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011675
Chris Lattnerc9addb72007-03-30 23:15:24 +000011676 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011677 unsigned GVFlags =
11678 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011679
Chris Lattnerdfed4132009-07-10 07:38:24 +000011680 // If a reference to this global requires an extra load, we can't fold it.
11681 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011682 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011683
Chris Lattnerdfed4132009-07-10 07:38:24 +000011684 // If BaseGV requires a register for the PIC base, we cannot also have a
11685 // BaseReg specified.
11686 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011687 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011688
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011689 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011690 if ((M != CodeModel::Small || R != Reloc::Static) &&
11691 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011692 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011693 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011694
Chris Lattnerc9addb72007-03-30 23:15:24 +000011695 switch (AM.Scale) {
11696 case 0:
11697 case 1:
11698 case 2:
11699 case 4:
11700 case 8:
11701 // These scales always work.
11702 break;
11703 case 3:
11704 case 5:
11705 case 9:
11706 // These scales are formed with basereg+scalereg. Only accept if there is
11707 // no basereg yet.
11708 if (AM.HasBaseReg)
11709 return false;
11710 break;
11711 default: // Other stuff never works.
11712 return false;
11713 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011714
Chris Lattnerc9addb72007-03-30 23:15:24 +000011715 return true;
11716}
11717
11718
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011719bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011720 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011721 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011722 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11723 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011724 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011725 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011726 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011727}
11728
Evan Cheng70e10d32012-07-17 06:53:39 +000011729bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11730 return Imm == (int32_t)Imm;
11731}
11732
11733bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011734 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011735 return Imm == (int32_t)Imm;
11736}
11737
Owen Andersone50ed302009-08-10 22:56:29 +000011738bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011739 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011740 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011741 unsigned NumBits1 = VT1.getSizeInBits();
11742 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011743 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011744 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011745 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011746}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011747
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011748bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011749 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011750 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011751}
11752
Owen Andersone50ed302009-08-10 22:56:29 +000011753bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011754 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011755 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011756}
11757
Owen Andersone50ed302009-08-10 22:56:29 +000011758bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011759 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011760 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011761}
11762
Evan Cheng60c07e12006-07-05 22:17:51 +000011763/// isShuffleMaskLegal - Targets can use this to indicate that they only
11764/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11765/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11766/// are assumed to be legal.
11767bool
Eric Christopherfd179292009-08-27 18:07:15 +000011768X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011769 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011770 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011771 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011772 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011773
Nate Begemana09008b2009-10-19 02:17:23 +000011774 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011775 return (VT.getVectorNumElements() == 2 ||
11776 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11777 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011778 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011779 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011780 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11781 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011782 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011783 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11784 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011785 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11786 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011787}
11788
Dan Gohman7d8143f2008-04-09 20:09:42 +000011789bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011790X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011791 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011792 unsigned NumElts = VT.getVectorNumElements();
11793 // FIXME: This collection of masks seems suspect.
11794 if (NumElts == 2)
11795 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011796 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011797 return (isMOVLMask(Mask, VT) ||
11798 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011799 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11800 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011801 }
11802 return false;
11803}
11804
11805//===----------------------------------------------------------------------===//
11806// X86 Scheduler Hooks
11807//===----------------------------------------------------------------------===//
11808
Mon P Wang63307c32008-05-05 19:05:59 +000011809// private utility function
11810MachineBasicBlock *
11811X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11812 MachineBasicBlock *MBB,
11813 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011814 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011815 unsigned LoadOpc,
11816 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011817 unsigned notOpc,
11818 unsigned EAXreg,
Craig Topper44d23822012-02-22 05:59:10 +000011819 const TargetRegisterClass *RC,
Richard Smith42fc29e2012-04-13 22:47:00 +000011820 bool Invert) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011821 // For the atomic bitwise operator, we generate
11822 // thisMBB:
11823 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011824 // ld t1 = [bitinstr.addr]
11825 // op t2 = t1, [bitinstr.val]
Richard Smith42fc29e2012-04-13 22:47:00 +000011826 // not t3 = t2 (if Invert)
Mon P Wangab3e7472008-05-05 22:56:23 +000011827 // mov EAX = t1
Richard Smith42fc29e2012-04-13 22:47:00 +000011828 // lcs dest = [bitinstr.addr], t3 [EAX is implicit]
Mon P Wang63307c32008-05-05 19:05:59 +000011829 // bz newMBB
11830 // fallthrough -->nextMBB
11831 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11832 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011833 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011834 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011835
Mon P Wang63307c32008-05-05 19:05:59 +000011836 /// First build the CFG
11837 MachineFunction *F = MBB->getParent();
11838 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011839 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11840 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11841 F->insert(MBBIter, newMBB);
11842 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011843
Dan Gohman14152b42010-07-06 20:24:04 +000011844 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11845 nextMBB->splice(nextMBB->begin(), thisMBB,
11846 llvm::next(MachineBasicBlock::iterator(bInstr)),
11847 thisMBB->end());
11848 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011849
Mon P Wang63307c32008-05-05 19:05:59 +000011850 // Update thisMBB to fall through to newMBB
11851 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011852
Mon P Wang63307c32008-05-05 19:05:59 +000011853 // newMBB jumps to itself and fall through to nextMBB
11854 newMBB->addSuccessor(nextMBB);
11855 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011856
Mon P Wang63307c32008-05-05 19:05:59 +000011857 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011858 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011859 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011860 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011861 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011862 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011863 int numArgs = bInstr->getNumOperands() - 1;
11864 for (int i=0; i < numArgs; ++i)
11865 argOpers[i] = &bInstr->getOperand(i+1);
11866
11867 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011868 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011869 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011870
Dale Johannesen140be2d2008-08-19 18:47:28 +000011871 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011872 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011873 for (int i=0; i <= lastAddrIndx; ++i)
11874 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011875
Dale Johannesen140be2d2008-08-19 18:47:28 +000011876 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011877 assert((argOpers[valArgIndx]->isReg() ||
11878 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011879 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011880 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011881 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011882 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011883 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Richard Smith42fc29e2012-04-13 22:47:00 +000011884 MIB.addReg(t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011885 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011886
Richard Smith42fc29e2012-04-13 22:47:00 +000011887 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11888 if (Invert) {
11889 MIB = BuildMI(newMBB, dl, TII->get(notOpc), t3).addReg(t2);
11890 }
11891 else
11892 t3 = t2;
11893
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011894 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Richard Smith2c651fe2012-04-16 18:43:53 +000011895 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011896
Dale Johannesene4d209d2009-02-03 20:21:25 +000011897 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011898 for (int i=0; i <= lastAddrIndx; ++i)
11899 (*MIB).addOperand(*argOpers[i]);
Richard Smith42fc29e2012-04-13 22:47:00 +000011900 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011901 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011902 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11903 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011904
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011905 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011906 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011907
Mon P Wang63307c32008-05-05 19:05:59 +000011908 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011909 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011910
Dan Gohman14152b42010-07-06 20:24:04 +000011911 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011912 return nextMBB;
11913}
11914
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011915// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011916MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011917X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11918 MachineBasicBlock *MBB,
11919 unsigned regOpcL,
11920 unsigned regOpcH,
11921 unsigned immOpcL,
11922 unsigned immOpcH,
Richard Smith42fc29e2012-04-13 22:47:00 +000011923 bool Invert) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011924 // For the atomic bitwise operator, we generate
11925 // thisMBB (instructions are in pairs, except cmpxchg8b)
11926 // ld t1,t2 = [bitinstr.addr]
11927 // newMBB:
11928 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11929 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011930 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Richard Smith42fc29e2012-04-13 22:47:00 +000011931 // neg t7, t8 < t5, t6 (if Invert)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011932 // mov ECX, EBX <- t5, t6
11933 // mov EAX, EDX <- t1, t2
11934 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11935 // mov t3, t4 <- EAX, EDX
11936 // bz newMBB
11937 // result in out1, out2
11938 // fallthrough -->nextMBB
11939
Craig Topperc9099502012-04-20 06:31:50 +000011940 const TargetRegisterClass *RC = &X86::GR32RegClass;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011941 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011942 const unsigned NotOpc = X86::NOT32r;
11943 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11944 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11945 MachineFunction::iterator MBBIter = MBB;
11946 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011947
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011948 /// First build the CFG
11949 MachineFunction *F = MBB->getParent();
11950 MachineBasicBlock *thisMBB = MBB;
11951 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11952 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11953 F->insert(MBBIter, newMBB);
11954 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011955
Dan Gohman14152b42010-07-06 20:24:04 +000011956 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11957 nextMBB->splice(nextMBB->begin(), thisMBB,
11958 llvm::next(MachineBasicBlock::iterator(bInstr)),
11959 thisMBB->end());
11960 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011961
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011962 // Update thisMBB to fall through to newMBB
11963 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011964
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011965 // newMBB jumps to itself and fall through to nextMBB
11966 newMBB->addSuccessor(nextMBB);
11967 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011968
Dale Johannesene4d209d2009-02-03 20:21:25 +000011969 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011970 // Insert instructions into newMBB based on incoming instruction
11971 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011972 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011973 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011974 MachineOperand& dest1Oper = bInstr->getOperand(0);
11975 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011976 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11977 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011978 argOpers[i] = &bInstr->getOperand(i+2);
11979
Dan Gohman71ea4e52010-05-14 21:01:44 +000011980 // We use some of the operands multiple times, so conservatively just
11981 // clear any kill flags that might be present.
11982 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11983 argOpers[i]->setIsKill(false);
11984 }
11985
Evan Chengad5b52f2010-01-08 19:14:57 +000011986 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011987 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011988
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011989 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011990 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011991 for (int i=0; i <= lastAddrIndx; ++i)
11992 (*MIB).addOperand(*argOpers[i]);
11993 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011994 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011995 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011996 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011997 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011998 MachineOperand newOp3 = *(argOpers[3]);
11999 if (newOp3.isImm())
12000 newOp3.setImm(newOp3.getImm()+4);
12001 else
12002 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012003 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000012004 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012005
12006 // t3/4 are defined later, at the bottom of the loop
12007 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
12008 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012009 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012010 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012011 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012012 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
12013
Evan Cheng306b4ca2010-01-08 23:41:50 +000012014 // The subsequent operations should be using the destination registers of
Richard Smith42fc29e2012-04-13 22:47:00 +000012015 // the PHI instructions.
12016 t1 = dest1Oper.getReg();
12017 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012018
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012019 int valArgIndx = lastAddrIndx + 1;
12020 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000012021 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012022 "invalid operand");
12023 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
12024 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012025 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000012026 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012027 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012028 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000012029 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012030 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012031 (*MIB).addOperand(*argOpers[valArgIndx]);
12032 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012033 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012034 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012035 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012036 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000012037 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012038 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012039 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000012040 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012041 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012042 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012043
Richard Smith42fc29e2012-04-13 22:47:00 +000012044 unsigned t7, t8;
12045 if (Invert) {
12046 t7 = F->getRegInfo().createVirtualRegister(RC);
12047 t8 = F->getRegInfo().createVirtualRegister(RC);
12048 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t7).addReg(t5);
12049 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t8).addReg(t6);
12050 } else {
12051 t7 = t5;
12052 t8 = t6;
12053 }
12054
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012055 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012056 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012057 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012058 MIB.addReg(t2);
12059
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012060 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012061 MIB.addReg(t7);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012062 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012063 MIB.addReg(t8);
Scott Michelfdc40a02009-02-17 22:15:04 +000012064
Dale Johannesene4d209d2009-02-03 20:21:25 +000012065 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012066 for (int i=0; i <= lastAddrIndx; ++i)
12067 (*MIB).addOperand(*argOpers[i]);
12068
12069 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012070 (*MIB).setMemRefs(bInstr->memoperands_begin(),
12071 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012072
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012073 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012074 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012075 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012076 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012077
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012078 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012079 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012080
Dan Gohman14152b42010-07-06 20:24:04 +000012081 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012082 return nextMBB;
12083}
12084
12085// private utility function
12086MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000012087X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
12088 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000012089 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000012090 // For the atomic min/max operator, we generate
12091 // thisMBB:
12092 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000012093 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000012094 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000012095 // cmp t1, t2
12096 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000012097 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000012098 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
12099 // bz newMBB
12100 // fallthrough -->nextMBB
12101 //
12102 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12103 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012104 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012105 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000012106
Mon P Wang63307c32008-05-05 19:05:59 +000012107 /// First build the CFG
12108 MachineFunction *F = MBB->getParent();
12109 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012110 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
12111 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
12112 F->insert(MBBIter, newMBB);
12113 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012114
Dan Gohman14152b42010-07-06 20:24:04 +000012115 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
12116 nextMBB->splice(nextMBB->begin(), thisMBB,
12117 llvm::next(MachineBasicBlock::iterator(mInstr)),
12118 thisMBB->end());
12119 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012120
Mon P Wang63307c32008-05-05 19:05:59 +000012121 // Update thisMBB to fall through to newMBB
12122 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012123
Mon P Wang63307c32008-05-05 19:05:59 +000012124 // newMBB jumps to newMBB and fall through to nextMBB
12125 newMBB->addSuccessor(nextMBB);
12126 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012127
Dale Johannesene4d209d2009-02-03 20:21:25 +000012128 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000012129 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012130 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000012131 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000012132 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012133 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000012134 int numArgs = mInstr->getNumOperands() - 1;
12135 for (int i=0; i < numArgs; ++i)
12136 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000012137
Mon P Wang63307c32008-05-05 19:05:59 +000012138 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012139 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012140 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000012141
Craig Topperc9099502012-04-20 06:31:50 +000012142 unsigned t1 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012143 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000012144 for (int i=0; i <= lastAddrIndx; ++i)
12145 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000012146
Mon P Wang63307c32008-05-05 19:05:59 +000012147 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000012148 assert((argOpers[valArgIndx]->isReg() ||
12149 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000012150 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000012151
Craig Topperc9099502012-04-20 06:31:50 +000012152 unsigned t2 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dan Gohmand735b802008-10-03 15:45:36 +000012153 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012154 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000012155 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012156 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000012157 (*MIB).addOperand(*argOpers[valArgIndx]);
12158
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012159 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012160 MIB.addReg(t1);
12161
Dale Johannesene4d209d2009-02-03 20:21:25 +000012162 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000012163 MIB.addReg(t1);
12164 MIB.addReg(t2);
12165
12166 // Generate movc
Craig Topperc9099502012-04-20 06:31:50 +000012167 unsigned t3 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012168 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000012169 MIB.addReg(t2);
12170 MIB.addReg(t1);
12171
12172 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000012173 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000012174 for (int i=0; i <= lastAddrIndx; ++i)
12175 (*MIB).addOperand(*argOpers[i]);
12176 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000012177 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012178 (*MIB).setMemRefs(mInstr->memoperands_begin(),
12179 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000012180
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012181 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000012182 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012183
Mon P Wang63307c32008-05-05 19:05:59 +000012184 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012185 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012186
Dan Gohman14152b42010-07-06 20:24:04 +000012187 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000012188 return nextMBB;
12189}
12190
Eric Christopherf83a5de2009-08-27 18:08:16 +000012191// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012192// or XMM0_V32I8 in AVX all of this code can be replaced with that
12193// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012194MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012195X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012196 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012197 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012198 "Target must have SSE4.2 or AVX features enabled");
12199
Eric Christopherb120ab42009-08-18 22:50:32 +000012200 DebugLoc dl = MI->getDebugLoc();
12201 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012202 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012203 if (!Subtarget->hasAVX()) {
12204 if (memArg)
12205 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12206 else
12207 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12208 } else {
12209 if (memArg)
12210 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12211 else
12212 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12213 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012214
Eric Christopher41c902f2010-11-30 08:20:21 +000012215 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012216 for (unsigned i = 0; i < numArgs; ++i) {
12217 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012218 if (!(Op.isReg() && Op.isImplicit()))
12219 MIB.addOperand(Op);
12220 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012221 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012222 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012223 .addReg(X86::XMM0);
12224
Dan Gohman14152b42010-07-06 20:24:04 +000012225 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012226 return BB;
12227}
12228
12229MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012230X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012231 DebugLoc dl = MI->getDebugLoc();
12232 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012233
Eric Christopher228232b2010-11-30 07:20:12 +000012234 // Address into RAX/EAX, other two args into ECX, EDX.
12235 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12236 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12237 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12238 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012239 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012240
Eric Christopher228232b2010-11-30 07:20:12 +000012241 unsigned ValOps = X86::AddrNumOperands;
12242 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12243 .addReg(MI->getOperand(ValOps).getReg());
12244 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12245 .addReg(MI->getOperand(ValOps+1).getReg());
12246
12247 // The instruction doesn't actually take any operands though.
12248 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012249
Eric Christopher228232b2010-11-30 07:20:12 +000012250 MI->eraseFromParent(); // The pseudo is gone now.
12251 return BB;
12252}
12253
12254MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012255X86TargetLowering::EmitVAARG64WithCustomInserter(
12256 MachineInstr *MI,
12257 MachineBasicBlock *MBB) const {
12258 // Emit va_arg instruction on X86-64.
12259
12260 // Operands to this pseudo-instruction:
12261 // 0 ) Output : destination address (reg)
12262 // 1-5) Input : va_list address (addr, i64mem)
12263 // 6 ) ArgSize : Size (in bytes) of vararg type
12264 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12265 // 8 ) Align : Alignment of type
12266 // 9 ) EFLAGS (implicit-def)
12267
12268 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12269 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12270
12271 unsigned DestReg = MI->getOperand(0).getReg();
12272 MachineOperand &Base = MI->getOperand(1);
12273 MachineOperand &Scale = MI->getOperand(2);
12274 MachineOperand &Index = MI->getOperand(3);
12275 MachineOperand &Disp = MI->getOperand(4);
12276 MachineOperand &Segment = MI->getOperand(5);
12277 unsigned ArgSize = MI->getOperand(6).getImm();
12278 unsigned ArgMode = MI->getOperand(7).getImm();
12279 unsigned Align = MI->getOperand(8).getImm();
12280
12281 // Memory Reference
12282 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12283 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12284 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12285
12286 // Machine Information
12287 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12288 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12289 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12290 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12291 DebugLoc DL = MI->getDebugLoc();
12292
12293 // struct va_list {
12294 // i32 gp_offset
12295 // i32 fp_offset
12296 // i64 overflow_area (address)
12297 // i64 reg_save_area (address)
12298 // }
12299 // sizeof(va_list) = 24
12300 // alignment(va_list) = 8
12301
12302 unsigned TotalNumIntRegs = 6;
12303 unsigned TotalNumXMMRegs = 8;
12304 bool UseGPOffset = (ArgMode == 1);
12305 bool UseFPOffset = (ArgMode == 2);
12306 unsigned MaxOffset = TotalNumIntRegs * 8 +
12307 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12308
12309 /* Align ArgSize to a multiple of 8 */
12310 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12311 bool NeedsAlign = (Align > 8);
12312
12313 MachineBasicBlock *thisMBB = MBB;
12314 MachineBasicBlock *overflowMBB;
12315 MachineBasicBlock *offsetMBB;
12316 MachineBasicBlock *endMBB;
12317
12318 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12319 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12320 unsigned OffsetReg = 0;
12321
12322 if (!UseGPOffset && !UseFPOffset) {
12323 // If we only pull from the overflow region, we don't create a branch.
12324 // We don't need to alter control flow.
12325 OffsetDestReg = 0; // unused
12326 OverflowDestReg = DestReg;
12327
12328 offsetMBB = NULL;
12329 overflowMBB = thisMBB;
12330 endMBB = thisMBB;
12331 } else {
12332 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12333 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12334 // If not, pull from overflow_area. (branch to overflowMBB)
12335 //
12336 // thisMBB
12337 // | .
12338 // | .
12339 // offsetMBB overflowMBB
12340 // | .
12341 // | .
12342 // endMBB
12343
12344 // Registers for the PHI in endMBB
12345 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12346 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12347
12348 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12349 MachineFunction *MF = MBB->getParent();
12350 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12351 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12352 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12353
12354 MachineFunction::iterator MBBIter = MBB;
12355 ++MBBIter;
12356
12357 // Insert the new basic blocks
12358 MF->insert(MBBIter, offsetMBB);
12359 MF->insert(MBBIter, overflowMBB);
12360 MF->insert(MBBIter, endMBB);
12361
12362 // Transfer the remainder of MBB and its successor edges to endMBB.
12363 endMBB->splice(endMBB->begin(), thisMBB,
12364 llvm::next(MachineBasicBlock::iterator(MI)),
12365 thisMBB->end());
12366 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12367
12368 // Make offsetMBB and overflowMBB successors of thisMBB
12369 thisMBB->addSuccessor(offsetMBB);
12370 thisMBB->addSuccessor(overflowMBB);
12371
12372 // endMBB is a successor of both offsetMBB and overflowMBB
12373 offsetMBB->addSuccessor(endMBB);
12374 overflowMBB->addSuccessor(endMBB);
12375
12376 // Load the offset value into a register
12377 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12378 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12379 .addOperand(Base)
12380 .addOperand(Scale)
12381 .addOperand(Index)
12382 .addDisp(Disp, UseFPOffset ? 4 : 0)
12383 .addOperand(Segment)
12384 .setMemRefs(MMOBegin, MMOEnd);
12385
12386 // Check if there is enough room left to pull this argument.
12387 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12388 .addReg(OffsetReg)
12389 .addImm(MaxOffset + 8 - ArgSizeA8);
12390
12391 // Branch to "overflowMBB" if offset >= max
12392 // Fall through to "offsetMBB" otherwise
12393 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12394 .addMBB(overflowMBB);
12395 }
12396
12397 // In offsetMBB, emit code to use the reg_save_area.
12398 if (offsetMBB) {
12399 assert(OffsetReg != 0);
12400
12401 // Read the reg_save_area address.
12402 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12403 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12404 .addOperand(Base)
12405 .addOperand(Scale)
12406 .addOperand(Index)
12407 .addDisp(Disp, 16)
12408 .addOperand(Segment)
12409 .setMemRefs(MMOBegin, MMOEnd);
12410
12411 // Zero-extend the offset
12412 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12413 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12414 .addImm(0)
12415 .addReg(OffsetReg)
12416 .addImm(X86::sub_32bit);
12417
12418 // Add the offset to the reg_save_area to get the final address.
12419 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12420 .addReg(OffsetReg64)
12421 .addReg(RegSaveReg);
12422
12423 // Compute the offset for the next argument
12424 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12425 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12426 .addReg(OffsetReg)
12427 .addImm(UseFPOffset ? 16 : 8);
12428
12429 // Store it back into the va_list.
12430 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12431 .addOperand(Base)
12432 .addOperand(Scale)
12433 .addOperand(Index)
12434 .addDisp(Disp, UseFPOffset ? 4 : 0)
12435 .addOperand(Segment)
12436 .addReg(NextOffsetReg)
12437 .setMemRefs(MMOBegin, MMOEnd);
12438
12439 // Jump to endMBB
12440 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12441 .addMBB(endMBB);
12442 }
12443
12444 //
12445 // Emit code to use overflow area
12446 //
12447
12448 // Load the overflow_area address into a register.
12449 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12450 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12451 .addOperand(Base)
12452 .addOperand(Scale)
12453 .addOperand(Index)
12454 .addDisp(Disp, 8)
12455 .addOperand(Segment)
12456 .setMemRefs(MMOBegin, MMOEnd);
12457
12458 // If we need to align it, do so. Otherwise, just copy the address
12459 // to OverflowDestReg.
12460 if (NeedsAlign) {
12461 // Align the overflow address
12462 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12463 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12464
12465 // aligned_addr = (addr + (align-1)) & ~(align-1)
12466 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12467 .addReg(OverflowAddrReg)
12468 .addImm(Align-1);
12469
12470 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12471 .addReg(TmpReg)
12472 .addImm(~(uint64_t)(Align-1));
12473 } else {
12474 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12475 .addReg(OverflowAddrReg);
12476 }
12477
12478 // Compute the next overflow address after this argument.
12479 // (the overflow address should be kept 8-byte aligned)
12480 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12481 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12482 .addReg(OverflowDestReg)
12483 .addImm(ArgSizeA8);
12484
12485 // Store the new overflow address.
12486 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12487 .addOperand(Base)
12488 .addOperand(Scale)
12489 .addOperand(Index)
12490 .addDisp(Disp, 8)
12491 .addOperand(Segment)
12492 .addReg(NextAddrReg)
12493 .setMemRefs(MMOBegin, MMOEnd);
12494
12495 // If we branched, emit the PHI to the front of endMBB.
12496 if (offsetMBB) {
12497 BuildMI(*endMBB, endMBB->begin(), DL,
12498 TII->get(X86::PHI), DestReg)
12499 .addReg(OffsetDestReg).addMBB(offsetMBB)
12500 .addReg(OverflowDestReg).addMBB(overflowMBB);
12501 }
12502
12503 // Erase the pseudo instruction
12504 MI->eraseFromParent();
12505
12506 return endMBB;
12507}
12508
12509MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012510X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12511 MachineInstr *MI,
12512 MachineBasicBlock *MBB) const {
12513 // Emit code to save XMM registers to the stack. The ABI says that the
12514 // number of registers to save is given in %al, so it's theoretically
12515 // possible to do an indirect jump trick to avoid saving all of them,
12516 // however this code takes a simpler approach and just executes all
12517 // of the stores if %al is non-zero. It's less code, and it's probably
12518 // easier on the hardware branch predictor, and stores aren't all that
12519 // expensive anyway.
12520
12521 // Create the new basic blocks. One block contains all the XMM stores,
12522 // and one block is the final destination regardless of whether any
12523 // stores were performed.
12524 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12525 MachineFunction *F = MBB->getParent();
12526 MachineFunction::iterator MBBIter = MBB;
12527 ++MBBIter;
12528 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12529 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12530 F->insert(MBBIter, XMMSaveMBB);
12531 F->insert(MBBIter, EndMBB);
12532
Dan Gohman14152b42010-07-06 20:24:04 +000012533 // Transfer the remainder of MBB and its successor edges to EndMBB.
12534 EndMBB->splice(EndMBB->begin(), MBB,
12535 llvm::next(MachineBasicBlock::iterator(MI)),
12536 MBB->end());
12537 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12538
Dan Gohmand6708ea2009-08-15 01:38:56 +000012539 // The original block will now fall through to the XMM save block.
12540 MBB->addSuccessor(XMMSaveMBB);
12541 // The XMMSaveMBB will fall through to the end block.
12542 XMMSaveMBB->addSuccessor(EndMBB);
12543
12544 // Now add the instructions.
12545 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12546 DebugLoc DL = MI->getDebugLoc();
12547
12548 unsigned CountReg = MI->getOperand(0).getReg();
12549 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12550 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12551
12552 if (!Subtarget->isTargetWin64()) {
12553 // If %al is 0, branch around the XMM save block.
12554 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012555 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012556 MBB->addSuccessor(EndMBB);
12557 }
12558
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012559 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012560 // In the XMM save block, save all the XMM argument registers.
12561 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12562 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012563 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012564 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012565 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012566 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012567 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012568 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012569 .addFrameIndex(RegSaveFrameIndex)
12570 .addImm(/*Scale=*/1)
12571 .addReg(/*IndexReg=*/0)
12572 .addImm(/*Disp=*/Offset)
12573 .addReg(/*Segment=*/0)
12574 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012575 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012576 }
12577
Dan Gohman14152b42010-07-06 20:24:04 +000012578 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012579
12580 return EndMBB;
12581}
Mon P Wang63307c32008-05-05 19:05:59 +000012582
Lang Hames6e3f7e42012-02-03 01:13:49 +000012583// The EFLAGS operand of SelectItr might be missing a kill marker
12584// because there were multiple uses of EFLAGS, and ISel didn't know
12585// which to mark. Figure out whether SelectItr should have had a
12586// kill marker, and set it if it should. Returns the correct kill
12587// marker value.
12588static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12589 MachineBasicBlock* BB,
12590 const TargetRegisterInfo* TRI) {
12591 // Scan forward through BB for a use/def of EFLAGS.
12592 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12593 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012594 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012595 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012596 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012597 if (mi.definesRegister(X86::EFLAGS))
12598 break; // Should have kill-flag - update below.
12599 }
12600
12601 // If we hit the end of the block, check whether EFLAGS is live into a
12602 // successor.
12603 if (miI == BB->end()) {
12604 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12605 sEnd = BB->succ_end();
12606 sItr != sEnd; ++sItr) {
12607 MachineBasicBlock* succ = *sItr;
12608 if (succ->isLiveIn(X86::EFLAGS))
12609 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012610 }
12611 }
12612
Lang Hames6e3f7e42012-02-03 01:13:49 +000012613 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12614 // out. SelectMI should have a kill flag on EFLAGS.
12615 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012616 return true;
12617}
12618
Evan Cheng60c07e12006-07-05 22:17:51 +000012619MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012620X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012621 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012622 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12623 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012624
Chris Lattner52600972009-09-02 05:57:00 +000012625 // To "insert" a SELECT_CC instruction, we actually have to insert the
12626 // diamond control-flow pattern. The incoming instruction knows the
12627 // destination vreg to set, the condition code register to branch on, the
12628 // true/false values to select between, and a branch opcode to use.
12629 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12630 MachineFunction::iterator It = BB;
12631 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012632
Chris Lattner52600972009-09-02 05:57:00 +000012633 // thisMBB:
12634 // ...
12635 // TrueVal = ...
12636 // cmpTY ccX, r1, r2
12637 // bCC copy1MBB
12638 // fallthrough --> copy0MBB
12639 MachineBasicBlock *thisMBB = BB;
12640 MachineFunction *F = BB->getParent();
12641 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12642 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012643 F->insert(It, copy0MBB);
12644 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012645
Bill Wendling730c07e2010-06-25 20:48:10 +000012646 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12647 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012648 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12649 if (!MI->killsRegister(X86::EFLAGS) &&
12650 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12651 copy0MBB->addLiveIn(X86::EFLAGS);
12652 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012653 }
12654
Dan Gohman14152b42010-07-06 20:24:04 +000012655 // Transfer the remainder of BB and its successor edges to sinkMBB.
12656 sinkMBB->splice(sinkMBB->begin(), BB,
12657 llvm::next(MachineBasicBlock::iterator(MI)),
12658 BB->end());
12659 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12660
12661 // Add the true and fallthrough blocks as its successors.
12662 BB->addSuccessor(copy0MBB);
12663 BB->addSuccessor(sinkMBB);
12664
12665 // Create the conditional branch instruction.
12666 unsigned Opc =
12667 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12668 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12669
Chris Lattner52600972009-09-02 05:57:00 +000012670 // copy0MBB:
12671 // %FalseValue = ...
12672 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012673 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012674
Chris Lattner52600972009-09-02 05:57:00 +000012675 // sinkMBB:
12676 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12677 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012678 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12679 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012680 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12681 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12682
Dan Gohman14152b42010-07-06 20:24:04 +000012683 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012684 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012685}
12686
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012687MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012688X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12689 bool Is64Bit) const {
12690 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12691 DebugLoc DL = MI->getDebugLoc();
12692 MachineFunction *MF = BB->getParent();
12693 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12694
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012695 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012696
12697 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12698 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12699
12700 // BB:
12701 // ... [Till the alloca]
12702 // If stacklet is not large enough, jump to mallocMBB
12703 //
12704 // bumpMBB:
12705 // Allocate by subtracting from RSP
12706 // Jump to continueMBB
12707 //
12708 // mallocMBB:
12709 // Allocate by call to runtime
12710 //
12711 // continueMBB:
12712 // ...
12713 // [rest of original BB]
12714 //
12715
12716 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12717 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12718 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12719
12720 MachineRegisterInfo &MRI = MF->getRegInfo();
12721 const TargetRegisterClass *AddrRegClass =
12722 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12723
12724 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12725 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12726 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012727 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012728 sizeVReg = MI->getOperand(1).getReg(),
12729 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12730
12731 MachineFunction::iterator MBBIter = BB;
12732 ++MBBIter;
12733
12734 MF->insert(MBBIter, bumpMBB);
12735 MF->insert(MBBIter, mallocMBB);
12736 MF->insert(MBBIter, continueMBB);
12737
12738 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12739 (MachineBasicBlock::iterator(MI)), BB->end());
12740 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12741
12742 // Add code to the main basic block to check if the stack limit has been hit,
12743 // and if so, jump to mallocMBB otherwise to bumpMBB.
12744 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012745 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012746 .addReg(tmpSPVReg).addReg(sizeVReg);
12747 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012748 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012749 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012750 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12751
12752 // bumpMBB simply decreases the stack pointer, since we know the current
12753 // stacklet has enough space.
12754 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012755 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012756 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012757 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012758 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12759
12760 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012761 const uint32_t *RegMask =
12762 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012763 if (Is64Bit) {
12764 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12765 .addReg(sizeVReg);
12766 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012767 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012768 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012769 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012770 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012771 } else {
12772 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12773 .addImm(12);
12774 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12775 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012776 .addExternalSymbol("__morestack_allocate_stack_space")
12777 .addRegMask(RegMask)
12778 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012779 }
12780
12781 if (!Is64Bit)
12782 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12783 .addImm(16);
12784
12785 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12786 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12787 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12788
12789 // Set up the CFG correctly.
12790 BB->addSuccessor(bumpMBB);
12791 BB->addSuccessor(mallocMBB);
12792 mallocMBB->addSuccessor(continueMBB);
12793 bumpMBB->addSuccessor(continueMBB);
12794
12795 // Take care of the PHI nodes.
12796 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12797 MI->getOperand(0).getReg())
12798 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12799 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12800
12801 // Delete the original pseudo instruction.
12802 MI->eraseFromParent();
12803
12804 // And we're done.
12805 return continueMBB;
12806}
12807
12808MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012809X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012810 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012811 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12812 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012813
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012814 assert(!Subtarget->isTargetEnvMacho());
12815
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012816 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12817 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012818
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012819 if (Subtarget->isTargetWin64()) {
12820 if (Subtarget->isTargetCygMing()) {
12821 // ___chkstk(Mingw64):
12822 // Clobbers R10, R11, RAX and EFLAGS.
12823 // Updates RSP.
12824 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12825 .addExternalSymbol("___chkstk")
12826 .addReg(X86::RAX, RegState::Implicit)
12827 .addReg(X86::RSP, RegState::Implicit)
12828 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12829 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12830 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12831 } else {
12832 // __chkstk(MSVCRT): does not update stack pointer.
12833 // Clobbers R10, R11 and EFLAGS.
12834 // FIXME: RAX(allocated size) might be reused and not killed.
12835 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12836 .addExternalSymbol("__chkstk")
12837 .addReg(X86::RAX, RegState::Implicit)
12838 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12839 // RAX has the offset to subtracted from RSP.
12840 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12841 .addReg(X86::RSP)
12842 .addReg(X86::RAX);
12843 }
12844 } else {
12845 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012846 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12847
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012848 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12849 .addExternalSymbol(StackProbeSymbol)
12850 .addReg(X86::EAX, RegState::Implicit)
12851 .addReg(X86::ESP, RegState::Implicit)
12852 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12853 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12854 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12855 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012856
Dan Gohman14152b42010-07-06 20:24:04 +000012857 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012858 return BB;
12859}
Chris Lattner52600972009-09-02 05:57:00 +000012860
12861MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012862X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12863 MachineBasicBlock *BB) const {
12864 // This is pretty easy. We're taking the value that we received from
12865 // our load from the relocation, sticking it in either RDI (x86-64)
12866 // or EAX and doing an indirect call. The return value will then
12867 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012868 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012869 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012870 DebugLoc DL = MI->getDebugLoc();
12871 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012872
12873 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012874 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012875
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012876 // Get a register mask for the lowered call.
12877 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
12878 // proper register mask.
12879 const uint32_t *RegMask =
12880 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012881 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012882 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12883 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012884 .addReg(X86::RIP)
12885 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012886 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012887 MI->getOperand(3).getTargetFlags())
12888 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012889 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012890 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012891 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000012892 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012893 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12894 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012895 .addReg(0)
12896 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012897 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012898 MI->getOperand(3).getTargetFlags())
12899 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012900 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012901 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012902 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012903 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012904 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12905 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012906 .addReg(TII->getGlobalBaseReg(F))
12907 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012908 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012909 MI->getOperand(3).getTargetFlags())
12910 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012911 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012912 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012913 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012914 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012915
Dan Gohman14152b42010-07-06 20:24:04 +000012916 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012917 return BB;
12918}
12919
12920MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012921X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012922 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012923 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012924 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012925 case X86::TAILJMPd64:
12926 case X86::TAILJMPr64:
12927 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000012928 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012929 case X86::TCRETURNdi64:
12930 case X86::TCRETURNri64:
12931 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012932 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012933 case X86::WIN_ALLOCA:
12934 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012935 case X86::SEG_ALLOCA_32:
12936 return EmitLoweredSegAlloca(MI, BB, false);
12937 case X86::SEG_ALLOCA_64:
12938 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012939 case X86::TLSCall_32:
12940 case X86::TLSCall_64:
12941 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012942 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012943 case X86::CMOV_FR32:
12944 case X86::CMOV_FR64:
12945 case X86::CMOV_V4F32:
12946 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012947 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012948 case X86::CMOV_V8F32:
12949 case X86::CMOV_V4F64:
12950 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012951 case X86::CMOV_GR16:
12952 case X86::CMOV_GR32:
12953 case X86::CMOV_RFP32:
12954 case X86::CMOV_RFP64:
12955 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012956 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012957
Dale Johannesen849f2142007-07-03 00:53:03 +000012958 case X86::FP32_TO_INT16_IN_MEM:
12959 case X86::FP32_TO_INT32_IN_MEM:
12960 case X86::FP32_TO_INT64_IN_MEM:
12961 case X86::FP64_TO_INT16_IN_MEM:
12962 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012963 case X86::FP64_TO_INT64_IN_MEM:
12964 case X86::FP80_TO_INT16_IN_MEM:
12965 case X86::FP80_TO_INT32_IN_MEM:
12966 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012967 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12968 DebugLoc DL = MI->getDebugLoc();
12969
Evan Cheng60c07e12006-07-05 22:17:51 +000012970 // Change the floating point control register to use "round towards zero"
12971 // mode when truncating to an integer value.
12972 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012973 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012974 addFrameReference(BuildMI(*BB, MI, DL,
12975 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012976
12977 // Load the old value of the high byte of the control word...
12978 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000012979 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012980 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012981 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012982
12983 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012984 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012985 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012986
12987 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012988 addFrameReference(BuildMI(*BB, MI, DL,
12989 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012990
12991 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012992 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012993 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012994
12995 // Get the X86 opcode to use.
12996 unsigned Opc;
12997 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012998 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012999 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13000 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13001 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13002 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13003 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13004 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013005 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13006 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13007 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013008 }
13009
13010 X86AddressMode AM;
13011 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013012 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013013 AM.BaseType = X86AddressMode::RegBase;
13014 AM.Base.Reg = Op.getReg();
13015 } else {
13016 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013017 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013018 }
13019 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013020 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013021 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013022 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013023 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013024 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013025 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013026 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013027 AM.GV = Op.getGlobal();
13028 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013029 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013030 }
Dan Gohman14152b42010-07-06 20:24:04 +000013031 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013032 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013033
13034 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013035 addFrameReference(BuildMI(*BB, MI, DL,
13036 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013037
Dan Gohman14152b42010-07-06 20:24:04 +000013038 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013039 return BB;
13040 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013041 // String/text processing lowering.
13042 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013043 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013044 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013045 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013046 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013047 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013048 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013049 case X86::VPCMPESTRM128MEM: {
13050 unsigned NumArgs;
13051 bool MemArg;
13052 switch (MI->getOpcode()) {
13053 default: llvm_unreachable("illegal opcode!");
13054 case X86::PCMPISTRM128REG:
13055 case X86::VPCMPISTRM128REG:
13056 NumArgs = 3; MemArg = false; break;
13057 case X86::PCMPISTRM128MEM:
13058 case X86::VPCMPISTRM128MEM:
13059 NumArgs = 3; MemArg = true; break;
13060 case X86::PCMPESTRM128REG:
13061 case X86::VPCMPESTRM128REG:
13062 NumArgs = 5; MemArg = false; break;
13063 case X86::PCMPESTRM128MEM:
13064 case X86::VPCMPESTRM128MEM:
13065 NumArgs = 5; MemArg = true; break;
13066 }
13067 return EmitPCMP(MI, BB, NumArgs, MemArg);
13068 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013069
Eric Christopher228232b2010-11-30 07:20:12 +000013070 // Thread synchronization.
13071 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013072 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013073
Eric Christopherb120ab42009-08-18 22:50:32 +000013074 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000013075 case X86::ATOMMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013076 case X86::ATOMMAX32:
Mon P Wang63307c32008-05-05 19:05:59 +000013077 case X86::ATOMUMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013078 case X86::ATOMUMAX32:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013079 case X86::ATOMMIN16:
13080 case X86::ATOMMAX16:
13081 case X86::ATOMUMIN16:
13082 case X86::ATOMUMAX16:
13083 case X86::ATOMMIN64:
13084 case X86::ATOMMAX64:
13085 case X86::ATOMUMIN64:
13086 case X86::ATOMUMAX64: {
13087 unsigned Opc;
13088 switch (MI->getOpcode()) {
13089 default: llvm_unreachable("illegal opcode!");
13090 case X86::ATOMMIN32: Opc = X86::CMOVL32rr; break;
13091 case X86::ATOMMAX32: Opc = X86::CMOVG32rr; break;
13092 case X86::ATOMUMIN32: Opc = X86::CMOVB32rr; break;
13093 case X86::ATOMUMAX32: Opc = X86::CMOVA32rr; break;
13094 case X86::ATOMMIN16: Opc = X86::CMOVL16rr; break;
13095 case X86::ATOMMAX16: Opc = X86::CMOVG16rr; break;
13096 case X86::ATOMUMIN16: Opc = X86::CMOVB16rr; break;
13097 case X86::ATOMUMAX16: Opc = X86::CMOVA16rr; break;
13098 case X86::ATOMMIN64: Opc = X86::CMOVL64rr; break;
13099 case X86::ATOMMAX64: Opc = X86::CMOVG64rr; break;
13100 case X86::ATOMUMIN64: Opc = X86::CMOVB64rr; break;
13101 case X86::ATOMUMAX64: Opc = X86::CMOVA64rr; break;
13102 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
13103 }
13104 return EmitAtomicMinMaxWithCustomInserter(MI, BB, Opc);
13105 }
13106
13107 case X86::ATOMAND32:
13108 case X86::ATOMOR32:
13109 case X86::ATOMXOR32:
13110 case X86::ATOMNAND32: {
13111 bool Invert = false;
13112 unsigned RegOpc, ImmOpc;
13113 switch (MI->getOpcode()) {
13114 default: llvm_unreachable("illegal opcode!");
13115 case X86::ATOMAND32:
13116 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; break;
13117 case X86::ATOMOR32:
13118 RegOpc = X86::OR32rr; ImmOpc = X86::OR32ri; break;
13119 case X86::ATOMXOR32:
13120 RegOpc = X86::XOR32rr; ImmOpc = X86::XOR32ri; break;
13121 case X86::ATOMNAND32:
13122 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; Invert = true; break;
13123 }
13124 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13125 X86::MOV32rm, X86::LCMPXCHG32,
13126 X86::NOT32r, X86::EAX,
13127 &X86::GR32RegClass, Invert);
13128 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013129
13130 case X86::ATOMAND16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013131 case X86::ATOMOR16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013132 case X86::ATOMXOR16:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013133 case X86::ATOMNAND16: {
13134 bool Invert = false;
13135 unsigned RegOpc, ImmOpc;
13136 switch (MI->getOpcode()) {
13137 default: llvm_unreachable("illegal opcode!");
13138 case X86::ATOMAND16:
13139 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; break;
13140 case X86::ATOMOR16:
13141 RegOpc = X86::OR16rr; ImmOpc = X86::OR16ri; break;
13142 case X86::ATOMXOR16:
13143 RegOpc = X86::XOR16rr; ImmOpc = X86::XOR16ri; break;
13144 case X86::ATOMNAND16:
13145 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; Invert = true; break;
13146 }
13147 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13148 X86::MOV16rm, X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013149 X86::NOT16r, X86::AX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013150 &X86::GR16RegClass, Invert);
13151 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013152
13153 case X86::ATOMAND8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013154 case X86::ATOMOR8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013155 case X86::ATOMXOR8:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013156 case X86::ATOMNAND8: {
13157 bool Invert = false;
13158 unsigned RegOpc, ImmOpc;
13159 switch (MI->getOpcode()) {
13160 default: llvm_unreachable("illegal opcode!");
13161 case X86::ATOMAND8:
13162 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; break;
13163 case X86::ATOMOR8:
13164 RegOpc = X86::OR8rr; ImmOpc = X86::OR8ri; break;
13165 case X86::ATOMXOR8:
13166 RegOpc = X86::XOR8rr; ImmOpc = X86::XOR8ri; break;
13167 case X86::ATOMNAND8:
13168 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; Invert = true; break;
13169 }
13170 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13171 X86::MOV8rm, X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013172 X86::NOT8r, X86::AL,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013173 &X86::GR8RegClass, Invert);
13174 }
13175
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013176 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000013177 case X86::ATOMAND64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013178 case X86::ATOMOR64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013179 case X86::ATOMXOR64:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013180 case X86::ATOMNAND64: {
13181 bool Invert = false;
13182 unsigned RegOpc, ImmOpc;
13183 switch (MI->getOpcode()) {
13184 default: llvm_unreachable("illegal opcode!");
13185 case X86::ATOMAND64:
13186 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; break;
13187 case X86::ATOMOR64:
13188 RegOpc = X86::OR64rr; ImmOpc = X86::OR64ri32; break;
13189 case X86::ATOMXOR64:
13190 RegOpc = X86::XOR64rr; ImmOpc = X86::XOR64ri32; break;
13191 case X86::ATOMNAND64:
13192 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; Invert = true; break;
13193 }
13194 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13195 X86::MOV64rm, X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013196 X86::NOT64r, X86::RAX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013197 &X86::GR64RegClass, Invert);
13198 }
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013199
13200 // This group does 64-bit operations on a 32-bit host.
13201 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013202 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013203 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013204 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013205 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013206 case X86::ATOMSUB6432:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013207 case X86::ATOMSWAP6432: {
13208 bool Invert = false;
13209 unsigned RegOpcL, RegOpcH, ImmOpcL, ImmOpcH;
13210 switch (MI->getOpcode()) {
13211 default: llvm_unreachable("illegal opcode!");
13212 case X86::ATOMAND6432:
13213 RegOpcL = RegOpcH = X86::AND32rr;
13214 ImmOpcL = ImmOpcH = X86::AND32ri;
13215 break;
13216 case X86::ATOMOR6432:
13217 RegOpcL = RegOpcH = X86::OR32rr;
13218 ImmOpcL = ImmOpcH = X86::OR32ri;
13219 break;
13220 case X86::ATOMXOR6432:
13221 RegOpcL = RegOpcH = X86::XOR32rr;
13222 ImmOpcL = ImmOpcH = X86::XOR32ri;
13223 break;
13224 case X86::ATOMNAND6432:
13225 RegOpcL = RegOpcH = X86::AND32rr;
13226 ImmOpcL = ImmOpcH = X86::AND32ri;
13227 Invert = true;
13228 break;
13229 case X86::ATOMADD6432:
13230 RegOpcL = X86::ADD32rr; RegOpcH = X86::ADC32rr;
13231 ImmOpcL = X86::ADD32ri; ImmOpcH = X86::ADC32ri;
13232 break;
13233 case X86::ATOMSUB6432:
13234 RegOpcL = X86::SUB32rr; RegOpcH = X86::SBB32rr;
13235 ImmOpcL = X86::SUB32ri; ImmOpcH = X86::SBB32ri;
13236 break;
13237 case X86::ATOMSWAP6432:
13238 RegOpcL = RegOpcH = X86::MOV32rr;
13239 ImmOpcL = ImmOpcH = X86::MOV32ri;
13240 break;
13241 }
13242 return EmitAtomicBit6432WithCustomInserter(MI, BB, RegOpcL, RegOpcH,
13243 ImmOpcL, ImmOpcH, Invert);
13244 }
13245
Dan Gohmand6708ea2009-08-15 01:38:56 +000013246 case X86::VASTART_SAVE_XMM_REGS:
13247 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013248
13249 case X86::VAARG_64:
13250 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013251 }
13252}
13253
13254//===----------------------------------------------------------------------===//
13255// X86 Optimization Hooks
13256//===----------------------------------------------------------------------===//
13257
Dan Gohman475871a2008-07-27 21:46:04 +000013258void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013259 APInt &KnownZero,
13260 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013261 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013262 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013263 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013264 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013265 assert((Opc >= ISD::BUILTIN_OP_END ||
13266 Opc == ISD::INTRINSIC_WO_CHAIN ||
13267 Opc == ISD::INTRINSIC_W_CHAIN ||
13268 Opc == ISD::INTRINSIC_VOID) &&
13269 "Should use MaskedValueIsZero if you don't know whether Op"
13270 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013271
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013272 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013273 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013274 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013275 case X86ISD::ADD:
13276 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013277 case X86ISD::ADC:
13278 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013279 case X86ISD::SMUL:
13280 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013281 case X86ISD::INC:
13282 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013283 case X86ISD::OR:
13284 case X86ISD::XOR:
13285 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013286 // These nodes' second result is a boolean.
13287 if (Op.getResNo() == 0)
13288 break;
13289 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013290 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013291 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013292 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013293 case ISD::INTRINSIC_WO_CHAIN: {
13294 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13295 unsigned NumLoBits = 0;
13296 switch (IntId) {
13297 default: break;
13298 case Intrinsic::x86_sse_movmsk_ps:
13299 case Intrinsic::x86_avx_movmsk_ps_256:
13300 case Intrinsic::x86_sse2_movmsk_pd:
13301 case Intrinsic::x86_avx_movmsk_pd_256:
13302 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013303 case Intrinsic::x86_sse2_pmovmskb_128:
13304 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013305 // High bits of movmskp{s|d}, pmovmskb are known zero.
13306 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013307 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013308 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13309 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13310 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13311 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13312 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13313 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013314 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013315 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013316 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013317 break;
13318 }
13319 }
13320 break;
13321 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013322 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013323}
Chris Lattner259e97c2006-01-31 19:43:35 +000013324
Owen Andersonbc146b02010-09-21 20:42:50 +000013325unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13326 unsigned Depth) const {
13327 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13328 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13329 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013330
Owen Andersonbc146b02010-09-21 20:42:50 +000013331 // Fallback case.
13332 return 1;
13333}
13334
Evan Cheng206ee9d2006-07-07 08:33:52 +000013335/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013336/// node is a GlobalAddress + offset.
13337bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013338 const GlobalValue* &GA,
13339 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013340 if (N->getOpcode() == X86ISD::Wrapper) {
13341 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013342 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013343 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013344 return true;
13345 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013346 }
Evan Chengad4196b2008-05-12 19:56:52 +000013347 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013348}
13349
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013350/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13351/// same as extracting the high 128-bit part of 256-bit vector and then
13352/// inserting the result into the low part of a new 256-bit vector
13353static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13354 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013355 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013356
13357 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013358 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013359 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13360 SVOp->getMaskElt(j) >= 0)
13361 return false;
13362
13363 return true;
13364}
13365
13366/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13367/// same as extracting the low 128-bit part of 256-bit vector and then
13368/// inserting the result into the high part of a new 256-bit vector
13369static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13370 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013371 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013372
13373 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013374 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013375 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13376 SVOp->getMaskElt(j) >= 0)
13377 return false;
13378
13379 return true;
13380}
13381
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013382/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13383static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013384 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013385 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013386 DebugLoc dl = N->getDebugLoc();
13387 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13388 SDValue V1 = SVOp->getOperand(0);
13389 SDValue V2 = SVOp->getOperand(1);
13390 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013391 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013392
13393 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13394 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13395 //
13396 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013397 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013398 // V UNDEF BUILD_VECTOR UNDEF
13399 // \ / \ /
13400 // CONCAT_VECTOR CONCAT_VECTOR
13401 // \ /
13402 // \ /
13403 // RESULT: V + zero extended
13404 //
13405 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13406 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13407 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13408 return SDValue();
13409
13410 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13411 return SDValue();
13412
13413 // To match the shuffle mask, the first half of the mask should
13414 // be exactly the first vector, and all the rest a splat with the
13415 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013416 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013417 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13418 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13419 return SDValue();
13420
Chad Rosier3d1161e2012-01-03 21:05:52 +000013421 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13422 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013423 if (Ld->hasNUsesOfValue(1, 0)) {
13424 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13425 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13426 SDValue ResNode =
13427 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13428 Ld->getMemoryVT(),
13429 Ld->getPointerInfo(),
13430 Ld->getAlignment(),
13431 false/*isVolatile*/, true/*ReadMem*/,
13432 false/*WriteMem*/);
13433 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13434 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013435 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013436
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013437 // Emit a zeroed vector and insert the desired subvector on its
13438 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013439 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013440 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013441 return DCI.CombineTo(N, InsV);
13442 }
13443
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013444 //===--------------------------------------------------------------------===//
13445 // Combine some shuffles into subvector extracts and inserts:
13446 //
13447
13448 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13449 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013450 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13451 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013452 return DCI.CombineTo(N, InsV);
13453 }
13454
13455 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13456 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013457 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13458 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013459 return DCI.CombineTo(N, InsV);
13460 }
13461
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013462 return SDValue();
13463}
13464
13465/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013466static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013467 TargetLowering::DAGCombinerInfo &DCI,
13468 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013469 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013470 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013471
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013472 // Don't create instructions with illegal types after legalize types has run.
13473 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13474 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13475 return SDValue();
13476
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013477 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013478 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013479 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013480 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013481
13482 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013483 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013484 return SDValue();
13485
13486 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13487 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13488 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013489 SmallVector<SDValue, 16> Elts;
13490 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013491 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013492
Nate Begemanfdea31a2010-03-24 20:49:50 +000013493 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013494}
Evan Chengd880b972008-05-09 21:53:03 +000013495
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013496
Craig Topperc16f8512012-04-25 06:39:39 +000013497/// DCI, PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013498/// a sequence of vector shuffle operations.
13499/// It is possible when we truncate 256-bit vector to 128-bit vector
13500
Chad Rosiera20e1e72012-08-01 18:39:17 +000013501SDValue X86TargetLowering::PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013502 DAGCombinerInfo &DCI) const {
13503 if (!DCI.isBeforeLegalizeOps())
13504 return SDValue();
13505
Craig Topper3ef43cf2012-04-24 06:36:35 +000013506 if (!Subtarget->hasAVX())
13507 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013508
13509 EVT VT = N->getValueType(0);
13510 SDValue Op = N->getOperand(0);
13511 EVT OpVT = Op.getValueType();
13512 DebugLoc dl = N->getDebugLoc();
13513
13514 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13515
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013516 if (Subtarget->hasAVX2()) {
13517 // AVX2: v4i64 -> v4i32
13518
13519 // VPERMD
13520 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13521
13522 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13523 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13524 ShufMask);
13525
Craig Topperd63fa652012-04-22 18:51:37 +000013526 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13527 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013528 }
13529
13530 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013531 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013532 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013533
13534 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013535 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013536
13537 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13538 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13539
13540 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013541 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013542
Craig Toppercacafd42012-08-14 08:18:43 +000013543 SDValue Undef = DAG.getUNDEF(VT);
13544 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13545 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013546
13547 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013548 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013549
Elena Demikhovsky73252572012-02-01 10:33:05 +000013550 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013551 }
Craig Topperd63fa652012-04-22 18:51:37 +000013552
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013553 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13554
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013555 if (Subtarget->hasAVX2()) {
13556 // AVX2: v8i32 -> v8i16
13557
13558 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013559
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013560 // PSHUFB
13561 SmallVector<SDValue,32> pshufbMask;
13562 for (unsigned i = 0; i < 2; ++i) {
13563 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13564 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13565 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13566 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13567 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13568 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13569 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13570 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13571 for (unsigned j = 0; j < 8; ++j)
13572 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13573 }
Craig Topperd63fa652012-04-22 18:51:37 +000013574 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13575 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013576 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13577
13578 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13579
13580 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013581 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013582 &ShufMask[0]);
13583
Craig Topperd63fa652012-04-22 18:51:37 +000013584 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13585 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013586
13587 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13588 }
13589
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013590 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013591 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013592
13593 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013594 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013595
13596 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13597 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13598
13599 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013600 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13601 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013602
Craig Toppercacafd42012-08-14 08:18:43 +000013603 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13604 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13605 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013606
13607 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13608 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13609
13610 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013611 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013612
Elena Demikhovsky73252572012-02-01 10:33:05 +000013613 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013614 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013615 }
13616
13617 return SDValue();
13618}
13619
Craig Topper89f4e662012-03-20 07:17:59 +000013620/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13621/// specific shuffle of a load can be folded into a single element load.
13622/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13623/// shuffles have been customed lowered so we need to handle those here.
13624static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13625 TargetLowering::DAGCombinerInfo &DCI) {
13626 if (DCI.isBeforeLegalizeOps())
13627 return SDValue();
13628
13629 SDValue InVec = N->getOperand(0);
13630 SDValue EltNo = N->getOperand(1);
13631
13632 if (!isa<ConstantSDNode>(EltNo))
13633 return SDValue();
13634
13635 EVT VT = InVec.getValueType();
13636
13637 bool HasShuffleIntoBitcast = false;
13638 if (InVec.getOpcode() == ISD::BITCAST) {
13639 // Don't duplicate a load with other uses.
13640 if (!InVec.hasOneUse())
13641 return SDValue();
13642 EVT BCVT = InVec.getOperand(0).getValueType();
13643 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13644 return SDValue();
13645 InVec = InVec.getOperand(0);
13646 HasShuffleIntoBitcast = true;
13647 }
13648
13649 if (!isTargetShuffle(InVec.getOpcode()))
13650 return SDValue();
13651
13652 // Don't duplicate a load with other uses.
13653 if (!InVec.hasOneUse())
13654 return SDValue();
13655
13656 SmallVector<int, 16> ShuffleMask;
13657 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013658 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13659 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013660 return SDValue();
13661
13662 // Select the input vector, guarding against out of range extract vector.
13663 unsigned NumElems = VT.getVectorNumElements();
13664 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13665 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13666 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13667 : InVec.getOperand(1);
13668
13669 // If inputs to shuffle are the same for both ops, then allow 2 uses
13670 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13671
13672 if (LdNode.getOpcode() == ISD::BITCAST) {
13673 // Don't duplicate a load with other uses.
13674 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13675 return SDValue();
13676
13677 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13678 LdNode = LdNode.getOperand(0);
13679 }
13680
13681 if (!ISD::isNormalLoad(LdNode.getNode()))
13682 return SDValue();
13683
13684 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13685
13686 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13687 return SDValue();
13688
13689 if (HasShuffleIntoBitcast) {
13690 // If there's a bitcast before the shuffle, check if the load type and
13691 // alignment is valid.
13692 unsigned Align = LN0->getAlignment();
13693 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13694 unsigned NewAlign = TLI.getTargetData()->
13695 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13696
13697 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13698 return SDValue();
13699 }
13700
13701 // All checks match so transform back to vector_shuffle so that DAG combiner
13702 // can finish the job
13703 DebugLoc dl = N->getDebugLoc();
13704
13705 // Create shuffle node taking into account the case that its a unary shuffle
13706 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13707 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13708 InVec.getOperand(0), Shuffle,
13709 &ShuffleMask[0]);
13710 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13711 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13712 EltNo);
13713}
13714
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013715/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13716/// generation and convert it from being a bunch of shuffles and extracts
13717/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013718static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013719 TargetLowering::DAGCombinerInfo &DCI) {
13720 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13721 if (NewOp.getNode())
13722 return NewOp;
13723
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013724 SDValue InputVector = N->getOperand(0);
13725
13726 // Only operate on vectors of 4 elements, where the alternative shuffling
13727 // gets to be more expensive.
13728 if (InputVector.getValueType() != MVT::v4i32)
13729 return SDValue();
13730
13731 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13732 // single use which is a sign-extend or zero-extend, and all elements are
13733 // used.
13734 SmallVector<SDNode *, 4> Uses;
13735 unsigned ExtractedElements = 0;
13736 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13737 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13738 if (UI.getUse().getResNo() != InputVector.getResNo())
13739 return SDValue();
13740
13741 SDNode *Extract = *UI;
13742 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13743 return SDValue();
13744
13745 if (Extract->getValueType(0) != MVT::i32)
13746 return SDValue();
13747 if (!Extract->hasOneUse())
13748 return SDValue();
13749 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13750 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13751 return SDValue();
13752 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13753 return SDValue();
13754
13755 // Record which element was extracted.
13756 ExtractedElements |=
13757 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13758
13759 Uses.push_back(Extract);
13760 }
13761
13762 // If not all the elements were used, this may not be worthwhile.
13763 if (ExtractedElements != 15)
13764 return SDValue();
13765
13766 // Ok, we've now decided to do the transformation.
13767 DebugLoc dl = InputVector.getDebugLoc();
13768
13769 // Store the value to a temporary stack slot.
13770 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013771 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13772 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013773
13774 // Replace each use (extract) with a load of the appropriate element.
13775 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13776 UE = Uses.end(); UI != UE; ++UI) {
13777 SDNode *Extract = *UI;
13778
Nadav Rotem86694292011-05-17 08:31:57 +000013779 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013780 SDValue Idx = Extract->getOperand(1);
13781 unsigned EltSize =
13782 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13783 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013784 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013785 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13786
Nadav Rotem86694292011-05-17 08:31:57 +000013787 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013788 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013789
13790 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013791 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013792 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013793 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013794
13795 // Replace the exact with the load.
13796 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13797 }
13798
13799 // The replacement was made in place; don't return anything.
13800 return SDValue();
13801}
13802
Duncan Sands6bcd2192011-09-17 16:49:39 +000013803/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13804/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013805static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013806 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013807 const X86Subtarget *Subtarget) {
13808 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013809 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013810 // Get the LHS/RHS of the select.
13811 SDValue LHS = N->getOperand(1);
13812 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013813 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013814
Dan Gohman670e5392009-09-21 18:03:22 +000013815 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013816 // instructions match the semantics of the common C idiom x<y?x:y but not
13817 // x<=y?x:y, because of how they handle negative zero (which can be
13818 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013819 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13820 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013821 (Subtarget->hasSSE2() ||
13822 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013823 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013824
Chris Lattner47b4ce82009-03-11 05:48:52 +000013825 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013826 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013827 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13828 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013829 switch (CC) {
13830 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013831 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013832 // Converting this to a min would handle NaNs incorrectly, and swapping
13833 // the operands would cause it to handle comparisons between positive
13834 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013835 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013836 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013837 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13838 break;
13839 std::swap(LHS, RHS);
13840 }
Dan Gohman670e5392009-09-21 18:03:22 +000013841 Opcode = X86ISD::FMIN;
13842 break;
13843 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013844 // Converting this to a min would handle comparisons between positive
13845 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013846 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013847 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13848 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013849 Opcode = X86ISD::FMIN;
13850 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013851 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013852 // Converting this to a min would handle both negative zeros and NaNs
13853 // incorrectly, but we can swap the operands to fix both.
13854 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013855 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013856 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013857 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013858 Opcode = X86ISD::FMIN;
13859 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013860
Dan Gohman670e5392009-09-21 18:03:22 +000013861 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013862 // Converting this to a max would handle comparisons between positive
13863 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013864 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013865 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013866 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013867 Opcode = X86ISD::FMAX;
13868 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013869 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013870 // Converting this to a max would handle NaNs incorrectly, and swapping
13871 // the operands would cause it to handle comparisons between positive
13872 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013873 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013874 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013875 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13876 break;
13877 std::swap(LHS, RHS);
13878 }
Dan Gohman670e5392009-09-21 18:03:22 +000013879 Opcode = X86ISD::FMAX;
13880 break;
13881 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013882 // Converting this to a max would handle both negative zeros and NaNs
13883 // incorrectly, but we can swap the operands to fix both.
13884 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013885 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013886 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013887 case ISD::SETGE:
13888 Opcode = X86ISD::FMAX;
13889 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013890 }
Dan Gohman670e5392009-09-21 18:03:22 +000013891 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013892 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13893 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013894 switch (CC) {
13895 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013896 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013897 // Converting this to a min would handle comparisons between positive
13898 // and negative zero incorrectly, and swapping the operands would
13899 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013900 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013901 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013902 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013903 break;
13904 std::swap(LHS, RHS);
13905 }
Dan Gohman670e5392009-09-21 18:03:22 +000013906 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013907 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013908 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013909 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013910 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013911 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13912 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013913 Opcode = X86ISD::FMIN;
13914 break;
13915 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013916 // Converting this to a min would handle both negative zeros and NaNs
13917 // incorrectly, but we can swap the operands to fix both.
13918 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013919 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013920 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013921 case ISD::SETGE:
13922 Opcode = X86ISD::FMIN;
13923 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013924
Dan Gohman670e5392009-09-21 18:03:22 +000013925 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013926 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013927 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013928 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013929 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013930 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013931 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013932 // Converting this to a max would handle comparisons between positive
13933 // and negative zero incorrectly, and swapping the operands would
13934 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013935 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013936 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013937 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013938 break;
13939 std::swap(LHS, RHS);
13940 }
Dan Gohman670e5392009-09-21 18:03:22 +000013941 Opcode = X86ISD::FMAX;
13942 break;
13943 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013944 // Converting this to a max would handle both negative zeros and NaNs
13945 // incorrectly, but we can swap the operands to fix both.
13946 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013947 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013948 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013949 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013950 Opcode = X86ISD::FMAX;
13951 break;
13952 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013953 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013954
Chris Lattner47b4ce82009-03-11 05:48:52 +000013955 if (Opcode)
13956 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013957 }
Eric Christopherfd179292009-08-27 18:07:15 +000013958
Chris Lattnerd1980a52009-03-12 06:52:53 +000013959 // If this is a select between two integer constants, try to do some
13960 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013961 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13962 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013963 // Don't do this for crazy integer types.
13964 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13965 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013966 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013967 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013968
Chris Lattnercee56e72009-03-13 05:53:31 +000013969 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013970 // Efficiently invertible.
13971 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13972 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13973 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13974 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013975 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013976 }
Eric Christopherfd179292009-08-27 18:07:15 +000013977
Chris Lattnerd1980a52009-03-12 06:52:53 +000013978 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013979 if (FalseC->getAPIntValue() == 0 &&
13980 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013981 if (NeedsCondInvert) // Invert the condition if needed.
13982 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13983 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013984
Chris Lattnerd1980a52009-03-12 06:52:53 +000013985 // Zero extend the condition if needed.
13986 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013987
Chris Lattnercee56e72009-03-13 05:53:31 +000013988 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013989 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013990 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013991 }
Eric Christopherfd179292009-08-27 18:07:15 +000013992
Chris Lattner97a29a52009-03-13 05:22:11 +000013993 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013994 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013995 if (NeedsCondInvert) // Invert the condition if needed.
13996 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13997 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013998
Chris Lattner97a29a52009-03-13 05:22:11 +000013999 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014000 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14001 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014002 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014003 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014004 }
Eric Christopherfd179292009-08-27 18:07:15 +000014005
Chris Lattnercee56e72009-03-13 05:53:31 +000014006 // Optimize cases that will turn into an LEA instruction. This requires
14007 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014008 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014009 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014010 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014011
Chris Lattnercee56e72009-03-13 05:53:31 +000014012 bool isFastMultiplier = false;
14013 if (Diff < 10) {
14014 switch ((unsigned char)Diff) {
14015 default: break;
14016 case 1: // result = add base, cond
14017 case 2: // result = lea base( , cond*2)
14018 case 3: // result = lea base(cond, cond*2)
14019 case 4: // result = lea base( , cond*4)
14020 case 5: // result = lea base(cond, cond*4)
14021 case 8: // result = lea base( , cond*8)
14022 case 9: // result = lea base(cond, cond*8)
14023 isFastMultiplier = true;
14024 break;
14025 }
14026 }
Eric Christopherfd179292009-08-27 18:07:15 +000014027
Chris Lattnercee56e72009-03-13 05:53:31 +000014028 if (isFastMultiplier) {
14029 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14030 if (NeedsCondInvert) // Invert the condition if needed.
14031 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14032 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014033
Chris Lattnercee56e72009-03-13 05:53:31 +000014034 // Zero extend the condition if needed.
14035 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14036 Cond);
14037 // Scale the condition by the difference.
14038 if (Diff != 1)
14039 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14040 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014041
Chris Lattnercee56e72009-03-13 05:53:31 +000014042 // Add the base if non-zero.
14043 if (FalseC->getAPIntValue() != 0)
14044 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14045 SDValue(FalseC, 0));
14046 return Cond;
14047 }
Eric Christopherfd179292009-08-27 18:07:15 +000014048 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014049 }
14050 }
Eric Christopherfd179292009-08-27 18:07:15 +000014051
Evan Cheng56f582d2012-01-04 01:41:39 +000014052 // Canonicalize max and min:
14053 // (x > y) ? x : y -> (x >= y) ? x : y
14054 // (x < y) ? x : y -> (x <= y) ? x : y
14055 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14056 // the need for an extra compare
14057 // against zero. e.g.
14058 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14059 // subl %esi, %edi
14060 // testl %edi, %edi
14061 // movl $0, %eax
14062 // cmovgl %edi, %eax
14063 // =>
14064 // xorl %eax, %eax
14065 // subl %esi, $edi
14066 // cmovsl %eax, %edi
14067 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14068 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14069 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14070 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14071 switch (CC) {
14072 default: break;
14073 case ISD::SETLT:
14074 case ISD::SETGT: {
14075 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14076 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14077 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14078 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14079 }
14080 }
14081 }
14082
Nadav Rotemcc616562012-01-15 19:27:55 +000014083 // If we know that this node is legal then we know that it is going to be
14084 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14085 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14086 // to simplify previous instructions.
14087 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14088 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014089 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014090 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014091
14092 // Don't optimize vector selects that map to mask-registers.
14093 if (BitWidth == 1)
14094 return SDValue();
14095
Nadav Rotemcc616562012-01-15 19:27:55 +000014096 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14097 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14098
14099 APInt KnownZero, KnownOne;
14100 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14101 DCI.isBeforeLegalizeOps());
14102 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14103 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14104 DCI.CommitTargetLoweringOpt(TLO);
14105 }
14106
Dan Gohman475871a2008-07-27 21:46:04 +000014107 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014108}
14109
Michael Liao2a33cec2012-08-10 19:58:13 +000014110// Check whether a boolean test is testing a boolean value generated by
14111// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14112// code.
14113//
14114// Simplify the following patterns:
14115// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14116// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14117// to (Op EFLAGS Cond)
14118//
14119// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14120// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14121// to (Op EFLAGS !Cond)
14122//
14123// where Op could be BRCOND or CMOV.
14124//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014125static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014126 // Quit if not CMP and SUB with its value result used.
14127 if (Cmp.getOpcode() != X86ISD::CMP &&
14128 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14129 return SDValue();
14130
14131 // Quit if not used as a boolean value.
14132 if (CC != X86::COND_E && CC != X86::COND_NE)
14133 return SDValue();
14134
14135 // Check CMP operands. One of them should be 0 or 1 and the other should be
14136 // an SetCC or extended from it.
14137 SDValue Op1 = Cmp.getOperand(0);
14138 SDValue Op2 = Cmp.getOperand(1);
14139
14140 SDValue SetCC;
14141 const ConstantSDNode* C = 0;
14142 bool needOppositeCond = (CC == X86::COND_E);
14143
14144 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14145 SetCC = Op2;
14146 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14147 SetCC = Op1;
14148 else // Quit if all operands are not constants.
14149 return SDValue();
14150
14151 if (C->getZExtValue() == 1)
14152 needOppositeCond = !needOppositeCond;
14153 else if (C->getZExtValue() != 0)
14154 // Quit if the constant is neither 0 or 1.
14155 return SDValue();
14156
14157 // Skip 'zext' node.
14158 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14159 SetCC = SetCC.getOperand(0);
14160
14161 // Quit if not SETCC.
14162 // FIXME: So far we only handle the boolean value generated from SETCC. If
14163 // there is other ways to generate boolean values, we need handle them here
14164 // as well.
14165 if (SetCC.getOpcode() != X86ISD::SETCC)
14166 return SDValue();
14167
14168 // Set the condition code or opposite one if necessary.
14169 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14170 if (needOppositeCond)
14171 CC = X86::GetOppositeBranchCondition(CC);
14172
14173 return SetCC.getOperand(1);
14174}
14175
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014176/// checkFlaggedOrCombine - DAG combination on X86ISD::OR, i.e. with EFLAGS
14177/// updated. If only flag result is used and the result is evaluated from a
14178/// series of element extraction, try to combine it into a PTEST.
14179static SDValue checkFlaggedOrCombine(SDValue Or, X86::CondCode &CC,
14180 SelectionDAG &DAG,
14181 const X86Subtarget *Subtarget) {
14182 SDNode *N = Or.getNode();
14183 DebugLoc DL = N->getDebugLoc();
14184
14185 // Only SSE4.1 and beyond supports PTEST or like.
14186 if (!Subtarget->hasSSE41())
14187 return SDValue();
14188
14189 if (N->getOpcode() != X86ISD::OR)
14190 return SDValue();
14191
14192 // Quit if the value result of OR is used.
14193 if (N->hasAnyUseOfValue(0))
14194 return SDValue();
14195
14196 // Quit if not used as a boolean value.
14197 if (CC != X86::COND_E && CC != X86::COND_NE)
14198 return SDValue();
14199
14200 SmallVector<SDValue, 8> Opnds;
14201 SDValue VecIn;
14202 EVT VT = MVT::Other;
14203 unsigned Mask = 0;
14204
14205 // Recognize a special case where a vector is casted into wide integer to
14206 // test all 0s.
14207 Opnds.push_back(N->getOperand(0));
14208 Opnds.push_back(N->getOperand(1));
14209
14210 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
14211 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
14212 // BFS traverse all OR'd operands.
14213 if (I->getOpcode() == ISD::OR) {
14214 Opnds.push_back(I->getOperand(0));
14215 Opnds.push_back(I->getOperand(1));
14216 // Re-evaluate the number of nodes to be traversed.
Michael Liao95c22a32012-08-28 23:42:17 +000014217 e += 2; // 2 more nodes (LHS and RHS) are pushed.
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014218 continue;
14219 }
14220
14221 // Quit if a non-EXTRACT_VECTOR_ELT
14222 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14223 return SDValue();
14224
14225 // Quit if without a constant index.
14226 SDValue Idx = I->getOperand(1);
14227 if (!isa<ConstantSDNode>(Idx))
14228 return SDValue();
14229
14230 // Check if all elements are extracted from the same vector.
14231 SDValue ExtractedFromVec = I->getOperand(0);
14232 if (VecIn.getNode() == 0) {
14233 VT = ExtractedFromVec.getValueType();
14234 // FIXME: only 128-bit vector is supported so far.
14235 if (!VT.is128BitVector())
14236 return SDValue();
14237 VecIn = ExtractedFromVec;
14238 } else if (VecIn != ExtractedFromVec)
14239 return SDValue();
14240
14241 // Record the constant index.
14242 Mask |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
14243 }
14244
14245 assert(VT.is128BitVector() && "Only 128-bit vector PTEST is supported so far.");
14246
14247 // Quit if not all elements are used.
14248 if (Mask != (1U << VT.getVectorNumElements()) - 1U)
14249 return SDValue();
14250
14251 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32, VecIn, VecIn);
14252}
14253
Chris Lattnerd1980a52009-03-12 06:52:53 +000014254/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14255static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014256 TargetLowering::DAGCombinerInfo &DCI,
14257 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014258 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014259
Chris Lattnerd1980a52009-03-12 06:52:53 +000014260 // If the flag operand isn't dead, don't touch this CMOV.
14261 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14262 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014263
Evan Chengb5a55d92011-05-24 01:48:22 +000014264 SDValue FalseOp = N->getOperand(0);
14265 SDValue TrueOp = N->getOperand(1);
14266 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14267 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014268
Evan Chengb5a55d92011-05-24 01:48:22 +000014269 if (CC == X86::COND_E || CC == X86::COND_NE) {
14270 switch (Cond.getOpcode()) {
14271 default: break;
14272 case X86ISD::BSR:
14273 case X86ISD::BSF:
14274 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14275 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14276 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14277 }
14278 }
14279
Michael Liao2a33cec2012-08-10 19:58:13 +000014280 SDValue Flags;
14281
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014282 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014283 if (Flags.getNode() &&
14284 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014285 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014286 SDValue Ops[] = { FalseOp, TrueOp,
14287 DAG.getConstant(CC, MVT::i8), Flags };
14288 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14289 Ops, array_lengthof(Ops));
14290 }
14291
14292 Flags = checkFlaggedOrCombine(Cond, CC, DAG, Subtarget);
14293 if (Flags.getNode()) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014294 SDValue Ops[] = { FalseOp, TrueOp,
14295 DAG.getConstant(CC, MVT::i8), Flags };
14296 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14297 Ops, array_lengthof(Ops));
14298 }
14299
Chris Lattnerd1980a52009-03-12 06:52:53 +000014300 // If this is a select between two integer constants, try to do some
14301 // optimizations. Note that the operands are ordered the opposite of SELECT
14302 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014303 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14304 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014305 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14306 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014307 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14308 CC = X86::GetOppositeBranchCondition(CC);
14309 std::swap(TrueC, FalseC);
14310 }
Eric Christopherfd179292009-08-27 18:07:15 +000014311
Chris Lattnerd1980a52009-03-12 06:52:53 +000014312 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014313 // This is efficient for any integer data type (including i8/i16) and
14314 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014315 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014316 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14317 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014318
Chris Lattnerd1980a52009-03-12 06:52:53 +000014319 // Zero extend the condition if needed.
14320 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014321
Chris Lattnerd1980a52009-03-12 06:52:53 +000014322 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14323 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014324 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014325 if (N->getNumValues() == 2) // Dead flag value?
14326 return DCI.CombineTo(N, Cond, SDValue());
14327 return Cond;
14328 }
Eric Christopherfd179292009-08-27 18:07:15 +000014329
Chris Lattnercee56e72009-03-13 05:53:31 +000014330 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14331 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014332 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014333 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14334 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014335
Chris Lattner97a29a52009-03-13 05:22:11 +000014336 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014337 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14338 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014339 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14340 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014341
Chris Lattner97a29a52009-03-13 05:22:11 +000014342 if (N->getNumValues() == 2) // Dead flag value?
14343 return DCI.CombineTo(N, Cond, SDValue());
14344 return Cond;
14345 }
Eric Christopherfd179292009-08-27 18:07:15 +000014346
Chris Lattnercee56e72009-03-13 05:53:31 +000014347 // Optimize cases that will turn into an LEA instruction. This requires
14348 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014349 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014350 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014351 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014352
Chris Lattnercee56e72009-03-13 05:53:31 +000014353 bool isFastMultiplier = false;
14354 if (Diff < 10) {
14355 switch ((unsigned char)Diff) {
14356 default: break;
14357 case 1: // result = add base, cond
14358 case 2: // result = lea base( , cond*2)
14359 case 3: // result = lea base(cond, cond*2)
14360 case 4: // result = lea base( , cond*4)
14361 case 5: // result = lea base(cond, cond*4)
14362 case 8: // result = lea base( , cond*8)
14363 case 9: // result = lea base(cond, cond*8)
14364 isFastMultiplier = true;
14365 break;
14366 }
14367 }
Eric Christopherfd179292009-08-27 18:07:15 +000014368
Chris Lattnercee56e72009-03-13 05:53:31 +000014369 if (isFastMultiplier) {
14370 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014371 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14372 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014373 // Zero extend the condition if needed.
14374 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14375 Cond);
14376 // Scale the condition by the difference.
14377 if (Diff != 1)
14378 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14379 DAG.getConstant(Diff, Cond.getValueType()));
14380
14381 // Add the base if non-zero.
14382 if (FalseC->getAPIntValue() != 0)
14383 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14384 SDValue(FalseC, 0));
14385 if (N->getNumValues() == 2) // Dead flag value?
14386 return DCI.CombineTo(N, Cond, SDValue());
14387 return Cond;
14388 }
Eric Christopherfd179292009-08-27 18:07:15 +000014389 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014390 }
14391 }
14392 return SDValue();
14393}
14394
14395
Evan Cheng0b0cd912009-03-28 05:57:29 +000014396/// PerformMulCombine - Optimize a single multiply with constant into two
14397/// in order to implement it with two cheaper instructions, e.g.
14398/// LEA + SHL, LEA + LEA.
14399static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14400 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014401 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14402 return SDValue();
14403
Owen Andersone50ed302009-08-10 22:56:29 +000014404 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014405 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014406 return SDValue();
14407
14408 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14409 if (!C)
14410 return SDValue();
14411 uint64_t MulAmt = C->getZExtValue();
14412 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14413 return SDValue();
14414
14415 uint64_t MulAmt1 = 0;
14416 uint64_t MulAmt2 = 0;
14417 if ((MulAmt % 9) == 0) {
14418 MulAmt1 = 9;
14419 MulAmt2 = MulAmt / 9;
14420 } else if ((MulAmt % 5) == 0) {
14421 MulAmt1 = 5;
14422 MulAmt2 = MulAmt / 5;
14423 } else if ((MulAmt % 3) == 0) {
14424 MulAmt1 = 3;
14425 MulAmt2 = MulAmt / 3;
14426 }
14427 if (MulAmt2 &&
14428 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14429 DebugLoc DL = N->getDebugLoc();
14430
14431 if (isPowerOf2_64(MulAmt2) &&
14432 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14433 // If second multiplifer is pow2, issue it first. We want the multiply by
14434 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14435 // is an add.
14436 std::swap(MulAmt1, MulAmt2);
14437
14438 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014439 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014440 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014441 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014442 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014443 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014444 DAG.getConstant(MulAmt1, VT));
14445
Eric Christopherfd179292009-08-27 18:07:15 +000014446 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014447 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014448 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014449 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014450 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014451 DAG.getConstant(MulAmt2, VT));
14452
14453 // Do not add new nodes to DAG combiner worklist.
14454 DCI.CombineTo(N, NewMul, false);
14455 }
14456 return SDValue();
14457}
14458
Evan Chengad9c0a32009-12-15 00:53:42 +000014459static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14460 SDValue N0 = N->getOperand(0);
14461 SDValue N1 = N->getOperand(1);
14462 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14463 EVT VT = N0.getValueType();
14464
14465 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14466 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014467 if (VT.isInteger() && !VT.isVector() &&
14468 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014469 N0.getOperand(1).getOpcode() == ISD::Constant) {
14470 SDValue N00 = N0.getOperand(0);
14471 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14472 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14473 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14474 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14475 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14476 APInt ShAmt = N1C->getAPIntValue();
14477 Mask = Mask.shl(ShAmt);
14478 if (Mask != 0)
14479 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14480 N00, DAG.getConstant(Mask, VT));
14481 }
14482 }
14483
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014484
14485 // Hardware support for vector shifts is sparse which makes us scalarize the
14486 // vector operations in many cases. Also, on sandybridge ADD is faster than
14487 // shl.
14488 // (shl V, 1) -> add V,V
14489 if (isSplatVector(N1.getNode())) {
14490 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14491 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14492 // We shift all of the values by one. In many cases we do not have
14493 // hardware support for this operation. This is better expressed as an ADD
14494 // of two values.
14495 if (N1C && (1 == N1C->getZExtValue())) {
14496 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14497 }
14498 }
14499
Evan Chengad9c0a32009-12-15 00:53:42 +000014500 return SDValue();
14501}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014502
Nate Begeman740ab032009-01-26 00:52:55 +000014503/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14504/// when possible.
14505static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014506 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014507 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014508 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014509 if (N->getOpcode() == ISD::SHL) {
14510 SDValue V = PerformSHLCombine(N, DAG);
14511 if (V.getNode()) return V;
14512 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014513
Nate Begeman740ab032009-01-26 00:52:55 +000014514 // On X86 with SSE2 support, we can transform this to a vector shift if
14515 // all elements are shifted by the same amount. We can't do this in legalize
14516 // because the a constant vector is typically transformed to a constant pool
14517 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014518 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014519 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014520
Craig Topper7be5dfd2011-11-12 09:58:49 +000014521 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14522 (!Subtarget->hasAVX2() ||
14523 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014524 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014525
Mon P Wang3becd092009-01-28 08:12:05 +000014526 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014527 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014528 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014529 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014530 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14531 unsigned NumElts = VT.getVectorNumElements();
14532 unsigned i = 0;
14533 for (; i != NumElts; ++i) {
14534 SDValue Arg = ShAmtOp.getOperand(i);
14535 if (Arg.getOpcode() == ISD::UNDEF) continue;
14536 BaseShAmt = Arg;
14537 break;
14538 }
Craig Topper37c26772012-01-17 04:44:50 +000014539 // Handle the case where the build_vector is all undef
14540 // FIXME: Should DAG allow this?
14541 if (i == NumElts)
14542 return SDValue();
14543
Mon P Wang3becd092009-01-28 08:12:05 +000014544 for (; i != NumElts; ++i) {
14545 SDValue Arg = ShAmtOp.getOperand(i);
14546 if (Arg.getOpcode() == ISD::UNDEF) continue;
14547 if (Arg != BaseShAmt) {
14548 return SDValue();
14549 }
14550 }
14551 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014552 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014553 SDValue InVec = ShAmtOp.getOperand(0);
14554 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14555 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14556 unsigned i = 0;
14557 for (; i != NumElts; ++i) {
14558 SDValue Arg = InVec.getOperand(i);
14559 if (Arg.getOpcode() == ISD::UNDEF) continue;
14560 BaseShAmt = Arg;
14561 break;
14562 }
14563 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14564 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014565 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014566 if (C->getZExtValue() == SplatIdx)
14567 BaseShAmt = InVec.getOperand(1);
14568 }
14569 }
Mon P Wang845b1892012-02-01 22:15:20 +000014570 if (BaseShAmt.getNode() == 0) {
14571 // Don't create instructions with illegal types after legalize
14572 // types has run.
14573 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14574 !DCI.isBeforeLegalize())
14575 return SDValue();
14576
Mon P Wangefa42202009-09-03 19:56:25 +000014577 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14578 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014579 }
Mon P Wang3becd092009-01-28 08:12:05 +000014580 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014581 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014582
Mon P Wangefa42202009-09-03 19:56:25 +000014583 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014584 if (EltVT.bitsGT(MVT::i32))
14585 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14586 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014587 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014588
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014589 // The shift amount is identical so we can do a vector shift.
14590 SDValue ValOp = N->getOperand(0);
14591 switch (N->getOpcode()) {
14592 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014593 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014594 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014595 switch (VT.getSimpleVT().SimpleTy) {
14596 default: return SDValue();
14597 case MVT::v2i64:
14598 case MVT::v4i32:
14599 case MVT::v8i16:
14600 case MVT::v4i64:
14601 case MVT::v8i32:
14602 case MVT::v16i16:
14603 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14604 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014605 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014606 switch (VT.getSimpleVT().SimpleTy) {
14607 default: return SDValue();
14608 case MVT::v4i32:
14609 case MVT::v8i16:
14610 case MVT::v8i32:
14611 case MVT::v16i16:
14612 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14613 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014614 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014615 switch (VT.getSimpleVT().SimpleTy) {
14616 default: return SDValue();
14617 case MVT::v2i64:
14618 case MVT::v4i32:
14619 case MVT::v8i16:
14620 case MVT::v4i64:
14621 case MVT::v8i32:
14622 case MVT::v16i16:
14623 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14624 }
Nate Begeman740ab032009-01-26 00:52:55 +000014625 }
Nate Begeman740ab032009-01-26 00:52:55 +000014626}
14627
Nate Begemanb65c1752010-12-17 22:55:37 +000014628
Stuart Hastings865f0932011-06-03 23:53:54 +000014629// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14630// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14631// and friends. Likewise for OR -> CMPNEQSS.
14632static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14633 TargetLowering::DAGCombinerInfo &DCI,
14634 const X86Subtarget *Subtarget) {
14635 unsigned opcode;
14636
14637 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14638 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014639 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014640 SDValue N0 = N->getOperand(0);
14641 SDValue N1 = N->getOperand(1);
14642 SDValue CMP0 = N0->getOperand(1);
14643 SDValue CMP1 = N1->getOperand(1);
14644 DebugLoc DL = N->getDebugLoc();
14645
14646 // The SETCCs should both refer to the same CMP.
14647 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14648 return SDValue();
14649
14650 SDValue CMP00 = CMP0->getOperand(0);
14651 SDValue CMP01 = CMP0->getOperand(1);
14652 EVT VT = CMP00.getValueType();
14653
14654 if (VT == MVT::f32 || VT == MVT::f64) {
14655 bool ExpectingFlags = false;
14656 // Check for any users that want flags:
14657 for (SDNode::use_iterator UI = N->use_begin(),
14658 UE = N->use_end();
14659 !ExpectingFlags && UI != UE; ++UI)
14660 switch (UI->getOpcode()) {
14661 default:
14662 case ISD::BR_CC:
14663 case ISD::BRCOND:
14664 case ISD::SELECT:
14665 ExpectingFlags = true;
14666 break;
14667 case ISD::CopyToReg:
14668 case ISD::SIGN_EXTEND:
14669 case ISD::ZERO_EXTEND:
14670 case ISD::ANY_EXTEND:
14671 break;
14672 }
14673
14674 if (!ExpectingFlags) {
14675 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14676 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14677
14678 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14679 X86::CondCode tmp = cc0;
14680 cc0 = cc1;
14681 cc1 = tmp;
14682 }
14683
14684 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14685 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14686 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14687 X86ISD::NodeType NTOperator = is64BitFP ?
14688 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14689 // FIXME: need symbolic constants for these magic numbers.
14690 // See X86ATTInstPrinter.cpp:printSSECC().
14691 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14692 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14693 DAG.getConstant(x86cc, MVT::i8));
14694 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14695 OnesOrZeroesF);
14696 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14697 DAG.getConstant(1, MVT::i32));
14698 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14699 return OneBitOfTruth;
14700 }
14701 }
14702 }
14703 }
14704 return SDValue();
14705}
14706
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014707/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14708/// so it can be folded inside ANDNP.
14709static bool CanFoldXORWithAllOnes(const SDNode *N) {
14710 EVT VT = N->getValueType(0);
14711
14712 // Match direct AllOnes for 128 and 256-bit vectors
14713 if (ISD::isBuildVectorAllOnes(N))
14714 return true;
14715
14716 // Look through a bit convert.
14717 if (N->getOpcode() == ISD::BITCAST)
14718 N = N->getOperand(0).getNode();
14719
14720 // Sometimes the operand may come from a insert_subvector building a 256-bit
14721 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014722 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014723 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14724 SDValue V1 = N->getOperand(0);
14725 SDValue V2 = N->getOperand(1);
14726
14727 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14728 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14729 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14730 ISD::isBuildVectorAllOnes(V2.getNode()))
14731 return true;
14732 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014733
14734 return false;
14735}
14736
Nate Begemanb65c1752010-12-17 22:55:37 +000014737static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14738 TargetLowering::DAGCombinerInfo &DCI,
14739 const X86Subtarget *Subtarget) {
14740 if (DCI.isBeforeLegalizeOps())
14741 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014742
Stuart Hastings865f0932011-06-03 23:53:54 +000014743 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14744 if (R.getNode())
14745 return R;
14746
Craig Topper54a11172011-10-14 07:06:56 +000014747 EVT VT = N->getValueType(0);
14748
Craig Topperb4c94572011-10-21 06:55:01 +000014749 // Create ANDN, BLSI, and BLSR instructions
14750 // BLSI is X & (-X)
14751 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014752 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14753 SDValue N0 = N->getOperand(0);
14754 SDValue N1 = N->getOperand(1);
14755 DebugLoc DL = N->getDebugLoc();
14756
14757 // Check LHS for not
14758 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14759 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14760 // Check RHS for not
14761 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14762 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14763
Craig Topperb4c94572011-10-21 06:55:01 +000014764 // Check LHS for neg
14765 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14766 isZero(N0.getOperand(0)))
14767 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14768
14769 // Check RHS for neg
14770 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14771 isZero(N1.getOperand(0)))
14772 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14773
14774 // Check LHS for X-1
14775 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14776 isAllOnes(N0.getOperand(1)))
14777 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14778
14779 // Check RHS for X-1
14780 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14781 isAllOnes(N1.getOperand(1)))
14782 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14783
Craig Topper54a11172011-10-14 07:06:56 +000014784 return SDValue();
14785 }
14786
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014787 // Want to form ANDNP nodes:
14788 // 1) In the hopes of then easily combining them with OR and AND nodes
14789 // to form PBLEND/PSIGN.
14790 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014791 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014792 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014793
Nate Begemanb65c1752010-12-17 22:55:37 +000014794 SDValue N0 = N->getOperand(0);
14795 SDValue N1 = N->getOperand(1);
14796 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014797
Nate Begemanb65c1752010-12-17 22:55:37 +000014798 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014799 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014800 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14801 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014802 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014803
14804 // Check RHS for vnot
14805 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014806 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14807 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014808 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014809
Nate Begemanb65c1752010-12-17 22:55:37 +000014810 return SDValue();
14811}
14812
Evan Cheng760d1942010-01-04 21:22:48 +000014813static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014814 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014815 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014816 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014817 return SDValue();
14818
Stuart Hastings865f0932011-06-03 23:53:54 +000014819 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14820 if (R.getNode())
14821 return R;
14822
Evan Cheng760d1942010-01-04 21:22:48 +000014823 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014824
Evan Cheng760d1942010-01-04 21:22:48 +000014825 SDValue N0 = N->getOperand(0);
14826 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014827
Nate Begemanb65c1752010-12-17 22:55:37 +000014828 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014829 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014830 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014831 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14832 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014833
Craig Topper1666cb62011-11-19 07:07:26 +000014834 // Canonicalize pandn to RHS
14835 if (N0.getOpcode() == X86ISD::ANDNP)
14836 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014837 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014838 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14839 SDValue Mask = N1.getOperand(0);
14840 SDValue X = N1.getOperand(1);
14841 SDValue Y;
14842 if (N0.getOperand(0) == Mask)
14843 Y = N0.getOperand(1);
14844 if (N0.getOperand(1) == Mask)
14845 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014846
Craig Topper1666cb62011-11-19 07:07:26 +000014847 // Check to see if the mask appeared in both the AND and ANDNP and
14848 if (!Y.getNode())
14849 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014850
Craig Topper1666cb62011-11-19 07:07:26 +000014851 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014852 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014853 if (Mask.getOpcode() == ISD::BITCAST)
14854 Mask = Mask.getOperand(0);
14855 if (X.getOpcode() == ISD::BITCAST)
14856 X = X.getOperand(0);
14857 if (Y.getOpcode() == ISD::BITCAST)
14858 Y = Y.getOperand(0);
14859
Craig Topper1666cb62011-11-19 07:07:26 +000014860 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014861
Craig Toppered2e13d2012-01-22 19:15:14 +000014862 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014863 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14864 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014865 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014866 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014867
14868 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014869 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014870 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14871 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14872 if ((SraAmt + 1) != EltBits)
14873 return SDValue();
14874
14875 DebugLoc DL = N->getDebugLoc();
14876
14877 // Now we know we at least have a plendvb with the mask val. See if
14878 // we can form a psignb/w/d.
14879 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000014880 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14881 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000014882 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
14883 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
14884 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014885 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000014886 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000014887 }
14888 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000014889 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000014890 return SDValue();
14891
14892 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14893
14894 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14895 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14896 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000014897 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000014898 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014899 }
14900 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014901
Craig Topper1666cb62011-11-19 07:07:26 +000014902 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14903 return SDValue();
14904
Nate Begemanb65c1752010-12-17 22:55:37 +000014905 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014906 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14907 std::swap(N0, N1);
14908 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14909 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014910 if (!N0.hasOneUse() || !N1.hasOneUse())
14911 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014912
14913 SDValue ShAmt0 = N0.getOperand(1);
14914 if (ShAmt0.getValueType() != MVT::i8)
14915 return SDValue();
14916 SDValue ShAmt1 = N1.getOperand(1);
14917 if (ShAmt1.getValueType() != MVT::i8)
14918 return SDValue();
14919 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14920 ShAmt0 = ShAmt0.getOperand(0);
14921 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14922 ShAmt1 = ShAmt1.getOperand(0);
14923
14924 DebugLoc DL = N->getDebugLoc();
14925 unsigned Opc = X86ISD::SHLD;
14926 SDValue Op0 = N0.getOperand(0);
14927 SDValue Op1 = N1.getOperand(0);
14928 if (ShAmt0.getOpcode() == ISD::SUB) {
14929 Opc = X86ISD::SHRD;
14930 std::swap(Op0, Op1);
14931 std::swap(ShAmt0, ShAmt1);
14932 }
14933
Evan Cheng8b1190a2010-04-28 01:18:01 +000014934 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014935 if (ShAmt1.getOpcode() == ISD::SUB) {
14936 SDValue Sum = ShAmt1.getOperand(0);
14937 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014938 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14939 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14940 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14941 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014942 return DAG.getNode(Opc, DL, VT,
14943 Op0, Op1,
14944 DAG.getNode(ISD::TRUNCATE, DL,
14945 MVT::i8, ShAmt0));
14946 }
14947 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14948 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14949 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014950 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014951 return DAG.getNode(Opc, DL, VT,
14952 N0.getOperand(0), N1.getOperand(0),
14953 DAG.getNode(ISD::TRUNCATE, DL,
14954 MVT::i8, ShAmt0));
14955 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014956
Evan Cheng760d1942010-01-04 21:22:48 +000014957 return SDValue();
14958}
14959
Manman Ren92363622012-06-07 22:39:10 +000014960// Generate NEG and CMOV for integer abs.
14961static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
14962 EVT VT = N->getValueType(0);
14963
14964 // Since X86 does not have CMOV for 8-bit integer, we don't convert
14965 // 8-bit integer abs to NEG and CMOV.
14966 if (VT.isInteger() && VT.getSizeInBits() == 8)
14967 return SDValue();
14968
14969 SDValue N0 = N->getOperand(0);
14970 SDValue N1 = N->getOperand(1);
14971 DebugLoc DL = N->getDebugLoc();
14972
14973 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
14974 // and change it to SUB and CMOV.
14975 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
14976 N0.getOpcode() == ISD::ADD &&
14977 N0.getOperand(1) == N1 &&
14978 N1.getOpcode() == ISD::SRA &&
14979 N1.getOperand(0) == N0.getOperand(0))
14980 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
14981 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
14982 // Generate SUB & CMOV.
14983 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
14984 DAG.getConstant(0, VT), N0.getOperand(0));
14985
14986 SDValue Ops[] = { N0.getOperand(0), Neg,
14987 DAG.getConstant(X86::COND_GE, MVT::i8),
14988 SDValue(Neg.getNode(), 1) };
14989 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
14990 Ops, array_lengthof(Ops));
14991 }
14992 return SDValue();
14993}
14994
Craig Topper3738ccd2011-12-27 06:27:23 +000014995// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000014996static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14997 TargetLowering::DAGCombinerInfo &DCI,
14998 const X86Subtarget *Subtarget) {
14999 if (DCI.isBeforeLegalizeOps())
15000 return SDValue();
15001
Manman Ren45d53b82012-06-08 18:58:26 +000015002 if (Subtarget->hasCMov()) {
15003 SDValue RV = performIntegerAbsCombine(N, DAG);
15004 if (RV.getNode())
15005 return RV;
15006 }
Manman Ren92363622012-06-07 22:39:10 +000015007
15008 // Try forming BMI if it is available.
15009 if (!Subtarget->hasBMI())
15010 return SDValue();
15011
Craig Topperb4c94572011-10-21 06:55:01 +000015012 EVT VT = N->getValueType(0);
15013
15014 if (VT != MVT::i32 && VT != MVT::i64)
15015 return SDValue();
15016
Craig Topper3738ccd2011-12-27 06:27:23 +000015017 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15018
Craig Topperb4c94572011-10-21 06:55:01 +000015019 // Create BLSMSK instructions by finding X ^ (X-1)
15020 SDValue N0 = N->getOperand(0);
15021 SDValue N1 = N->getOperand(1);
15022 DebugLoc DL = N->getDebugLoc();
15023
15024 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15025 isAllOnes(N0.getOperand(1)))
15026 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15027
15028 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15029 isAllOnes(N1.getOperand(1)))
15030 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15031
15032 return SDValue();
15033}
15034
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015035/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15036static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015037 TargetLowering::DAGCombinerInfo &DCI,
15038 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015039 LoadSDNode *Ld = cast<LoadSDNode>(N);
15040 EVT RegVT = Ld->getValueType(0);
15041 EVT MemVT = Ld->getMemoryVT();
15042 DebugLoc dl = Ld->getDebugLoc();
15043 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15044
15045 ISD::LoadExtType Ext = Ld->getExtensionType();
15046
Nadav Rotemca6f2962011-09-18 19:00:23 +000015047 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015048 // shuffle. We need SSE4 for the shuffles.
15049 // TODO: It is possible to support ZExt by zeroing the undef values
15050 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015051 if (RegVT.isVector() && RegVT.isInteger() &&
15052 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015053 assert(MemVT != RegVT && "Cannot extend to the same type");
15054 assert(MemVT.isVector() && "Must load a vector from memory");
15055
15056 unsigned NumElems = RegVT.getVectorNumElements();
15057 unsigned RegSz = RegVT.getSizeInBits();
15058 unsigned MemSz = MemVT.getSizeInBits();
15059 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015060
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015061 // All sizes must be a power of two.
15062 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15063 return SDValue();
15064
15065 // Attempt to load the original value using scalar loads.
15066 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015067 MVT SclrLoadTy = MVT::i8;
15068 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15069 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15070 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015071 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015072 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015073 }
15074 }
15075
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015076 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15077 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15078 (64 <= MemSz))
15079 SclrLoadTy = MVT::f64;
15080
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015081 // Calculate the number of scalar loads that we need to perform
15082 // in order to load our vector from memory.
15083 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015084
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015085 // Represent our vector as a sequence of elements which are the
15086 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015087 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15088 RegSz/SclrLoadTy.getSizeInBits());
15089
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015090 // Represent the data using the same element type that is stored in
15091 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015092 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15093 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015094
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015095 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15096 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015097
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015098 // We can't shuffle using an illegal type.
15099 if (!TLI.isTypeLegal(WideVecVT))
15100 return SDValue();
15101
15102 SmallVector<SDValue, 8> Chains;
15103 SDValue Ptr = Ld->getBasePtr();
15104 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15105 TLI.getPointerTy());
15106 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15107
15108 for (unsigned i = 0; i < NumLoads; ++i) {
15109 // Perform a single load.
15110 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15111 Ptr, Ld->getPointerInfo(),
15112 Ld->isVolatile(), Ld->isNonTemporal(),
15113 Ld->isInvariant(), Ld->getAlignment());
15114 Chains.push_back(ScalarLoad.getValue(1));
15115 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15116 // another round of DAGCombining.
15117 if (i == 0)
15118 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15119 else
15120 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15121 ScalarLoad, DAG.getIntPtrConstant(i));
15122
15123 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15124 }
15125
15126 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15127 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015128
15129 // Bitcast the loaded value to a vector of the original element type, in
15130 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015131 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015132 unsigned SizeRatio = RegSz/MemSz;
15133
15134 // Redistribute the loaded elements into the different locations.
15135 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015136 for (unsigned i = 0; i != NumElems; ++i)
15137 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015138
15139 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015140 DAG.getUNDEF(WideVecVT),
15141 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015142
15143 // Bitcast to the requested type.
15144 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15145 // Replace the original load with the new sequence
15146 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015147 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015148 }
15149
15150 return SDValue();
15151}
15152
Chris Lattner149a4e52008-02-22 02:09:43 +000015153/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015154static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015155 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015156 StoreSDNode *St = cast<StoreSDNode>(N);
15157 EVT VT = St->getValue().getValueType();
15158 EVT StVT = St->getMemoryVT();
15159 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015160 SDValue StoredVal = St->getOperand(1);
15161 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15162
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015163 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015164 // On Sandy Bridge, 256-bit memory operations are executed by two
15165 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15166 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015167 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015168 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15169 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015170 SDValue Value0 = StoredVal.getOperand(0);
15171 SDValue Value1 = StoredVal.getOperand(1);
15172
15173 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15174 SDValue Ptr0 = St->getBasePtr();
15175 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15176
15177 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15178 St->getPointerInfo(), St->isVolatile(),
15179 St->isNonTemporal(), St->getAlignment());
15180 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15181 St->getPointerInfo(), St->isVolatile(),
15182 St->isNonTemporal(), St->getAlignment());
15183 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15184 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015185
15186 // Optimize trunc store (of multiple scalars) to shuffle and store.
15187 // First, pack all of the elements in one place. Next, store to memory
15188 // in fewer chunks.
15189 if (St->isTruncatingStore() && VT.isVector()) {
15190 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15191 unsigned NumElems = VT.getVectorNumElements();
15192 assert(StVT != VT && "Cannot truncate to the same type");
15193 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15194 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15195
15196 // From, To sizes and ElemCount must be pow of two
15197 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015198 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015199 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015200 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015201
Nadav Rotem614061b2011-08-10 19:30:14 +000015202 unsigned SizeRatio = FromSz / ToSz;
15203
15204 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15205
15206 // Create a type on which we perform the shuffle
15207 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15208 StVT.getScalarType(), NumElems*SizeRatio);
15209
15210 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15211
15212 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15213 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015214 for (unsigned i = 0; i != NumElems; ++i)
15215 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015216
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015217 // Can't shuffle using an illegal type.
15218 if (!TLI.isTypeLegal(WideVecVT))
15219 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015220
15221 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015222 DAG.getUNDEF(WideVecVT),
15223 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015224 // At this point all of the data is stored at the bottom of the
15225 // register. We now need to save it to mem.
15226
15227 // Find the largest store unit
15228 MVT StoreType = MVT::i8;
15229 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15230 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15231 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015232 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015233 StoreType = Tp;
15234 }
15235
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015236 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15237 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15238 (64 <= NumElems * ToSz))
15239 StoreType = MVT::f64;
15240
Nadav Rotem614061b2011-08-10 19:30:14 +000015241 // Bitcast the original vector into a vector of store-size units
15242 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015243 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015244 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15245 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15246 SmallVector<SDValue, 8> Chains;
15247 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15248 TLI.getPointerTy());
15249 SDValue Ptr = St->getBasePtr();
15250
15251 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015252 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015253 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15254 StoreType, ShuffWide,
15255 DAG.getIntPtrConstant(i));
15256 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15257 St->getPointerInfo(), St->isVolatile(),
15258 St->isNonTemporal(), St->getAlignment());
15259 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15260 Chains.push_back(Ch);
15261 }
15262
15263 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15264 Chains.size());
15265 }
15266
15267
Chris Lattner149a4e52008-02-22 02:09:43 +000015268 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15269 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015270 // A preferable solution to the general problem is to figure out the right
15271 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015272
15273 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015274 if (VT.getSizeInBits() != 64)
15275 return SDValue();
15276
Devang Patel578efa92009-06-05 21:57:13 +000015277 const Function *F = DAG.getMachineFunction().getFunction();
15278 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015279 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015280 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015281 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015282 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015283 isa<LoadSDNode>(St->getValue()) &&
15284 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15285 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015286 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015287 LoadSDNode *Ld = 0;
15288 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015289 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015290 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015291 // Must be a store of a load. We currently handle two cases: the load
15292 // is a direct child, and it's under an intervening TokenFactor. It is
15293 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015294 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015295 Ld = cast<LoadSDNode>(St->getChain());
15296 else if (St->getValue().hasOneUse() &&
15297 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015298 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015299 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015300 TokenFactorIndex = i;
15301 Ld = cast<LoadSDNode>(St->getValue());
15302 } else
15303 Ops.push_back(ChainVal->getOperand(i));
15304 }
15305 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015306
Evan Cheng536e6672009-03-12 05:59:15 +000015307 if (!Ld || !ISD::isNormalLoad(Ld))
15308 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015309
Evan Cheng536e6672009-03-12 05:59:15 +000015310 // If this is not the MMX case, i.e. we are just turning i64 load/store
15311 // into f64 load/store, avoid the transformation if there are multiple
15312 // uses of the loaded value.
15313 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15314 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015315
Evan Cheng536e6672009-03-12 05:59:15 +000015316 DebugLoc LdDL = Ld->getDebugLoc();
15317 DebugLoc StDL = N->getDebugLoc();
15318 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15319 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15320 // pair instead.
15321 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015322 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015323 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15324 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015325 Ld->isNonTemporal(), Ld->isInvariant(),
15326 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015327 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015328 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015329 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015330 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015331 Ops.size());
15332 }
Evan Cheng536e6672009-03-12 05:59:15 +000015333 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015334 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015335 St->isVolatile(), St->isNonTemporal(),
15336 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015337 }
Evan Cheng536e6672009-03-12 05:59:15 +000015338
15339 // Otherwise, lower to two pairs of 32-bit loads / stores.
15340 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015341 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15342 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015343
Owen Anderson825b72b2009-08-11 20:47:22 +000015344 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015345 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015346 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015347 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015348 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015349 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015350 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015351 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015352 MinAlign(Ld->getAlignment(), 4));
15353
15354 SDValue NewChain = LoLd.getValue(1);
15355 if (TokenFactorIndex != -1) {
15356 Ops.push_back(LoLd);
15357 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015358 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015359 Ops.size());
15360 }
15361
15362 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015363 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15364 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015365
15366 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015367 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015368 St->isVolatile(), St->isNonTemporal(),
15369 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015370 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015371 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015372 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015373 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015374 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015375 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015376 }
Dan Gohman475871a2008-07-27 21:46:04 +000015377 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015378}
15379
Duncan Sands17470be2011-09-22 20:15:48 +000015380/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15381/// and return the operands for the horizontal operation in LHS and RHS. A
15382/// horizontal operation performs the binary operation on successive elements
15383/// of its first operand, then on successive elements of its second operand,
15384/// returning the resulting values in a vector. For example, if
15385/// A = < float a0, float a1, float a2, float a3 >
15386/// and
15387/// B = < float b0, float b1, float b2, float b3 >
15388/// then the result of doing a horizontal operation on A and B is
15389/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15390/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15391/// A horizontal-op B, for some already available A and B, and if so then LHS is
15392/// set to A, RHS to B, and the routine returns 'true'.
15393/// Note that the binary operation should have the property that if one of the
15394/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015395static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015396 // Look for the following pattern: if
15397 // A = < float a0, float a1, float a2, float a3 >
15398 // B = < float b0, float b1, float b2, float b3 >
15399 // and
15400 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15401 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15402 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15403 // which is A horizontal-op B.
15404
15405 // At least one of the operands should be a vector shuffle.
15406 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15407 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15408 return false;
15409
15410 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015411
15412 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15413 "Unsupported vector type for horizontal add/sub");
15414
15415 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15416 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015417 unsigned NumElts = VT.getVectorNumElements();
15418 unsigned NumLanes = VT.getSizeInBits()/128;
15419 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015420 assert((NumLaneElts % 2 == 0) &&
15421 "Vector type should have an even number of elements in each lane");
15422 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015423
15424 // View LHS in the form
15425 // LHS = VECTOR_SHUFFLE A, B, LMask
15426 // If LHS is not a shuffle then pretend it is the shuffle
15427 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15428 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15429 // type VT.
15430 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015431 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015432 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15433 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15434 A = LHS.getOperand(0);
15435 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15436 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015437 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15438 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015439 } else {
15440 if (LHS.getOpcode() != ISD::UNDEF)
15441 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015442 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015443 LMask[i] = i;
15444 }
15445
15446 // Likewise, view RHS in the form
15447 // RHS = VECTOR_SHUFFLE C, D, RMask
15448 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015449 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015450 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15451 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15452 C = RHS.getOperand(0);
15453 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15454 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015455 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15456 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015457 } else {
15458 if (RHS.getOpcode() != ISD::UNDEF)
15459 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015460 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015461 RMask[i] = i;
15462 }
15463
15464 // Check that the shuffles are both shuffling the same vectors.
15465 if (!(A == C && B == D) && !(A == D && B == C))
15466 return false;
15467
15468 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15469 if (!A.getNode() && !B.getNode())
15470 return false;
15471
15472 // If A and B occur in reverse order in RHS, then "swap" them (which means
15473 // rewriting the mask).
15474 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015475 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015476
15477 // At this point LHS and RHS are equivalent to
15478 // LHS = VECTOR_SHUFFLE A, B, LMask
15479 // RHS = VECTOR_SHUFFLE A, B, RMask
15480 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015481 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015482 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015483
Craig Topperf8363302011-12-02 08:18:41 +000015484 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015485 if (LIdx < 0 || RIdx < 0 ||
15486 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15487 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015488 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015489
Craig Topperf8363302011-12-02 08:18:41 +000015490 // Check that successive elements are being operated on. If not, this is
15491 // not a horizontal operation.
15492 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15493 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015494 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015495 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015496 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015497 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015498 }
15499
15500 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15501 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15502 return true;
15503}
15504
15505/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15506static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15507 const X86Subtarget *Subtarget) {
15508 EVT VT = N->getValueType(0);
15509 SDValue LHS = N->getOperand(0);
15510 SDValue RHS = N->getOperand(1);
15511
15512 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015513 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015514 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015515 isHorizontalBinOp(LHS, RHS, true))
15516 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15517 return SDValue();
15518}
15519
15520/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15521static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15522 const X86Subtarget *Subtarget) {
15523 EVT VT = N->getValueType(0);
15524 SDValue LHS = N->getOperand(0);
15525 SDValue RHS = N->getOperand(1);
15526
15527 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015528 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015529 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015530 isHorizontalBinOp(LHS, RHS, false))
15531 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15532 return SDValue();
15533}
15534
Chris Lattner6cf73262008-01-25 06:14:17 +000015535/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15536/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015537static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015538 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15539 // F[X]OR(0.0, x) -> x
15540 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015541 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15542 if (C->getValueAPF().isPosZero())
15543 return N->getOperand(1);
15544 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15545 if (C->getValueAPF().isPosZero())
15546 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015547 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015548}
15549
Nadav Rotemd60cb112012-08-19 13:06:16 +000015550/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15551/// X86ISD::FMAX nodes.
15552static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15553 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15554
15555 // Only perform optimizations if UnsafeMath is used.
15556 if (!DAG.getTarget().Options.UnsafeFPMath)
15557 return SDValue();
15558
15559 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015560 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015561 unsigned NewOp = 0;
15562 switch (N->getOpcode()) {
15563 default: llvm_unreachable("unknown opcode");
15564 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15565 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15566 }
15567
15568 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
15569 N->getOperand(0), N->getOperand(1));
15570}
15571
15572
Chris Lattneraf723b92008-01-25 05:46:26 +000015573/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015574static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015575 // FAND(0.0, x) -> 0.0
15576 // FAND(x, 0.0) -> 0.0
15577 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15578 if (C->getValueAPF().isPosZero())
15579 return N->getOperand(0);
15580 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15581 if (C->getValueAPF().isPosZero())
15582 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015583 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015584}
15585
Dan Gohmane5af2d32009-01-29 01:59:02 +000015586static SDValue PerformBTCombine(SDNode *N,
15587 SelectionDAG &DAG,
15588 TargetLowering::DAGCombinerInfo &DCI) {
15589 // BT ignores high bits in the bit index operand.
15590 SDValue Op1 = N->getOperand(1);
15591 if (Op1.hasOneUse()) {
15592 unsigned BitWidth = Op1.getValueSizeInBits();
15593 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15594 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015595 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15596 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015597 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015598 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15599 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15600 DCI.CommitTargetLoweringOpt(TLO);
15601 }
15602 return SDValue();
15603}
Chris Lattner83e6c992006-10-04 06:57:07 +000015604
Eli Friedman7a5e5552009-06-07 06:52:44 +000015605static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15606 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015607 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015608 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015609 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015610 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015611 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015612 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015613 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015614 }
15615 return SDValue();
15616}
15617
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015618static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15619 TargetLowering::DAGCombinerInfo &DCI,
15620 const X86Subtarget *Subtarget) {
15621 if (!DCI.isBeforeLegalizeOps())
15622 return SDValue();
15623
Craig Topper3ef43cf2012-04-24 06:36:35 +000015624 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015625 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015626
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015627 EVT VT = N->getValueType(0);
15628 SDValue Op = N->getOperand(0);
15629 EVT OpVT = Op.getValueType();
15630 DebugLoc dl = N->getDebugLoc();
15631
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015632 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15633 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015634
Craig Topper3ef43cf2012-04-24 06:36:35 +000015635 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015636 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015637
15638 // Optimize vectors in AVX mode
15639 // Sign extend v8i16 to v8i32 and
15640 // v4i32 to v4i64
15641 //
15642 // Divide input vector into two parts
15643 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15644 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15645 // concat the vectors to original VT
15646
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015647 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015648 SDValue Undef = DAG.getUNDEF(OpVT);
15649
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015650 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015651 for (unsigned i = 0; i != NumElems/2; ++i)
15652 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015653
Craig Toppercacafd42012-08-14 08:18:43 +000015654 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015655
15656 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015657 for (unsigned i = 0; i != NumElems/2; ++i)
15658 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015659
Craig Toppercacafd42012-08-14 08:18:43 +000015660 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015661
Craig Topper3ef43cf2012-04-24 06:36:35 +000015662 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015663 VT.getVectorNumElements()/2);
15664
Craig Topper3ef43cf2012-04-24 06:36:35 +000015665 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015666 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15667
15668 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15669 }
15670 return SDValue();
15671}
15672
Michael Liaof6c24ee2012-08-10 14:39:24 +000015673static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015674 const X86Subtarget* Subtarget) {
15675 DebugLoc dl = N->getDebugLoc();
15676 EVT VT = N->getValueType(0);
15677
Craig Topperb1bdd7d2012-08-30 06:56:15 +000015678 // Let legalize expand this if it isn't a legal type yet.
15679 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
15680 return SDValue();
15681
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015682 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000015683 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
15684 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015685 return SDValue();
15686
15687 SDValue A = N->getOperand(0);
15688 SDValue B = N->getOperand(1);
15689 SDValue C = N->getOperand(2);
15690
15691 bool NegA = (A.getOpcode() == ISD::FNEG);
15692 bool NegB = (B.getOpcode() == ISD::FNEG);
15693 bool NegC = (C.getOpcode() == ISD::FNEG);
15694
Michael Liaof6c24ee2012-08-10 14:39:24 +000015695 // Negative multiplication when NegA xor NegB
15696 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015697 if (NegA)
15698 A = A.getOperand(0);
15699 if (NegB)
15700 B = B.getOperand(0);
15701 if (NegC)
15702 C = C.getOperand(0);
15703
15704 unsigned Opcode;
15705 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000015706 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015707 else
Craig Topperbf404372012-08-31 15:40:30 +000015708 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
15709
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015710 return DAG.getNode(Opcode, dl, VT, A, B, C);
15711}
15712
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015713static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015714 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015715 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015716 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15717 // (and (i32 x86isd::setcc_carry), 1)
15718 // This eliminates the zext. This transformation is necessary because
15719 // ISD::SETCC is always legalized to i8.
15720 DebugLoc dl = N->getDebugLoc();
15721 SDValue N0 = N->getOperand(0);
15722 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015723 EVT OpVT = N0.getValueType();
15724
Evan Cheng2e489c42009-12-16 00:53:11 +000015725 if (N0.getOpcode() == ISD::AND &&
15726 N0.hasOneUse() &&
15727 N0.getOperand(0).hasOneUse()) {
15728 SDValue N00 = N0.getOperand(0);
15729 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15730 return SDValue();
15731 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15732 if (!C || C->getZExtValue() != 1)
15733 return SDValue();
15734 return DAG.getNode(ISD::AND, dl, VT,
15735 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15736 N00.getOperand(0), N00.getOperand(1)),
15737 DAG.getConstant(1, VT));
15738 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015739
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015740 // Optimize vectors in AVX mode:
15741 //
15742 // v8i16 -> v8i32
15743 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15744 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15745 // Concat upper and lower parts.
15746 //
15747 // v4i32 -> v4i64
15748 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15749 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15750 // Concat upper and lower parts.
15751 //
Craig Topperc16f8512012-04-25 06:39:39 +000015752 if (!DCI.isBeforeLegalizeOps())
15753 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015754
Craig Topperc16f8512012-04-25 06:39:39 +000015755 if (!Subtarget->hasAVX())
15756 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015757
Craig Topperc16f8512012-04-25 06:39:39 +000015758 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15759 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015760
Craig Topperc16f8512012-04-25 06:39:39 +000015761 if (Subtarget->hasAVX2())
15762 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015763
Craig Topperc16f8512012-04-25 06:39:39 +000015764 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15765 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15766 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015767
Craig Topperc16f8512012-04-25 06:39:39 +000015768 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15769 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015770
Craig Topperc16f8512012-04-25 06:39:39 +000015771 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15772 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15773
15774 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015775 }
15776
Evan Cheng2e489c42009-12-16 00:53:11 +000015777 return SDValue();
15778}
15779
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015780// Optimize x == -y --> x+y == 0
15781// x != -y --> x+y != 0
15782static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15783 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15784 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015785 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015786
15787 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15788 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15789 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15790 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15791 LHS.getValueType(), RHS, LHS.getOperand(1));
15792 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15793 addV, DAG.getConstant(0, addV.getValueType()), CC);
15794 }
15795 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15796 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15797 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15798 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15799 RHS.getValueType(), LHS, RHS.getOperand(1));
15800 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15801 addV, DAG.getConstant(0, addV.getValueType()), CC);
15802 }
15803 return SDValue();
15804}
15805
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015806// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015807static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
15808 TargetLowering::DAGCombinerInfo &DCI,
15809 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015810 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015811 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15812 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015813
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015814 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15815 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15816 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015817 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015818 return DAG.getNode(ISD::AND, DL, MVT::i8,
15819 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015820 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015821 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015822
Michael Liao2a33cec2012-08-10 19:58:13 +000015823 SDValue Flags;
15824
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015825 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15826 if (Flags.getNode()) {
15827 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15828 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15829 }
15830
15831 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015832 if (Flags.getNode()) {
15833 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15834 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15835 }
15836
15837 return SDValue();
15838}
15839
15840// Optimize branch condition evaluation.
15841//
15842static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15843 TargetLowering::DAGCombinerInfo &DCI,
15844 const X86Subtarget *Subtarget) {
15845 DebugLoc DL = N->getDebugLoc();
15846 SDValue Chain = N->getOperand(0);
15847 SDValue Dest = N->getOperand(1);
15848 SDValue EFLAGS = N->getOperand(3);
15849 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15850
15851 SDValue Flags;
15852
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015853 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15854 if (Flags.getNode()) {
15855 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15856 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15857 Flags);
15858 }
15859
15860 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015861 if (Flags.getNode()) {
15862 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15863 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15864 Flags);
15865 }
15866
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015867 return SDValue();
15868}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015869
Craig Topper7fd5e162012-04-24 06:02:29 +000015870static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015871 SDValue Op0 = N->getOperand(0);
15872 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015873
15874 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015875 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015876 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015877 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015878 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15879 // Notice that we use SINT_TO_FP because we know that the high bits
15880 // are zero and SINT_TO_FP is better supported by the hardware.
15881 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15882 }
15883
15884 return SDValue();
15885}
15886
Benjamin Kramer1396c402011-06-18 11:09:41 +000015887static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15888 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015889 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015890 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015891
15892 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015893 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015894 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015895 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015896 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
15897 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15898 }
15899
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015900 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
15901 // a 32-bit target where SSE doesn't support i64->FP operations.
15902 if (Op0.getOpcode() == ISD::LOAD) {
15903 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
15904 EVT VT = Ld->getValueType(0);
15905 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
15906 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
15907 !XTLI->getSubtarget()->is64Bit() &&
15908 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000015909 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
15910 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015911 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
15912 return FILDChain;
15913 }
15914 }
15915 return SDValue();
15916}
15917
Craig Topper7fd5e162012-04-24 06:02:29 +000015918static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
15919 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015920
15921 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000015922 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
15923 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015924 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015925 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
15926 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
15927 }
15928
15929 return SDValue();
15930}
15931
Chris Lattner23a01992010-12-20 01:37:09 +000015932// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
15933static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
15934 X86TargetLowering::DAGCombinerInfo &DCI) {
15935 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
15936 // the result is either zero or one (depending on the input carry bit).
15937 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
15938 if (X86::isZeroNode(N->getOperand(0)) &&
15939 X86::isZeroNode(N->getOperand(1)) &&
15940 // We don't have a good way to replace an EFLAGS use, so only do this when
15941 // dead right now.
15942 SDValue(N, 1).use_empty()) {
15943 DebugLoc DL = N->getDebugLoc();
15944 EVT VT = N->getValueType(0);
15945 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
15946 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
15947 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
15948 DAG.getConstant(X86::COND_B,MVT::i8),
15949 N->getOperand(2)),
15950 DAG.getConstant(1, VT));
15951 return DCI.CombineTo(N, Res1, CarryOut);
15952 }
15953
15954 return SDValue();
15955}
15956
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015957// fold (add Y, (sete X, 0)) -> adc 0, Y
15958// (add Y, (setne X, 0)) -> sbb -1, Y
15959// (sub (sete X, 0), Y) -> sbb 0, Y
15960// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015961static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015962 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015963
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015964 // Look through ZExts.
15965 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
15966 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
15967 return SDValue();
15968
15969 SDValue SetCC = Ext.getOperand(0);
15970 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
15971 return SDValue();
15972
15973 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
15974 if (CC != X86::COND_E && CC != X86::COND_NE)
15975 return SDValue();
15976
15977 SDValue Cmp = SetCC.getOperand(1);
15978 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000015979 !X86::isZeroNode(Cmp.getOperand(1)) ||
15980 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015981 return SDValue();
15982
15983 SDValue CmpOp0 = Cmp.getOperand(0);
15984 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
15985 DAG.getConstant(1, CmpOp0.getValueType()));
15986
15987 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
15988 if (CC == X86::COND_NE)
15989 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
15990 DL, OtherVal.getValueType(), OtherVal,
15991 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
15992 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
15993 DL, OtherVal.getValueType(), OtherVal,
15994 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
15995}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015996
Craig Topper54f952a2011-11-19 09:02:40 +000015997/// PerformADDCombine - Do target-specific dag combines on integer adds.
15998static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
15999 const X86Subtarget *Subtarget) {
16000 EVT VT = N->getValueType(0);
16001 SDValue Op0 = N->getOperand(0);
16002 SDValue Op1 = N->getOperand(1);
16003
16004 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016005 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016006 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016007 isHorizontalBinOp(Op0, Op1, true))
16008 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16009
16010 return OptimizeConditionalInDecrement(N, DAG);
16011}
16012
16013static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16014 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016015 SDValue Op0 = N->getOperand(0);
16016 SDValue Op1 = N->getOperand(1);
16017
16018 // X86 can't encode an immediate LHS of a sub. See if we can push the
16019 // negation into a preceding instruction.
16020 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016021 // If the RHS of the sub is a XOR with one use and a constant, invert the
16022 // immediate. Then add one to the LHS of the sub so we can turn
16023 // X-Y -> X+~Y+1, saving one register.
16024 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16025 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016026 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016027 EVT VT = Op0.getValueType();
16028 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16029 Op1.getOperand(0),
16030 DAG.getConstant(~XorC, VT));
16031 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016032 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016033 }
16034 }
16035
Craig Topper54f952a2011-11-19 09:02:40 +000016036 // Try to synthesize horizontal adds from adds of shuffles.
16037 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016038 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016039 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16040 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016041 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16042
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016043 return OptimizeConditionalInDecrement(N, DAG);
16044}
16045
Dan Gohman475871a2008-07-27 21:46:04 +000016046SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016047 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016048 SelectionDAG &DAG = DCI.DAG;
16049 switch (N->getOpcode()) {
16050 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016051 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016052 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016053 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016054 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016055 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016056 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16057 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016058 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016059 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016060 case ISD::SHL:
16061 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016062 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016063 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016064 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016065 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016066 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016067 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016068 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016069 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000016070 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000016071 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16072 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016073 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016074 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016075 case X86ISD::FMIN:
16076 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016077 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016078 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016079 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016080 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016081 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016082 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000016083 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG, DCI);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016084 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016085 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016086 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016087 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016088 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016089 case X86ISD::UNPCKH:
16090 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016091 case X86ISD::MOVHLPS:
16092 case X86ISD::MOVLHPS:
16093 case X86ISD::PSHUFD:
16094 case X86ISD::PSHUFHW:
16095 case X86ISD::PSHUFLW:
16096 case X86ISD::MOVSS:
16097 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016098 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016099 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016100 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016101 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016102 }
16103
Dan Gohman475871a2008-07-27 21:46:04 +000016104 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016105}
16106
Evan Chenge5b51ac2010-04-17 06:13:15 +000016107/// isTypeDesirableForOp - Return true if the target has native support for
16108/// the specified value type and it is 'desirable' to use the type for the
16109/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16110/// instruction encodings are longer and some i16 instructions are slow.
16111bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16112 if (!isTypeLegal(VT))
16113 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016114 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016115 return true;
16116
16117 switch (Opc) {
16118 default:
16119 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016120 case ISD::LOAD:
16121 case ISD::SIGN_EXTEND:
16122 case ISD::ZERO_EXTEND:
16123 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016124 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016125 case ISD::SRL:
16126 case ISD::SUB:
16127 case ISD::ADD:
16128 case ISD::MUL:
16129 case ISD::AND:
16130 case ISD::OR:
16131 case ISD::XOR:
16132 return false;
16133 }
16134}
16135
16136/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016137/// beneficial for dag combiner to promote the specified node. If true, it
16138/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016139bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016140 EVT VT = Op.getValueType();
16141 if (VT != MVT::i16)
16142 return false;
16143
Evan Cheng4c26e932010-04-19 19:29:22 +000016144 bool Promote = false;
16145 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016146 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016147 default: break;
16148 case ISD::LOAD: {
16149 LoadSDNode *LD = cast<LoadSDNode>(Op);
16150 // If the non-extending load has a single use and it's not live out, then it
16151 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016152 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16153 Op.hasOneUse()*/) {
16154 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16155 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16156 // The only case where we'd want to promote LOAD (rather then it being
16157 // promoted as an operand is when it's only use is liveout.
16158 if (UI->getOpcode() != ISD::CopyToReg)
16159 return false;
16160 }
16161 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016162 Promote = true;
16163 break;
16164 }
16165 case ISD::SIGN_EXTEND:
16166 case ISD::ZERO_EXTEND:
16167 case ISD::ANY_EXTEND:
16168 Promote = true;
16169 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016170 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016171 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016172 SDValue N0 = Op.getOperand(0);
16173 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016174 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016175 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016176 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016177 break;
16178 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016179 case ISD::ADD:
16180 case ISD::MUL:
16181 case ISD::AND:
16182 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016183 case ISD::XOR:
16184 Commute = true;
16185 // fallthrough
16186 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016187 SDValue N0 = Op.getOperand(0);
16188 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016189 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016190 return false;
16191 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016192 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016193 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016194 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016195 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016196 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016197 }
16198 }
16199
16200 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016201 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016202}
16203
Evan Cheng60c07e12006-07-05 22:17:51 +000016204//===----------------------------------------------------------------------===//
16205// X86 Inline Assembly Support
16206//===----------------------------------------------------------------------===//
16207
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016208namespace {
16209 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016210 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016211 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016212
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016213 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016214 StringRef piece(*args[i]);
16215 if (!s.startswith(piece)) // Check if the piece matches.
16216 return false;
16217
16218 s = s.substr(piece.size());
16219 StringRef::size_type pos = s.find_first_not_of(" \t");
16220 if (pos == 0) // We matched a prefix.
16221 return false;
16222
16223 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016224 }
16225
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016226 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016227 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016228 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016229}
16230
Chris Lattnerb8105652009-07-20 17:51:36 +000016231bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16232 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016233
16234 std::string AsmStr = IA->getAsmString();
16235
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016236 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16237 if (!Ty || Ty->getBitWidth() % 16 != 0)
16238 return false;
16239
Chris Lattnerb8105652009-07-20 17:51:36 +000016240 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016241 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016242 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016243
16244 switch (AsmPieces.size()) {
16245 default: return false;
16246 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016247 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016248 // we will turn this bswap into something that will be lowered to logical
16249 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16250 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016251 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016252 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16253 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16254 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16255 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16256 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16257 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016258 // No need to check constraints, nothing other than the equivalent of
16259 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016260 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016261 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016262
Chris Lattnerb8105652009-07-20 17:51:36 +000016263 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016264 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016265 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016266 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16267 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016268 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016269 const std::string &ConstraintsStr = IA->getConstraintString();
16270 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016271 std::sort(AsmPieces.begin(), AsmPieces.end());
16272 if (AsmPieces.size() == 4 &&
16273 AsmPieces[0] == "~{cc}" &&
16274 AsmPieces[1] == "~{dirflag}" &&
16275 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016276 AsmPieces[3] == "~{fpsr}")
16277 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016278 }
16279 break;
16280 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016281 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016282 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016283 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16284 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16285 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016286 AsmPieces.clear();
16287 const std::string &ConstraintsStr = IA->getConstraintString();
16288 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16289 std::sort(AsmPieces.begin(), AsmPieces.end());
16290 if (AsmPieces.size() == 4 &&
16291 AsmPieces[0] == "~{cc}" &&
16292 AsmPieces[1] == "~{dirflag}" &&
16293 AsmPieces[2] == "~{flags}" &&
16294 AsmPieces[3] == "~{fpsr}")
16295 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016296 }
Evan Cheng55d42002011-01-08 01:24:27 +000016297
16298 if (CI->getType()->isIntegerTy(64)) {
16299 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16300 if (Constraints.size() >= 2 &&
16301 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16302 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16303 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016304 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16305 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16306 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016307 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016308 }
16309 }
16310 break;
16311 }
16312 return false;
16313}
16314
16315
16316
Chris Lattnerf4dff842006-07-11 02:54:03 +000016317/// getConstraintType - Given a constraint letter, return the type of
16318/// constraint it is for this target.
16319X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016320X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16321 if (Constraint.size() == 1) {
16322 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016323 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016324 case 'q':
16325 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016326 case 'f':
16327 case 't':
16328 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016329 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016330 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016331 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016332 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016333 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016334 case 'a':
16335 case 'b':
16336 case 'c':
16337 case 'd':
16338 case 'S':
16339 case 'D':
16340 case 'A':
16341 return C_Register;
16342 case 'I':
16343 case 'J':
16344 case 'K':
16345 case 'L':
16346 case 'M':
16347 case 'N':
16348 case 'G':
16349 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016350 case 'e':
16351 case 'Z':
16352 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016353 default:
16354 break;
16355 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016356 }
Chris Lattner4234f572007-03-25 02:14:49 +000016357 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016358}
16359
John Thompson44ab89e2010-10-29 17:29:13 +000016360/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016361/// This object must already have been set up with the operand type
16362/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016363TargetLowering::ConstraintWeight
16364 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016365 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016366 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016367 Value *CallOperandVal = info.CallOperandVal;
16368 // If we don't have a value, we can't do a match,
16369 // but allow it at the lowest weight.
16370 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016371 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016372 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016373 // Look at the constraint type.
16374 switch (*constraint) {
16375 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016376 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16377 case 'R':
16378 case 'q':
16379 case 'Q':
16380 case 'a':
16381 case 'b':
16382 case 'c':
16383 case 'd':
16384 case 'S':
16385 case 'D':
16386 case 'A':
16387 if (CallOperandVal->getType()->isIntegerTy())
16388 weight = CW_SpecificReg;
16389 break;
16390 case 'f':
16391 case 't':
16392 case 'u':
16393 if (type->isFloatingPointTy())
16394 weight = CW_SpecificReg;
16395 break;
16396 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016397 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016398 weight = CW_SpecificReg;
16399 break;
16400 case 'x':
16401 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016402 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016403 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016404 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016405 break;
16406 case 'I':
16407 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16408 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016409 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016410 }
16411 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016412 case 'J':
16413 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16414 if (C->getZExtValue() <= 63)
16415 weight = CW_Constant;
16416 }
16417 break;
16418 case 'K':
16419 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16420 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16421 weight = CW_Constant;
16422 }
16423 break;
16424 case 'L':
16425 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16426 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16427 weight = CW_Constant;
16428 }
16429 break;
16430 case 'M':
16431 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16432 if (C->getZExtValue() <= 3)
16433 weight = CW_Constant;
16434 }
16435 break;
16436 case 'N':
16437 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16438 if (C->getZExtValue() <= 0xff)
16439 weight = CW_Constant;
16440 }
16441 break;
16442 case 'G':
16443 case 'C':
16444 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16445 weight = CW_Constant;
16446 }
16447 break;
16448 case 'e':
16449 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16450 if ((C->getSExtValue() >= -0x80000000LL) &&
16451 (C->getSExtValue() <= 0x7fffffffLL))
16452 weight = CW_Constant;
16453 }
16454 break;
16455 case 'Z':
16456 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16457 if (C->getZExtValue() <= 0xffffffff)
16458 weight = CW_Constant;
16459 }
16460 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016461 }
16462 return weight;
16463}
16464
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016465/// LowerXConstraint - try to replace an X constraint, which matches anything,
16466/// with another that has more specific requirements based on the type of the
16467/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016468const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016469LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016470 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16471 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016472 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016473 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016474 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016475 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016476 return "x";
16477 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016478
Chris Lattner5e764232008-04-26 23:02:14 +000016479 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016480}
16481
Chris Lattner48884cd2007-08-25 00:47:38 +000016482/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16483/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016484void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016485 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016486 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016487 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016488 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016489
Eric Christopher100c8332011-06-02 23:16:42 +000016490 // Only support length 1 constraints for now.
16491 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016492
Eric Christopher100c8332011-06-02 23:16:42 +000016493 char ConstraintLetter = Constraint[0];
16494 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016495 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016496 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016497 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016498 if (C->getZExtValue() <= 31) {
16499 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016500 break;
16501 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016502 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016503 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016504 case 'J':
16505 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016506 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016507 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16508 break;
16509 }
16510 }
16511 return;
16512 case 'K':
16513 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016514 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016515 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16516 break;
16517 }
16518 }
16519 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016520 case 'N':
16521 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016522 if (C->getZExtValue() <= 255) {
16523 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016524 break;
16525 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016526 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016527 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016528 case 'e': {
16529 // 32-bit signed value
16530 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016531 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16532 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016533 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016534 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016535 break;
16536 }
16537 // FIXME gcc accepts some relocatable values here too, but only in certain
16538 // memory models; it's complicated.
16539 }
16540 return;
16541 }
16542 case 'Z': {
16543 // 32-bit unsigned value
16544 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016545 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16546 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016547 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16548 break;
16549 }
16550 }
16551 // FIXME gcc accepts some relocatable values here too, but only in certain
16552 // memory models; it's complicated.
16553 return;
16554 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016555 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016556 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016557 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016558 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016559 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016560 break;
16561 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016562
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016563 // In any sort of PIC mode addresses need to be computed at runtime by
16564 // adding in a register or some sort of table lookup. These can't
16565 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016566 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016567 return;
16568
Chris Lattnerdc43a882007-05-03 16:52:29 +000016569 // If we are in non-pic codegen mode, we allow the address of a global (with
16570 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016571 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016572 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016573
Chris Lattner49921962009-05-08 18:23:14 +000016574 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16575 while (1) {
16576 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16577 Offset += GA->getOffset();
16578 break;
16579 } else if (Op.getOpcode() == ISD::ADD) {
16580 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16581 Offset += C->getZExtValue();
16582 Op = Op.getOperand(0);
16583 continue;
16584 }
16585 } else if (Op.getOpcode() == ISD::SUB) {
16586 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16587 Offset += -C->getZExtValue();
16588 Op = Op.getOperand(0);
16589 continue;
16590 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016591 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016592
Chris Lattner49921962009-05-08 18:23:14 +000016593 // Otherwise, this isn't something we can handle, reject it.
16594 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016595 }
Eric Christopherfd179292009-08-27 18:07:15 +000016596
Dan Gohman46510a72010-04-15 01:51:59 +000016597 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016598 // If we require an extra load to get this address, as in PIC mode, we
16599 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016600 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16601 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016602 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016603
Devang Patel0d881da2010-07-06 22:08:15 +000016604 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16605 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016606 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016607 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016608 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016609
Gabor Greifba36cb52008-08-28 21:40:38 +000016610 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016611 Ops.push_back(Result);
16612 return;
16613 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016614 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016615}
16616
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016617std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016618X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016619 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016620 // First, see if this is a constraint that directly corresponds to an LLVM
16621 // register class.
16622 if (Constraint.size() == 1) {
16623 // GCC Constraint Letters
16624 switch (Constraint[0]) {
16625 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016626 // TODO: Slight differences here in allocation order and leaving
16627 // RIP in the class. Do they matter any more here than they do
16628 // in the normal allocation?
16629 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16630 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016631 if (VT == MVT::i32 || VT == MVT::f32)
16632 return std::make_pair(0U, &X86::GR32RegClass);
16633 if (VT == MVT::i16)
16634 return std::make_pair(0U, &X86::GR16RegClass);
16635 if (VT == MVT::i8 || VT == MVT::i1)
16636 return std::make_pair(0U, &X86::GR8RegClass);
16637 if (VT == MVT::i64 || VT == MVT::f64)
16638 return std::make_pair(0U, &X86::GR64RegClass);
16639 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016640 }
16641 // 32-bit fallthrough
16642 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016643 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016644 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16645 if (VT == MVT::i16)
16646 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16647 if (VT == MVT::i8 || VT == MVT::i1)
16648 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16649 if (VT == MVT::i64)
16650 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016651 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016652 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016653 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016654 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016655 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016656 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016657 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016658 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016659 return std::make_pair(0U, &X86::GR32RegClass);
16660 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016661 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016662 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016663 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016664 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016665 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016666 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016667 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16668 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016669 case 'f': // FP Stack registers.
16670 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16671 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016672 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016673 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016674 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016675 return std::make_pair(0U, &X86::RFP64RegClass);
16676 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016677 case 'y': // MMX_REGS if MMX allowed.
16678 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016679 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016680 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016681 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016682 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016683 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016684 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016685
Owen Anderson825b72b2009-08-11 20:47:22 +000016686 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016687 default: break;
16688 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016689 case MVT::f32:
16690 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016691 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016692 case MVT::f64:
16693 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016694 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016695 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016696 case MVT::v16i8:
16697 case MVT::v8i16:
16698 case MVT::v4i32:
16699 case MVT::v2i64:
16700 case MVT::v4f32:
16701 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016702 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016703 // AVX types.
16704 case MVT::v32i8:
16705 case MVT::v16i16:
16706 case MVT::v8i32:
16707 case MVT::v4i64:
16708 case MVT::v8f32:
16709 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016710 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016711 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016712 break;
16713 }
16714 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016715
Chris Lattnerf76d1802006-07-31 23:26:50 +000016716 // Use the default implementation in TargetLowering to convert the register
16717 // constraint into a member of a register class.
16718 std::pair<unsigned, const TargetRegisterClass*> Res;
16719 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016720
16721 // Not found as a standard register?
16722 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016723 // Map st(0) -> st(7) -> ST0
16724 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16725 tolower(Constraint[1]) == 's' &&
16726 tolower(Constraint[2]) == 't' &&
16727 Constraint[3] == '(' &&
16728 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16729 Constraint[5] == ')' &&
16730 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016731
Chris Lattner56d77c72009-09-13 22:41:48 +000016732 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016733 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016734 return Res;
16735 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016736
Chris Lattner56d77c72009-09-13 22:41:48 +000016737 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016738 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016739 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016740 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016741 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016742 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016743
16744 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016745 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016746 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016747 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016748 return Res;
16749 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016750
Dale Johannesen330169f2008-11-13 21:52:36 +000016751 // 'A' means EAX + EDX.
16752 if (Constraint == "A") {
16753 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016754 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016755 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016756 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016757 return Res;
16758 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016759
Chris Lattnerf76d1802006-07-31 23:26:50 +000016760 // Otherwise, check to see if this is a register class of the wrong value
16761 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16762 // turn into {ax},{dx}.
16763 if (Res.second->hasType(VT))
16764 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016765
Chris Lattnerf76d1802006-07-31 23:26:50 +000016766 // All of the single-register GCC register classes map their values onto
16767 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16768 // really want an 8-bit or 32-bit register, map to the appropriate register
16769 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016770 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016771 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016772 unsigned DestReg = 0;
16773 switch (Res.first) {
16774 default: break;
16775 case X86::AX: DestReg = X86::AL; break;
16776 case X86::DX: DestReg = X86::DL; break;
16777 case X86::CX: DestReg = X86::CL; break;
16778 case X86::BX: DestReg = X86::BL; break;
16779 }
16780 if (DestReg) {
16781 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016782 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016783 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016784 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016785 unsigned DestReg = 0;
16786 switch (Res.first) {
16787 default: break;
16788 case X86::AX: DestReg = X86::EAX; break;
16789 case X86::DX: DestReg = X86::EDX; break;
16790 case X86::CX: DestReg = X86::ECX; break;
16791 case X86::BX: DestReg = X86::EBX; break;
16792 case X86::SI: DestReg = X86::ESI; break;
16793 case X86::DI: DestReg = X86::EDI; break;
16794 case X86::BP: DestReg = X86::EBP; break;
16795 case X86::SP: DestReg = X86::ESP; break;
16796 }
16797 if (DestReg) {
16798 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016799 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016800 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016801 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016802 unsigned DestReg = 0;
16803 switch (Res.first) {
16804 default: break;
16805 case X86::AX: DestReg = X86::RAX; break;
16806 case X86::DX: DestReg = X86::RDX; break;
16807 case X86::CX: DestReg = X86::RCX; break;
16808 case X86::BX: DestReg = X86::RBX; break;
16809 case X86::SI: DestReg = X86::RSI; break;
16810 case X86::DI: DestReg = X86::RDI; break;
16811 case X86::BP: DestReg = X86::RBP; break;
16812 case X86::SP: DestReg = X86::RSP; break;
16813 }
16814 if (DestReg) {
16815 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016816 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016817 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016818 }
Craig Topperc9099502012-04-20 06:31:50 +000016819 } else if (Res.second == &X86::FR32RegClass ||
16820 Res.second == &X86::FR64RegClass ||
16821 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016822 // Handle references to XMM physical registers that got mapped into the
16823 // wrong class. This can happen with constraints like {xmm0} where the
16824 // target independent register mapper will just pick the first match it can
16825 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016826
16827 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016828 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016829 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016830 Res.second = &X86::FR64RegClass;
16831 else if (X86::VR128RegClass.hasType(VT))
16832 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016833 else if (X86::VR256RegClass.hasType(VT))
16834 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016835 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016836
Chris Lattnerf76d1802006-07-31 23:26:50 +000016837 return Res;
16838}