blob: baa83c65112d4e24ddf272e75f06fe8757198cd7 [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) {
Benjamin Kramer562b2402012-09-15 12:44:27 +0000650 setOperationAction(ISD::FSIN , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
Benjamin Kramer562b2402012-09-15 12:44:27 +0000652 setOperationAction(ISD::FCOS , MVT::f32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000654 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000655 addLegalFPImmediate(APFloat(+0.0)); // FLD0
656 addLegalFPImmediate(APFloat(+1.0)); // FLD1
657 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
658 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000659 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
660 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
661 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
662 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000663 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000664
Cameron Zwarich33390842011-07-08 21:39:21 +0000665 // We don't support FMA.
666 setOperationAction(ISD::FMA, MVT::f64, Expand);
667 setOperationAction(ISD::FMA, MVT::f32, Expand);
668
Dale Johannesen59a58732007-08-05 18:49:15 +0000669 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000670 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000671 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
673 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000674 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000675 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000676 addLegalFPImmediate(TmpFlt); // FLD0
677 TmpFlt.changeSign();
678 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000679
680 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000681 APFloat TmpFlt2(+1.0);
682 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
683 &ignored);
684 addLegalFPImmediate(TmpFlt2); // FLD1
685 TmpFlt2.changeSign();
686 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
687 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000688
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000689 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
691 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000692 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000693
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000694 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
695 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
696 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
697 setOperationAction(ISD::FRINT, MVT::f80, Expand);
698 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000699 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000700 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000701
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000702 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
704 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
705 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000706
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::FLOG, MVT::f80, Expand);
708 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
709 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
710 setOperationAction(ISD::FEXP, MVT::f80, Expand);
711 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000712
Mon P Wangf007a8b2008-11-06 05:31:54 +0000713 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000714 // (for widening) or expand (for scalarization). Then we will selectively
715 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000716 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
717 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
731 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
732 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000733 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000734 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
735 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
738 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
739 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000740 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
742 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
743 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000744 setOperationAction(ISD::FFLOOR, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
749 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
750 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
751 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000752 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000754 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
757 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
758 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
759 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
760 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000761 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
765 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
766 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
768 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
769 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
770 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000771 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000772 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
773 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
774 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
775 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000776 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000777 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
778 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000779 setTruncStoreAction((MVT::SimpleValueType)VT,
780 (MVT::SimpleValueType)InnerVT, Expand);
781 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
782 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
783 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000784 }
785
Evan Chengc7ce29b2009-02-13 22:36:38 +0000786 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
787 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000788 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000789 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000790 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000791 }
792
Dale Johannesen0488fb62010-09-30 23:57:10 +0000793 // MMX-sized vectors (other than x86mmx) are expected to be expanded
794 // into smaller operations.
795 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
796 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
797 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
798 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
799 setOperationAction(ISD::AND, MVT::v8i8, Expand);
800 setOperationAction(ISD::AND, MVT::v4i16, Expand);
801 setOperationAction(ISD::AND, MVT::v2i32, Expand);
802 setOperationAction(ISD::AND, MVT::v1i64, Expand);
803 setOperationAction(ISD::OR, MVT::v8i8, Expand);
804 setOperationAction(ISD::OR, MVT::v4i16, Expand);
805 setOperationAction(ISD::OR, MVT::v2i32, Expand);
806 setOperationAction(ISD::OR, MVT::v1i64, Expand);
807 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
808 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
809 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
810 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
811 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
812 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
813 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
814 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
815 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
816 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
817 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
818 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
819 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000820 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
821 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
822 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
823 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000824
Craig Topper1accb7e2012-01-10 06:54:16 +0000825 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000826 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000827
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
829 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
830 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
831 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
832 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
833 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000834 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
836 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
837 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
838 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
839 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000840 }
841
Craig Topper1accb7e2012-01-10 06:54:16 +0000842 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000843 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000844
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000845 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
846 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000847 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
848 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
849 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
850 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000851
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
853 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
854 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
855 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
856 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
857 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
858 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
859 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
860 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
861 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
862 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
863 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
864 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
865 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
866 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
867 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000868 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000869
Nadav Rotem354efd82011-09-18 14:57:03 +0000870 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000871 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
872 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
873 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000874
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
876 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
877 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
878 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
879 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000880
Evan Cheng2c3ae372006-04-12 21:21:57 +0000881 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000882 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000883 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000884 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000885 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000886 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000887 // Do not attempt to custom lower non-128-bit vectors
888 if (!VT.is128BitVector())
889 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000890 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
891 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
892 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000893 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000894
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
896 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
897 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
898 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
899 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
900 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000901
Nate Begemancdd1eec2008-02-12 22:51:28 +0000902 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
904 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000905 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000906
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000907 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000908 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000909 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000910
911 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000912 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000913 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000914
Craig Topper0d1f1762012-08-12 00:34:56 +0000915 setOperationAction(ISD::AND, VT, Promote);
916 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
917 setOperationAction(ISD::OR, VT, Promote);
918 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
919 setOperationAction(ISD::XOR, VT, Promote);
920 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
921 setOperationAction(ISD::LOAD, VT, Promote);
922 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
923 setOperationAction(ISD::SELECT, VT, Promote);
924 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000925 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000926
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000928
Evan Cheng2c3ae372006-04-12 21:21:57 +0000929 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
931 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
932 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
933 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000934
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
936 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Michael Liaob8150d82012-09-10 18:33:51 +0000937
938 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000939 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000940
Craig Topperd0a31172012-01-10 06:37:29 +0000941 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000942 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
943 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
944 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
945 setOperationAction(ISD::FRINT, MVT::f32, Legal);
946 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
947 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
948 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
949 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
950 setOperationAction(ISD::FRINT, MVT::f64, Legal);
951 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
952
Craig Topper12fb5c62012-09-08 17:42:27 +0000953 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
954 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
955
Nate Begeman14d12ca2008-02-11 04:19:36 +0000956 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000958
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000959 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
960 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
961 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
962 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
963 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000964
Nate Begeman14d12ca2008-02-11 04:19:36 +0000965 // i8 and i16 vectors are custom , because the source register and source
966 // source memory operand types are not the same width. f32 vectors are
967 // custom since the immediate controlling the insert encodes additional
968 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
970 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
971 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
972 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000973
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
975 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
976 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
977 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000978
Pete Coopera77214a2011-11-14 19:38:42 +0000979 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000980 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000981 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000982 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
983 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000984 }
985 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000986
Craig Topper1accb7e2012-01-10 06:54:16 +0000987 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000988 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000989 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000990
Nadav Rotem43012222011-05-11 08:12:09 +0000991 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000992 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000993
Nadav Rotem43012222011-05-11 08:12:09 +0000994 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000995 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000996
997 if (Subtarget->hasAVX2()) {
998 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
999 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
1000
1001 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
1002 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
1003
1004 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
1005 } else {
1006 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1007 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1008
1009 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1010 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1011
1012 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1013 }
Nadav Rotem43012222011-05-11 08:12:09 +00001014 }
1015
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001016 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001017 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1018 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1019 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1020 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1021 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1022 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001023
Owen Anderson825b72b2009-08-11 20:47:22 +00001024 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001025 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1026 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001027
Owen Anderson825b72b2009-08-11 20:47:22 +00001028 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1029 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1030 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1031 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1032 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001033 setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001034 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001035 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001036
Owen Anderson825b72b2009-08-11 20:47:22 +00001037 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1038 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1039 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1040 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1041 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
Craig Topper12fb5c62012-09-08 17:42:27 +00001042 setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001043 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001044 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001045
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001046 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1047 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001048 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001049
Michael Liaob8150d82012-09-10 18:33:51 +00001050 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal);
1051
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001052 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1053 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1054
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001055 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1056 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1057
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001058 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001059 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001060
Duncan Sands28b77e92011-09-06 19:07:46 +00001061 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1062 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1063 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1064 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001065
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001066 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1067 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1068 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1069
Craig Topperaaa643c2011-11-09 07:28:55 +00001070 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1071 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1072 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1073 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001074
Craig Topperbf404372012-08-31 15:40:30 +00001075 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001076 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1077 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1078 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1079 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1080 setOperationAction(ISD::FMA, MVT::f32, Custom);
1081 setOperationAction(ISD::FMA, MVT::f64, Custom);
1082 }
Craig Topper880ef452012-08-11 22:34:26 +00001083
Craig Topperaaa643c2011-11-09 07:28:55 +00001084 if (Subtarget->hasAVX2()) {
1085 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1086 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1087 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1088 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001089
Craig Topperaaa643c2011-11-09 07:28:55 +00001090 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1091 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1092 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1093 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001094
Craig Topperaaa643c2011-11-09 07:28:55 +00001095 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1096 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1097 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001098 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001099
1100 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001101
1102 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1103 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1104
1105 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1106 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1107
1108 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001109 } else {
1110 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1111 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1112 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1113 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1114
1115 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1116 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1117 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1118 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1119
1120 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1121 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1122 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1123 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001124
1125 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1126 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1127
1128 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1129 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1130
1131 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001132 }
Craig Topper13894fa2011-08-24 06:14:18 +00001133
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001134 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001135 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1136 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001137 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001138
1139 // Extract subvector is special because the value type
1140 // (result) is 128-bit but the source is 256-bit wide.
1141 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001142 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001143
1144 // Do not attempt to custom lower other non-256-bit vectors
1145 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001146 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001147
Craig Topper0d1f1762012-08-12 00:34:56 +00001148 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1149 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1150 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1151 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1152 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1153 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1154 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001155 }
1156
David Greene54d8eba2011-01-27 22:38:56 +00001157 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001158 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001159 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001160
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001161 // Do not attempt to promote non-256-bit vectors
1162 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001163 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001164
Craig Topper0d1f1762012-08-12 00:34:56 +00001165 setOperationAction(ISD::AND, VT, Promote);
1166 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1167 setOperationAction(ISD::OR, VT, Promote);
1168 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1169 setOperationAction(ISD::XOR, VT, Promote);
1170 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1171 setOperationAction(ISD::LOAD, VT, Promote);
1172 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1173 setOperationAction(ISD::SELECT, VT, Promote);
1174 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001175 }
David Greene9b9838d2009-06-29 16:47:10 +00001176 }
1177
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001178 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1179 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001180 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1181 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001182 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1183 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001184 }
1185
Evan Cheng6be2c582006-04-05 23:38:46 +00001186 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001187 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001188 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001189
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001190
Eli Friedman962f5492010-06-02 19:35:46 +00001191 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1192 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001193 //
Eli Friedman962f5492010-06-02 19:35:46 +00001194 // FIXME: We really should do custom legalization for addition and
1195 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1196 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001197 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1198 // Add/Sub/Mul with overflow operations are custom lowered.
1199 MVT VT = IntVTs[i];
1200 setOperationAction(ISD::SADDO, VT, Custom);
1201 setOperationAction(ISD::UADDO, VT, Custom);
1202 setOperationAction(ISD::SSUBO, VT, Custom);
1203 setOperationAction(ISD::USUBO, VT, Custom);
1204 setOperationAction(ISD::SMULO, VT, Custom);
1205 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001206 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001207
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001208 // There are no 8-bit 3-address imul/mul instructions
1209 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1210 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001211
Evan Chengd54f2d52009-03-31 19:38:51 +00001212 if (!Subtarget->is64Bit()) {
1213 // These libcalls are not available in 32-bit.
1214 setLibcallName(RTLIB::SHL_I128, 0);
1215 setLibcallName(RTLIB::SRL_I128, 0);
1216 setLibcallName(RTLIB::SRA_I128, 0);
1217 }
1218
Evan Cheng206ee9d2006-07-07 08:33:52 +00001219 // We have target-specific dag combine patterns for the following nodes:
1220 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001221 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001222 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001223 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001224 setTargetDAGCombine(ISD::SHL);
1225 setTargetDAGCombine(ISD::SRA);
1226 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001227 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001228 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001229 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001230 setTargetDAGCombine(ISD::FADD);
1231 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001232 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001233 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001234 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001235 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001236 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001237 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001238 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001239 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001240 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001241 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001242 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001243 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001244 if (Subtarget->is64Bit())
1245 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001246 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001247
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001248 computeRegisterProperties();
1249
Evan Cheng05219282011-01-06 06:52:41 +00001250 // On Darwin, -Os means optimize for size without hurting performance,
1251 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001252 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001253 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001254 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001255 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1256 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1257 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001258 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001259 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001260
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001261 // Predictable cmov don't hurt on atom because it's in-order.
1262 predictableSelectIsExpensive = !Subtarget->isAtom();
1263
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001264 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001265}
1266
Scott Michel5b8f82e2008-03-10 15:42:14 +00001267
Duncan Sands28b77e92011-09-06 19:07:46 +00001268EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1269 if (!VT.isVector()) return MVT::i8;
1270 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001271}
1272
1273
Evan Cheng29286502008-01-23 23:17:41 +00001274/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1275/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001276static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001277 if (MaxAlign == 16)
1278 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001279 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001280 if (VTy->getBitWidth() == 128)
1281 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001282 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001283 unsigned EltAlign = 0;
1284 getMaxByValAlign(ATy->getElementType(), EltAlign);
1285 if (EltAlign > MaxAlign)
1286 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001287 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001288 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1289 unsigned EltAlign = 0;
1290 getMaxByValAlign(STy->getElementType(i), EltAlign);
1291 if (EltAlign > MaxAlign)
1292 MaxAlign = EltAlign;
1293 if (MaxAlign == 16)
1294 break;
1295 }
1296 }
Evan Cheng29286502008-01-23 23:17:41 +00001297}
1298
1299/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1300/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001301/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1302/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001303unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001304 if (Subtarget->is64Bit()) {
1305 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001306 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001307 if (TyAlign > 8)
1308 return TyAlign;
1309 return 8;
1310 }
1311
Evan Cheng29286502008-01-23 23:17:41 +00001312 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001313 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001314 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001315 return Align;
1316}
Chris Lattner2b02a442007-02-25 08:29:00 +00001317
Evan Chengf0df0312008-05-15 08:39:06 +00001318/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001319/// and store operations as a result of memset, memcpy, and memmove
1320/// lowering. If DstAlign is zero that means it's safe to destination
1321/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1322/// means there isn't a need to check it against alignment requirement,
1323/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001324/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001325/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1326/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1327/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001328/// It returns EVT::Other if the type should be determined using generic
1329/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001330EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001331X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1332 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001333 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001334 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001335 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001336 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1337 // linux. This is because the stack realignment code can't handle certain
1338 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001339 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001340 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001341 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001342 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001343 (Subtarget->isUnalignedMemAccessFast() ||
1344 ((DstAlign == 0 || DstAlign >= 16) &&
1345 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001346 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001347 if (Subtarget->getStackAlignment() >= 32) {
1348 if (Subtarget->hasAVX2())
1349 return MVT::v8i32;
1350 if (Subtarget->hasAVX())
1351 return MVT::v8f32;
1352 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001353 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001354 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001355 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001356 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001357 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001358 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001359 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001360 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001361 // Do not use f64 to lower memcpy if source is string constant. It's
1362 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001363 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001364 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001365 }
Evan Chengf0df0312008-05-15 08:39:06 +00001366 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001367 return MVT::i64;
1368 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001369}
1370
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001371/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1372/// current function. The returned value is a member of the
1373/// MachineJumpTableInfo::JTEntryKind enum.
1374unsigned X86TargetLowering::getJumpTableEncoding() const {
1375 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1376 // symbol.
1377 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1378 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001379 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001380
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001381 // Otherwise, use the normal jump table encoding heuristics.
1382 return TargetLowering::getJumpTableEncoding();
1383}
1384
Chris Lattnerc64daab2010-01-26 05:02:42 +00001385const MCExpr *
1386X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1387 const MachineBasicBlock *MBB,
1388 unsigned uid,MCContext &Ctx) const{
1389 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1390 Subtarget->isPICStyleGOT());
1391 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1392 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001393 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1394 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001395}
1396
Evan Chengcc415862007-11-09 01:32:10 +00001397/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1398/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001399SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001400 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001401 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001402 // This doesn't have DebugLoc associated with it, but is not really the
1403 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001404 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001405 return Table;
1406}
1407
Chris Lattner589c6f62010-01-26 06:28:43 +00001408/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1409/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1410/// MCExpr.
1411const MCExpr *X86TargetLowering::
1412getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1413 MCContext &Ctx) const {
1414 // X86-64 uses RIP relative addressing based on the jump table label.
1415 if (Subtarget->isPICStyleRIPRel())
1416 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1417
1418 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001419 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001420}
1421
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001422// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001423std::pair<const TargetRegisterClass*, uint8_t>
1424X86TargetLowering::findRepresentativeClass(EVT VT) const{
1425 const TargetRegisterClass *RRC = 0;
1426 uint8_t Cost = 1;
1427 switch (VT.getSimpleVT().SimpleTy) {
1428 default:
1429 return TargetLowering::findRepresentativeClass(VT);
1430 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001431 RRC = Subtarget->is64Bit() ?
1432 (const TargetRegisterClass*)&X86::GR64RegClass :
1433 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001434 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001435 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001436 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001437 break;
1438 case MVT::f32: case MVT::f64:
1439 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1440 case MVT::v4f32: case MVT::v2f64:
1441 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1442 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001443 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001444 break;
1445 }
1446 return std::make_pair(RRC, Cost);
1447}
1448
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001449bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1450 unsigned &Offset) const {
1451 if (!Subtarget->isTargetLinux())
1452 return false;
1453
1454 if (Subtarget->is64Bit()) {
1455 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1456 Offset = 0x28;
1457 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1458 AddressSpace = 256;
1459 else
1460 AddressSpace = 257;
1461 } else {
1462 // %gs:0x14 on i386
1463 Offset = 0x14;
1464 AddressSpace = 256;
1465 }
1466 return true;
1467}
1468
1469
Chris Lattner2b02a442007-02-25 08:29:00 +00001470//===----------------------------------------------------------------------===//
1471// Return Value Calling Convention Implementation
1472//===----------------------------------------------------------------------===//
1473
Chris Lattner59ed56b2007-02-28 04:55:35 +00001474#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001475
Michael J. Spencerec38de22010-10-10 22:04:20 +00001476bool
Eric Christopher471e4222011-06-08 23:55:35 +00001477X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001478 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001479 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001480 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001481 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001482 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001483 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001484 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001485}
1486
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487SDValue
1488X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001489 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001490 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001491 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001492 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001493 MachineFunction &MF = DAG.getMachineFunction();
1494 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001495
Chris Lattner9774c912007-02-27 05:28:59 +00001496 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001497 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001498 RVLocs, *DAG.getContext());
1499 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001500
Evan Chengdcea1632010-02-04 02:40:39 +00001501 // Add the regs to the liveout set for the function.
1502 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1503 for (unsigned i = 0; i != RVLocs.size(); ++i)
1504 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1505 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001506
Dan Gohman475871a2008-07-27 21:46:04 +00001507 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001508
Dan Gohman475871a2008-07-27 21:46:04 +00001509 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001510 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1511 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001512 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1513 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001514
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001515 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001516 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1517 CCValAssign &VA = RVLocs[i];
1518 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001519 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001520 EVT ValVT = ValToCopy.getValueType();
1521
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001522 // Promote values to the appropriate types
1523 if (VA.getLocInfo() == CCValAssign::SExt)
1524 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1525 else if (VA.getLocInfo() == CCValAssign::ZExt)
1526 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1527 else if (VA.getLocInfo() == CCValAssign::AExt)
1528 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1529 else if (VA.getLocInfo() == CCValAssign::BCvt)
1530 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1531
Dale Johannesenc4510512010-09-24 19:05:48 +00001532 // If this is x86-64, and we disabled SSE, we can't return FP values,
1533 // or SSE or MMX vectors.
1534 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1535 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001536 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001537 report_fatal_error("SSE register return with SSE disabled");
1538 }
1539 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1540 // llvm-gcc has never done it right and no one has noticed, so this
1541 // should be OK for now.
1542 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001543 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001544 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001545
Chris Lattner447ff682008-03-11 03:23:40 +00001546 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1547 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001548 if (VA.getLocReg() == X86::ST0 ||
1549 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001550 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1551 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001552 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001553 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001554 RetOps.push_back(ValToCopy);
1555 // Don't emit a copytoreg.
1556 continue;
1557 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001558
Evan Cheng242b38b2009-02-23 09:03:22 +00001559 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1560 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001561 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001562 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001563 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001564 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001565 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1566 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001567 // If we don't have SSE2 available, convert to v4f32 so the generated
1568 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001569 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001570 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001571 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001572 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001573 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001574
Dale Johannesendd64c412009-02-04 00:33:20 +00001575 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001576 Flag = Chain.getValue(1);
1577 }
Dan Gohman61a92132008-04-21 23:59:07 +00001578
1579 // The x86-64 ABI for returning structs by value requires that we copy
1580 // the sret argument into %rax for the return. We saved the argument into
1581 // a virtual register in the entry block, so now we copy the value out
1582 // and into %rax.
1583 if (Subtarget->is64Bit() &&
1584 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1585 MachineFunction &MF = DAG.getMachineFunction();
1586 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1587 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001588 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001589 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001590 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001591
Dale Johannesendd64c412009-02-04 00:33:20 +00001592 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001593 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001594
1595 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001596 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001597 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001598
Chris Lattner447ff682008-03-11 03:23:40 +00001599 RetOps[0] = Chain; // Update chain.
1600
1601 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001602 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001603 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001604
1605 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001606 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001607}
1608
Evan Chengbf010eb2012-04-10 01:51:00 +00001609bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001610 if (N->getNumValues() != 1)
1611 return false;
1612 if (!N->hasNUsesOfValue(1, 0))
1613 return false;
1614
Evan Chengbf010eb2012-04-10 01:51:00 +00001615 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001616 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001617 if (Copy->getOpcode() == ISD::CopyToReg) {
1618 // If the copy has a glue operand, we conservatively assume it isn't safe to
1619 // perform a tail call.
1620 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1621 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001622 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001623 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001624 return false;
1625
Evan Cheng1bf891a2010-12-01 22:59:46 +00001626 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001627 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001628 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001629 if (UI->getOpcode() != X86ISD::RET_FLAG)
1630 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001631 HasRet = true;
1632 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001633
Evan Chengbf010eb2012-04-10 01:51:00 +00001634 if (!HasRet)
1635 return false;
1636
1637 Chain = TCChain;
1638 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001639}
1640
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001641EVT
1642X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001643 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001644 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001645 // TODO: Is this also valid on 32-bit?
1646 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001647 ReturnMVT = MVT::i8;
1648 else
1649 ReturnMVT = MVT::i32;
1650
1651 EVT MinVT = getRegisterType(Context, ReturnMVT);
1652 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001653}
1654
Dan Gohman98ca4f22009-08-05 01:29:28 +00001655/// LowerCallResult - Lower the result values of a call into the
1656/// appropriate copies out of appropriate physical registers.
1657///
1658SDValue
1659X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001660 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001661 const SmallVectorImpl<ISD::InputArg> &Ins,
1662 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001663 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001664
Chris Lattnere32bbf62007-02-28 07:09:55 +00001665 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001666 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001667 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001668 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001669 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001670 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001671
Chris Lattner3085e152007-02-25 08:59:22 +00001672 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001673 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001674 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001675 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001676
Torok Edwin3f142c32009-02-01 18:15:56 +00001677 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001678 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001679 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001680 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001681 }
1682
Evan Cheng79fb3b42009-02-20 20:43:02 +00001683 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001684
1685 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001686 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001687 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001688 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001689 // instead.
1690 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1691 // If we prefer to use the value in xmm registers, copy it out as f80 and
1692 // use a truncate to move it from fp stack reg to xmm reg.
1693 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001694 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001695 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1696 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001697 Val = Chain.getValue(0);
1698
1699 // Round the f80 to the right size, which also moves it to the appropriate
1700 // xmm register.
1701 if (CopyVT != VA.getValVT())
1702 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1703 // This truncation won't change the value.
1704 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001705 } else {
1706 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1707 CopyVT, InFlag).getValue(1);
1708 Val = Chain.getValue(0);
1709 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001710 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001712 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001713
Dan Gohman98ca4f22009-08-05 01:29:28 +00001714 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001715}
1716
1717
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001718//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001719// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001720//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001721// StdCall calling convention seems to be standard for many Windows' API
1722// routines and around. It differs from C calling convention just a little:
1723// callee should clean up the stack, not caller. Symbols should be also
1724// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001725// For info on fast calling convention see Fast Calling Convention (tail call)
1726// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001727
Dan Gohman98ca4f22009-08-05 01:29:28 +00001728/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001729/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001730enum StructReturnType {
1731 NotStructReturn,
1732 RegStructReturn,
1733 StackStructReturn
1734};
1735static StructReturnType
1736callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001737 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001738 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001739
Rafael Espindola1cee7102012-07-25 13:41:10 +00001740 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1741 if (!Flags.isSRet())
1742 return NotStructReturn;
1743 if (Flags.isInReg())
1744 return RegStructReturn;
1745 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001746}
1747
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001748/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001749/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001750static StructReturnType
1751argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001753 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001754
Rafael Espindola1cee7102012-07-25 13:41:10 +00001755 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1756 if (!Flags.isSRet())
1757 return NotStructReturn;
1758 if (Flags.isInReg())
1759 return RegStructReturn;
1760 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001761}
1762
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001763/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1764/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001765/// the specific parameter attribute. The copy will be passed as a byval
1766/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001767static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001768CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001769 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1770 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001771 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001772
Dale Johannesendd64c412009-02-04 00:33:20 +00001773 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001774 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001775 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001776}
1777
Chris Lattner29689432010-03-11 00:22:57 +00001778/// IsTailCallConvention - Return true if the calling convention is one that
1779/// supports tail call optimization.
1780static bool IsTailCallConvention(CallingConv::ID CC) {
1781 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1782}
1783
Evan Cheng485fafc2011-03-21 01:19:09 +00001784bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001785 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001786 return false;
1787
1788 CallSite CS(CI);
1789 CallingConv::ID CalleeCC = CS.getCallingConv();
1790 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1791 return false;
1792
1793 return true;
1794}
1795
Evan Cheng0c439eb2010-01-27 00:07:07 +00001796/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1797/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001798static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1799 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001800 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001801}
1802
Dan Gohman98ca4f22009-08-05 01:29:28 +00001803SDValue
1804X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001805 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001806 const SmallVectorImpl<ISD::InputArg> &Ins,
1807 DebugLoc dl, SelectionDAG &DAG,
1808 const CCValAssign &VA,
1809 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001810 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001811 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001812 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001813 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1814 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001815 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001816 EVT ValVT;
1817
1818 // If value is passed by pointer we have address passed instead of the value
1819 // itself.
1820 if (VA.getLocInfo() == CCValAssign::Indirect)
1821 ValVT = VA.getLocVT();
1822 else
1823 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001824
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001825 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001826 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001827 // In case of tail call optimization mark all arguments mutable. Since they
1828 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001829 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001830 unsigned Bytes = Flags.getByValSize();
1831 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1832 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001833 return DAG.getFrameIndex(FI, getPointerTy());
1834 } else {
1835 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001836 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001837 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1838 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001839 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001840 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001841 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001842}
1843
Dan Gohman475871a2008-07-27 21:46:04 +00001844SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001845X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001846 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847 bool isVarArg,
1848 const SmallVectorImpl<ISD::InputArg> &Ins,
1849 DebugLoc dl,
1850 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001851 SmallVectorImpl<SDValue> &InVals)
1852 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001853 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001854 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001855
Gordon Henriksen86737662008-01-05 16:56:59 +00001856 const Function* Fn = MF.getFunction();
1857 if (Fn->hasExternalLinkage() &&
1858 Subtarget->isTargetCygMing() &&
1859 Fn->getName() == "main")
1860 FuncInfo->setForceFramePointer(true);
1861
Evan Cheng1bc78042006-04-26 01:20:17 +00001862 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001864 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001865 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001866
Chris Lattner29689432010-03-11 00:22:57 +00001867 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1868 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001869
Chris Lattner638402b2007-02-28 07:00:42 +00001870 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001871 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001872 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001873 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001874
1875 // Allocate shadow area for Win64
1876 if (IsWin64) {
1877 CCInfo.AllocateStack(32, 8);
1878 }
1879
Duncan Sands45907662010-10-31 13:21:44 +00001880 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001881
Chris Lattnerf39f7712007-02-28 05:46:49 +00001882 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001883 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001884 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1885 CCValAssign &VA = ArgLocs[i];
1886 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1887 // places.
1888 assert(VA.getValNo() != LastVal &&
1889 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001890 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001891 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001892
Chris Lattnerf39f7712007-02-28 05:46:49 +00001893 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001894 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001895 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001896 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001897 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001898 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001899 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001900 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001901 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001902 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001903 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001904 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001905 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001906 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001907 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001908 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001909 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001910 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001911 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001912
Devang Patel68e6bee2011-02-21 23:21:26 +00001913 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001915
Chris Lattnerf39f7712007-02-28 05:46:49 +00001916 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1917 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1918 // right size.
1919 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001920 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001921 DAG.getValueType(VA.getValVT()));
1922 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001923 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001924 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001925 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001926 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001927
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001928 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001929 // Handle MMX values passed in XMM regs.
1930 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001931 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1932 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001933 } else
1934 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001935 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001936 } else {
1937 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001939 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001940
1941 // If value is passed via pointer - do a load.
1942 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001943 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001944 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001945
Dan Gohman98ca4f22009-08-05 01:29:28 +00001946 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001947 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001948
Dan Gohman61a92132008-04-21 23:59:07 +00001949 // The x86-64 ABI for returning structs by value requires that we copy
1950 // the sret argument into %rax for the return. Save the argument into
1951 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001952 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001953 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1954 unsigned Reg = FuncInfo->getSRetReturnReg();
1955 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001956 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001957 FuncInfo->setSRetReturnReg(Reg);
1958 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001959 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001960 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001961 }
1962
Chris Lattnerf39f7712007-02-28 05:46:49 +00001963 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001964 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001965 if (FuncIsMadeTailCallSafe(CallConv,
1966 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001967 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001968
Evan Cheng1bc78042006-04-26 01:20:17 +00001969 // If the function takes variable number of arguments, make a frame index for
1970 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001971 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001972 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1973 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001974 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001975 }
1976 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001977 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1978
1979 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001980 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001981 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001982 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001983 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001984 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1985 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001986 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001987 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1988 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1989 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001990 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001991 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001992
1993 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001994 // The XMM registers which might contain var arg parameters are shadowed
1995 // in their paired GPR. So we only need to save the GPR to their home
1996 // slots.
1997 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001998 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001999 } else {
2000 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
2001 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002002
Chad Rosier30450e82011-12-22 22:35:21 +00002003 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
2004 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002005 }
2006 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
2007 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002008
Devang Patel578efa92009-06-05 21:57:13 +00002009 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00002010 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002011 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002012 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2013 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002014 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002015 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002016 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002017 // Kernel mode asks for SSE to be disabled, so don't push them
2018 // on the stack.
2019 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002020
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002021 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002022 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002023 // Get to the caller-allocated home save location. Add 8 to account
2024 // for the return address.
2025 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002026 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002027 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002028 // Fixup to set vararg frame on shadow area (4 x i64).
2029 if (NumIntRegs < 4)
2030 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002031 } else {
2032 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002033 // registers, then we must store them to their spots on the stack so
2034 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002035 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2036 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2037 FuncInfo->setRegSaveFrameIndex(
2038 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002039 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002040 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002041
Gordon Henriksen86737662008-01-05 16:56:59 +00002042 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002043 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002044 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2045 getPointerTy());
2046 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002047 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002048 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2049 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002050 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002051 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002052 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002053 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002054 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002055 MachinePointerInfo::getFixedStack(
2056 FuncInfo->getRegSaveFrameIndex(), Offset),
2057 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002058 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002059 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002060 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002061
Dan Gohmanface41a2009-08-16 21:24:25 +00002062 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2063 // Now store the XMM (fp + vector) parameter registers.
2064 SmallVector<SDValue, 11> SaveXMMOps;
2065 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002066
Craig Topperc9099502012-04-20 06:31:50 +00002067 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002068 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2069 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002070
Dan Gohman1e93df62010-04-17 14:41:14 +00002071 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2072 FuncInfo->getRegSaveFrameIndex()));
2073 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2074 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002075
Dan Gohmanface41a2009-08-16 21:24:25 +00002076 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002077 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002078 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002079 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2080 SaveXMMOps.push_back(Val);
2081 }
2082 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2083 MVT::Other,
2084 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002085 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002086
2087 if (!MemOps.empty())
2088 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2089 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002090 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002091 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002092
Gordon Henriksen86737662008-01-05 16:56:59 +00002093 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002094 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2095 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002096 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002097 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002098 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002099 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002100 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002101 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002102 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002103 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002104
Gordon Henriksen86737662008-01-05 16:56:59 +00002105 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002106 // RegSaveFrameIndex is X86-64 only.
2107 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002108 if (CallConv == CallingConv::X86_FastCall ||
2109 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002110 // fastcc functions can't have varargs.
2111 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002112 }
Evan Cheng25caf632006-05-23 21:06:34 +00002113
Rafael Espindola76927d752011-08-30 19:39:58 +00002114 FuncInfo->setArgumentStackSize(StackSize);
2115
Dan Gohman98ca4f22009-08-05 01:29:28 +00002116 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002117}
2118
Dan Gohman475871a2008-07-27 21:46:04 +00002119SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002120X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2121 SDValue StackPtr, SDValue Arg,
2122 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002123 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002124 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002125 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002126 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002127 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002128 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002129 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002130
2131 return DAG.getStore(Chain, dl, Arg, PtrOff,
2132 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002133 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002134}
2135
Bill Wendling64e87322009-01-16 19:25:27 +00002136/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002137/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002138SDValue
2139X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002140 SDValue &OutRetAddr, SDValue Chain,
2141 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002142 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002143 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002144 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002145 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002146
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002147 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002148 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002149 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002150 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002151}
2152
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002153/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002154/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002155static SDValue
2156EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002157 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002158 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002159 // Store the return address to the appropriate stack slot.
2160 if (!FPDiff) return Chain;
2161 // Calculate the new stack slot for the return address.
2162 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002163 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002164 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002165 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002166 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002167 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002168 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002169 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002170 return Chain;
2171}
2172
Dan Gohman98ca4f22009-08-05 01:29:28 +00002173SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002174X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002175 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002176 SelectionDAG &DAG = CLI.DAG;
2177 DebugLoc &dl = CLI.DL;
2178 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2179 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2180 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2181 SDValue Chain = CLI.Chain;
2182 SDValue Callee = CLI.Callee;
2183 CallingConv::ID CallConv = CLI.CallConv;
2184 bool &isTailCall = CLI.IsTailCall;
2185 bool isVarArg = CLI.IsVarArg;
2186
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187 MachineFunction &MF = DAG.getMachineFunction();
2188 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002189 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002190 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002191 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002192 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002193
Nick Lewycky22de16d2012-01-19 00:34:10 +00002194 if (MF.getTarget().Options.DisableTailCalls)
2195 isTailCall = false;
2196
Evan Cheng5f941932010-02-05 02:21:12 +00002197 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002198 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002199 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002200 isVarArg, SR != NotStructReturn,
2201 MF.getFunction()->hasStructRetAttr(),
2202 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002203
2204 // Sibcalls are automatically detected tailcalls which do not require
2205 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002206 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002207 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002208
2209 if (isTailCall)
2210 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002211 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002212
Chris Lattner29689432010-03-11 00:22:57 +00002213 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2214 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002215
Chris Lattner638402b2007-02-28 07:00:42 +00002216 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002217 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002218 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002219 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002220
2221 // Allocate shadow area for Win64
2222 if (IsWin64) {
2223 CCInfo.AllocateStack(32, 8);
2224 }
2225
Duncan Sands45907662010-10-31 13:21:44 +00002226 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002227
Chris Lattner423c5f42007-02-28 05:31:48 +00002228 // Get a count of how many bytes are to be pushed on the stack.
2229 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002230 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002231 // This is a sibcall. The memory operands are available in caller's
2232 // own caller's stack.
2233 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002234 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2235 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002236 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002237
Gordon Henriksen86737662008-01-05 16:56:59 +00002238 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002239 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002240 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002241 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002242 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2243 FPDiff = NumBytesCallerPushed - NumBytes;
2244
2245 // Set the delta of movement of the returnaddr stackslot.
2246 // But only set if delta is greater than previous delta.
2247 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2248 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2249 }
2250
Evan Chengf22f9b32010-02-06 03:28:46 +00002251 if (!IsSibcall)
2252 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002253
Dan Gohman475871a2008-07-27 21:46:04 +00002254 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002255 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002256 if (isTailCall && FPDiff)
2257 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2258 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002259
Dan Gohman475871a2008-07-27 21:46:04 +00002260 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2261 SmallVector<SDValue, 8> MemOpChains;
2262 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002263
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002264 // Walk the register/memloc assignments, inserting copies/loads. In the case
2265 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002266 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2267 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002268 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002269 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002270 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002271 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002272
Chris Lattner423c5f42007-02-28 05:31:48 +00002273 // Promote the value if needed.
2274 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002275 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002276 case CCValAssign::Full: break;
2277 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002278 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002279 break;
2280 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002281 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002282 break;
2283 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002284 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002285 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002286 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002287 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2288 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002289 } else
2290 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2291 break;
2292 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002293 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002294 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002295 case CCValAssign::Indirect: {
2296 // Store the argument.
2297 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002298 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002299 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002300 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002301 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002302 Arg = SpillSlot;
2303 break;
2304 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002305 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002306
Chris Lattner423c5f42007-02-28 05:31:48 +00002307 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002308 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2309 if (isVarArg && IsWin64) {
2310 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2311 // shadow reg if callee is a varargs function.
2312 unsigned ShadowReg = 0;
2313 switch (VA.getLocReg()) {
2314 case X86::XMM0: ShadowReg = X86::RCX; break;
2315 case X86::XMM1: ShadowReg = X86::RDX; break;
2316 case X86::XMM2: ShadowReg = X86::R8; break;
2317 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002318 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002319 if (ShadowReg)
2320 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002321 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002322 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002323 assert(VA.isMemLoc());
2324 if (StackPtr.getNode() == 0)
2325 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2326 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2327 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002328 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002330
Evan Cheng32fe1032006-05-25 00:59:30 +00002331 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002332 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002333 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002334
Chris Lattner88e1fd52009-07-09 04:24:46 +00002335 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002336 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2337 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002338 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002339 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2340 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002341 } else {
2342 // If we are tail calling and generating PIC/GOT style code load the
2343 // address of the callee into ECX. The value in ecx is used as target of
2344 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2345 // for tail calls on PIC/GOT architectures. Normally we would just put the
2346 // address of GOT into ebx and then call target@PLT. But for tail calls
2347 // ebx would be restored (since ebx is callee saved) before jumping to the
2348 // target@PLT.
2349
2350 // Note: The actual moving to ECX is done further down.
2351 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2352 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2353 !G->getGlobal()->hasProtectedVisibility())
2354 Callee = LowerGlobalAddress(Callee, DAG);
2355 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002356 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002357 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002358 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002359
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002360 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002361 // From AMD64 ABI document:
2362 // For calls that may call functions that use varargs or stdargs
2363 // (prototype-less calls or calls to functions containing ellipsis (...) in
2364 // the declaration) %al is used as hidden argument to specify the number
2365 // of SSE registers used. The contents of %al do not need to match exactly
2366 // the number of registers, but must be an ubound on the number of SSE
2367 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002368
Gordon Henriksen86737662008-01-05 16:56:59 +00002369 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002370 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002371 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2372 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2373 };
2374 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002375 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002376 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002377
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002378 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2379 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002380 }
2381
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002382 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002383 if (isTailCall) {
2384 // Force all the incoming stack arguments to be loaded from the stack
2385 // before any new outgoing arguments are stored to the stack, because the
2386 // outgoing stack slots may alias the incoming argument stack slots, and
2387 // the alias isn't otherwise explicit. This is slightly more conservative
2388 // than necessary, because it means that each store effectively depends
2389 // on every argument instead of just those arguments it would clobber.
2390 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2391
Dan Gohman475871a2008-07-27 21:46:04 +00002392 SmallVector<SDValue, 8> MemOpChains2;
2393 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002394 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002395 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002396 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2397 CCValAssign &VA = ArgLocs[i];
2398 if (VA.isRegLoc())
2399 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002400 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002401 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002402 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002403 // Create frame index.
2404 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002405 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002406 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002407 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002408
Duncan Sands276dcbd2008-03-21 09:14:45 +00002409 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002410 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002411 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002412 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002413 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002414 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002415 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002416
Dan Gohman98ca4f22009-08-05 01:29:28 +00002417 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2418 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002419 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002420 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002421 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002422 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002423 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002424 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002425 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002426 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002427 }
2428 }
2429
2430 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002431 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002432 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002433
2434 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002435 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002436 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002437 }
2438
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002439 // Build a sequence of copy-to-reg nodes chained together with token chain
2440 // and flag operands which copy the outgoing args into registers.
2441 SDValue InFlag;
2442 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2443 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2444 RegsToPass[i].second, InFlag);
2445 InFlag = Chain.getValue(1);
2446 }
2447
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002448 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2449 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2450 // In the 64-bit large code model, we have to make all calls
2451 // through a register, since the call instruction's 32-bit
2452 // pc-relative offset may not be large enough to hold the whole
2453 // address.
2454 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002455 // If the callee is a GlobalAddress node (quite common, every direct call
2456 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2457 // it.
2458
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002459 // We should use extra load for direct calls to dllimported functions in
2460 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002461 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002462 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002463 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002464 bool ExtraLoad = false;
2465 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002466
Chris Lattner48a7d022009-07-09 05:02:21 +00002467 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2468 // external symbols most go through the PLT in PIC mode. If the symbol
2469 // has hidden or protected visibility, or if it is static or local, then
2470 // we don't need to use the PLT - we can directly call it.
2471 if (Subtarget->isTargetELF() &&
2472 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002473 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002474 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002475 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002476 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002477 (!Subtarget->getTargetTriple().isMacOSX() ||
2478 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002479 // PC-relative references to external symbols should go through $stub,
2480 // unless we're building with the leopard linker or later, which
2481 // automatically synthesizes these stubs.
2482 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002483 } else if (Subtarget->isPICStyleRIPRel() &&
2484 isa<Function>(GV) &&
2485 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2486 // If the function is marked as non-lazy, generate an indirect call
2487 // which loads from the GOT directly. This avoids runtime overhead
2488 // at the cost of eager binding (and one extra byte of encoding).
2489 OpFlags = X86II::MO_GOTPCREL;
2490 WrapperKind = X86ISD::WrapperRIP;
2491 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002492 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002493
Devang Patel0d881da2010-07-06 22:08:15 +00002494 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002495 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002496
2497 // Add a wrapper if needed.
2498 if (WrapperKind != ISD::DELETED_NODE)
2499 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2500 // Add extra indirection if needed.
2501 if (ExtraLoad)
2502 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2503 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002504 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002505 }
Bill Wendling056292f2008-09-16 21:48:12 +00002506 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002507 unsigned char OpFlags = 0;
2508
Evan Cheng1bf891a2010-12-01 22:59:46 +00002509 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2510 // external symbols should go through the PLT.
2511 if (Subtarget->isTargetELF() &&
2512 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2513 OpFlags = X86II::MO_PLT;
2514 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002515 (!Subtarget->getTargetTriple().isMacOSX() ||
2516 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002517 // PC-relative references to external symbols should go through $stub,
2518 // unless we're building with the leopard linker or later, which
2519 // automatically synthesizes these stubs.
2520 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002521 }
Eric Christopherfd179292009-08-27 18:07:15 +00002522
Chris Lattner48a7d022009-07-09 05:02:21 +00002523 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2524 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002525 }
2526
Chris Lattnerd96d0722007-02-25 06:40:16 +00002527 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002528 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002529 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002530
Evan Chengf22f9b32010-02-06 03:28:46 +00002531 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002532 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2533 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002534 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002535 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002536
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002537 Ops.push_back(Chain);
2538 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002539
Dan Gohman98ca4f22009-08-05 01:29:28 +00002540 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002541 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002542
Gordon Henriksen86737662008-01-05 16:56:59 +00002543 // Add argument registers to the end of the list so that they are known live
2544 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002545 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2546 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2547 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002548
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002549 // Add a register mask operand representing the call-preserved registers.
2550 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2551 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2552 assert(Mask && "Missing call preserved mask for calling convention");
2553 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002554
Gabor Greifba36cb52008-08-28 21:40:38 +00002555 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002556 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002557
Dan Gohman98ca4f22009-08-05 01:29:28 +00002558 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002559 // We used to do:
2560 //// If this is the first return lowered for this function, add the regs
2561 //// to the liveout set for the function.
2562 // This isn't right, although it's probably harmless on x86; liveouts
2563 // should be computed from returns not tail calls. Consider a void
2564 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002565 return DAG.getNode(X86ISD::TC_RETURN, dl,
2566 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002567 }
2568
Dale Johannesenace16102009-02-03 19:33:06 +00002569 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002570 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002571
Chris Lattner2d297092006-05-23 18:50:38 +00002572 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002573 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002574 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2575 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002576 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002577 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002578 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002579 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002580 // pops the hidden struct pointer, so we have to push it back.
2581 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002582 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002583 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002584 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002585 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002586
Gordon Henriksenae636f82008-01-03 16:47:34 +00002587 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002588 if (!IsSibcall) {
2589 Chain = DAG.getCALLSEQ_END(Chain,
2590 DAG.getIntPtrConstant(NumBytes, true),
2591 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2592 true),
2593 InFlag);
2594 InFlag = Chain.getValue(1);
2595 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002596
Chris Lattner3085e152007-02-25 08:59:22 +00002597 // Handle result values, copying them out of physregs into vregs that we
2598 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002599 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2600 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002601}
2602
Evan Cheng25ab6902006-09-08 06:48:29 +00002603
2604//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002605// Fast Calling Convention (tail call) implementation
2606//===----------------------------------------------------------------------===//
2607
2608// Like std call, callee cleans arguments, convention except that ECX is
2609// reserved for storing the tail called function address. Only 2 registers are
2610// free for argument passing (inreg). Tail call optimization is performed
2611// provided:
2612// * tailcallopt is enabled
2613// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002614// On X86_64 architecture with GOT-style position independent code only local
2615// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002616// To keep the stack aligned according to platform abi the function
2617// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2618// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002619// If a tail called function callee has more arguments than the caller the
2620// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002621// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002622// original REtADDR, but before the saved framepointer or the spilled registers
2623// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2624// stack layout:
2625// arg1
2626// arg2
2627// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002628// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002629// move area ]
2630// (possible EBP)
2631// ESI
2632// EDI
2633// local1 ..
2634
2635/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2636/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002637unsigned
2638X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2639 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002640 MachineFunction &MF = DAG.getMachineFunction();
2641 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002642 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002643 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002644 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002645 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002646 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002647 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2648 // Number smaller than 12 so just add the difference.
2649 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2650 } else {
2651 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002652 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002653 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002654 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002655 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002656}
2657
Evan Cheng5f941932010-02-05 02:21:12 +00002658/// MatchingStackOffset - Return true if the given stack call argument is
2659/// already available in the same position (relatively) of the caller's
2660/// incoming argument stack.
2661static
2662bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2663 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2664 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002665 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2666 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002667 if (Arg.getOpcode() == ISD::CopyFromReg) {
2668 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002669 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002670 return false;
2671 MachineInstr *Def = MRI->getVRegDef(VR);
2672 if (!Def)
2673 return false;
2674 if (!Flags.isByVal()) {
2675 if (!TII->isLoadFromStackSlot(Def, FI))
2676 return false;
2677 } else {
2678 unsigned Opcode = Def->getOpcode();
2679 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2680 Def->getOperand(1).isFI()) {
2681 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002682 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002683 } else
2684 return false;
2685 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002686 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2687 if (Flags.isByVal())
2688 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002689 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002690 // define @foo(%struct.X* %A) {
2691 // tail call @bar(%struct.X* byval %A)
2692 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002693 return false;
2694 SDValue Ptr = Ld->getBasePtr();
2695 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2696 if (!FINode)
2697 return false;
2698 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002699 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002700 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002701 FI = FINode->getIndex();
2702 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002703 } else
2704 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002705
Evan Cheng4cae1332010-03-05 08:38:04 +00002706 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002707 if (!MFI->isFixedObjectIndex(FI))
2708 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002709 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002710}
2711
Dan Gohman98ca4f22009-08-05 01:29:28 +00002712/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2713/// for tail call optimization. Targets which want to do tail call
2714/// optimization should implement this function.
2715bool
2716X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002717 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002718 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002719 bool isCalleeStructRet,
2720 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002721 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002722 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002723 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002724 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002725 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002726 CalleeCC != CallingConv::C)
2727 return false;
2728
Evan Cheng7096ae42010-01-29 06:45:59 +00002729 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002730 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002731 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002732 CallingConv::ID CallerCC = CallerF->getCallingConv();
2733 bool CCMatch = CallerCC == CalleeCC;
2734
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002735 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002736 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002737 return true;
2738 return false;
2739 }
2740
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002741 // Look for obvious safe cases to perform tail call optimization that do not
2742 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002743
Evan Cheng2c12cb42010-03-26 16:26:03 +00002744 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2745 // emit a special epilogue.
2746 if (RegInfo->needsStackRealignment(MF))
2747 return false;
2748
Evan Chenga375d472010-03-15 18:54:48 +00002749 // Also avoid sibcall optimization if either caller or callee uses struct
2750 // return semantics.
2751 if (isCalleeStructRet || isCallerStructRet)
2752 return false;
2753
Chad Rosier2416da32011-06-24 21:15:36 +00002754 // An stdcall caller is expected to clean up its arguments; the callee
2755 // isn't going to do that.
2756 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2757 return false;
2758
Chad Rosier871f6642011-05-18 19:59:50 +00002759 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002760 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002761 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002762
2763 // Optimizing for varargs on Win64 is unlikely to be safe without
2764 // additional testing.
2765 if (Subtarget->isTargetWin64())
2766 return false;
2767
Chad Rosier871f6642011-05-18 19:59:50 +00002768 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002769 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002770 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002771
Chad Rosier871f6642011-05-18 19:59:50 +00002772 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2773 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2774 if (!ArgLocs[i].isRegLoc())
2775 return false;
2776 }
2777
Chad Rosier30450e82011-12-22 22:35:21 +00002778 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2779 // stack. Therefore, if it's not used by the call it is not safe to optimize
2780 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002781 bool Unused = false;
2782 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2783 if (!Ins[i].Used) {
2784 Unused = true;
2785 break;
2786 }
2787 }
2788 if (Unused) {
2789 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002790 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002791 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002792 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002793 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002794 CCValAssign &VA = RVLocs[i];
2795 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2796 return false;
2797 }
2798 }
2799
Evan Cheng13617962010-04-30 01:12:32 +00002800 // If the calling conventions do not match, then we'd better make sure the
2801 // results are returned in the same way as what the caller expects.
2802 if (!CCMatch) {
2803 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002804 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002805 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002806 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2807
2808 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002809 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002810 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002811 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2812
2813 if (RVLocs1.size() != RVLocs2.size())
2814 return false;
2815 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2816 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2817 return false;
2818 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2819 return false;
2820 if (RVLocs1[i].isRegLoc()) {
2821 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2822 return false;
2823 } else {
2824 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2825 return false;
2826 }
2827 }
2828 }
2829
Evan Chenga6bff982010-01-30 01:22:00 +00002830 // If the callee takes no arguments then go on to check the results of the
2831 // call.
2832 if (!Outs.empty()) {
2833 // Check if stack adjustment is needed. For now, do not do this if any
2834 // argument is passed on the stack.
2835 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002836 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002837 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002838
2839 // Allocate shadow area for Win64
2840 if (Subtarget->isTargetWin64()) {
2841 CCInfo.AllocateStack(32, 8);
2842 }
2843
Duncan Sands45907662010-10-31 13:21:44 +00002844 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002845 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002846 MachineFunction &MF = DAG.getMachineFunction();
2847 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2848 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002849
2850 // Check if the arguments are already laid out in the right way as
2851 // the caller's fixed stack objects.
2852 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002853 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2854 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002855 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002856 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2857 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002858 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002859 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002860 if (VA.getLocInfo() == CCValAssign::Indirect)
2861 return false;
2862 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002863 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2864 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002865 return false;
2866 }
2867 }
2868 }
Evan Cheng9c044672010-05-29 01:35:22 +00002869
2870 // If the tailcall address may be in a register, then make sure it's
2871 // possible to register allocate for it. In 32-bit, the call address can
2872 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002873 // callee-saved registers are restored. These happen to be the same
2874 // registers used to pass 'inreg' arguments so watch out for those.
2875 if (!Subtarget->is64Bit() &&
2876 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002877 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002878 unsigned NumInRegs = 0;
2879 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2880 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002881 if (!VA.isRegLoc())
2882 continue;
2883 unsigned Reg = VA.getLocReg();
2884 switch (Reg) {
2885 default: break;
2886 case X86::EAX: case X86::EDX: case X86::ECX:
2887 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002888 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002889 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002890 }
2891 }
2892 }
Evan Chenga6bff982010-01-30 01:22:00 +00002893 }
Evan Chengb1712452010-01-27 06:25:16 +00002894
Evan Cheng86809cc2010-02-03 03:28:02 +00002895 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002896}
2897
Dan Gohman3df24e62008-09-03 23:12:08 +00002898FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002899X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2900 const TargetLibraryInfo *libInfo) const {
2901 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002902}
2903
2904
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002905//===----------------------------------------------------------------------===//
2906// Other Lowering Hooks
2907//===----------------------------------------------------------------------===//
2908
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002909static bool MayFoldLoad(SDValue Op) {
2910 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2911}
2912
2913static bool MayFoldIntoStore(SDValue Op) {
2914 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2915}
2916
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002917static bool isTargetShuffle(unsigned Opcode) {
2918 switch(Opcode) {
2919 default: return false;
2920 case X86ISD::PSHUFD:
2921 case X86ISD::PSHUFHW:
2922 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002923 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002924 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002925 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002926 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002927 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002928 case X86ISD::MOVLPS:
2929 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002930 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002931 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002932 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002933 case X86ISD::MOVSS:
2934 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002935 case X86ISD::UNPCKL:
2936 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002937 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002938 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002939 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002940 return true;
2941 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002942}
2943
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002944static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002945 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002946 switch(Opc) {
2947 default: llvm_unreachable("Unknown x86 shuffle node");
2948 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002949 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002950 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002951 return DAG.getNode(Opc, dl, VT, V1);
2952 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002953}
2954
2955static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002956 SDValue V1, unsigned TargetMask,
2957 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002958 switch(Opc) {
2959 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002960 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002961 case X86ISD::PSHUFHW:
2962 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002963 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002964 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002965 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2966 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002967}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002968
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002969static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002970 SDValue V1, SDValue V2, unsigned TargetMask,
2971 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002972 switch(Opc) {
2973 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002974 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002975 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002976 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002977 return DAG.getNode(Opc, dl, VT, V1, V2,
2978 DAG.getConstant(TargetMask, MVT::i8));
2979 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002980}
2981
2982static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2983 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2984 switch(Opc) {
2985 default: llvm_unreachable("Unknown x86 shuffle node");
2986 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002987 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002988 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002989 case X86ISD::MOVLPS:
2990 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002991 case X86ISD::MOVSS:
2992 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002993 case X86ISD::UNPCKL:
2994 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002995 return DAG.getNode(Opc, dl, VT, V1, V2);
2996 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002997}
2998
Dan Gohmand858e902010-04-17 15:26:15 +00002999SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003000 MachineFunction &MF = DAG.getMachineFunction();
3001 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3002 int ReturnAddrIndex = FuncInfo->getRAIndex();
3003
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003004 if (ReturnAddrIndex == 0) {
3005 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00003006 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00003007 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003008 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00003009 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003010 }
3011
Evan Cheng25ab6902006-09-08 06:48:29 +00003012 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003013}
3014
3015
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003016bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3017 bool hasSymbolicDisplacement) {
3018 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003019 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003020 return false;
3021
3022 // If we don't have a symbolic displacement - we don't have any extra
3023 // restrictions.
3024 if (!hasSymbolicDisplacement)
3025 return true;
3026
3027 // FIXME: Some tweaks might be needed for medium code model.
3028 if (M != CodeModel::Small && M != CodeModel::Kernel)
3029 return false;
3030
3031 // For small code model we assume that latest object is 16MB before end of 31
3032 // bits boundary. We may also accept pretty large negative constants knowing
3033 // that all objects are in the positive half of address space.
3034 if (M == CodeModel::Small && Offset < 16*1024*1024)
3035 return true;
3036
3037 // For kernel code model we know that all object resist in the negative half
3038 // of 32bits address space. We may not accept negative offsets, since they may
3039 // be just off and we may accept pretty large positive ones.
3040 if (M == CodeModel::Kernel && Offset > 0)
3041 return true;
3042
3043 return false;
3044}
3045
Evan Chengef41ff62011-06-23 17:54:54 +00003046/// isCalleePop - Determines whether the callee is required to pop its
3047/// own arguments. Callee pop is necessary to support tail calls.
3048bool X86::isCalleePop(CallingConv::ID CallingConv,
3049 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3050 if (IsVarArg)
3051 return false;
3052
3053 switch (CallingConv) {
3054 default:
3055 return false;
3056 case CallingConv::X86_StdCall:
3057 return !is64Bit;
3058 case CallingConv::X86_FastCall:
3059 return !is64Bit;
3060 case CallingConv::X86_ThisCall:
3061 return !is64Bit;
3062 case CallingConv::Fast:
3063 return TailCallOpt;
3064 case CallingConv::GHC:
3065 return TailCallOpt;
3066 }
3067}
3068
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003069/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3070/// specific condition code, returning the condition code and the LHS/RHS of the
3071/// comparison to make.
3072static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3073 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003074 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003075 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3076 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3077 // X > -1 -> X == 0, jump !sign.
3078 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003079 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003080 }
3081 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003082 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003083 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003084 }
3085 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003086 // X < 1 -> X <= 0
3087 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003088 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003089 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003090 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003091
Evan Chengd9558e02006-01-06 00:43:03 +00003092 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003093 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003094 case ISD::SETEQ: return X86::COND_E;
3095 case ISD::SETGT: return X86::COND_G;
3096 case ISD::SETGE: return X86::COND_GE;
3097 case ISD::SETLT: return X86::COND_L;
3098 case ISD::SETLE: return X86::COND_LE;
3099 case ISD::SETNE: return X86::COND_NE;
3100 case ISD::SETULT: return X86::COND_B;
3101 case ISD::SETUGT: return X86::COND_A;
3102 case ISD::SETULE: return X86::COND_BE;
3103 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003104 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003105 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003106
Chris Lattner4c78e022008-12-23 23:42:27 +00003107 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003108
Chris Lattner4c78e022008-12-23 23:42:27 +00003109 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003110 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3111 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003112 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3113 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003114 }
3115
Chris Lattner4c78e022008-12-23 23:42:27 +00003116 switch (SetCCOpcode) {
3117 default: break;
3118 case ISD::SETOLT:
3119 case ISD::SETOLE:
3120 case ISD::SETUGT:
3121 case ISD::SETUGE:
3122 std::swap(LHS, RHS);
3123 break;
3124 }
3125
3126 // On a floating point condition, the flags are set as follows:
3127 // ZF PF CF op
3128 // 0 | 0 | 0 | X > Y
3129 // 0 | 0 | 1 | X < Y
3130 // 1 | 0 | 0 | X == Y
3131 // 1 | 1 | 1 | unordered
3132 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003133 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003134 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003135 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003136 case ISD::SETOLT: // flipped
3137 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003138 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003139 case ISD::SETOLE: // flipped
3140 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003141 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003142 case ISD::SETUGT: // flipped
3143 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003144 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003145 case ISD::SETUGE: // flipped
3146 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003147 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003148 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003149 case ISD::SETNE: return X86::COND_NE;
3150 case ISD::SETUO: return X86::COND_P;
3151 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003152 case ISD::SETOEQ:
3153 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003154 }
Evan Chengd9558e02006-01-06 00:43:03 +00003155}
3156
Evan Cheng4a460802006-01-11 00:33:36 +00003157/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3158/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003159/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003160static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003161 switch (X86CC) {
3162 default:
3163 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003164 case X86::COND_B:
3165 case X86::COND_BE:
3166 case X86::COND_E:
3167 case X86::COND_P:
3168 case X86::COND_A:
3169 case X86::COND_AE:
3170 case X86::COND_NE:
3171 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003172 return true;
3173 }
3174}
3175
Evan Chengeb2f9692009-10-27 19:56:55 +00003176/// isFPImmLegal - Returns true if the target can instruction select the
3177/// specified FP immediate natively. If false, the legalizer will
3178/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003179bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003180 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3181 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3182 return true;
3183 }
3184 return false;
3185}
3186
Nate Begeman9008ca62009-04-27 18:41:29 +00003187/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3188/// the specified range (L, H].
3189static bool isUndefOrInRange(int Val, int Low, int Hi) {
3190 return (Val < 0) || (Val >= Low && Val < Hi);
3191}
3192
3193/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3194/// specified value.
3195static bool isUndefOrEqual(int Val, int CmpVal) {
3196 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003197 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003199}
3200
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003201/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003202/// from position Pos and ending in Pos+Size, falls within the specified
3203/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003204static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003205 unsigned Pos, unsigned Size, int Low) {
3206 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003207 if (!isUndefOrEqual(Mask[i], Low))
3208 return false;
3209 return true;
3210}
3211
Nate Begeman9008ca62009-04-27 18:41:29 +00003212/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3213/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3214/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003215static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003216 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003218 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003219 return (Mask[0] < 2 && Mask[1] < 2);
3220 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003221}
3222
Nate Begeman9008ca62009-04-27 18:41:29 +00003223/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3224/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003225static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3226 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003227 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003228
Nate Begeman9008ca62009-04-27 18:41:29 +00003229 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003230 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3231 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003232
Evan Cheng506d3df2006-03-29 23:07:14 +00003233 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003234 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003235 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003236 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003237
Craig Toppera9a568a2012-05-02 08:03:44 +00003238 if (VT == MVT::v16i16) {
3239 // Lower quadword copied in order or undef.
3240 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3241 return false;
3242
3243 // Upper quadword shuffled.
3244 for (unsigned i = 12; i != 16; ++i)
3245 if (!isUndefOrInRange(Mask[i], 12, 16))
3246 return false;
3247 }
3248
Evan Cheng506d3df2006-03-29 23:07:14 +00003249 return true;
3250}
3251
Nate Begeman9008ca62009-04-27 18:41:29 +00003252/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3253/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003254static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3255 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003256 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003257
Rafael Espindola15684b22009-04-24 12:40:33 +00003258 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003259 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3260 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003261
Rafael Espindola15684b22009-04-24 12:40:33 +00003262 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003263 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003264 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003265 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003266
Craig Toppera9a568a2012-05-02 08:03:44 +00003267 if (VT == MVT::v16i16) {
3268 // Upper quadword copied in order.
3269 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3270 return false;
3271
3272 // Lower quadword shuffled.
3273 for (unsigned i = 8; i != 12; ++i)
3274 if (!isUndefOrInRange(Mask[i], 8, 12))
3275 return false;
3276 }
3277
Rafael Espindola15684b22009-04-24 12:40:33 +00003278 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003279}
3280
Nate Begemana09008b2009-10-19 02:17:23 +00003281/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3282/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003283static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3284 const X86Subtarget *Subtarget) {
3285 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3286 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003287 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003288
Craig Topper0e2037b2012-01-20 05:53:00 +00003289 unsigned NumElts = VT.getVectorNumElements();
3290 unsigned NumLanes = VT.getSizeInBits()/128;
3291 unsigned NumLaneElts = NumElts/NumLanes;
3292
3293 // Do not handle 64-bit element shuffles with palignr.
3294 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003295 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003296
Craig Topper0e2037b2012-01-20 05:53:00 +00003297 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3298 unsigned i;
3299 for (i = 0; i != NumLaneElts; ++i) {
3300 if (Mask[i+l] >= 0)
3301 break;
3302 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003303
Craig Topper0e2037b2012-01-20 05:53:00 +00003304 // Lane is all undef, go to next lane
3305 if (i == NumLaneElts)
3306 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003307
Craig Topper0e2037b2012-01-20 05:53:00 +00003308 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003309
Craig Topper0e2037b2012-01-20 05:53:00 +00003310 // Make sure its in this lane in one of the sources
3311 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3312 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003313 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003314
3315 // If not lane 0, then we must match lane 0
3316 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3317 return false;
3318
3319 // Correct second source to be contiguous with first source
3320 if (Start >= (int)NumElts)
3321 Start -= NumElts - NumLaneElts;
3322
3323 // Make sure we're shifting in the right direction.
3324 if (Start <= (int)(i+l))
3325 return false;
3326
3327 Start -= i;
3328
3329 // Check the rest of the elements to see if they are consecutive.
3330 for (++i; i != NumLaneElts; ++i) {
3331 int Idx = Mask[i+l];
3332
3333 // Make sure its in this lane
3334 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3335 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3336 return false;
3337
3338 // If not lane 0, then we must match lane 0
3339 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3340 return false;
3341
3342 if (Idx >= (int)NumElts)
3343 Idx -= NumElts - NumLaneElts;
3344
3345 if (!isUndefOrEqual(Idx, Start+i))
3346 return false;
3347
3348 }
Nate Begemana09008b2009-10-19 02:17:23 +00003349 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003350
Nate Begemana09008b2009-10-19 02:17:23 +00003351 return true;
3352}
3353
Craig Topper1a7700a2012-01-19 08:19:12 +00003354/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3355/// the two vector operands have swapped position.
3356static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3357 unsigned NumElems) {
3358 for (unsigned i = 0; i != NumElems; ++i) {
3359 int idx = Mask[i];
3360 if (idx < 0)
3361 continue;
3362 else if (idx < (int)NumElems)
3363 Mask[i] = idx + NumElems;
3364 else
3365 Mask[i] = idx - NumElems;
3366 }
3367}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003368
Craig Topper1a7700a2012-01-19 08:19:12 +00003369/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3370/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3371/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3372/// reverse of what x86 shuffles want.
3373static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3374 bool Commuted = false) {
3375 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003376 return false;
3377
Craig Topper1a7700a2012-01-19 08:19:12 +00003378 unsigned NumElems = VT.getVectorNumElements();
3379 unsigned NumLanes = VT.getSizeInBits()/128;
3380 unsigned NumLaneElems = NumElems/NumLanes;
3381
3382 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003383 return false;
3384
3385 // VSHUFPSY divides the resulting vector into 4 chunks.
3386 // The sources are also splitted into 4 chunks, and each destination
3387 // chunk must come from a different source chunk.
3388 //
3389 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3390 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3391 //
3392 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3393 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3394 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003395 // VSHUFPDY divides the resulting vector into 4 chunks.
3396 // The sources are also splitted into 4 chunks, and each destination
3397 // chunk must come from a different source chunk.
3398 //
3399 // SRC1 => X3 X2 X1 X0
3400 // SRC2 => Y3 Y2 Y1 Y0
3401 //
3402 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3403 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003404 unsigned HalfLaneElems = NumLaneElems/2;
3405 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3406 for (unsigned i = 0; i != NumLaneElems; ++i) {
3407 int Idx = Mask[i+l];
3408 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3409 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3410 return false;
3411 // For VSHUFPSY, the mask of the second half must be the same as the
3412 // first but with the appropriate offsets. This works in the same way as
3413 // VPERMILPS works with masks.
3414 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3415 continue;
3416 if (!isUndefOrEqual(Idx, Mask[i]+l))
3417 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003418 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003419 }
3420
3421 return true;
3422}
3423
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003424/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3425/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003426static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003427 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003428 return false;
3429
Craig Topper7a9a28b2012-08-12 02:23:29 +00003430 unsigned NumElems = VT.getVectorNumElements();
3431
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003432 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003433 return false;
3434
Evan Cheng2064a2b2006-03-28 06:50:32 +00003435 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003436 return isUndefOrEqual(Mask[0], 6) &&
3437 isUndefOrEqual(Mask[1], 7) &&
3438 isUndefOrEqual(Mask[2], 2) &&
3439 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003440}
3441
Nate Begeman0b10b912009-11-07 23:17:15 +00003442/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3443/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3444/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003445static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003446 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003447 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003448
Craig Topper7a9a28b2012-08-12 02:23:29 +00003449 unsigned NumElems = VT.getVectorNumElements();
3450
Nate Begeman0b10b912009-11-07 23:17:15 +00003451 if (NumElems != 4)
3452 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003453
Craig Topperdd637ae2012-02-19 05:41:45 +00003454 return isUndefOrEqual(Mask[0], 2) &&
3455 isUndefOrEqual(Mask[1], 3) &&
3456 isUndefOrEqual(Mask[2], 2) &&
3457 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003458}
3459
Evan Cheng5ced1d82006-04-06 23:23:56 +00003460/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3461/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003462static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003463 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003464 return false;
3465
Craig Topperdd637ae2012-02-19 05:41:45 +00003466 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003467
Evan Cheng5ced1d82006-04-06 23:23:56 +00003468 if (NumElems != 2 && NumElems != 4)
3469 return false;
3470
Chad Rosier238ae312012-04-30 17:47:15 +00003471 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003472 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003473 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003474
Chad Rosier238ae312012-04-30 17:47:15 +00003475 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003476 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003477 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003478
3479 return true;
3480}
3481
Nate Begeman0b10b912009-11-07 23:17:15 +00003482/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3483/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003484static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003485 if (!VT.is128BitVector())
3486 return false;
3487
Craig Topperdd637ae2012-02-19 05:41:45 +00003488 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003489
Craig Topper7a9a28b2012-08-12 02:23:29 +00003490 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003491 return false;
3492
Chad Rosier238ae312012-04-30 17:47:15 +00003493 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003494 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003495 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003496
Chad Rosier238ae312012-04-30 17:47:15 +00003497 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3498 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003499 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003500
3501 return true;
3502}
3503
Elena Demikhovsky15963732012-06-26 08:04:10 +00003504//
3505// Some special combinations that can be optimized.
3506//
3507static
3508SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3509 SelectionDAG &DAG) {
3510 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003511 DebugLoc dl = SVOp->getDebugLoc();
3512
3513 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3514 return SDValue();
3515
3516 ArrayRef<int> Mask = SVOp->getMask();
3517
3518 // These are the special masks that may be optimized.
3519 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3520 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3521 bool MatchEvenMask = true;
3522 bool MatchOddMask = true;
3523 for (int i=0; i<8; ++i) {
3524 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3525 MatchEvenMask = false;
3526 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3527 MatchOddMask = false;
3528 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003529
Elena Demikhovsky32510202012-09-04 12:49:02 +00003530 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003531 return SDValue();
Elena Demikhovsky32510202012-09-04 12:49:02 +00003532
Elena Demikhovsky15963732012-06-26 08:04:10 +00003533 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3534
Elena Demikhovsky32510202012-09-04 12:49:02 +00003535 SDValue Op0 = SVOp->getOperand(0);
3536 SDValue Op1 = SVOp->getOperand(1);
3537
3538 if (MatchEvenMask) {
3539 // Shift the second operand right to 32 bits.
3540 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3541 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3542 } else {
3543 // Shift the first operand left to 32 bits.
3544 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3545 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3546 }
3547 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3548 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003549}
3550
Evan Cheng0038e592006-03-28 00:39:58 +00003551/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3552/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003553static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003554 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003555 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003556
3557 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3558 "Unsupported vector type for unpckh");
3559
Craig Topper6347e862011-11-21 06:57:39 +00003560 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003561 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003562 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003563
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003564 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3565 // independently on 128-bit lanes.
3566 unsigned NumLanes = VT.getSizeInBits()/128;
3567 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003568
Craig Topper94438ba2011-12-16 08:06:31 +00003569 for (unsigned l = 0; l != NumLanes; ++l) {
3570 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3571 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003572 i += 2, ++j) {
3573 int BitI = Mask[i];
3574 int BitI1 = Mask[i+1];
3575 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003576 return false;
David Greenea20244d2011-03-02 17:23:43 +00003577 if (V2IsSplat) {
3578 if (!isUndefOrEqual(BitI1, NumElts))
3579 return false;
3580 } else {
3581 if (!isUndefOrEqual(BitI1, j + NumElts))
3582 return false;
3583 }
Evan Cheng39623da2006-04-20 08:58:49 +00003584 }
Evan Cheng0038e592006-03-28 00:39:58 +00003585 }
David Greenea20244d2011-03-02 17:23:43 +00003586
Evan Cheng0038e592006-03-28 00:39:58 +00003587 return true;
3588}
3589
Evan Cheng4fcb9222006-03-28 02:43:26 +00003590/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3591/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003592static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003593 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003594 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003595
3596 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3597 "Unsupported vector type for unpckh");
3598
Craig Topper6347e862011-11-21 06:57:39 +00003599 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003600 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003601 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003602
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003603 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3604 // independently on 128-bit lanes.
3605 unsigned NumLanes = VT.getSizeInBits()/128;
3606 unsigned NumLaneElts = NumElts/NumLanes;
3607
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003608 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003609 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3610 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003611 int BitI = Mask[i];
3612 int BitI1 = Mask[i+1];
3613 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003614 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003615 if (V2IsSplat) {
3616 if (isUndefOrEqual(BitI1, NumElts))
3617 return false;
3618 } else {
3619 if (!isUndefOrEqual(BitI1, j+NumElts))
3620 return false;
3621 }
Evan Cheng39623da2006-04-20 08:58:49 +00003622 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003623 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003624 return true;
3625}
3626
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003627/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3628/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3629/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003630static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003631 bool HasAVX2) {
3632 unsigned NumElts = VT.getVectorNumElements();
3633
3634 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3635 "Unsupported vector type for unpckh");
3636
3637 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3638 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003639 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003640
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003641 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3642 // FIXME: Need a better way to get rid of this, there's no latency difference
3643 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3644 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003645 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003646 return false;
3647
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003648 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3649 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003650 unsigned NumLanes = VT.getSizeInBits()/128;
3651 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003652
Craig Topper94438ba2011-12-16 08:06:31 +00003653 for (unsigned l = 0; l != NumLanes; ++l) {
3654 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3655 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003656 i += 2, ++j) {
3657 int BitI = Mask[i];
3658 int BitI1 = Mask[i+1];
3659
3660 if (!isUndefOrEqual(BitI, j))
3661 return false;
3662 if (!isUndefOrEqual(BitI1, j))
3663 return false;
3664 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003665 }
David Greenea20244d2011-03-02 17:23:43 +00003666
Rafael Espindola15684b22009-04-24 12:40:33 +00003667 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003668}
3669
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003670/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3671/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3672/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003673static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003674 unsigned NumElts = VT.getVectorNumElements();
3675
3676 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3677 "Unsupported vector type for unpckh");
3678
3679 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3680 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003681 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003682
Craig Topper94438ba2011-12-16 08:06:31 +00003683 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3684 // independently on 128-bit lanes.
3685 unsigned NumLanes = VT.getSizeInBits()/128;
3686 unsigned NumLaneElts = NumElts/NumLanes;
3687
3688 for (unsigned l = 0; l != NumLanes; ++l) {
3689 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3690 i != (l+1)*NumLaneElts; i += 2, ++j) {
3691 int BitI = Mask[i];
3692 int BitI1 = Mask[i+1];
3693 if (!isUndefOrEqual(BitI, j))
3694 return false;
3695 if (!isUndefOrEqual(BitI1, j))
3696 return false;
3697 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003698 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003699 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003700}
3701
Evan Cheng017dcc62006-04-21 01:05:10 +00003702/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3703/// specifies a shuffle of elements that is suitable for input to MOVSS,
3704/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003705static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003706 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003707 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003708 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003709 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003710
Craig Topperc612d792012-01-02 09:17:37 +00003711 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003712
Nate Begeman9008ca62009-04-27 18:41:29 +00003713 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003714 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003715
Craig Topperc612d792012-01-02 09:17:37 +00003716 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003717 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003718 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003719
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003720 return true;
3721}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003722
Craig Topper70b883b2011-11-28 10:14:51 +00003723/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003724/// as permutations between 128-bit chunks or halves. As an example: this
3725/// shuffle bellow:
3726/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3727/// The first half comes from the second half of V1 and the second half from the
3728/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003729static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003730 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003731 return false;
3732
3733 // The shuffle result is divided into half A and half B. In total the two
3734 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3735 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003736 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003737 bool MatchA = false, MatchB = false;
3738
3739 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003740 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003741 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3742 MatchA = true;
3743 break;
3744 }
3745 }
3746
3747 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003748 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003749 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3750 MatchB = true;
3751 break;
3752 }
3753 }
3754
3755 return MatchA && MatchB;
3756}
3757
Craig Topper70b883b2011-11-28 10:14:51 +00003758/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3759/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003760static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003761 EVT VT = SVOp->getValueType(0);
3762
Craig Topperc612d792012-01-02 09:17:37 +00003763 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003764
Craig Topperc612d792012-01-02 09:17:37 +00003765 unsigned FstHalf = 0, SndHalf = 0;
3766 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003767 if (SVOp->getMaskElt(i) > 0) {
3768 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3769 break;
3770 }
3771 }
Craig Topperc612d792012-01-02 09:17:37 +00003772 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003773 if (SVOp->getMaskElt(i) > 0) {
3774 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3775 break;
3776 }
3777 }
3778
3779 return (FstHalf | (SndHalf << 4));
3780}
3781
Craig Topper70b883b2011-11-28 10:14:51 +00003782/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003783/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3784/// Note that VPERMIL mask matching is different depending whether theunderlying
3785/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3786/// to the same elements of the low, but to the higher half of the source.
3787/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003788/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003789static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003790 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003791 return false;
3792
Craig Topperc612d792012-01-02 09:17:37 +00003793 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003794 // Only match 256-bit with 32/64-bit types
3795 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003796 return false;
3797
Craig Topperc612d792012-01-02 09:17:37 +00003798 unsigned NumLanes = VT.getSizeInBits()/128;
3799 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003800 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003801 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003802 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003803 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003804 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003805 continue;
3806 // VPERMILPS handling
3807 if (Mask[i] < 0)
3808 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003809 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003810 return false;
3811 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003812 }
3813
3814 return true;
3815}
3816
Craig Topper5aaffa82012-02-19 02:53:47 +00003817/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003818/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003819/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003820static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003821 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003822 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003823 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003824
3825 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003826 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003827 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003828
Nate Begeman9008ca62009-04-27 18:41:29 +00003829 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003830 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003831
Craig Topperc612d792012-01-02 09:17:37 +00003832 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003833 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3834 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3835 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003836 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003837
Evan Cheng39623da2006-04-20 08:58:49 +00003838 return true;
3839}
3840
Evan Chengd9539472006-04-14 21:59:03 +00003841/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3842/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003843/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003844static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003845 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003846 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003847 return false;
3848
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003849 unsigned NumElems = VT.getVectorNumElements();
3850
3851 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3852 (VT.getSizeInBits() == 256 && NumElems != 8))
3853 return false;
3854
3855 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003856 for (unsigned i = 0; i != NumElems; i += 2)
3857 if (!isUndefOrEqual(Mask[i], i+1) ||
3858 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003859 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003860
3861 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003862}
3863
3864/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3865/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003866/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003867static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003868 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003869 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003870 return false;
3871
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003872 unsigned NumElems = VT.getVectorNumElements();
3873
3874 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3875 (VT.getSizeInBits() == 256 && NumElems != 8))
3876 return false;
3877
3878 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003879 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003880 if (!isUndefOrEqual(Mask[i], i) ||
3881 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003882 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003883
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003884 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003885}
3886
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003887/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3888/// specifies a shuffle of elements that is suitable for input to 256-bit
3889/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003890static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003891 if (!HasAVX || !VT.is256BitVector())
3892 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003893
Craig Topper7a9a28b2012-08-12 02:23:29 +00003894 unsigned NumElts = VT.getVectorNumElements();
3895 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003896 return false;
3897
Craig Topperc612d792012-01-02 09:17:37 +00003898 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003899 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003900 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003901 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003902 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003903 return false;
3904 return true;
3905}
3906
Evan Cheng0b457f02008-09-25 20:50:48 +00003907/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003908/// specifies a shuffle of elements that is suitable for input to 128-bit
3909/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003910static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003911 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003912 return false;
3913
Craig Topperc612d792012-01-02 09:17:37 +00003914 unsigned e = VT.getVectorNumElements() / 2;
3915 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003916 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003917 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003918 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003919 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003920 return false;
3921 return true;
3922}
3923
David Greenec38a03e2011-02-03 15:50:00 +00003924/// isVEXTRACTF128Index - Return true if the specified
3925/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3926/// suitable for input to VEXTRACTF128.
3927bool X86::isVEXTRACTF128Index(SDNode *N) {
3928 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3929 return false;
3930
3931 // The index should be aligned on a 128-bit boundary.
3932 uint64_t Index =
3933 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3934
3935 unsigned VL = N->getValueType(0).getVectorNumElements();
3936 unsigned VBits = N->getValueType(0).getSizeInBits();
3937 unsigned ElSize = VBits / VL;
3938 bool Result = (Index * ElSize) % 128 == 0;
3939
3940 return Result;
3941}
3942
David Greeneccacdc12011-02-04 16:08:29 +00003943/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3944/// operand specifies a subvector insert that is suitable for input to
3945/// VINSERTF128.
3946bool X86::isVINSERTF128Index(SDNode *N) {
3947 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3948 return false;
3949
3950 // The index should be aligned on a 128-bit boundary.
3951 uint64_t Index =
3952 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3953
3954 unsigned VL = N->getValueType(0).getVectorNumElements();
3955 unsigned VBits = N->getValueType(0).getSizeInBits();
3956 unsigned ElSize = VBits / VL;
3957 bool Result = (Index * ElSize) % 128 == 0;
3958
3959 return Result;
3960}
3961
Evan Cheng63d33002006-03-22 08:01:21 +00003962/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003963/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003964/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003965static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003966 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003967
Craig Topper1a7700a2012-01-19 08:19:12 +00003968 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3969 "Unsupported vector type for PSHUF/SHUFP");
3970
3971 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3972 // independently on 128-bit lanes.
3973 unsigned NumElts = VT.getVectorNumElements();
3974 unsigned NumLanes = VT.getSizeInBits()/128;
3975 unsigned NumLaneElts = NumElts/NumLanes;
3976
3977 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3978 "Only supports 2 or 4 elements per lane");
3979
3980 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003981 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003982 for (unsigned i = 0; i != NumElts; ++i) {
3983 int Elt = N->getMaskElt(i);
3984 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003985 Elt &= NumLaneElts - 1;
3986 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003987 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003988 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003989
Evan Cheng63d33002006-03-22 08:01:21 +00003990 return Mask;
3991}
3992
Evan Cheng506d3df2006-03-29 23:07:14 +00003993/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003994/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00003995static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00003996 EVT VT = N->getValueType(0);
3997
3998 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
3999 "Unsupported vector type for PSHUFHW");
4000
4001 unsigned NumElts = VT.getVectorNumElements();
4002
Evan Cheng506d3df2006-03-29 23:07:14 +00004003 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004004 for (unsigned l = 0; l != NumElts; l += 8) {
4005 // 8 nodes per lane, but we only care about the last 4.
4006 for (unsigned i = 0; i < 4; ++i) {
4007 int Elt = N->getMaskElt(l+i+4);
4008 if (Elt < 0) continue;
4009 Elt &= 0x3; // only 2-bits.
4010 Mask |= Elt << (i * 2);
4011 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004012 }
Craig Topper6b28d352012-05-03 07:12:59 +00004013
Evan Cheng506d3df2006-03-29 23:07:14 +00004014 return Mask;
4015}
4016
4017/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004018/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004019static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004020 EVT VT = N->getValueType(0);
4021
4022 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4023 "Unsupported vector type for PSHUFHW");
4024
4025 unsigned NumElts = VT.getVectorNumElements();
4026
Evan Cheng506d3df2006-03-29 23:07:14 +00004027 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004028 for (unsigned l = 0; l != NumElts; l += 8) {
4029 // 8 nodes per lane, but we only care about the first 4.
4030 for (unsigned i = 0; i < 4; ++i) {
4031 int Elt = N->getMaskElt(l+i);
4032 if (Elt < 0) continue;
4033 Elt &= 0x3; // only 2-bits
4034 Mask |= Elt << (i * 2);
4035 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004036 }
Craig Topper6b28d352012-05-03 07:12:59 +00004037
Evan Cheng506d3df2006-03-29 23:07:14 +00004038 return Mask;
4039}
4040
Nate Begemana09008b2009-10-19 02:17:23 +00004041/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4042/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004043static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4044 EVT VT = SVOp->getValueType(0);
4045 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004046
Craig Topper0e2037b2012-01-20 05:53:00 +00004047 unsigned NumElts = VT.getVectorNumElements();
4048 unsigned NumLanes = VT.getSizeInBits()/128;
4049 unsigned NumLaneElts = NumElts/NumLanes;
4050
4051 int Val = 0;
4052 unsigned i;
4053 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004054 Val = SVOp->getMaskElt(i);
4055 if (Val >= 0)
4056 break;
4057 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004058 if (Val >= (int)NumElts)
4059 Val -= NumElts - NumLaneElts;
4060
Eli Friedman63f8dde2011-07-25 21:36:45 +00004061 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004062 return (Val - i) * EltSize;
4063}
4064
David Greenec38a03e2011-02-03 15:50:00 +00004065/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4066/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4067/// instructions.
4068unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4069 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4070 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4071
4072 uint64_t Index =
4073 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4074
4075 EVT VecVT = N->getOperand(0).getValueType();
4076 EVT ElVT = VecVT.getVectorElementType();
4077
4078 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004079 return Index / NumElemsPerChunk;
4080}
4081
David Greeneccacdc12011-02-04 16:08:29 +00004082/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4083/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4084/// instructions.
4085unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4086 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4087 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4088
4089 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004090 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004091
4092 EVT VecVT = N->getValueType(0);
4093 EVT ElVT = VecVT.getVectorElementType();
4094
4095 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004096 return Index / NumElemsPerChunk;
4097}
4098
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004099/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4100/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4101/// Handles 256-bit.
4102static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4103 EVT VT = N->getValueType(0);
4104
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004105 unsigned NumElts = VT.getVectorNumElements();
4106
Craig Topper095c5282012-04-15 23:48:57 +00004107 assert((VT.is256BitVector() && NumElts == 4) &&
4108 "Unsupported vector type for VPERMQ/VPERMPD");
4109
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004110 unsigned Mask = 0;
4111 for (unsigned i = 0; i != NumElts; ++i) {
4112 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004113 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004114 continue;
4115 Mask |= Elt << (i*2);
4116 }
4117
4118 return Mask;
4119}
Evan Cheng37b73872009-07-30 08:33:02 +00004120/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4121/// constant +0.0.
4122bool X86::isZeroNode(SDValue Elt) {
4123 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004124 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004125 (isa<ConstantFPSDNode>(Elt) &&
4126 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4127}
4128
Nate Begeman9008ca62009-04-27 18:41:29 +00004129/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4130/// their permute mask.
4131static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4132 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004133 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004134 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004135 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004136
Nate Begeman5a5ca152009-04-29 05:20:52 +00004137 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004138 int Idx = SVOp->getMaskElt(i);
4139 if (Idx >= 0) {
4140 if (Idx < (int)NumElems)
4141 Idx += NumElems;
4142 else
4143 Idx -= NumElems;
4144 }
4145 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004146 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004147 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4148 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004149}
4150
Evan Cheng533a0aa2006-04-19 20:35:22 +00004151/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4152/// match movhlps. The lower half elements should come from upper half of
4153/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004154/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004155static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004156 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004157 return false;
4158 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004159 return false;
4160 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004161 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004162 return false;
4163 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004164 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004165 return false;
4166 return true;
4167}
4168
Evan Cheng5ced1d82006-04-06 23:23:56 +00004169/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004170/// is promoted to a vector. It also returns the LoadSDNode by reference if
4171/// required.
4172static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004173 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4174 return false;
4175 N = N->getOperand(0).getNode();
4176 if (!ISD::isNON_EXTLoad(N))
4177 return false;
4178 if (LD)
4179 *LD = cast<LoadSDNode>(N);
4180 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004181}
4182
Dan Gohman65fd6562011-11-03 21:49:52 +00004183// Test whether the given value is a vector value which will be legalized
4184// into a load.
4185static bool WillBeConstantPoolLoad(SDNode *N) {
4186 if (N->getOpcode() != ISD::BUILD_VECTOR)
4187 return false;
4188
4189 // Check for any non-constant elements.
4190 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4191 switch (N->getOperand(i).getNode()->getOpcode()) {
4192 case ISD::UNDEF:
4193 case ISD::ConstantFP:
4194 case ISD::Constant:
4195 break;
4196 default:
4197 return false;
4198 }
4199
4200 // Vectors of all-zeros and all-ones are materialized with special
4201 // instructions rather than being loaded.
4202 return !ISD::isBuildVectorAllZeros(N) &&
4203 !ISD::isBuildVectorAllOnes(N);
4204}
4205
Evan Cheng533a0aa2006-04-19 20:35:22 +00004206/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4207/// match movlp{s|d}. The lower half elements should come from lower half of
4208/// V1 (and in order), and the upper half elements should come from the upper
4209/// half of V2 (and in order). And since V1 will become the source of the
4210/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004211static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004212 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004213 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004214 return false;
4215
Evan Cheng466685d2006-10-09 20:57:25 +00004216 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004217 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004218 // Is V2 is a vector load, don't do this transformation. We will try to use
4219 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004220 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004221 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004222
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004223 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004224
Evan Cheng533a0aa2006-04-19 20:35:22 +00004225 if (NumElems != 2 && NumElems != 4)
4226 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004227 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004228 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004229 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004230 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004231 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004232 return false;
4233 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004234}
4235
Evan Cheng39623da2006-04-20 08:58:49 +00004236/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4237/// all the same.
4238static bool isSplatVector(SDNode *N) {
4239 if (N->getOpcode() != ISD::BUILD_VECTOR)
4240 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004241
Dan Gohman475871a2008-07-27 21:46:04 +00004242 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004243 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4244 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004245 return false;
4246 return true;
4247}
4248
Evan Cheng213d2cf2007-05-17 18:45:50 +00004249/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004250/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004251/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004252static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004253 SDValue V1 = N->getOperand(0);
4254 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004255 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4256 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004257 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004258 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004259 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004260 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4261 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004262 if (Opc != ISD::BUILD_VECTOR ||
4263 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004264 return false;
4265 } else if (Idx >= 0) {
4266 unsigned Opc = V1.getOpcode();
4267 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4268 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004269 if (Opc != ISD::BUILD_VECTOR ||
4270 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004271 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004272 }
4273 }
4274 return true;
4275}
4276
4277/// getZeroVector - Returns a vector of specified type with all zero elements.
4278///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004279static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004280 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004281 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004282 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004283
Dale Johannesen0488fb62010-09-30 23:57:10 +00004284 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004285 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004286 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004287 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004288 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004289 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4290 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4291 } else { // SSE1
4292 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4293 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4294 }
Craig Topper9d352402012-04-23 07:24:41 +00004295 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004296 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004297 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4298 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4299 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4300 } else {
4301 // 256-bit logic and arithmetic instructions in AVX are all
4302 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4303 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4304 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4305 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4306 }
Craig Topper9d352402012-04-23 07:24:41 +00004307 } else
4308 llvm_unreachable("Unexpected vector type");
4309
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004310 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004311}
4312
Chris Lattner8a594482007-11-25 00:24:49 +00004313/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004314/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4315/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4316/// Then bitcast to their original type, ensuring they get CSE'd.
4317static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4318 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004319 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004320 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004321
Owen Anderson825b72b2009-08-11 20:47:22 +00004322 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004323 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004324 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004325 if (HasAVX2) { // AVX2
4326 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4327 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4328 } else { // AVX
4329 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004330 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004331 }
Craig Topper9d352402012-04-23 07:24:41 +00004332 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004333 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004334 } else
4335 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004336
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004337 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004338}
4339
Evan Cheng39623da2006-04-20 08:58:49 +00004340/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4341/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004342static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004343 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004344 if (Mask[i] > (int)NumElems) {
4345 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004346 }
Evan Cheng39623da2006-04-20 08:58:49 +00004347 }
Evan Cheng39623da2006-04-20 08:58:49 +00004348}
4349
Evan Cheng017dcc62006-04-21 01:05:10 +00004350/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4351/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004352static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004353 SDValue V2) {
4354 unsigned NumElems = VT.getVectorNumElements();
4355 SmallVector<int, 8> Mask;
4356 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004357 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004358 Mask.push_back(i);
4359 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004360}
4361
Nate Begeman9008ca62009-04-27 18:41:29 +00004362/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004363static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004364 SDValue V2) {
4365 unsigned NumElems = VT.getVectorNumElements();
4366 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004367 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004368 Mask.push_back(i);
4369 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004370 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004371 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004372}
4373
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004374/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004375static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 SDValue V2) {
4377 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004378 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004379 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004380 Mask.push_back(i + Half);
4381 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004382 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004383 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004384}
4385
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004386// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004387// a generic shuffle instruction because the target has no such instructions.
4388// Generate shuffles which repeat i16 and i8 several times until they can be
4389// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004390static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004391 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004393 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004394
Nate Begeman9008ca62009-04-27 18:41:29 +00004395 while (NumElems > 4) {
4396 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004397 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004398 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004399 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004400 EltNo -= NumElems/2;
4401 }
4402 NumElems >>= 1;
4403 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004404 return V;
4405}
Eric Christopherfd179292009-08-27 18:07:15 +00004406
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004407/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4408static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4409 EVT VT = V.getValueType();
4410 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004411 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004412
Craig Topper9d352402012-04-23 07:24:41 +00004413 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004414 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004415 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004416 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4417 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004418 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004419 // To use VPERMILPS to splat scalars, the second half of indicies must
4420 // refer to the higher part, which is a duplication of the lower one,
4421 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004422 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4423 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004424
4425 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4426 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4427 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004428 } else
4429 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004430
4431 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4432}
4433
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004434/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004435static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4436 EVT SrcVT = SV->getValueType(0);
4437 SDValue V1 = SV->getOperand(0);
4438 DebugLoc dl = SV->getDebugLoc();
4439
4440 int EltNo = SV->getSplatIndex();
4441 int NumElems = SrcVT.getVectorNumElements();
4442 unsigned Size = SrcVT.getSizeInBits();
4443
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004444 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4445 "Unknown how to promote splat for type");
4446
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004447 // Extract the 128-bit part containing the splat element and update
4448 // the splat element index when it refers to the higher register.
4449 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004450 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4451 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004452 EltNo -= NumElems/2;
4453 }
4454
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004455 // All i16 and i8 vector types can't be used directly by a generic shuffle
4456 // instruction because the target has no such instruction. Generate shuffles
4457 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004458 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004459 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004460 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004461 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004462
4463 // Recreate the 256-bit vector and place the same 128-bit vector
4464 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004465 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004466 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004467 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004468 }
4469
4470 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004471}
4472
Evan Chengba05f722006-04-21 23:03:30 +00004473/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004474/// vector of zero or undef vector. This produces a shuffle where the low
4475/// element of V2 is swizzled into the zero/undef vector, landing at element
4476/// 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 +00004477static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004478 bool IsZero,
4479 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004480 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004481 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004482 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004483 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 unsigned NumElems = VT.getVectorNumElements();
4485 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004486 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004487 // If this is the insertion idx, put the low elt of V2 here.
4488 MaskVec.push_back(i == Idx ? NumElems : i);
4489 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004490}
4491
Craig Toppera1ffc682012-03-20 06:42:26 +00004492/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4493/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004494/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004495static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004496 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004497 unsigned NumElems = VT.getVectorNumElements();
4498 SDValue ImmN;
4499
Craig Topper89f4e662012-03-20 07:17:59 +00004500 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004501 switch(N->getOpcode()) {
4502 case X86ISD::SHUFP:
4503 ImmN = N->getOperand(N->getNumOperands()-1);
4504 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4505 break;
4506 case X86ISD::UNPCKH:
4507 DecodeUNPCKHMask(VT, Mask);
4508 break;
4509 case X86ISD::UNPCKL:
4510 DecodeUNPCKLMask(VT, Mask);
4511 break;
4512 case X86ISD::MOVHLPS:
4513 DecodeMOVHLPSMask(NumElems, Mask);
4514 break;
4515 case X86ISD::MOVLHPS:
4516 DecodeMOVLHPSMask(NumElems, Mask);
4517 break;
4518 case X86ISD::PSHUFD:
4519 case X86ISD::VPERMILP:
4520 ImmN = N->getOperand(N->getNumOperands()-1);
4521 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004522 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004523 break;
4524 case X86ISD::PSHUFHW:
4525 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004526 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004527 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004528 break;
4529 case X86ISD::PSHUFLW:
4530 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004531 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004532 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004533 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004534 case X86ISD::VPERMI:
4535 ImmN = N->getOperand(N->getNumOperands()-1);
4536 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4537 IsUnary = true;
4538 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004539 case X86ISD::MOVSS:
4540 case X86ISD::MOVSD: {
4541 // The index 0 always comes from the first element of the second source,
4542 // this is why MOVSS and MOVSD are used in the first place. The other
4543 // elements come from the other positions of the first source vector
4544 Mask.push_back(NumElems);
4545 for (unsigned i = 1; i != NumElems; ++i) {
4546 Mask.push_back(i);
4547 }
4548 break;
4549 }
4550 case X86ISD::VPERM2X128:
4551 ImmN = N->getOperand(N->getNumOperands()-1);
4552 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004553 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004554 break;
4555 case X86ISD::MOVDDUP:
4556 case X86ISD::MOVLHPD:
4557 case X86ISD::MOVLPD:
4558 case X86ISD::MOVLPS:
4559 case X86ISD::MOVSHDUP:
4560 case X86ISD::MOVSLDUP:
4561 case X86ISD::PALIGN:
4562 // Not yet implemented
4563 return false;
4564 default: llvm_unreachable("unknown target shuffle node");
4565 }
4566
4567 return true;
4568}
4569
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004570/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4571/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004572static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004573 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004574 if (Depth == 6)
4575 return SDValue(); // Limit search depth.
4576
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004577 SDValue V = SDValue(N, 0);
4578 EVT VT = V.getValueType();
4579 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004580
4581 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4582 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004583 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004584
Craig Topper3d092db2012-03-21 02:14:01 +00004585 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004586 return DAG.getUNDEF(VT.getVectorElementType());
4587
Craig Topperd156dc12012-02-06 07:17:51 +00004588 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004589 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4590 : SV->getOperand(1);
4591 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004592 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004593
4594 // Recurse into target specific vector shuffles to find scalars.
4595 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004596 MVT ShufVT = V.getValueType().getSimpleVT();
4597 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004598 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004599 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004600 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004601
Craig Topperd978c542012-05-06 19:46:21 +00004602 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004603 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004604
Craig Topper3d092db2012-03-21 02:14:01 +00004605 int Elt = ShuffleMask[Index];
4606 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004607 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004608
Craig Topper3d092db2012-03-21 02:14:01 +00004609 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004610 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004611 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004612 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004613 }
4614
4615 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004616 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004617 V = V.getOperand(0);
4618 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004619 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004620
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004621 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004622 return SDValue();
4623 }
4624
4625 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4626 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004627 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004628
4629 if (V.getOpcode() == ISD::BUILD_VECTOR)
4630 return V.getOperand(Index);
4631
4632 return SDValue();
4633}
4634
4635/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4636/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004637/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004638static
Craig Topper3d092db2012-03-21 02:14:01 +00004639unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004640 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004641 unsigned i;
4642 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004643 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004644 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004645 if (!(Elt.getNode() &&
4646 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4647 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004648 }
4649
4650 return i;
4651}
4652
Craig Topper3d092db2012-03-21 02:14:01 +00004653/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4654/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004655/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4656static
Craig Topper3d092db2012-03-21 02:14:01 +00004657bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4658 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4659 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004660 bool SeenV1 = false;
4661 bool SeenV2 = false;
4662
Craig Topper3d092db2012-03-21 02:14:01 +00004663 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004664 int Idx = SVOp->getMaskElt(i);
4665 // Ignore undef indicies
4666 if (Idx < 0)
4667 continue;
4668
Craig Topper3d092db2012-03-21 02:14:01 +00004669 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004670 SeenV1 = true;
4671 else
4672 SeenV2 = true;
4673
4674 // Only accept consecutive elements from the same vector
4675 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4676 return false;
4677 }
4678
4679 OpNum = SeenV1 ? 0 : 1;
4680 return true;
4681}
4682
4683/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4684/// logical left shift of a vector.
4685static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4686 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4687 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4688 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4689 false /* check zeros from right */, DAG);
4690 unsigned OpSrc;
4691
4692 if (!NumZeros)
4693 return false;
4694
4695 // Considering the elements in the mask that are not consecutive zeros,
4696 // check if they consecutively come from only one of the source vectors.
4697 //
4698 // V1 = {X, A, B, C} 0
4699 // \ \ \ /
4700 // vector_shuffle V1, V2 <1, 2, 3, X>
4701 //
4702 if (!isShuffleMaskConsecutive(SVOp,
4703 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004704 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004705 NumZeros, // Where to start looking in the src vector
4706 NumElems, // Number of elements in vector
4707 OpSrc)) // Which source operand ?
4708 return false;
4709
4710 isLeft = false;
4711 ShAmt = NumZeros;
4712 ShVal = SVOp->getOperand(OpSrc);
4713 return true;
4714}
4715
4716/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4717/// logical left shift of a vector.
4718static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4719 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4720 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4721 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4722 true /* check zeros from left */, DAG);
4723 unsigned OpSrc;
4724
4725 if (!NumZeros)
4726 return false;
4727
4728 // Considering the elements in the mask that are not consecutive zeros,
4729 // check if they consecutively come from only one of the source vectors.
4730 //
4731 // 0 { A, B, X, X } = V2
4732 // / \ / /
4733 // vector_shuffle V1, V2 <X, X, 4, 5>
4734 //
4735 if (!isShuffleMaskConsecutive(SVOp,
4736 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004737 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004738 0, // Where to start looking in the src vector
4739 NumElems, // Number of elements in vector
4740 OpSrc)) // Which source operand ?
4741 return false;
4742
4743 isLeft = true;
4744 ShAmt = NumZeros;
4745 ShVal = SVOp->getOperand(OpSrc);
4746 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004747}
4748
4749/// isVectorShift - Returns true if the shuffle can be implemented as a
4750/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004751static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004752 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004753 // Although the logic below support any bitwidth size, there are no
4754 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004755 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004756 return false;
4757
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004758 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4759 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4760 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004761
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004762 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004763}
4764
Evan Chengc78d3b42006-04-24 18:01:45 +00004765/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4766///
Dan Gohman475871a2008-07-27 21:46:04 +00004767static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004768 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004769 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004770 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004771 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004772 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004773 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004774
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004775 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004776 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004777 bool First = true;
4778 for (unsigned i = 0; i < 16; ++i) {
4779 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4780 if (ThisIsNonZero && First) {
4781 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004782 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004783 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004784 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004785 First = false;
4786 }
4787
4788 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004789 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004790 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4791 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004792 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004793 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004794 }
4795 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004796 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4797 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4798 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004799 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004800 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004801 } else
4802 ThisElt = LastElt;
4803
Gabor Greifba36cb52008-08-28 21:40:38 +00004804 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004805 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004806 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004807 }
4808 }
4809
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004810 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004811}
4812
Bill Wendlinga348c562007-03-22 18:42:45 +00004813/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004814///
Dan Gohman475871a2008-07-27 21:46:04 +00004815static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004816 unsigned NumNonZero, unsigned NumZero,
4817 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004818 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004819 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004820 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004821 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004822
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004823 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004824 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004825 bool First = true;
4826 for (unsigned i = 0; i < 8; ++i) {
4827 bool isNonZero = (NonZeros & (1 << i)) != 0;
4828 if (isNonZero) {
4829 if (First) {
4830 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004831 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004832 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004833 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004834 First = false;
4835 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004836 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004837 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004838 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004839 }
4840 }
4841
4842 return V;
4843}
4844
Evan Chengf26ffe92008-05-29 08:22:04 +00004845/// getVShift - Return a vector logical shift node.
4846///
Owen Andersone50ed302009-08-10 22:56:29 +00004847static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004848 unsigned NumBits, SelectionDAG &DAG,
4849 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004850 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004851 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004852 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004853 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4854 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004855 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004856 DAG.getConstant(NumBits,
4857 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004858}
4859
Dan Gohman475871a2008-07-27 21:46:04 +00004860SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004861X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004862 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004863
Evan Chengc3630942009-12-09 21:00:30 +00004864 // Check if the scalar load can be widened into a vector load. And if
4865 // the address is "base + cst" see if the cst can be "absorbed" into
4866 // the shuffle mask.
4867 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4868 SDValue Ptr = LD->getBasePtr();
4869 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4870 return SDValue();
4871 EVT PVT = LD->getValueType(0);
4872 if (PVT != MVT::i32 && PVT != MVT::f32)
4873 return SDValue();
4874
4875 int FI = -1;
4876 int64_t Offset = 0;
4877 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4878 FI = FINode->getIndex();
4879 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004880 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004881 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4882 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4883 Offset = Ptr.getConstantOperandVal(1);
4884 Ptr = Ptr.getOperand(0);
4885 } else {
4886 return SDValue();
4887 }
4888
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004889 // FIXME: 256-bit vector instructions don't require a strict alignment,
4890 // improve this code to support it better.
4891 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004892 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004893 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004894 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004895 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004896 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004897 // Can't change the alignment. FIXME: It's possible to compute
4898 // the exact stack offset and reference FI + adjust offset instead.
4899 // If someone *really* cares about this. That's the way to implement it.
4900 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004901 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004902 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004903 }
4904 }
4905
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004906 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004907 // Ptr + (Offset & ~15).
4908 if (Offset < 0)
4909 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004910 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004911 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004912 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004913 if (StartOffset)
4914 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4915 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4916
4917 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004918 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004919
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004920 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4921 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004922 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004923 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004924
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004925 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004926 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004927 Mask.push_back(EltNo);
4928
Craig Toppercc3000632012-01-30 07:50:31 +00004929 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004930 }
4931
4932 return SDValue();
4933}
4934
Michael J. Spencerec38de22010-10-10 22:04:20 +00004935/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4936/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004937/// load which has the same value as a build_vector whose operands are 'elts'.
4938///
4939/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004940///
Nate Begeman1449f292010-03-24 22:19:06 +00004941/// FIXME: we'd also like to handle the case where the last elements are zero
4942/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4943/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004944static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004945 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004946 EVT EltVT = VT.getVectorElementType();
4947 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004948
Nate Begemanfdea31a2010-03-24 20:49:50 +00004949 LoadSDNode *LDBase = NULL;
4950 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004951
Nate Begeman1449f292010-03-24 22:19:06 +00004952 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004953 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004954 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004955 for (unsigned i = 0; i < NumElems; ++i) {
4956 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004957
Nate Begemanfdea31a2010-03-24 20:49:50 +00004958 if (!Elt.getNode() ||
4959 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4960 return SDValue();
4961 if (!LDBase) {
4962 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4963 return SDValue();
4964 LDBase = cast<LoadSDNode>(Elt.getNode());
4965 LastLoadedElt = i;
4966 continue;
4967 }
4968 if (Elt.getOpcode() == ISD::UNDEF)
4969 continue;
4970
4971 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4972 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4973 return SDValue();
4974 LastLoadedElt = i;
4975 }
Nate Begeman1449f292010-03-24 22:19:06 +00004976
4977 // If we have found an entire vector of loads and undefs, then return a large
4978 // load of the entire vector width starting at the base pointer. If we found
4979 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004980 if (LastLoadedElt == NumElems - 1) {
4981 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004982 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004983 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004984 LDBase->isVolatile(), LDBase->isNonTemporal(),
4985 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004986 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004987 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004988 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004989 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00004990 }
4991 if (NumElems == 4 && LastLoadedElt == 1 &&
4992 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004993 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4994 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004995 SDValue ResNode =
4996 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4997 LDBase->getPointerInfo(),
4998 LDBase->getAlignment(),
4999 false/*isVolatile*/, true/*ReadMem*/,
5000 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00005001
5002 // Make sure the newly-created LOAD is in the same position as LDBase in
5003 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5004 // update uses of LDBase's output chain to use the TokenFactor.
5005 if (LDBase->hasAnyUseOfValue(1)) {
5006 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5007 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5008 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5009 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5010 SDValue(ResNode.getNode(), 1));
5011 }
5012
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005013 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005014 }
5015 return SDValue();
5016}
5017
Nadav Rotem9d68b062012-04-08 12:54:54 +00005018/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5019/// to generate a splat value for the following cases:
5020/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005021/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005022/// a scalar load, or a constant.
5023/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005024/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005025SDValue
Craig Topper55b24052012-09-11 06:15:32 +00005026X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005027 if (!Subtarget->hasAVX())
5028 return SDValue();
5029
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005030 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005031 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005032
Craig Topper5da8a802012-05-04 05:49:51 +00005033 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5034 "Unsupported vector type for broadcast.");
5035
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005036 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005037 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005038
Nadav Rotem9d68b062012-04-08 12:54:54 +00005039 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005040 default:
5041 // Unknown pattern found.
5042 return SDValue();
5043
5044 case ISD::BUILD_VECTOR: {
5045 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005046 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005047 return SDValue();
5048
Nadav Rotem9d68b062012-04-08 12:54:54 +00005049 Ld = Op.getOperand(0);
5050 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5051 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005052
5053 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005054 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005055 // Constants may have multiple users.
5056 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005057 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005058 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005059 }
5060
5061 case ISD::VECTOR_SHUFFLE: {
5062 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5063
5064 // Shuffles must have a splat mask where the first element is
5065 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005066 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005067 return SDValue();
5068
5069 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005070 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005071 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5072
5073 if (!Subtarget->hasAVX2())
5074 return SDValue();
5075
5076 // Use the register form of the broadcast instruction available on AVX2.
5077 if (VT.is256BitVector())
5078 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5079 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5080 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005081
5082 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005083 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005084 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005085
5086 // The scalar_to_vector node and the suspected
5087 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005088 // Constants may have multiple users.
5089 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005090 return SDValue();
5091 break;
5092 }
5093 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005094
Craig Topper7a9a28b2012-08-12 02:23:29 +00005095 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005096
5097 // Handle the broadcasting a single constant scalar from the constant pool
5098 // into a vector. On Sandybridge it is still better to load a constant vector
5099 // from the constant pool and not to broadcast it from a scalar.
5100 if (ConstSplatVal && Subtarget->hasAVX2()) {
5101 EVT CVT = Ld.getValueType();
5102 assert(!CVT.isVector() && "Must not broadcast a vector type");
5103 unsigned ScalarSize = CVT.getSizeInBits();
5104
Craig Topper5da8a802012-05-04 05:49:51 +00005105 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005106 const Constant *C = 0;
5107 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5108 C = CI->getConstantIntValue();
5109 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5110 C = CF->getConstantFPValue();
5111
5112 assert(C && "Invalid constant type");
5113
Nadav Rotem154819d2012-04-09 07:45:58 +00005114 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005115 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005116 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005117 MachinePointerInfo::getConstantPool(),
5118 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005119
Nadav Rotem9d68b062012-04-08 12:54:54 +00005120 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5121 }
5122 }
5123
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005124 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005125 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5126
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005127 // Handle AVX2 in-register broadcasts.
5128 if (!IsLoad && Subtarget->hasAVX2() &&
5129 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5130 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5131
5132 // The scalar source must be a normal load.
5133 if (!IsLoad)
5134 return SDValue();
5135
Craig Topper5da8a802012-05-04 05:49:51 +00005136 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005137 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005138
Craig Toppera9376332012-01-10 08:23:59 +00005139 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005140 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005141 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005142 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005143 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005144 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005145
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005146 // Unsupported broadcast.
5147 return SDValue();
5148}
5149
Michael Liao7091b242012-08-14 21:24:47 +00005150// LowerVectorFpExtend - Recognize the scalarized FP_EXTEND from v2f32 to v2f64
5151// and convert it into X86ISD::VFPEXT due to the current ISD::FP_EXTEND has the
5152// constraint of matching input/output vector elements.
5153SDValue
5154X86TargetLowering::LowerVectorFpExtend(SDValue &Op, SelectionDAG &DAG) const {
5155 DebugLoc DL = Op.getDebugLoc();
5156 SDNode *N = Op.getNode();
5157 EVT VT = Op.getValueType();
5158 unsigned NumElts = Op.getNumOperands();
5159
5160 // Check supported types and sub-targets.
5161 //
5162 // Only v2f32 -> v2f64 needs special handling.
5163 if (VT != MVT::v2f64 || !Subtarget->hasSSE2())
5164 return SDValue();
5165
5166 SDValue VecIn;
5167 EVT VecInVT;
5168 SmallVector<int, 8> Mask;
5169 EVT SrcVT = MVT::Other;
5170
5171 // Check the patterns could be translated into X86vfpext.
5172 for (unsigned i = 0; i < NumElts; ++i) {
5173 SDValue In = N->getOperand(i);
5174 unsigned Opcode = In.getOpcode();
5175
5176 // Skip if the element is undefined.
5177 if (Opcode == ISD::UNDEF) {
5178 Mask.push_back(-1);
5179 continue;
5180 }
5181
5182 // Quit if one of the elements is not defined from 'fpext'.
5183 if (Opcode != ISD::FP_EXTEND)
5184 return SDValue();
5185
5186 // Check how the source of 'fpext' is defined.
5187 SDValue L2In = In.getOperand(0);
5188 EVT L2InVT = L2In.getValueType();
5189
5190 // Check the original type
5191 if (SrcVT == MVT::Other)
5192 SrcVT = L2InVT;
5193 else if (SrcVT != L2InVT) // Quit if non-homogenous typed.
5194 return SDValue();
5195
5196 // Check whether the value being 'fpext'ed is extracted from the same
5197 // source.
5198 Opcode = L2In.getOpcode();
5199
5200 // Quit if it's not extracted with a constant index.
5201 if (Opcode != ISD::EXTRACT_VECTOR_ELT ||
5202 !isa<ConstantSDNode>(L2In.getOperand(1)))
5203 return SDValue();
5204
5205 SDValue ExtractedFromVec = L2In.getOperand(0);
5206
5207 if (VecIn.getNode() == 0) {
5208 VecIn = ExtractedFromVec;
5209 VecInVT = ExtractedFromVec.getValueType();
5210 } else if (VecIn != ExtractedFromVec) // Quit if built from more than 1 vec.
5211 return SDValue();
5212
5213 Mask.push_back(cast<ConstantSDNode>(L2In.getOperand(1))->getZExtValue());
5214 }
5215
Michael Liao24438b82012-08-20 17:59:18 +00005216 // Quit if all operands of BUILD_VECTOR are undefined.
5217 if (!VecIn.getNode())
5218 return SDValue();
5219
Michael Liao7091b242012-08-14 21:24:47 +00005220 // Fill the remaining mask as undef.
5221 for (unsigned i = NumElts; i < VecInVT.getVectorNumElements(); ++i)
5222 Mask.push_back(-1);
5223
5224 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
5225 DAG.getVectorShuffle(VecInVT, DL,
5226 VecIn, DAG.getUNDEF(VecInVT),
5227 &Mask[0]));
5228}
5229
Evan Chengc3630942009-12-09 21:00:30 +00005230SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005231X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005232 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005233
David Greenef125a292011-02-08 19:04:41 +00005234 EVT VT = Op.getValueType();
5235 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005236 unsigned NumElems = Op.getNumOperands();
5237
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005238 // Vectors containing all zeros can be matched by pxor and xorps later
5239 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5240 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5241 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005242 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005243 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005244
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005245 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005246 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005247
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005248 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005249 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5250 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005251 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005252 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005253 return Op;
5254
Craig Topper07a27622012-01-22 03:07:48 +00005255 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005256 }
5257
Nadav Rotem154819d2012-04-09 07:45:58 +00005258 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005259 if (Broadcast.getNode())
5260 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005261
Michael Liao7091b242012-08-14 21:24:47 +00005262 SDValue FpExt = LowerVectorFpExtend(Op, DAG);
5263 if (FpExt.getNode())
5264 return FpExt;
5265
Owen Andersone50ed302009-08-10 22:56:29 +00005266 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005267
Evan Cheng0db9fe62006-04-25 20:13:52 +00005268 unsigned NumZero = 0;
5269 unsigned NumNonZero = 0;
5270 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005271 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005272 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005273 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005274 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005275 if (Elt.getOpcode() == ISD::UNDEF)
5276 continue;
5277 Values.insert(Elt);
5278 if (Elt.getOpcode() != ISD::Constant &&
5279 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005280 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005281 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005282 NumZero++;
5283 else {
5284 NonZeros |= (1 << i);
5285 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005286 }
5287 }
5288
Chris Lattner97a2a562010-08-26 05:24:29 +00005289 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5290 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005291 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005292
Chris Lattner67f453a2008-03-09 05:42:06 +00005293 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005294 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005295 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005296 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005297
Chris Lattner62098042008-03-09 01:05:04 +00005298 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5299 // the value are obviously zero, truncate the value to i32 and do the
5300 // insertion that way. Only do this if the value is non-constant or if the
5301 // value is a constant being inserted into element 0. It is cheaper to do
5302 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005303 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005304 (!IsAllConstants || Idx == 0)) {
5305 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005306 // Handle SSE only.
5307 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5308 EVT VecVT = MVT::v4i32;
5309 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005310
Chris Lattner62098042008-03-09 01:05:04 +00005311 // Truncate the value (which may itself be a constant) to i32, and
5312 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005313 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005314 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005315 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005316
Chris Lattner62098042008-03-09 01:05:04 +00005317 // Now we have our 32-bit value zero extended in the low element of
5318 // a vector. If Idx != 0, swizzle it into place.
5319 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005320 SmallVector<int, 4> Mask;
5321 Mask.push_back(Idx);
5322 for (unsigned i = 1; i != VecElts; ++i)
5323 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005324 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005325 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005326 }
Craig Topper07a27622012-01-22 03:07:48 +00005327 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005328 }
5329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005330
Chris Lattner19f79692008-03-08 22:59:52 +00005331 // If we have a constant or non-constant insertion into the low element of
5332 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5333 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005334 // depending on what the source datatype is.
5335 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005336 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005337 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005338
5339 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005340 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005341 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005342 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005343 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5344 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005345 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005346 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005347 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5348 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005349 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005350 }
5351
5352 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005353 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005354 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005355 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005356 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005357 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005358 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005359 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005360 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005361 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005362 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005363 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005364 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005365
5366 // Is it a vector logical left shift?
5367 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005368 X86::isZeroNode(Op.getOperand(0)) &&
5369 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005370 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005371 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005372 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005373 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005374 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005375 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005376
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005377 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005378 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005379
Chris Lattner19f79692008-03-08 22:59:52 +00005380 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5381 // is a non-constant being inserted into an element other than the low one,
5382 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5383 // movd/movss) to move this into the low element, then shuffle it into
5384 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005385 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005386 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005387
Evan Cheng0db9fe62006-04-25 20:13:52 +00005388 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005389 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005390 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005391 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005392 MaskVec.push_back(i == Idx ? 0 : 1);
5393 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005394 }
5395 }
5396
Chris Lattner67f453a2008-03-09 05:42:06 +00005397 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005398 if (Values.size() == 1) {
5399 if (EVTBits == 32) {
5400 // Instead of a shuffle like this:
5401 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5402 // Check if it's possible to issue this instead.
5403 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5404 unsigned Idx = CountTrailingZeros_32(NonZeros);
5405 SDValue Item = Op.getOperand(Idx);
5406 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5407 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5408 }
Dan Gohman475871a2008-07-27 21:46:04 +00005409 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005410 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005411
Dan Gohmana3941172007-07-24 22:55:08 +00005412 // A vector full of immediates; various special cases are already
5413 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005414 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005415 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005416
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005417 // For AVX-length vectors, build the individual 128-bit pieces and use
5418 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005419 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005420 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005421 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005422 V.push_back(Op.getOperand(i));
5423
5424 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5425
5426 // Build both the lower and upper subvector.
5427 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5428 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5429 NumElems/2);
5430
5431 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005432 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005433 }
5434
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005435 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005436 if (EVTBits == 64) {
5437 if (NumNonZero == 1) {
5438 // One half is zero or undef.
5439 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005440 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005441 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005442 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005443 }
Dan Gohman475871a2008-07-27 21:46:04 +00005444 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005445 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005446
5447 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005448 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005449 SDValue V = LowerBuildVectorv16i8(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
Bill Wendling826f36f2007-03-28 00:57:11 +00005454 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005455 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005456 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005457 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005458 }
5459
5460 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005461 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005462 if (NumElems == 4 && NumZero > 0) {
5463 for (unsigned i = 0; i < 4; ++i) {
5464 bool isZero = !(NonZeros & (1 << i));
5465 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005466 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005467 else
Dale Johannesenace16102009-02-03 19:33:06 +00005468 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005469 }
5470
5471 for (unsigned i = 0; i < 2; ++i) {
5472 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5473 default: break;
5474 case 0:
5475 V[i] = V[i*2]; // Must be a zero vector.
5476 break;
5477 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005478 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005479 break;
5480 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005481 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005482 break;
5483 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005484 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005485 break;
5486 }
5487 }
5488
Benjamin Kramer9c683542012-01-30 15:16:21 +00005489 bool Reverse1 = (NonZeros & 0x3) == 2;
5490 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5491 int MaskVec[] = {
5492 Reverse1 ? 1 : 0,
5493 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005494 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5495 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005496 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005497 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005498 }
5499
Craig Topper7a9a28b2012-08-12 02:23:29 +00005500 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005501 // Check for a build vector of consecutive loads.
5502 for (unsigned i = 0; i < NumElems; ++i)
5503 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005504
Nate Begemanfdea31a2010-03-24 20:49:50 +00005505 // Check for elements which are consecutive loads.
5506 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5507 if (LD.getNode())
5508 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005509
5510 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005511 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005512 SDValue Result;
5513 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5514 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5515 else
5516 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005517
Chris Lattner24faf612010-08-28 17:59:08 +00005518 for (unsigned i = 1; i < NumElems; ++i) {
5519 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5520 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005521 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005522 }
5523 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005524 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005525
Chris Lattner6e80e442010-08-28 17:15:43 +00005526 // Otherwise, expand into a number of unpckl*, start by extending each of
5527 // our (non-undef) elements to the full vector width with the element in the
5528 // bottom slot of the vector (which generates no code for SSE).
5529 for (unsigned i = 0; i < NumElems; ++i) {
5530 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5531 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5532 else
5533 V[i] = DAG.getUNDEF(VT);
5534 }
5535
5536 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005537 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5538 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5539 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005540 unsigned EltStride = NumElems >> 1;
5541 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005542 for (unsigned i = 0; i < EltStride; ++i) {
5543 // If V[i+EltStride] is undef and this is the first round of mixing,
5544 // then it is safe to just drop this shuffle: V[i] is already in the
5545 // right place, the one element (since it's the first round) being
5546 // inserted as undef can be dropped. This isn't safe for successive
5547 // rounds because they will permute elements within both vectors.
5548 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5549 EltStride == NumElems/2)
5550 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005551
Chris Lattner6e80e442010-08-28 17:15:43 +00005552 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005553 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005554 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005555 }
5556 return V[0];
5557 }
Dan Gohman475871a2008-07-27 21:46:04 +00005558 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005559}
5560
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005561// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5562// to create 256-bit vectors from two other 128-bit ones.
5563static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5564 DebugLoc dl = Op.getDebugLoc();
5565 EVT ResVT = Op.getValueType();
5566
Craig Topper7a9a28b2012-08-12 02:23:29 +00005567 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005568
5569 SDValue V1 = Op.getOperand(0);
5570 SDValue V2 = Op.getOperand(1);
5571 unsigned NumElems = ResVT.getVectorNumElements();
5572
Craig Topper4c7972d2012-04-22 18:15:59 +00005573 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005574}
5575
Craig Topper55b24052012-09-11 06:15:32 +00005576static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005577 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005578
5579 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5580 // from two other 128-bit ones.
5581 return LowerAVXCONCAT_VECTORS(Op, DAG);
5582}
5583
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005584// Try to lower a shuffle node into a simple blend instruction.
Craig Topper55b24052012-09-11 06:15:32 +00005585static SDValue
5586LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
5587 const X86Subtarget *Subtarget, SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005588 SDValue V1 = SVOp->getOperand(0);
5589 SDValue V2 = SVOp->getOperand(1);
5590 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005591 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005592 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005593
Nadav Roteme6113782012-04-11 06:40:27 +00005594 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005595 return SDValue();
5596
Craig Topper1842ba02012-04-23 06:38:28 +00005597 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005598 MVT OpTy;
5599
Craig Topper708e44f2012-04-23 07:36:33 +00005600 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005601 default: return SDValue();
5602 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005603 ISDNo = X86ISD::BLENDPW;
5604 OpTy = MVT::v8i16;
5605 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005606 case MVT::v4i32:
5607 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005608 ISDNo = X86ISD::BLENDPS;
5609 OpTy = MVT::v4f32;
5610 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005611 case MVT::v2i64:
5612 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005613 ISDNo = X86ISD::BLENDPD;
5614 OpTy = MVT::v2f64;
5615 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005616 case MVT::v8i32:
5617 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005618 if (!Subtarget->hasAVX())
5619 return SDValue();
5620 ISDNo = X86ISD::BLENDPS;
5621 OpTy = MVT::v8f32;
5622 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005623 case MVT::v4i64:
5624 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005625 if (!Subtarget->hasAVX())
5626 return SDValue();
5627 ISDNo = X86ISD::BLENDPD;
5628 OpTy = MVT::v4f64;
5629 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005630 }
5631 assert(ISDNo && "Invalid Op Number");
5632
5633 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005634
Craig Topper1842ba02012-04-23 06:38:28 +00005635 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005636 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005637 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005638 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005639 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005640 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005641 else
5642 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005643 }
5644
Nadav Roteme6113782012-04-11 06:40:27 +00005645 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5646 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5647 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5648 DAG.getConstant(MaskVals, MVT::i32));
5649 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005650}
5651
Nate Begemanb9a47b82009-02-23 08:49:38 +00005652// v8i16 shuffles - Prefer shuffles in the following order:
5653// 1. [all] pshuflw, pshufhw, optional move
5654// 2. [ssse3] 1 x pshufb
5655// 3. [ssse3] 2 x pshufb + 1 x por
5656// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Craig Topper55b24052012-09-11 06:15:32 +00005657static SDValue
5658LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget,
5659 SelectionDAG &DAG) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005660 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005661 SDValue V1 = SVOp->getOperand(0);
5662 SDValue V2 = SVOp->getOperand(1);
5663 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005664 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005665
Nate Begemanb9a47b82009-02-23 08:49:38 +00005666 // Determine if more than 1 of the words in each of the low and high quadwords
5667 // of the result come from the same quadword of one of the two inputs. Undef
5668 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005669 unsigned LoQuad[] = { 0, 0, 0, 0 };
5670 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005671 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005672 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005673 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005674 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005675 MaskVals.push_back(EltIdx);
5676 if (EltIdx < 0) {
5677 ++Quad[0];
5678 ++Quad[1];
5679 ++Quad[2];
5680 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005681 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005682 }
5683 ++Quad[EltIdx / 4];
5684 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005685 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005686
Nate Begemanb9a47b82009-02-23 08:49:38 +00005687 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005688 unsigned MaxQuad = 1;
5689 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005690 if (LoQuad[i] > MaxQuad) {
5691 BestLoQuad = i;
5692 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005693 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005694 }
5695
Nate Begemanb9a47b82009-02-23 08:49:38 +00005696 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005697 MaxQuad = 1;
5698 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005699 if (HiQuad[i] > MaxQuad) {
5700 BestHiQuad = i;
5701 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005702 }
5703 }
5704
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005706 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005707 // single pshufb instruction is necessary. If There are more than 2 input
5708 // quads, disable the next transformation since it does not help SSSE3.
5709 bool V1Used = InputQuads[0] || InputQuads[1];
5710 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005711 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005713 BestLoQuad = InputQuads[0] ? 0 : 1;
5714 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005715 }
5716 if (InputQuads.count() > 2) {
5717 BestLoQuad = -1;
5718 BestHiQuad = -1;
5719 }
5720 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005721
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5723 // the shuffle mask. If a quad is scored as -1, that means that it contains
5724 // words from all 4 input quadwords.
5725 SDValue NewV;
5726 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005727 int MaskV[] = {
5728 BestLoQuad < 0 ? 0 : BestLoQuad,
5729 BestHiQuad < 0 ? 1 : BestHiQuad
5730 };
Eric Christopherfd179292009-08-27 18:07:15 +00005731 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005732 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5733 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5734 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005735
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5737 // source words for the shuffle, to aid later transformations.
5738 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005739 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005740 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005741 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005742 if (idx != (int)i)
5743 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005744 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005745 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005746 AllWordsInNewV = false;
5747 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005748 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005749
Nate Begemanb9a47b82009-02-23 08:49:38 +00005750 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5751 if (AllWordsInNewV) {
5752 for (int i = 0; i != 8; ++i) {
5753 int idx = MaskVals[i];
5754 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005755 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005756 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005757 if ((idx != i) && idx < 4)
5758 pshufhw = false;
5759 if ((idx != i) && idx > 3)
5760 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005761 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005762 V1 = NewV;
5763 V2Used = false;
5764 BestLoQuad = 0;
5765 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005766 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005767
Nate Begemanb9a47b82009-02-23 08:49:38 +00005768 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5769 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005770 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005771 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5772 unsigned TargetMask = 0;
5773 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005774 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005775 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5776 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5777 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005778 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005779 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005780 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005781 }
Eric Christopherfd179292009-08-27 18:07:15 +00005782
Nate Begemanb9a47b82009-02-23 08:49:38 +00005783 // If we have SSSE3, and all words of the result are from 1 input vector,
5784 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5785 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005786 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005787 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005788
Nate Begemanb9a47b82009-02-23 08:49:38 +00005789 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005790 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 // mask, and elements that come from V1 in the V2 mask, so that the two
5792 // results can be OR'd together.
5793 bool TwoInputs = V1Used && V2Used;
5794 for (unsigned i = 0; i != 8; ++i) {
5795 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005796 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5797 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5798 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5799 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005800 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005801 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005802 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005803 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005804 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005805 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005806 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005807
Nate Begemanb9a47b82009-02-23 08:49:38 +00005808 // Calculate the shuffle mask for the second input, shuffle it, and
5809 // OR it with the first shuffled input.
5810 pshufbMask.clear();
5811 for (unsigned i = 0; i != 8; ++i) {
5812 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005813 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5814 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5815 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5816 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005817 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005818 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005819 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005820 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005821 MVT::v16i8, &pshufbMask[0], 16));
5822 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005823 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 }
5825
5826 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5827 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005828 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005829 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005830 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005831 for (int i = 0; i != 4; ++i) {
5832 int idx = MaskVals[i];
5833 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005834 InOrder.set(i);
5835 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005836 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005837 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005838 }
5839 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005840 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005841 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005842
Craig Topperdd637ae2012-02-19 05:41:45 +00005843 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5844 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005845 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005846 NewV.getOperand(0),
5847 getShufflePSHUFLWImmediate(SVOp), DAG);
5848 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005849 }
Eric Christopherfd179292009-08-27 18:07:15 +00005850
Nate Begemanb9a47b82009-02-23 08:49:38 +00005851 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5852 // and update MaskVals with the new element order.
5853 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005854 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005855 for (unsigned i = 4; i != 8; ++i) {
5856 int idx = MaskVals[i];
5857 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005858 InOrder.set(i);
5859 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005860 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005861 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005862 }
5863 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005864 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005865 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005866
Craig Topperdd637ae2012-02-19 05:41:45 +00005867 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5868 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005869 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005870 NewV.getOperand(0),
5871 getShufflePSHUFHWImmediate(SVOp), DAG);
5872 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005873 }
Eric Christopherfd179292009-08-27 18:07:15 +00005874
Nate Begemanb9a47b82009-02-23 08:49:38 +00005875 // In case BestHi & BestLo were both -1, which means each quadword has a word
5876 // from each of the four input quadwords, calculate the InOrder bitvector now
5877 // before falling through to the insert/extract cleanup.
5878 if (BestLoQuad == -1 && BestHiQuad == -1) {
5879 NewV = V1;
5880 for (int i = 0; i != 8; ++i)
5881 if (MaskVals[i] < 0 || MaskVals[i] == i)
5882 InOrder.set(i);
5883 }
Eric Christopherfd179292009-08-27 18:07:15 +00005884
Nate Begemanb9a47b82009-02-23 08:49:38 +00005885 // The other elements are put in the right place using pextrw and pinsrw.
5886 for (unsigned i = 0; i != 8; ++i) {
5887 if (InOrder[i])
5888 continue;
5889 int EltIdx = MaskVals[i];
5890 if (EltIdx < 0)
5891 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005892 SDValue ExtOp = (EltIdx < 8) ?
5893 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5894 DAG.getIntPtrConstant(EltIdx)) :
5895 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005896 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005897 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005898 DAG.getIntPtrConstant(i));
5899 }
5900 return NewV;
5901}
5902
5903// v16i8 shuffles - Prefer shuffles in the following order:
5904// 1. [ssse3] 1 x pshufb
5905// 2. [ssse3] 2 x pshufb + 1 x por
5906// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5907static
Nate Begeman9008ca62009-04-27 18:41:29 +00005908SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005909 SelectionDAG &DAG,
5910 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005911 SDValue V1 = SVOp->getOperand(0);
5912 SDValue V2 = SVOp->getOperand(1);
5913 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005914 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005915
Nate Begemanb9a47b82009-02-23 08:49:38 +00005916 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005917 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005918 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005919
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005921 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005922 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005923
Nate Begemanb9a47b82009-02-23 08:49:38 +00005924 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005925 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005926 //
5927 // Otherwise, we have elements from both input vectors, and must zero out
5928 // elements that come from V2 in the first mask, and V1 in the second mask
5929 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005930 for (unsigned i = 0; i != 16; ++i) {
5931 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005932 if (EltIdx < 0 || EltIdx >= 16)
5933 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005934 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005935 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005936 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005937 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005938 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005939
5940 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5941 // the 2nd operand if it's undefined or zero.
5942 if (V2.getOpcode() == ISD::UNDEF ||
5943 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005944 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005945
Nate Begemanb9a47b82009-02-23 08:49:38 +00005946 // Calculate the shuffle mask for the second input, shuffle it, and
5947 // OR it with the first shuffled input.
5948 pshufbMask.clear();
5949 for (unsigned i = 0; i != 16; ++i) {
5950 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005951 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005952 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005953 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005954 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005955 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005956 MVT::v16i8, &pshufbMask[0], 16));
5957 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005958 }
Eric Christopherfd179292009-08-27 18:07:15 +00005959
Nate Begemanb9a47b82009-02-23 08:49:38 +00005960 // No SSSE3 - Calculate in place words and then fix all out of place words
5961 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5962 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005963 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5964 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005965 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005966 for (int i = 0; i != 8; ++i) {
5967 int Elt0 = MaskVals[i*2];
5968 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005969
Nate Begemanb9a47b82009-02-23 08:49:38 +00005970 // This word of the result is all undef, skip it.
5971 if (Elt0 < 0 && Elt1 < 0)
5972 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005973
Nate Begemanb9a47b82009-02-23 08:49:38 +00005974 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005975 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005976 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005977
Nate Begemanb9a47b82009-02-23 08:49:38 +00005978 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5979 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5980 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005981
5982 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5983 // using a single extract together, load it and store it.
5984 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005985 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005986 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005987 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005988 DAG.getIntPtrConstant(i));
5989 continue;
5990 }
5991
Nate Begemanb9a47b82009-02-23 08:49:38 +00005992 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005993 // source byte is not also odd, shift the extracted word left 8 bits
5994 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005995 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005996 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005997 DAG.getIntPtrConstant(Elt1 / 2));
5998 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005999 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00006000 DAG.getConstant(8,
6001 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006002 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006003 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
6004 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00006005 }
6006 // If Elt0 is defined, extract it from the appropriate source. If the
6007 // source byte is not also even, shift the extracted word right 8 bits. If
6008 // Elt1 was also defined, OR the extracted values together before
6009 // inserting them in the result.
6010 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006011 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006012 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6013 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006014 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006015 DAG.getConstant(8,
6016 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006017 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006018 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6019 DAG.getConstant(0x00FF, MVT::i16));
6020 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006021 : InsElt0;
6022 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006023 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006024 DAG.getIntPtrConstant(i));
6025 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006026 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006027}
6028
Elena Demikhovsky41789462012-09-06 12:42:01 +00006029// v32i8 shuffles - Translate to VPSHUFB if possible.
6030static
6031SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
Craig Topper55b24052012-09-11 06:15:32 +00006032 const X86Subtarget *Subtarget,
6033 SelectionDAG &DAG) {
Elena Demikhovsky41789462012-09-06 12:42:01 +00006034 EVT VT = SVOp->getValueType(0);
6035 SDValue V1 = SVOp->getOperand(0);
6036 SDValue V2 = SVOp->getOperand(1);
6037 DebugLoc dl = SVOp->getDebugLoc();
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006038 SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006039
6040 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006041 bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode());
6042 bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode());
Elena Demikhovsky41789462012-09-06 12:42:01 +00006043
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006044 // VPSHUFB may be generated if
6045 // (1) one of input vector is undefined or zeroinitializer.
6046 // The mask value 0x80 puts 0 in the corresponding slot of the vector.
6047 // And (2) the mask indexes don't cross the 128-bit lane.
Craig Topper55b24052012-09-11 06:15:32 +00006048 if (VT != MVT::v32i8 || !Subtarget->hasAVX2() ||
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006049 (!V2IsUndef && !V2IsAllZero && !V1IsAllZero))
Elena Demikhovsky41789462012-09-06 12:42:01 +00006050 return SDValue();
6051
Elena Demikhovsky8100d242012-09-10 12:13:11 +00006052 if (V1IsAllZero && !V2IsAllZero) {
6053 CommuteVectorShuffleMask(MaskVals, 32);
6054 V1 = V2;
6055 }
6056 SmallVector<SDValue, 32> pshufbMask;
Elena Demikhovsky41789462012-09-06 12:42:01 +00006057 for (unsigned i = 0; i != 32; i++) {
6058 int EltIdx = MaskVals[i];
6059 if (EltIdx < 0 || EltIdx >= 32)
6060 EltIdx = 0x80;
6061 else {
6062 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6063 // Cross lane is not allowed.
6064 return SDValue();
6065 EltIdx &= 0xf;
6066 }
6067 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6068 }
6069 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6070 DAG.getNode(ISD::BUILD_VECTOR, dl,
6071 MVT::v32i8, &pshufbMask[0], 32));
6072}
6073
Evan Cheng7a831ce2007-12-15 03:00:47 +00006074/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006075/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006076/// done when every pair / quad of shuffle mask elements point to elements in
6077/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006078/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006079static
Nate Begeman9008ca62009-04-27 18:41:29 +00006080SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006081 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006082 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006083 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006084 MVT NewVT;
6085 unsigned Scale;
6086 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006087 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006088 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6089 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6090 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6091 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6092 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6093 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006094 }
6095
Nate Begeman9008ca62009-04-27 18:41:29 +00006096 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006097 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006098 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006099 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006100 int EltIdx = SVOp->getMaskElt(i+j);
6101 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006102 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006103 if (StartIdx < 0)
6104 StartIdx = (EltIdx / Scale);
6105 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006106 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006107 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006108 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006109 }
6110
Craig Topper11ac1f82012-05-04 04:08:44 +00006111 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6112 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006113 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006114}
6115
Evan Chengd880b972008-05-09 21:53:03 +00006116/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006117///
Owen Andersone50ed302009-08-10 22:56:29 +00006118static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006119 SDValue SrcOp, SelectionDAG &DAG,
6120 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006121 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006122 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006123 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006124 LD = dyn_cast<LoadSDNode>(SrcOp);
6125 if (!LD) {
6126 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6127 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006128 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006129 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006130 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006131 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006132 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006133 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006134 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006135 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006136 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6137 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6138 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006139 SrcOp.getOperand(0)
6140 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006141 }
6142 }
6143 }
6144
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006145 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006146 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006147 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006148 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006149}
6150
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006151/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6152/// which could not be matched by any known target speficic shuffle
6153static SDValue
6154LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006155
6156 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6157 if (NewOp.getNode())
6158 return NewOp;
6159
Craig Topper8f35c132012-01-20 09:29:03 +00006160 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006161
Craig Topper8f35c132012-01-20 09:29:03 +00006162 unsigned NumElems = VT.getVectorNumElements();
6163 unsigned NumLaneElems = NumElems / 2;
6164
Craig Topper8f35c132012-01-20 09:29:03 +00006165 DebugLoc dl = SVOp->getDebugLoc();
6166 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006167 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006168 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006169
Craig Topper9a2b6e12012-04-06 07:45:23 +00006170 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006171 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006172 // Build a shuffle mask for the output, discovering on the fly which
6173 // input vectors to use as shuffle operands (recorded in InputUsed).
6174 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006175 // out with UseBuildVector set.
6176 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006177 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006178 unsigned LaneStart = l * NumLaneElems;
6179 for (unsigned i = 0; i != NumLaneElems; ++i) {
6180 // The mask element. This indexes into the input.
6181 int Idx = SVOp->getMaskElt(i+LaneStart);
6182 if (Idx < 0) {
6183 // the mask element does not index into any input vector.
6184 Mask.push_back(-1);
6185 continue;
6186 }
Craig Topper8f35c132012-01-20 09:29:03 +00006187
Craig Topper9a2b6e12012-04-06 07:45:23 +00006188 // The input vector this mask element indexes into.
6189 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006190
Craig Topper9a2b6e12012-04-06 07:45:23 +00006191 // Turn the index into an offset from the start of the input vector.
6192 Idx -= Input * NumLaneElems;
6193
6194 // Find or create a shuffle vector operand to hold this input.
6195 unsigned OpNo;
6196 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6197 if (InputUsed[OpNo] == Input)
6198 // This input vector is already an operand.
6199 break;
6200 if (InputUsed[OpNo] < 0) {
6201 // Create a new operand for this input vector.
6202 InputUsed[OpNo] = Input;
6203 break;
6204 }
6205 }
6206
6207 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006208 // More than two input vectors used! Give up on trying to create a
6209 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6210 UseBuildVector = true;
6211 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006212 }
6213
6214 // Add the mask index for the new shuffle vector.
6215 Mask.push_back(Idx + OpNo * NumLaneElems);
6216 }
6217
Craig Topper8ae97ba2012-05-21 06:40:16 +00006218 if (UseBuildVector) {
6219 SmallVector<SDValue, 16> SVOps;
6220 for (unsigned i = 0; i != NumLaneElems; ++i) {
6221 // The mask element. This indexes into the input.
6222 int Idx = SVOp->getMaskElt(i+LaneStart);
6223 if (Idx < 0) {
6224 SVOps.push_back(DAG.getUNDEF(EltVT));
6225 continue;
6226 }
6227
6228 // The input vector this mask element indexes into.
6229 int Input = Idx / NumElems;
6230
6231 // Turn the index into an offset from the start of the input vector.
6232 Idx -= Input * NumElems;
6233
6234 // Extract the vector element by hand.
6235 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6236 SVOp->getOperand(Input),
6237 DAG.getIntPtrConstant(Idx)));
6238 }
6239
6240 // Construct the output using a BUILD_VECTOR.
6241 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6242 SVOps.size());
6243 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006244 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006245 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006246 } else {
6247 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006248 (InputUsed[0] % 2) * NumLaneElems,
6249 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006250 // If only one input was used, use an undefined vector for the other.
6251 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6252 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006253 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006254 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006255 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006256 }
6257
6258 Mask.clear();
6259 }
Craig Topper8f35c132012-01-20 09:29:03 +00006260
6261 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006262 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006263}
6264
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006265/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6266/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006267static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006268LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006269 SDValue V1 = SVOp->getOperand(0);
6270 SDValue V2 = SVOp->getOperand(1);
6271 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006272 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006273
Craig Topper7a9a28b2012-08-12 02:23:29 +00006274 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006275
Benjamin Kramer9c683542012-01-30 15:16:21 +00006276 std::pair<int, int> Locs[4];
6277 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006278 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006279
Evan Chengace3c172008-07-22 21:13:36 +00006280 unsigned NumHi = 0;
6281 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006282 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006283 int Idx = PermMask[i];
6284 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006285 Locs[i] = std::make_pair(-1, -1);
6286 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006287 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6288 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006289 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006290 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006291 NumLo++;
6292 } else {
6293 Locs[i] = std::make_pair(1, NumHi);
6294 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006295 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006296 NumHi++;
6297 }
6298 }
6299 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006300
Evan Chengace3c172008-07-22 21:13:36 +00006301 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006302 // If no more than two elements come from either vector. This can be
6303 // implemented with two shuffles. First shuffle gather the elements.
6304 // The second shuffle, which takes the first shuffle as both of its
6305 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006306 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006307
Benjamin Kramer9c683542012-01-30 15:16:21 +00006308 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006309
Benjamin Kramer9c683542012-01-30 15:16:21 +00006310 for (unsigned i = 0; i != 4; ++i)
6311 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006312 unsigned Idx = (i < 2) ? 0 : 4;
6313 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006314 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006315 }
Evan Chengace3c172008-07-22 21:13:36 +00006316
Nate Begeman9008ca62009-04-27 18:41:29 +00006317 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006318 }
6319
6320 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006321 // Otherwise, we must have three elements from one vector, call it X, and
6322 // one element from the other, call it Y. First, use a shufps to build an
6323 // intermediate vector with the one element from Y and the element from X
6324 // that will be in the same half in the final destination (the indexes don't
6325 // matter). Then, use a shufps to build the final vector, taking the half
6326 // containing the element from Y from the intermediate, and the other half
6327 // from X.
6328 if (NumHi == 3) {
6329 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006330 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006331 std::swap(V1, V2);
6332 }
6333
6334 // Find the element from V2.
6335 unsigned HiIndex;
6336 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006337 int Val = PermMask[HiIndex];
6338 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006339 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006340 if (Val >= 4)
6341 break;
6342 }
6343
Nate Begeman9008ca62009-04-27 18:41:29 +00006344 Mask1[0] = PermMask[HiIndex];
6345 Mask1[1] = -1;
6346 Mask1[2] = PermMask[HiIndex^1];
6347 Mask1[3] = -1;
6348 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006349
6350 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006351 Mask1[0] = PermMask[0];
6352 Mask1[1] = PermMask[1];
6353 Mask1[2] = HiIndex & 1 ? 6 : 4;
6354 Mask1[3] = HiIndex & 1 ? 4 : 6;
6355 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006356 }
Craig Topper69947b92012-04-23 06:57:04 +00006357
6358 Mask1[0] = HiIndex & 1 ? 2 : 0;
6359 Mask1[1] = HiIndex & 1 ? 0 : 2;
6360 Mask1[2] = PermMask[2];
6361 Mask1[3] = PermMask[3];
6362 if (Mask1[2] >= 0)
6363 Mask1[2] += 4;
6364 if (Mask1[3] >= 0)
6365 Mask1[3] += 4;
6366 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006367 }
6368
6369 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006370 int LoMask[] = { -1, -1, -1, -1 };
6371 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006372
Benjamin Kramer9c683542012-01-30 15:16:21 +00006373 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006374 unsigned MaskIdx = 0;
6375 unsigned LoIdx = 0;
6376 unsigned HiIdx = 2;
6377 for (unsigned i = 0; i != 4; ++i) {
6378 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006379 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006380 MaskIdx = 1;
6381 LoIdx = 0;
6382 HiIdx = 2;
6383 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006384 int Idx = PermMask[i];
6385 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006386 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006387 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006388 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006389 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006390 LoIdx++;
6391 } else {
6392 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006393 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006394 HiIdx++;
6395 }
6396 }
6397
Nate Begeman9008ca62009-04-27 18:41:29 +00006398 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6399 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006400 int MaskOps[] = { -1, -1, -1, -1 };
6401 for (unsigned i = 0; i != 4; ++i)
6402 if (Locs[i].first != -1)
6403 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006404 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006405}
6406
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006407static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006408 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006409 V = V.getOperand(0);
6410 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6411 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006412 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6413 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6414 // BUILD_VECTOR (load), undef
6415 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006416 if (MayFoldLoad(V))
6417 return true;
6418 return false;
6419}
6420
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006421// FIXME: the version above should always be used. Since there's
6422// a bug where several vector shuffles can't be folded because the
6423// DAG is not updated during lowering and a node claims to have two
6424// uses while it only has one, use this version, and let isel match
6425// another instruction if the load really happens to have more than
6426// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006427// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006428static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006429 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006430 V = V.getOperand(0);
6431 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6432 V = V.getOperand(0);
6433 if (ISD::isNormalLoad(V.getNode()))
6434 return true;
6435 return false;
6436}
6437
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006438static
Evan Cheng835580f2010-10-07 20:50:20 +00006439SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6440 EVT VT = Op.getValueType();
6441
6442 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006443 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6444 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006445 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6446 V1, DAG));
6447}
6448
6449static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006450SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006451 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006452 SDValue V1 = Op.getOperand(0);
6453 SDValue V2 = Op.getOperand(1);
6454 EVT VT = Op.getValueType();
6455
6456 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6457
Craig Topper1accb7e2012-01-10 06:54:16 +00006458 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006459 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6460
Evan Cheng0899f5c2011-08-31 02:05:24 +00006461 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6462 return DAG.getNode(ISD::BITCAST, dl, VT,
6463 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6464 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6465 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006466}
6467
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006468static
6469SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6470 SDValue V1 = Op.getOperand(0);
6471 SDValue V2 = Op.getOperand(1);
6472 EVT VT = Op.getValueType();
6473
6474 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6475 "unsupported shuffle type");
6476
6477 if (V2.getOpcode() == ISD::UNDEF)
6478 V2 = V1;
6479
6480 // v4i32 or v4f32
6481 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6482}
6483
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006484static
Craig Topper1accb7e2012-01-10 06:54:16 +00006485SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006486 SDValue V1 = Op.getOperand(0);
6487 SDValue V2 = Op.getOperand(1);
6488 EVT VT = Op.getValueType();
6489 unsigned NumElems = VT.getVectorNumElements();
6490
6491 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6492 // operand of these instructions is only memory, so check if there's a
6493 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6494 // same masks.
6495 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006496
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006497 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006498 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006499 CanFoldLoad = true;
6500
6501 // When V1 is a load, it can be folded later into a store in isel, example:
6502 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6503 // turns into:
6504 // (MOVLPSmr addr:$src1, VR128:$src2)
6505 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006506 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006507 CanFoldLoad = true;
6508
Dan Gohman65fd6562011-11-03 21:49:52 +00006509 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006510 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006511 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006512 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6513
6514 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006515 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006516 if (SVOp->getMaskElt(1) != -1)
6517 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006518 }
6519
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006520 // movl and movlp will both match v2i64, but v2i64 is never matched by
6521 // movl earlier because we make it strict to avoid messing with the movlp load
6522 // folding logic (see the code above getMOVLP call). Match it here then,
6523 // this is horrible, but will stay like this until we move all shuffle
6524 // matching to x86 specific nodes. Note that for the 1st condition all
6525 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006526 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006527 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6528 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006529 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006530 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006531 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006532 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006533
6534 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6535
6536 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006537 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006538 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006539}
6540
Nadav Rotem154819d2012-04-09 07:45:58 +00006541SDValue
6542X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006543 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6544 EVT VT = Op.getValueType();
6545 DebugLoc dl = Op.getDebugLoc();
6546 SDValue V1 = Op.getOperand(0);
6547 SDValue V2 = Op.getOperand(1);
6548
6549 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006550 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006551
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006552 // Handle splat operations
6553 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006554 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006555 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006556
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006557 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006558 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006559 if (Broadcast.getNode())
6560 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006561
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006562 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006563 if ((Size == 128 && NumElem <= 4) ||
6564 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006565 return SDValue();
6566
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006567 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006568 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006569 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006570
6571 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6572 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006573 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6574 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006575 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6576 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006577 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006578 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006579 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006580 // FIXME: Figure out a cleaner way to do this.
6581 // Try to make use of movq to zero out the top part.
6582 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6583 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6584 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006585 EVT NewVT = NewOp.getValueType();
6586 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6587 NewVT, true, false))
6588 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006589 DAG, Subtarget, dl);
6590 }
6591 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6592 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006593 if (NewOp.getNode()) {
6594 EVT NewVT = NewOp.getValueType();
6595 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6596 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6597 DAG, Subtarget, dl);
6598 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006599 }
6600 }
6601 return SDValue();
6602}
6603
Dan Gohman475871a2008-07-27 21:46:04 +00006604SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006605X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006606 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006607 SDValue V1 = Op.getOperand(0);
6608 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006609 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006610 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006611 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006612 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006613 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006614 bool V1IsSplat = false;
6615 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006616 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006617 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006618 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006619 MachineFunction &MF = DAG.getMachineFunction();
6620 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006621
Craig Topper3426a3e2011-11-14 06:46:21 +00006622 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006623
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006624 if (V1IsUndef && V2IsUndef)
6625 return DAG.getUNDEF(VT);
6626
6627 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006628
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006629 // Vector shuffle lowering takes 3 steps:
6630 //
6631 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6632 // narrowing and commutation of operands should be handled.
6633 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6634 // shuffle nodes.
6635 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6636 // so the shuffle can be broken into other shuffles and the legalizer can
6637 // try the lowering again.
6638 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006639 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006640 // be matched during isel, all of them must be converted to a target specific
6641 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006642
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006643 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6644 // narrowing and commutation of operands should be handled. The actual code
6645 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006646 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006647 if (NewOp.getNode())
6648 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006649
Craig Topper5aaffa82012-02-19 02:53:47 +00006650 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6651
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006652 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6653 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006654 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006655 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006656 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006657 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006658
Craig Topperdd637ae2012-02-19 05:41:45 +00006659 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006660 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006661 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006662
Craig Topperdd637ae2012-02-19 05:41:45 +00006663 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006664 return getMOVHighToLow(Op, dl, DAG);
6665
6666 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006667 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006668 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006669 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006670
Craig Topper5aaffa82012-02-19 02:53:47 +00006671 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006672 // The actual implementation will match the mask in the if above and then
6673 // during isel it can match several different instructions, not only pshufd
6674 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006675 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6676 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006677
Craig Topper5aaffa82012-02-19 02:53:47 +00006678 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006679
Craig Topperdbd98a42012-02-07 06:28:42 +00006680 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6681 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6682
Craig Topper1accb7e2012-01-10 06:54:16 +00006683 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006684 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6685
Craig Topperb3982da2011-12-31 23:50:21 +00006686 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006687 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006688 }
Eric Christopherfd179292009-08-27 18:07:15 +00006689
Evan Chengf26ffe92008-05-29 08:22:04 +00006690 // Check if this can be converted into a logical shift.
6691 bool isLeft = false;
6692 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006693 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006694 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006695 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006696 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006697 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006698 EVT EltVT = VT.getVectorElementType();
6699 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006700 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006701 }
Eric Christopherfd179292009-08-27 18:07:15 +00006702
Craig Topper5aaffa82012-02-19 02:53:47 +00006703 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006704 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006705 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006706 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006707 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006708 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6709
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006710 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006711 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6712 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006713 }
Eric Christopherfd179292009-08-27 18:07:15 +00006714
Nate Begeman9008ca62009-04-27 18:41:29 +00006715 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006716 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006717 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006718
Craig Topperdd637ae2012-02-19 05:41:45 +00006719 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006720 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006721
Craig Topperdd637ae2012-02-19 05:41:45 +00006722 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006723 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006724
Craig Topperdd637ae2012-02-19 05:41:45 +00006725 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006726 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006727
Craig Topperdd637ae2012-02-19 05:41:45 +00006728 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006729 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006730
Craig Topperdd637ae2012-02-19 05:41:45 +00006731 if (ShouldXformToMOVHLPS(M, VT) ||
6732 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006733 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006734
Evan Chengf26ffe92008-05-29 08:22:04 +00006735 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006736 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006737 EVT EltVT = VT.getVectorElementType();
6738 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006739 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006740 }
Eric Christopherfd179292009-08-27 18:07:15 +00006741
Evan Cheng9eca5e82006-10-25 21:49:50 +00006742 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006743 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6744 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006745 V1IsSplat = isSplatVector(V1.getNode());
6746 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006747
Chris Lattner8a594482007-11-25 00:24:49 +00006748 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006749 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6750 CommuteVectorShuffleMask(M, NumElems);
6751 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006752 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006753 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006754 }
6755
Craig Topperbeabc6c2011-12-05 06:56:46 +00006756 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006757 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006758 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006759 return V1;
6760 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6761 // the instruction selector will not match, so get a canonical MOVL with
6762 // swapped operands to undo the commute.
6763 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006764 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006765
Craig Topperbeabc6c2011-12-05 06:56:46 +00006766 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006767 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006768
Craig Topperbeabc6c2011-12-05 06:56:46 +00006769 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006770 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006771
Evan Cheng9bbbb982006-10-25 20:48:19 +00006772 if (V2IsSplat) {
6773 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006774 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006775 // new vector_shuffle with the corrected mask.p
6776 SmallVector<int, 8> NewMask(M.begin(), M.end());
6777 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006778 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006779 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006780 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006781 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006782 }
6783
Evan Cheng9eca5e82006-10-25 21:49:50 +00006784 if (Commuted) {
6785 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006786 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006787 CommuteVectorShuffleMask(M, NumElems);
6788 std::swap(V1, V2);
6789 std::swap(V1IsSplat, V2IsSplat);
6790 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006791
Craig Topper39a9e482012-02-11 06:24:48 +00006792 if (isUNPCKLMask(M, VT, HasAVX2))
6793 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006794
Craig Topper39a9e482012-02-11 06:24:48 +00006795 if (isUNPCKHMask(M, VT, HasAVX2))
6796 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006797 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006798
Nate Begeman9008ca62009-04-27 18:41:29 +00006799 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006800 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006801 return CommuteVectorShuffle(SVOp, DAG);
6802
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006803 // The checks below are all present in isShuffleMaskLegal, but they are
6804 // inlined here right now to enable us to directly emit target specific
6805 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006806
Craig Topper0e2037b2012-01-20 05:53:00 +00006807 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006808 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006809 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006810 DAG);
6811
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006812 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6813 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006814 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006815 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006816 }
6817
Craig Toppera9a568a2012-05-02 08:03:44 +00006818 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006819 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006820 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006821 DAG);
6822
Craig Toppera9a568a2012-05-02 08:03:44 +00006823 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006824 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006825 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006826 DAG);
6827
Craig Topper1a7700a2012-01-19 08:19:12 +00006828 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006829 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006830 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006831
Craig Topper94438ba2011-12-16 08:06:31 +00006832 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006833 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006834 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006835 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006836
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006837 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006838 // Generate target specific nodes for 128 or 256-bit shuffles only
6839 // supported in the AVX instruction set.
6840 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006841
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006842 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006843 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006844 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6845
Craig Topper70b883b2011-11-28 10:14:51 +00006846 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006847 if (isVPERMILPMask(M, VT, HasAVX)) {
6848 if (HasAVX2 && VT == MVT::v8i32)
6849 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006850 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006851 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006852 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006853 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006854
Craig Topper70b883b2011-11-28 10:14:51 +00006855 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006856 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006857 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006858 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006859
Craig Topper1842ba02012-04-23 06:38:28 +00006860 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006861 if (BlendOp.getNode())
6862 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006863
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006864 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006865 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006866 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006867 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006868 }
Craig Topper92040742012-04-16 06:43:40 +00006869 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6870 &permclMask[0], 8);
6871 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006872 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006873 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006874 }
Craig Topper095c5282012-04-15 23:48:57 +00006875
Craig Topper8325c112012-04-16 00:41:45 +00006876 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6877 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006878 getShuffleCLImmediate(SVOp), DAG);
6879
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006880
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006881 //===--------------------------------------------------------------------===//
6882 // Since no target specific shuffle was selected for this generic one,
6883 // lower it into other known shuffles. FIXME: this isn't true yet, but
6884 // this is the plan.
6885 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006886
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006887 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6888 if (VT == MVT::v8i16) {
Craig Topper55b24052012-09-11 06:15:32 +00006889 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG);
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006890 if (NewOp.getNode())
6891 return NewOp;
6892 }
6893
6894 if (VT == MVT::v16i8) {
6895 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6896 if (NewOp.getNode())
6897 return NewOp;
6898 }
6899
Elena Demikhovsky41789462012-09-06 12:42:01 +00006900 if (VT == MVT::v32i8) {
Craig Topper55b24052012-09-11 06:15:32 +00006901 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG);
Elena Demikhovsky41789462012-09-06 12:42:01 +00006902 if (NewOp.getNode())
6903 return NewOp;
6904 }
6905
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006906 // Handle all 128-bit wide vectors with 4 elements, and match them with
6907 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006908 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006909 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6910
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006911 // Handle general 256-bit shuffles
6912 if (VT.is256BitVector())
6913 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6914
Dan Gohman475871a2008-07-27 21:46:04 +00006915 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006916}
6917
Dan Gohman475871a2008-07-27 21:46:04 +00006918SDValue
6919X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006920 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006921 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006922 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006923
Craig Topper7a9a28b2012-08-12 02:23:29 +00006924 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006925 return SDValue();
6926
Duncan Sands83ec4b62008-06-06 12:08:01 +00006927 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006928 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006929 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006930 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006931 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006932 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006933 }
6934
6935 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006936 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6937 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6938 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006939 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6940 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006941 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006943 Op.getOperand(0)),
6944 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006945 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Craig Topper7c022842012-09-12 06:20:41 +00006946 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006947 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00006948 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006949 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006950 }
6951
6952 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006953 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6954 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006955 // result has a single use which is a store or a bitcast to i32. And in
6956 // the case of a store, it's not worth it if the index is a constant 0,
6957 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006958 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006959 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006960 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006961 if ((User->getOpcode() != ISD::STORE ||
6962 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6963 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006964 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006965 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006966 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006967 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006968 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006969 Op.getOperand(0)),
6970 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006971 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006972 }
6973
6974 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006975 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006976 if (isa<ConstantSDNode>(Op.getOperand(1)))
6977 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006978 }
Dan Gohman475871a2008-07-27 21:46:04 +00006979 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006980}
6981
6982
Dan Gohman475871a2008-07-27 21:46:04 +00006983SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006984X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6985 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006986 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006987 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006988
David Greene74a579d2011-02-10 16:57:36 +00006989 SDValue Vec = Op.getOperand(0);
6990 EVT VecVT = Vec.getValueType();
6991
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006992 // If this is a 256-bit vector result, first extract the 128-bit vector and
6993 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006994 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00006995 DebugLoc dl = Op.getNode()->getDebugLoc();
6996 unsigned NumElems = VecVT.getVectorNumElements();
6997 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006998 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6999
7000 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007001 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00007002
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007003 if (IdxVal >= NumElems/2)
7004 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00007005 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007006 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00007007 }
7008
Craig Topper7a9a28b2012-08-12 02:23:29 +00007009 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00007010
Craig Topperd0a31172012-01-10 06:37:29 +00007011 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007012 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007013 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00007014 return Res;
7015 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00007016
Owen Andersone50ed302009-08-10 22:56:29 +00007017 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007018 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007019 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007020 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007021 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007022 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007023 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007024 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7025 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007026 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007027 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007028 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007029 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007030 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007031 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Craig Topper7c022842012-09-12 06:20:41 +00007032 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007033 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Craig Topper7c022842012-09-12 06:20:41 +00007034 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007035 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007036 }
7037
7038 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007039 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007040 if (Idx == 0)
7041 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007042
Evan Cheng0db9fe62006-04-25 20:13:52 +00007043 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007044 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007045 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007046 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007047 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007048 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007049 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007050 }
7051
7052 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007053 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7054 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7055 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007056 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007057 if (Idx == 0)
7058 return Op;
7059
7060 // UNPCKHPD the element to the lowest double word, then movsd.
7061 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7062 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007063 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007064 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007065 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007066 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007067 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007068 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007069 }
7070
Dan Gohman475871a2008-07-27 21:46:04 +00007071 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007072}
7073
Dan Gohman475871a2008-07-27 21:46:04 +00007074SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007075X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7076 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007077 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007078 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007079 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007080
Dan Gohman475871a2008-07-27 21:46:04 +00007081 SDValue N0 = Op.getOperand(0);
7082 SDValue N1 = Op.getOperand(1);
7083 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007084
Craig Topper7a9a28b2012-08-12 02:23:29 +00007085 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007086 return SDValue();
7087
Dan Gohman8a55ce42009-09-23 21:02:20 +00007088 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007089 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007090 unsigned Opc;
7091 if (VT == MVT::v8i16)
7092 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007093 else if (VT == MVT::v16i8)
7094 Opc = X86ISD::PINSRB;
7095 else
7096 Opc = X86ISD::PINSRB;
7097
Nate Begeman14d12ca2008-02-11 04:19:36 +00007098 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7099 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007100 if (N1.getValueType() != MVT::i32)
7101 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7102 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007103 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007104 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007105 }
7106
7107 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007108 // Bits [7:6] of the constant are the source select. This will always be
7109 // zero here. The DAG Combiner may combine an extract_elt index into these
7110 // bits. For example (insert (extract, 3), 2) could be matched by putting
7111 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007112 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007113 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007114 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007115 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007116 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007117 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007118 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007119 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007120 }
7121
7122 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007123 // PINSR* works with constant index.
7124 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007125 }
Dan Gohman475871a2008-07-27 21:46:04 +00007126 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007127}
7128
Dan Gohman475871a2008-07-27 21:46:04 +00007129SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007130X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007131 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007132 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007133
David Greene6b381262011-02-09 15:32:06 +00007134 DebugLoc dl = Op.getDebugLoc();
7135 SDValue N0 = Op.getOperand(0);
7136 SDValue N1 = Op.getOperand(1);
7137 SDValue N2 = Op.getOperand(2);
7138
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007139 // If this is a 256-bit vector result, first extract the 128-bit vector,
7140 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007141 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007142 if (!isa<ConstantSDNode>(N2))
7143 return SDValue();
7144
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007145 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007146 unsigned NumElems = VT.getVectorNumElements();
7147 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007148 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007149
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007150 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007151 bool Upper = IdxVal >= NumElems/2;
7152 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7153 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007154
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007155 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007156 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007157 }
7158
Craig Topperd0a31172012-01-10 06:37:29 +00007159 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007160 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7161
Dan Gohman8a55ce42009-09-23 21:02:20 +00007162 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007163 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007164
Dan Gohman8a55ce42009-09-23 21:02:20 +00007165 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007166 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7167 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007168 if (N1.getValueType() != MVT::i32)
7169 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7170 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007171 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007172 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007173 }
Dan Gohman475871a2008-07-27 21:46:04 +00007174 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007175}
7176
Craig Topper55b24052012-09-11 06:15:32 +00007177static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007178 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007179 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007180 EVT OpVT = Op.getValueType();
7181
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007182 // If this is a 256-bit vector result, first insert into a 128-bit
7183 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007184 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007185 // Insert into a 128-bit vector.
7186 EVT VT128 = EVT::getVectorVT(*Context,
7187 OpVT.getVectorElementType(),
7188 OpVT.getVectorNumElements() / 2);
7189
7190 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7191
7192 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007193 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007194 }
7195
Craig Topperd77d2fe2012-04-29 20:22:05 +00007196 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007197 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007198 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007199
Owen Anderson825b72b2009-08-11 20:47:22 +00007200 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007201 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007202 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007203 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007204}
7205
David Greene91585092011-01-26 15:38:49 +00007206// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7207// a simple subregister reference or explicit instructions to grab
7208// upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007209static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7210 SelectionDAG &DAG) {
David Greene91585092011-01-26 15:38:49 +00007211 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007212 DebugLoc dl = Op.getNode()->getDebugLoc();
7213 SDValue Vec = Op.getNode()->getOperand(0);
7214 SDValue Idx = Op.getNode()->getOperand(1);
7215
Craig Topper7a9a28b2012-08-12 02:23:29 +00007216 if (Op.getNode()->getValueType(0).is128BitVector() &&
7217 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007218 isa<ConstantSDNode>(Idx)) {
7219 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7220 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007221 }
David Greene91585092011-01-26 15:38:49 +00007222 }
7223 return SDValue();
7224}
7225
David Greenecfe33c42011-01-26 19:13:22 +00007226// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7227// simple superregister reference or explicit instructions to insert
7228// the upper bits of a vector.
Craig Topper55b24052012-09-11 06:15:32 +00007229static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
7230 SelectionDAG &DAG) {
David Greenecfe33c42011-01-26 19:13:22 +00007231 if (Subtarget->hasAVX()) {
7232 DebugLoc dl = Op.getNode()->getDebugLoc();
7233 SDValue Vec = Op.getNode()->getOperand(0);
7234 SDValue SubVec = Op.getNode()->getOperand(1);
7235 SDValue Idx = Op.getNode()->getOperand(2);
7236
Craig Topper7a9a28b2012-08-12 02:23:29 +00007237 if (Op.getNode()->getValueType(0).is256BitVector() &&
7238 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007239 isa<ConstantSDNode>(Idx)) {
7240 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7241 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007242 }
7243 }
7244 return SDValue();
7245}
7246
Bill Wendling056292f2008-09-16 21:48:12 +00007247// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7248// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7249// one of the above mentioned nodes. It has to be wrapped because otherwise
7250// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7251// be used to form addressing mode. These wrapped nodes will be selected
7252// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007253SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007254X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007255 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007256
Chris Lattner41621a22009-06-26 19:22:52 +00007257 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7258 // global base reg.
7259 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007260 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007261 CodeModel::Model M = getTargetMachine().getCodeModel();
7262
Chris Lattner4f066492009-07-11 20:29:19 +00007263 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007264 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007265 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007266 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007267 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007268 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007269 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007270
Evan Cheng1606e8e2009-03-13 07:51:59 +00007271 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007272 CP->getAlignment(),
7273 CP->getOffset(), OpFlag);
7274 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007275 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007276 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007277 if (OpFlag) {
7278 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007279 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007280 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007281 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007282 }
7283
7284 return Result;
7285}
7286
Dan Gohmand858e902010-04-17 15:26:15 +00007287SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007288 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007289
Chris Lattner18c59872009-06-27 04:16:01 +00007290 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7291 // global base reg.
7292 unsigned char OpFlag = 0;
7293 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007294 CodeModel::Model M = getTargetMachine().getCodeModel();
7295
Chris Lattner4f066492009-07-11 20:29:19 +00007296 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007297 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007298 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007299 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007300 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007301 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007302 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007303
Chris Lattner18c59872009-06-27 04:16:01 +00007304 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7305 OpFlag);
7306 DebugLoc DL = JT->getDebugLoc();
7307 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007308
Chris Lattner18c59872009-06-27 04:16:01 +00007309 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007310 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007311 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7312 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007313 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007314 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007315
Chris Lattner18c59872009-06-27 04:16:01 +00007316 return Result;
7317}
7318
7319SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007320X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007321 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007322
Chris Lattner18c59872009-06-27 04:16:01 +00007323 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7324 // global base reg.
7325 unsigned char OpFlag = 0;
7326 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007327 CodeModel::Model M = getTargetMachine().getCodeModel();
7328
Chris Lattner4f066492009-07-11 20:29:19 +00007329 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007330 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7331 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7332 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007333 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007334 } else if (Subtarget->isPICStyleGOT()) {
7335 OpFlag = X86II::MO_GOT;
7336 } else if (Subtarget->isPICStyleStubPIC()) {
7337 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7338 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7339 OpFlag = X86II::MO_DARWIN_NONLAZY;
7340 }
Eric Christopherfd179292009-08-27 18:07:15 +00007341
Chris Lattner18c59872009-06-27 04:16:01 +00007342 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007343
Chris Lattner18c59872009-06-27 04:16:01 +00007344 DebugLoc DL = Op.getDebugLoc();
7345 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007346
7347
Chris Lattner18c59872009-06-27 04:16:01 +00007348 // With PIC, the address is actually $g + Offset.
7349 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007350 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007351 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7352 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007353 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007354 Result);
7355 }
Eric Christopherfd179292009-08-27 18:07:15 +00007356
Eli Friedman586272d2011-08-11 01:48:05 +00007357 // For symbols that require a load from a stub to get the address, emit the
7358 // load.
7359 if (isGlobalStubReference(OpFlag))
7360 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007361 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007362
Chris Lattner18c59872009-06-27 04:16:01 +00007363 return Result;
7364}
7365
Dan Gohman475871a2008-07-27 21:46:04 +00007366SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007367X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007368 // Create the TargetBlockAddressAddress node.
7369 unsigned char OpFlags =
7370 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007371 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007372 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007373 int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
Dan Gohman29cbade2009-11-20 23:18:13 +00007374 DebugLoc dl = Op.getDebugLoc();
Michael Liao6c7ccaa2012-09-12 21:43:09 +00007375 SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset,
7376 OpFlags);
Dan Gohman29cbade2009-11-20 23:18:13 +00007377
Dan Gohmanf705adb2009-10-30 01:28:02 +00007378 if (Subtarget->isPICStyleRIPRel() &&
7379 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007380 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7381 else
7382 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007383
Dan Gohman29cbade2009-11-20 23:18:13 +00007384 // With PIC, the address is actually $g + Offset.
7385 if (isGlobalRelativeToPICBase(OpFlags)) {
7386 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7387 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7388 Result);
7389 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007390
7391 return Result;
7392}
7393
7394SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007395X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007396 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007397 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007398 // Create the TargetGlobalAddress node, folding in the constant
7399 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007400 unsigned char OpFlags =
7401 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007402 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007403 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007404 if (OpFlags == X86II::MO_NO_FLAG &&
7405 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007406 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007407 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007408 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007409 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007410 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007411 }
Eric Christopherfd179292009-08-27 18:07:15 +00007412
Chris Lattner4f066492009-07-11 20:29:19 +00007413 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007414 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007415 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7416 else
7417 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007418
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007419 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007420 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007421 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7422 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007423 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007424 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007425
Chris Lattner36c25012009-07-10 07:34:39 +00007426 // For globals that require a load from a stub to get the address, emit the
7427 // load.
7428 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007429 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007430 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007431
Dan Gohman6520e202008-10-18 02:06:02 +00007432 // If there was a non-zero offset that we didn't fold, create an explicit
7433 // addition for it.
7434 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007435 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007436 DAG.getConstant(Offset, getPointerTy()));
7437
Evan Cheng0db9fe62006-04-25 20:13:52 +00007438 return Result;
7439}
7440
Evan Chengda43bcf2008-09-24 00:05:32 +00007441SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007442X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007443 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007444 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007445 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007446}
7447
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007448static SDValue
7449GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007450 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007451 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007452 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007453 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007454 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007455 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007456 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007457 GA->getOffset(),
7458 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007459
7460 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7461 : X86ISD::TLSADDR;
7462
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007463 if (InFlag) {
7464 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007465 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007466 } else {
7467 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007468 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007469 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007470
7471 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007472 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007473
Rafael Espindola15f1b662009-04-24 12:59:40 +00007474 SDValue Flag = Chain.getValue(1);
7475 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007476}
7477
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007478// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007479static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007480LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007481 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007482 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007483 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7484 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Craig Topper7c022842012-09-12 06:20:41 +00007485 DAG.getNode(X86ISD::GlobalBaseReg,
7486 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007487 InFlag = Chain.getValue(1);
7488
Chris Lattnerb903bed2009-06-26 21:20:29 +00007489 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007490}
7491
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007492// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007493static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007494LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007495 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007496 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7497 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007498}
7499
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007500static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7501 SelectionDAG &DAG,
7502 const EVT PtrVT,
7503 bool is64Bit) {
7504 DebugLoc dl = GA->getDebugLoc();
7505
7506 // Get the start address of the TLS block for this module.
7507 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7508 .getInfo<X86MachineFunctionInfo>();
7509 MFI->incNumLocalDynamicTLSAccesses();
7510
7511 SDValue Base;
7512 if (is64Bit) {
7513 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7514 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7515 } else {
7516 SDValue InFlag;
7517 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7518 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7519 InFlag = Chain.getValue(1);
7520 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7521 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7522 }
7523
7524 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7525 // of Base.
7526
7527 // Build x@dtpoff.
7528 unsigned char OperandFlags = X86II::MO_DTPOFF;
7529 unsigned WrapperKind = X86ISD::Wrapper;
7530 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7531 GA->getValueType(0),
7532 GA->getOffset(), OperandFlags);
7533 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7534
7535 // Add x@dtpoff with the base.
7536 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7537}
7538
Hans Wennborg228756c2012-05-11 10:11:01 +00007539// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007540static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007541 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007542 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007543 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007544
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007545 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7546 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7547 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007548
Michael J. Spencerec38de22010-10-10 22:04:20 +00007549 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007550 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007551 MachinePointerInfo(Ptr),
7552 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007553
Chris Lattnerb903bed2009-06-26 21:20:29 +00007554 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007555 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7556 // initialexec.
7557 unsigned WrapperKind = X86ISD::Wrapper;
7558 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007559 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007560 } else if (model == TLSModel::InitialExec) {
7561 if (is64Bit) {
7562 OperandFlags = X86II::MO_GOTTPOFF;
7563 WrapperKind = X86ISD::WrapperRIP;
7564 } else {
7565 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7566 }
Chris Lattner18c59872009-06-27 04:16:01 +00007567 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007568 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007569 }
Eric Christopherfd179292009-08-27 18:07:15 +00007570
Hans Wennborg228756c2012-05-11 10:11:01 +00007571 // emit "addl x@ntpoff,%eax" (local exec)
7572 // or "addl x@indntpoff,%eax" (initial exec)
7573 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007574 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007575 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007576 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007577 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007578
Hans Wennborg228756c2012-05-11 10:11:01 +00007579 if (model == TLSModel::InitialExec) {
7580 if (isPIC && !is64Bit) {
7581 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7582 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7583 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007584 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007585
7586 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7587 MachinePointerInfo::getGOT(), false, false, false,
7588 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007589 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007590
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007591 // The address of the thread local variable is the add of the thread
7592 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007593 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007594}
7595
Dan Gohman475871a2008-07-27 21:46:04 +00007596SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007597X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007598
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007599 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007600 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007601
Eric Christopher30ef0e52010-06-03 04:07:48 +00007602 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007603 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007604
Eric Christopher30ef0e52010-06-03 04:07:48 +00007605 switch (model) {
7606 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007607 if (Subtarget->is64Bit())
7608 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7609 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007610 case TLSModel::LocalDynamic:
7611 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7612 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007613 case TLSModel::InitialExec:
7614 case TLSModel::LocalExec:
7615 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007616 Subtarget->is64Bit(),
7617 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007618 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007619 llvm_unreachable("Unknown TLS model.");
7620 }
7621
7622 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007623 // Darwin only has one model of TLS. Lower to that.
7624 unsigned char OpFlag = 0;
7625 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7626 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007627
Eric Christopher30ef0e52010-06-03 04:07:48 +00007628 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7629 // global base reg.
7630 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7631 !Subtarget->is64Bit();
7632 if (PIC32)
7633 OpFlag = X86II::MO_TLVP_PIC_BASE;
7634 else
7635 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007636 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007637 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007638 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007639 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007640 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007641
Eric Christopher30ef0e52010-06-03 04:07:48 +00007642 // With PIC32, the address is actually $g + Offset.
7643 if (PIC32)
7644 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7645 DAG.getNode(X86ISD::GlobalBaseReg,
7646 DebugLoc(), getPointerTy()),
7647 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007648
Eric Christopher30ef0e52010-06-03 04:07:48 +00007649 // Lowering the machine isd will make sure everything is in the right
7650 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007651 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007652 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007653 SDValue Args[] = { Chain, Offset };
7654 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007655
Eric Christopher30ef0e52010-06-03 04:07:48 +00007656 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7657 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7658 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007659
Eric Christopher30ef0e52010-06-03 04:07:48 +00007660 // And our return value (tls address) is in the standard call return value
7661 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007662 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007663 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7664 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007665 }
7666
7667 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007668 // Just use the implicit TLS architecture
7669 // Need to generate someting similar to:
7670 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7671 // ; from TEB
7672 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7673 // mov rcx, qword [rdx+rcx*8]
7674 // mov eax, .tls$:tlsvar
7675 // [rax+rcx] contains the address
7676 // Windows 64bit: gs:0x58
7677 // Windows 32bit: fs:__tls_array
7678
7679 // If GV is an alias then use the aliasee for determining
7680 // thread-localness.
7681 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7682 GV = GA->resolveAliasedGlobal(false);
7683 DebugLoc dl = GA->getDebugLoc();
7684 SDValue Chain = DAG.getEntryNode();
7685
7686 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7687 // %gs:0x58 (64-bit).
7688 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7689 ? Type::getInt8PtrTy(*DAG.getContext(),
7690 256)
7691 : Type::getInt32PtrTy(*DAG.getContext(),
7692 257));
7693
7694 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7695 Subtarget->is64Bit()
7696 ? DAG.getIntPtrConstant(0x58)
7697 : DAG.getExternalSymbol("_tls_array",
7698 getPointerTy()),
7699 MachinePointerInfo(Ptr),
7700 false, false, false, 0);
7701
7702 // Load the _tls_index variable
7703 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7704 if (Subtarget->is64Bit())
7705 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7706 IDX, MachinePointerInfo(), MVT::i32,
7707 false, false, 0);
7708 else
7709 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7710 false, false, false, 0);
7711
7712 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007713 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007714 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7715
7716 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7717 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7718 false, false, false, 0);
7719
7720 // Get the offset of start of .tls section
7721 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7722 GA->getValueType(0),
7723 GA->getOffset(), X86II::MO_SECREL);
7724 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7725
7726 // The address of the thread local variable is the add of the thread
7727 // pointer with the offset of the variable.
7728 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007729 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007730
David Blaikie4d6ccb52012-01-20 21:51:11 +00007731 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007732}
7733
Evan Cheng0db9fe62006-04-25 20:13:52 +00007734
Chad Rosierb90d2a92012-01-03 23:19:12 +00007735/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7736/// and take a 2 x i32 value to shift plus a shift amount.
7737SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007738 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007739 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007740 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007741 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007742 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007743 SDValue ShOpLo = Op.getOperand(0);
7744 SDValue ShOpHi = Op.getOperand(1);
7745 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007746 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007747 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007748 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007749
Dan Gohman475871a2008-07-27 21:46:04 +00007750 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007751 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007752 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7753 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007754 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007755 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7756 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007757 }
Evan Chenge3413162006-01-09 18:33:28 +00007758
Owen Anderson825b72b2009-08-11 20:47:22 +00007759 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7760 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007761 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007762 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007763
Dan Gohman475871a2008-07-27 21:46:04 +00007764 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007765 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007766 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7767 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007768
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007769 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007770 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7771 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007772 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007773 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7774 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007775 }
7776
Dan Gohman475871a2008-07-27 21:46:04 +00007777 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007778 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007779}
Evan Chenga3195e82006-01-12 22:54:21 +00007780
Dan Gohmand858e902010-04-17 15:26:15 +00007781SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7782 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007783 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007784
Dale Johannesen0488fb62010-09-30 23:57:10 +00007785 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007786 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007787
Owen Anderson825b72b2009-08-11 20:47:22 +00007788 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007789 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007790
Eli Friedman36df4992009-05-27 00:47:34 +00007791 // These are really Legal; return the operand so the caller accepts it as
7792 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007793 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007794 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007795 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007796 Subtarget->is64Bit()) {
7797 return Op;
7798 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007799
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007800 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007801 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007802 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007803 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007804 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007805 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007806 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007807 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007808 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007809 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7810}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007811
Owen Andersone50ed302009-08-10 22:56:29 +00007812SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007813 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007814 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007815 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007816 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007817 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007818 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007819 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007820 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007821 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007822 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007823
Chris Lattner492a43e2010-09-22 01:28:21 +00007824 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007825
Stuart Hastings84be9582011-06-02 15:57:11 +00007826 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7827 MachineMemOperand *MMO;
7828 if (FI) {
7829 int SSFI = FI->getIndex();
7830 MMO =
7831 DAG.getMachineFunction()
7832 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7833 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7834 } else {
7835 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7836 StackSlot = StackSlot.getOperand(1);
7837 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007838 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007839 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7840 X86ISD::FILD, DL,
7841 Tys, Ops, array_lengthof(Ops),
7842 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007843
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007844 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007845 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007846 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007847
7848 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7849 // shouldn't be necessary except that RFP cannot be live across
7850 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007851 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007852 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7853 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007854 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007855 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007856 SDValue Ops[] = {
7857 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7858 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007859 MachineMemOperand *MMO =
7860 DAG.getMachineFunction()
7861 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007862 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007863
Chris Lattner492a43e2010-09-22 01:28:21 +00007864 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7865 Ops, array_lengthof(Ops),
7866 Op.getValueType(), MMO);
7867 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007868 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007869 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007870 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007871
Evan Cheng0db9fe62006-04-25 20:13:52 +00007872 return Result;
7873}
7874
Bill Wendling8b8a6362009-01-17 03:56:04 +00007875// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007876SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7877 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007878 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007879 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007880 movq %rax, %xmm0
7881 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7882 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7883 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007884 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007885 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007886 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007887 addpd %xmm1, %xmm0
7888 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007889 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007890
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007891 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007892 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007893
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007894 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007895 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7896 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007897 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007898
Chris Lattner97484792012-01-25 09:56:22 +00007899 SmallVector<Constant*,2> CV1;
7900 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007901 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007902 CV1.push_back(
7903 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7904 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007905 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007906
Bill Wendling397ae212012-01-05 02:13:20 +00007907 // Load the 64-bit value into an XMM register.
7908 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7909 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007910 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007911 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007912 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007913 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7914 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7915 CLod0);
7916
Owen Anderson825b72b2009-08-11 20:47:22 +00007917 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007918 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007919 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007920 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007921 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007922 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007923
Craig Topperd0a31172012-01-10 06:37:29 +00007924 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007925 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7926 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7927 } else {
7928 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7929 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7930 S2F, 0x4E, DAG);
7931 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7932 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7933 Sub);
7934 }
7935
7936 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007937 DAG.getIntPtrConstant(0));
7938}
7939
Bill Wendling8b8a6362009-01-17 03:56:04 +00007940// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007941SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7942 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007943 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007944 // FP constant to bias correct the final result.
7945 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007946 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007947
7948 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007949 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007950 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007951
Eli Friedmanf3704762011-08-29 21:15:46 +00007952 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007953 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007954
Owen Anderson825b72b2009-08-11 20:47:22 +00007955 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007956 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007957 DAG.getIntPtrConstant(0));
7958
7959 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007960 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007961 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007962 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007963 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007964 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007965 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007966 MVT::v2f64, Bias)));
7967 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007968 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007969 DAG.getIntPtrConstant(0));
7970
7971 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007972 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007973
7974 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007975 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007976
Craig Topper69947b92012-04-23 06:57:04 +00007977 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007978 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007979 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007980 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007981 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007982
7983 // Handle final rounding.
7984 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007985}
7986
Dan Gohmand858e902010-04-17 15:26:15 +00007987SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7988 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007989 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007990 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007991
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007992 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007993 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7994 // the optimization here.
7995 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007996 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007997
Owen Andersone50ed302009-08-10 22:56:29 +00007998 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007999 EVT DstVT = Op.getValueType();
8000 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008001 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008002 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00008003 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00008004 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00008005 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00008006
8007 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00008008 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008009 if (SrcVT == MVT::i32) {
8010 SDValue WordOff = DAG.getConstant(4, getPointerTy());
8011 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
8012 getPointerTy(), StackSlot, WordOff);
8013 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008014 StackSlot, MachinePointerInfo(),
8015 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008016 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008017 OffsetSlot, MachinePointerInfo(),
8018 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008019 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
8020 return Fild;
8021 }
8022
8023 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8024 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008025 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008026 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008027 // For i64 source, we need to add the appropriate power of 2 if the input
8028 // was negative. This is the same as the optimization in
8029 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8030 // we must be careful to do the computation in x87 extended precision, not
8031 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008032 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8033 MachineMemOperand *MMO =
8034 DAG.getMachineFunction()
8035 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8036 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008037
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008038 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8039 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008040 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8041 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008042
8043 APInt FF(32, 0x5F800000ULL);
8044
8045 // Check whether the sign bit is set.
8046 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8047 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8048 ISD::SETLT);
8049
8050 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8051 SDValue FudgePtr = DAG.getConstantPool(
8052 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8053 getPointerTy());
8054
8055 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8056 SDValue Zero = DAG.getIntPtrConstant(0);
8057 SDValue Four = DAG.getIntPtrConstant(4);
8058 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8059 Zero, Four);
8060 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8061
8062 // Load the value out, extending it from f32 to f80.
8063 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008064 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008065 FudgePtr, MachinePointerInfo::getConstantPool(),
8066 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008067 // Extend everything to 80 bits to force it to be done on x87.
8068 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8069 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008070}
8071
Dan Gohman475871a2008-07-27 21:46:04 +00008072std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008073FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008074 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008075
Owen Andersone50ed302009-08-10 22:56:29 +00008076 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008077
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008078 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008079 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8080 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008081 }
8082
Owen Anderson825b72b2009-08-11 20:47:22 +00008083 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8084 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008085 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008086
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008087 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008088 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008089 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008090 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008091 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008092 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008093 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008094 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008095
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008096 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8097 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008098 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008099 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008100 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008101 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008102
Evan Cheng0db9fe62006-04-25 20:13:52 +00008103 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008104 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8105 Opc = X86ISD::WIN_FTOL;
8106 else
8107 switch (DstTy.getSimpleVT().SimpleTy) {
8108 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8109 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8110 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8111 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8112 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008113
Dan Gohman475871a2008-07-27 21:46:04 +00008114 SDValue Chain = DAG.getEntryNode();
8115 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008116 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008117 // FIXME This causes a redundant load/store if the SSE-class value is already
8118 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008119 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008120 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008121 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008122 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008123 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008124 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008125 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008126 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008127 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008128
Chris Lattner492a43e2010-09-22 01:28:21 +00008129 MachineMemOperand *MMO =
8130 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8131 MachineMemOperand::MOLoad, MemSize, MemSize);
8132 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8133 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008134 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008135 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008136 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8137 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008138
Chris Lattner07290932010-09-22 01:05:16 +00008139 MachineMemOperand *MMO =
8140 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8141 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008142
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008143 if (Opc != X86ISD::WIN_FTOL) {
8144 // Build the FP_TO_INT*_IN_MEM
8145 SDValue Ops[] = { Chain, Value, StackSlot };
8146 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8147 Ops, 3, DstTy, MMO);
8148 return std::make_pair(FIST, StackSlot);
8149 } else {
8150 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8151 DAG.getVTList(MVT::Other, MVT::Glue),
8152 Chain, Value);
8153 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8154 MVT::i32, ftol.getValue(1));
8155 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8156 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008157 SDValue Ops[] = { eax, edx };
8158 SDValue pair = IsReplace
8159 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8160 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008161 return std::make_pair(pair, SDValue());
8162 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008163}
8164
Dan Gohmand858e902010-04-17 15:26:15 +00008165SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8166 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008167 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008168 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008169
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008170 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8171 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008172 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008173 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8174 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008175
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008176 if (StackSlot.getNode())
8177 // Load the result.
8178 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8179 FIST, StackSlot, MachinePointerInfo(),
8180 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008181
8182 // The node is the result.
8183 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008184}
8185
Dan Gohmand858e902010-04-17 15:26:15 +00008186SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8187 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008188 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8189 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008190 SDValue FIST = Vals.first, StackSlot = Vals.second;
8191 assert(FIST.getNode() && "Unexpected failure");
8192
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008193 if (StackSlot.getNode())
8194 // Load the result.
8195 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8196 FIST, StackSlot, MachinePointerInfo(),
8197 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008198
8199 // The node is the result.
8200 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008201}
8202
Craig Topper43620672012-09-08 07:31:51 +00008203SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008204 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008205 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008206 EVT VT = Op.getValueType();
8207 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008208 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8209 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008210 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008211 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008212 }
Craig Topper43620672012-09-08 07:31:51 +00008213 Constant *C;
8214 if (EltVT == MVT::f64)
8215 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8216 else
8217 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8218 C = ConstantVector::getSplat(NumElts, C);
8219 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8220 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008221 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008222 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008223 false, false, false, Alignment);
8224 if (VT.isVector()) {
8225 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8226 return DAG.getNode(ISD::BITCAST, dl, VT,
8227 DAG.getNode(ISD::AND, dl, ANDVT,
8228 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8229 Op.getOperand(0)),
8230 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8231 }
Dale Johannesenace16102009-02-03 19:33:06 +00008232 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008233}
8234
Dan Gohmand858e902010-04-17 15:26:15 +00008235SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008236 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008237 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008238 EVT VT = Op.getValueType();
8239 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008240 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8241 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008242 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008243 NumElts = VT.getVectorNumElements();
8244 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008245 Constant *C;
8246 if (EltVT == MVT::f64)
8247 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8248 else
8249 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8250 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008251 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8252 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008253 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008254 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008255 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008256 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008257 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008258 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008259 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008260 DAG.getNode(ISD::BITCAST, dl, XORVT,
8261 Op.getOperand(0)),
8262 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008263 }
Craig Topper69947b92012-04-23 06:57:04 +00008264
8265 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008266}
8267
Dan Gohmand858e902010-04-17 15:26:15 +00008268SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008269 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008270 SDValue Op0 = Op.getOperand(0);
8271 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008272 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008273 EVT VT = Op.getValueType();
8274 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008275
8276 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008277 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008278 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008279 SrcVT = VT;
8280 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008281 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008282 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008283 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008284 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008285 }
8286
8287 // At this point the operands and the result should have the same
8288 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008289
Evan Cheng68c47cb2007-01-05 07:55:56 +00008290 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008291 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008292 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008293 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8294 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008295 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008296 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8297 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8298 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8299 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008300 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008301 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008302 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008303 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008304 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008305 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008306 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008307
8308 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008309 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008310 // Op0 is MVT::f32, Op1 is MVT::f64.
8311 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8312 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8313 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008314 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008315 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008316 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008317 }
8318
Evan Cheng73d6cf12007-01-05 21:37:56 +00008319 // Clear first operand sign bit.
8320 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008321 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008322 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8323 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008324 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008325 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8326 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8327 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8328 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008329 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008330 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008331 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008332 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008333 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008334 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008335 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008336
8337 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008338 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008339}
8340
Craig Topper55b24052012-09-11 06:15:32 +00008341static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008342 SDValue N0 = Op.getOperand(0);
8343 DebugLoc dl = Op.getDebugLoc();
8344 EVT VT = Op.getValueType();
8345
8346 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8347 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8348 DAG.getConstant(1, VT));
8349 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8350}
8351
Michael Liaof966e4e2012-09-13 20:24:54 +00008352// LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able.
8353//
8354SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, SelectionDAG &DAG) const {
8355 assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
8356
8357 if (!Subtarget->hasSSE41())
8358 return SDValue();
8359
8360 if (!Op->hasOneUse())
8361 return SDValue();
8362
8363 SDNode *N = Op.getNode();
8364 DebugLoc DL = N->getDebugLoc();
8365
8366 SmallVector<SDValue, 8> Opnds;
8367 DenseMap<SDValue, unsigned> VecInMap;
8368 EVT VT = MVT::Other;
8369
8370 // Recognize a special case where a vector is casted into wide integer to
8371 // test all 0s.
8372 Opnds.push_back(N->getOperand(0));
8373 Opnds.push_back(N->getOperand(1));
8374
8375 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
8376 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
8377 // BFS traverse all OR'd operands.
8378 if (I->getOpcode() == ISD::OR) {
8379 Opnds.push_back(I->getOperand(0));
8380 Opnds.push_back(I->getOperand(1));
8381 // Re-evaluate the number of nodes to be traversed.
8382 e += 2; // 2 more nodes (LHS and RHS) are pushed.
8383 continue;
8384 }
8385
8386 // Quit if a non-EXTRACT_VECTOR_ELT
8387 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8388 return SDValue();
8389
8390 // Quit if without a constant index.
8391 SDValue Idx = I->getOperand(1);
8392 if (!isa<ConstantSDNode>(Idx))
8393 return SDValue();
8394
8395 SDValue ExtractedFromVec = I->getOperand(0);
8396 DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
8397 if (M == VecInMap.end()) {
8398 VT = ExtractedFromVec.getValueType();
8399 // Quit if not 128/256-bit vector.
8400 if (!VT.is128BitVector() && !VT.is256BitVector())
8401 return SDValue();
8402 // Quit if not the same type.
8403 if (VecInMap.begin() != VecInMap.end() &&
8404 VT != VecInMap.begin()->first.getValueType())
8405 return SDValue();
8406 M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
8407 }
8408 M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
8409 }
8410
8411 assert((VT.is128BitVector() || VT.is256BitVector()) &&
Michael Liao9aba7ea2012-09-13 20:30:16 +00008412 "Not extracted from 128-/256-bit vector.");
Michael Liaof966e4e2012-09-13 20:24:54 +00008413
8414 unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
8415 SmallVector<SDValue, 8> VecIns;
8416
8417 for (DenseMap<SDValue, unsigned>::const_iterator
8418 I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
8419 // Quit if not all elements are used.
8420 if (I->second != FullMask)
8421 return SDValue();
8422 VecIns.push_back(I->first);
8423 }
8424
8425 EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8426
8427 // Cast all vectors into TestVT for PTEST.
8428 for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
8429 VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]);
8430
8431 // If more than one full vectors are evaluated, OR them first before PTEST.
8432 for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
8433 // Each iteration will OR 2 nodes and append the result until there is only
8434 // 1 node left, i.e. the final OR'd value of all vectors.
8435 SDValue LHS = VecIns[Slot];
8436 SDValue RHS = VecIns[Slot + 1];
8437 VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
8438 }
8439
8440 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
8441 VecIns.back(), VecIns.back());
8442}
8443
Dan Gohman076aee32009-03-04 19:44:21 +00008444/// Emit nodes that will be selected as "test Op0,Op0", or something
8445/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008446SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008447 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008448 DebugLoc dl = Op.getDebugLoc();
8449
Dan Gohman31125812009-03-07 01:58:32 +00008450 // CF and OF aren't always set the way we want. Determine which
8451 // of these we need.
8452 bool NeedCF = false;
8453 bool NeedOF = false;
8454 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008455 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008456 case X86::COND_A: case X86::COND_AE:
8457 case X86::COND_B: case X86::COND_BE:
8458 NeedCF = true;
8459 break;
8460 case X86::COND_G: case X86::COND_GE:
8461 case X86::COND_L: case X86::COND_LE:
8462 case X86::COND_O: case X86::COND_NO:
8463 NeedOF = true;
8464 break;
Dan Gohman31125812009-03-07 01:58:32 +00008465 }
8466
Dan Gohman076aee32009-03-04 19:44:21 +00008467 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008468 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8469 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008470 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8471 // Emit a CMP with 0, which is the TEST pattern.
8472 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8473 DAG.getConstant(0, Op.getValueType()));
8474
8475 unsigned Opcode = 0;
8476 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008477
8478 // Truncate operations may prevent the merge of the SETCC instruction
8479 // and the arithmetic intruction before it. Attempt to truncate the operands
8480 // of the arithmetic instruction and use a reduced bit-width instruction.
8481 bool NeedTruncation = false;
8482 SDValue ArithOp = Op;
8483 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8484 SDValue Arith = Op->getOperand(0);
8485 // Both the trunc and the arithmetic op need to have one user each.
8486 if (Arith->hasOneUse())
8487 switch (Arith.getOpcode()) {
8488 default: break;
8489 case ISD::ADD:
8490 case ISD::SUB:
8491 case ISD::AND:
8492 case ISD::OR:
8493 case ISD::XOR: {
8494 NeedTruncation = true;
8495 ArithOp = Arith;
8496 }
8497 }
8498 }
8499
8500 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8501 // which may be the result of a CAST. We use the variable 'Op', which is the
8502 // non-casted variable when we check for possible users.
8503 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008504 case ISD::ADD:
8505 // Due to an isel shortcoming, be conservative if this add is likely to be
8506 // selected as part of a load-modify-store instruction. When the root node
8507 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8508 // uses of other nodes in the match, such as the ADD in this case. This
8509 // leads to the ADD being left around and reselected, with the result being
8510 // two adds in the output. Alas, even if none our users are stores, that
8511 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8512 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8513 // climbing the DAG back to the root, and it doesn't seem to be worth the
8514 // effort.
8515 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008516 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8517 if (UI->getOpcode() != ISD::CopyToReg &&
8518 UI->getOpcode() != ISD::SETCC &&
8519 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008520 goto default_case;
8521
8522 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008523 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008524 // An add of one will be selected as an INC.
8525 if (C->getAPIntValue() == 1) {
8526 Opcode = X86ISD::INC;
8527 NumOperands = 1;
8528 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008529 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008530
8531 // An add of negative one (subtract of one) will be selected as a DEC.
8532 if (C->getAPIntValue().isAllOnesValue()) {
8533 Opcode = X86ISD::DEC;
8534 NumOperands = 1;
8535 break;
8536 }
Dan Gohman076aee32009-03-04 19:44:21 +00008537 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008538
8539 // Otherwise use a regular EFLAGS-setting add.
8540 Opcode = X86ISD::ADD;
8541 NumOperands = 2;
8542 break;
8543 case ISD::AND: {
8544 // If the primary and result isn't used, don't bother using X86ISD::AND,
8545 // because a TEST instruction will be better.
8546 bool NonFlagUse = false;
8547 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8548 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8549 SDNode *User = *UI;
8550 unsigned UOpNo = UI.getOperandNo();
8551 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8552 // Look pass truncate.
8553 UOpNo = User->use_begin().getOperandNo();
8554 User = *User->use_begin();
8555 }
8556
8557 if (User->getOpcode() != ISD::BRCOND &&
8558 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008559 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008560 NonFlagUse = true;
8561 break;
8562 }
Dan Gohman076aee32009-03-04 19:44:21 +00008563 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008564
8565 if (!NonFlagUse)
8566 break;
8567 }
8568 // FALL THROUGH
8569 case ISD::SUB:
8570 case ISD::OR:
8571 case ISD::XOR:
8572 // Due to the ISEL shortcoming noted above, be conservative if this op is
8573 // likely to be selected as part of a load-modify-store instruction.
8574 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8575 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8576 if (UI->getOpcode() == ISD::STORE)
8577 goto default_case;
8578
8579 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008580 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008581 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008582 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008583 case ISD::XOR: Opcode = X86ISD::XOR; break;
8584 case ISD::AND: Opcode = X86ISD::AND; break;
Michael Liaof966e4e2012-09-13 20:24:54 +00008585 case ISD::OR: {
8586 if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
8587 SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG);
8588 if (EFLAGS.getNode())
8589 return EFLAGS;
8590 }
8591 Opcode = X86ISD::OR;
8592 break;
8593 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008594 }
8595
8596 NumOperands = 2;
8597 break;
8598 case X86ISD::ADD:
8599 case X86ISD::SUB:
8600 case X86ISD::INC:
8601 case X86ISD::DEC:
8602 case X86ISD::OR:
8603 case X86ISD::XOR:
8604 case X86ISD::AND:
8605 return SDValue(Op.getNode(), 1);
8606 default:
8607 default_case:
8608 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008609 }
8610
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008611 // If we found that truncation is beneficial, perform the truncation and
8612 // update 'Op'.
8613 if (NeedTruncation) {
8614 EVT VT = Op.getValueType();
8615 SDValue WideVal = Op->getOperand(0);
8616 EVT WideVT = WideVal.getValueType();
8617 unsigned ConvertedOp = 0;
8618 // Use a target machine opcode to prevent further DAGCombine
8619 // optimizations that may separate the arithmetic operations
8620 // from the setcc node.
8621 switch (WideVal.getOpcode()) {
8622 default: break;
8623 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8624 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8625 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8626 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8627 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8628 }
8629
8630 if (ConvertedOp) {
8631 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8632 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8633 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8634 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8635 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8636 }
8637 }
8638 }
8639
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008640 if (Opcode == 0)
8641 // Emit a CMP with 0, which is the TEST pattern.
8642 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8643 DAG.getConstant(0, Op.getValueType()));
8644
8645 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8646 SmallVector<SDValue, 4> Ops;
8647 for (unsigned i = 0; i != NumOperands; ++i)
8648 Ops.push_back(Op.getOperand(i));
8649
8650 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8651 DAG.ReplaceAllUsesWith(Op, New);
8652 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008653}
8654
8655/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8656/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008657SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008658 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008659 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8660 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008661 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008662
8663 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008664 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8665 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8666 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8667 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8668 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8669 Op0, Op1);
8670 return SDValue(Sub.getNode(), 1);
8671 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008672 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008673}
8674
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008675/// Convert a comparison if required by the subtarget.
8676SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8677 SelectionDAG &DAG) const {
8678 // If the subtarget does not support the FUCOMI instruction, floating-point
8679 // comparisons have to be converted.
8680 if (Subtarget->hasCMov() ||
8681 Cmp.getOpcode() != X86ISD::CMP ||
8682 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8683 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8684 return Cmp;
8685
8686 // The instruction selector will select an FUCOM instruction instead of
8687 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8688 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8689 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8690 DebugLoc dl = Cmp.getDebugLoc();
8691 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8692 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8693 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8694 DAG.getConstant(8, MVT::i8));
8695 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8696 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8697}
8698
Evan Chengd40d03e2010-01-06 19:38:29 +00008699/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8700/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008701SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8702 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008703 SDValue Op0 = And.getOperand(0);
8704 SDValue Op1 = And.getOperand(1);
8705 if (Op0.getOpcode() == ISD::TRUNCATE)
8706 Op0 = Op0.getOperand(0);
8707 if (Op1.getOpcode() == ISD::TRUNCATE)
8708 Op1 = Op1.getOperand(0);
8709
Evan Chengd40d03e2010-01-06 19:38:29 +00008710 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008711 if (Op1.getOpcode() == ISD::SHL)
8712 std::swap(Op0, Op1);
8713 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008714 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8715 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008716 // If we looked past a truncate, check that it's only truncating away
8717 // known zeros.
8718 unsigned BitWidth = Op0.getValueSizeInBits();
8719 unsigned AndBitWidth = And.getValueSizeInBits();
8720 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008721 APInt Zeros, Ones;
8722 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008723 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8724 return SDValue();
8725 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008726 LHS = Op1;
8727 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008728 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008729 } else if (Op1.getOpcode() == ISD::Constant) {
8730 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008731 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008732 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008733
8734 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008735 LHS = AndLHS.getOperand(0);
8736 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008737 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008738
8739 // Use BT if the immediate can't be encoded in a TEST instruction.
8740 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8741 LHS = AndLHS;
8742 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8743 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008744 }
Evan Cheng0488db92007-09-25 01:57:46 +00008745
Evan Chengd40d03e2010-01-06 19:38:29 +00008746 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008747 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008748 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008749 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008750 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008751 // Also promote i16 to i32 for performance / code size reason.
8752 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008753 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008754 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008755
Evan Chengd40d03e2010-01-06 19:38:29 +00008756 // If the operand types disagree, extend the shift amount to match. Since
8757 // BT ignores high bits (like shifts) we can use anyextend.
8758 if (LHS.getValueType() != RHS.getValueType())
8759 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008760
Evan Chengd40d03e2010-01-06 19:38:29 +00008761 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8762 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8763 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8764 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008765 }
8766
Evan Cheng54de3ea2010-01-05 06:52:31 +00008767 return SDValue();
8768}
8769
Dan Gohmand858e902010-04-17 15:26:15 +00008770SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008771
8772 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8773
Evan Cheng54de3ea2010-01-05 06:52:31 +00008774 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8775 SDValue Op0 = Op.getOperand(0);
8776 SDValue Op1 = Op.getOperand(1);
8777 DebugLoc dl = Op.getDebugLoc();
8778 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8779
8780 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008781 // Lower (X & (1 << N)) == 0 to BT(X, N).
8782 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8783 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008784 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008785 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008786 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008787 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8788 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8789 if (NewSetCC.getNode())
8790 return NewSetCC;
8791 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008792
Chris Lattner481eebc2010-12-19 21:23:48 +00008793 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8794 // these.
8795 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008796 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008797 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8798 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008799
Chris Lattner481eebc2010-12-19 21:23:48 +00008800 // If the input is a setcc, then reuse the input setcc or use a new one with
8801 // the inverted condition.
8802 if (Op0.getOpcode() == X86ISD::SETCC) {
8803 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8804 bool Invert = (CC == ISD::SETNE) ^
8805 cast<ConstantSDNode>(Op1)->isNullValue();
8806 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008807
Evan Cheng2c755ba2010-02-27 07:36:59 +00008808 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008809 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8810 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8811 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008812 }
8813
Evan Chenge5b51ac2010-04-17 06:13:15 +00008814 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008815 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008816 if (X86CC == X86::COND_INVALID)
8817 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008818
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008819 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008820 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008821 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008822 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008823}
8824
Craig Topper89af15e2011-09-18 08:03:58 +00008825// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008826// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008827static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008828 EVT VT = Op.getValueType();
8829
Craig Topper7a9a28b2012-08-12 02:23:29 +00008830 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008831 "Unsupported value type for operation");
8832
Craig Topper66ddd152012-04-27 22:54:43 +00008833 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008834 DebugLoc dl = Op.getDebugLoc();
8835 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008836
8837 // Extract the LHS vectors
8838 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008839 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8840 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008841
8842 // Extract the RHS vectors
8843 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008844 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8845 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008846
8847 // Issue the operation on the smaller types and concatenate the result back
8848 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8849 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8850 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8851 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8852 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8853}
8854
8855
Dan Gohmand858e902010-04-17 15:26:15 +00008856SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008857 SDValue Cond;
8858 SDValue Op0 = Op.getOperand(0);
8859 SDValue Op1 = Op.getOperand(1);
8860 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008861 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008862 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8863 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008864 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008865
8866 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008867#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008868 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008869 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8870#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008871
Craig Topper523908d2012-08-13 02:34:03 +00008872 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008873 bool Swap = false;
8874
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008875 // SSE Condition code mapping:
8876 // 0 - EQ
8877 // 1 - LT
8878 // 2 - LE
8879 // 3 - UNORD
8880 // 4 - NEQ
8881 // 5 - NLT
8882 // 6 - NLE
8883 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008884 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008885 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008886 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008887 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008888 case ISD::SETOGT:
8889 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008890 case ISD::SETLT:
8891 case ISD::SETOLT: SSECC = 1; break;
8892 case ISD::SETOGE:
8893 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008894 case ISD::SETLE:
8895 case ISD::SETOLE: SSECC = 2; break;
8896 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008897 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008898 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008899 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008900 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008901 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008902 case ISD::SETUGT: SSECC = 6; break;
8903 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008904 case ISD::SETUEQ:
8905 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008906 }
8907 if (Swap)
8908 std::swap(Op0, Op1);
8909
Nate Begemanfb8ead02008-07-25 19:05:58 +00008910 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008911 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008912 unsigned CC0, CC1;
8913 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008914 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008915 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8916 } else {
8917 assert(SetCCOpcode == ISD::SETONE);
8918 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008919 }
Craig Topper523908d2012-08-13 02:34:03 +00008920
8921 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8922 DAG.getConstant(CC0, MVT::i8));
8923 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8924 DAG.getConstant(CC1, MVT::i8));
8925 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008926 }
8927 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008928 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8929 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008930 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008931
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008932 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008933 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008934 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008935
Nate Begeman30a0de92008-07-17 16:51:19 +00008936 // We are handling one of the integer comparisons here. Since SSE only has
8937 // GT and EQ comparisons for integer, swapping operands and multiple
8938 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008939 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008940 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008941
Nate Begeman30a0de92008-07-17 16:51:19 +00008942 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008943 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008944 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008945 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008946 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008947 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008948 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008949 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008950 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008951 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008952 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008953 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008954 }
8955 if (Swap)
8956 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008957
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008958 // Check that the operation in question is available (most are plain SSE2,
8959 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008960 if (VT == MVT::v2i64) {
8961 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8962 return SDValue();
8963 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8964 return SDValue();
8965 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008966
Nate Begeman30a0de92008-07-17 16:51:19 +00008967 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8968 // bits of the inputs before performing those operations.
8969 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008970 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008971 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8972 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008973 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008974 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8975 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008976 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8977 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008978 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008979
Dale Johannesenace16102009-02-03 19:33:06 +00008980 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008981
8982 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008983 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008984 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008985
Nate Begeman30a0de92008-07-17 16:51:19 +00008986 return Result;
8987}
Evan Cheng0488db92007-09-25 01:57:46 +00008988
Evan Cheng370e5342008-12-03 08:38:43 +00008989// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008990static bool isX86LogicalCmp(SDValue Op) {
8991 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008992 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8993 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008994 return true;
8995 if (Op.getResNo() == 1 &&
8996 (Opc == X86ISD::ADD ||
8997 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008998 Opc == X86ISD::ADC ||
8999 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00009000 Opc == X86ISD::SMUL ||
9001 Opc == X86ISD::UMUL ||
9002 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00009003 Opc == X86ISD::DEC ||
9004 Opc == X86ISD::OR ||
9005 Opc == X86ISD::XOR ||
9006 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00009007 return true;
9008
Chris Lattner9637d5b2010-12-05 07:49:54 +00009009 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
9010 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009011
Dan Gohman076aee32009-03-04 19:44:21 +00009012 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00009013}
9014
Chris Lattnera2b56002010-12-05 01:23:24 +00009015static bool isZero(SDValue V) {
9016 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9017 return C && C->isNullValue();
9018}
9019
Chris Lattner96908b12010-12-05 02:00:51 +00009020static bool isAllOnes(SDValue V) {
9021 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
9022 return C && C->isAllOnesValue();
9023}
9024
Evan Chengb64dd5f2012-08-07 22:21:00 +00009025static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
9026 if (V.getOpcode() != ISD::TRUNCATE)
9027 return false;
9028
9029 SDValue VOp0 = V.getOperand(0);
9030 unsigned InBits = VOp0.getValueSizeInBits();
9031 unsigned Bits = V.getValueSizeInBits();
9032 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
9033}
9034
Dan Gohmand858e902010-04-17 15:26:15 +00009035SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009036 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009037 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00009038 SDValue Op1 = Op.getOperand(1);
9039 SDValue Op2 = Op.getOperand(2);
9040 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009041 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00009042
Dan Gohman1a492952009-10-20 16:22:37 +00009043 if (Cond.getOpcode() == ISD::SETCC) {
9044 SDValue NewCond = LowerSETCC(Cond, DAG);
9045 if (NewCond.getNode())
9046 Cond = NewCond;
9047 }
Evan Cheng734503b2006-09-11 02:19:56 +00009048
Chris Lattnera2b56002010-12-05 01:23:24 +00009049 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009050 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00009051 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00009052 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009053 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00009054 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
9055 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009056 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009057
Chris Lattnera2b56002010-12-05 01:23:24 +00009058 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009059
9060 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00009061 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
9062 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00009063
9064 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00009065 // Apply further optimizations for special cases
9066 // (select (x != 0), -1, 0) -> neg & sbb
9067 // (select (x == 0), 0, -1) -> neg & sbb
9068 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00009069 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00009070 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
9071 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00009072 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
9073 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00009074 CmpOp0);
9075 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9076 DAG.getConstant(X86::COND_B, MVT::i8),
9077 SDValue(Neg.getNode(), 1));
9078 return Res;
9079 }
9080
Chris Lattnera2b56002010-12-05 01:23:24 +00009081 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
9082 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009083 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009084
Chris Lattner96908b12010-12-05 02:00:51 +00009085 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00009086 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9087 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009088
Chris Lattner96908b12010-12-05 02:00:51 +00009089 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
9090 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009091
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009092 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00009093 if (N2C == 0 || !N2C->isNullValue())
9094 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
9095 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009096 }
9097 }
9098
Chris Lattnera2b56002010-12-05 01:23:24 +00009099 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00009100 if (Cond.getOpcode() == ISD::AND &&
9101 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9102 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009103 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009104 Cond = Cond.getOperand(0);
9105 }
9106
Evan Cheng3f41d662007-10-08 22:16:29 +00009107 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9108 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009109 unsigned CondOpcode = Cond.getOpcode();
9110 if (CondOpcode == X86ISD::SETCC ||
9111 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009112 CC = Cond.getOperand(0);
9113
Dan Gohman475871a2008-07-27 21:46:04 +00009114 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009115 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00009116 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00009117
Evan Cheng3f41d662007-10-08 22:16:29 +00009118 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009119 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00009120 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009121 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009122
Chris Lattnerd1980a52009-03-12 06:52:53 +00009123 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9124 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009125 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009126 addTest = false;
9127 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009128 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9129 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9130 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9131 Cond.getOperand(0).getValueType() != MVT::i8)) {
9132 SDValue LHS = Cond.getOperand(0);
9133 SDValue RHS = Cond.getOperand(1);
9134 unsigned X86Opcode;
9135 unsigned X86Cond;
9136 SDVTList VTs;
9137 switch (CondOpcode) {
9138 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9139 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9140 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9141 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9142 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9143 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9144 default: llvm_unreachable("unexpected overflowing operator");
9145 }
9146 if (CondOpcode == ISD::UMULO)
9147 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9148 MVT::i32);
9149 else
9150 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9151
9152 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9153
9154 if (CondOpcode == ISD::UMULO)
9155 Cond = X86Op.getValue(2);
9156 else
9157 Cond = X86Op.getValue(1);
9158
9159 CC = DAG.getConstant(X86Cond, MVT::i8);
9160 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009161 }
9162
9163 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009164 // Look pass the truncate if the high bits are known zero.
9165 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9166 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009167
9168 // We know the result of AND is compared against zero. Try to match
9169 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009170 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009171 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009172 if (NewSetCC.getNode()) {
9173 CC = NewSetCC.getOperand(0);
9174 Cond = NewSetCC.getOperand(1);
9175 addTest = false;
9176 }
9177 }
9178 }
9179
9180 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009181 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009182 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009183 }
9184
Benjamin Kramere915ff32010-12-22 23:09:28 +00009185 // a < b ? -1 : 0 -> RES = ~setcc_carry
9186 // a < b ? 0 : -1 -> RES = setcc_carry
9187 // a >= b ? -1 : 0 -> RES = setcc_carry
9188 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009189 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009190 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009191 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9192
9193 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9194 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9195 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9196 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9197 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9198 return DAG.getNOT(DL, Res, Res.getValueType());
9199 return Res;
9200 }
9201 }
9202
Evan Cheng0488db92007-09-25 01:57:46 +00009203 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9204 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009205 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009206 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009207 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009208}
9209
Evan Cheng370e5342008-12-03 08:38:43 +00009210// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9211// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9212// from the AND / OR.
9213static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9214 Opc = Op.getOpcode();
9215 if (Opc != ISD::OR && Opc != ISD::AND)
9216 return false;
9217 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9218 Op.getOperand(0).hasOneUse() &&
9219 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9220 Op.getOperand(1).hasOneUse());
9221}
9222
Evan Cheng961d6d42009-02-02 08:19:07 +00009223// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9224// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009225static bool isXor1OfSetCC(SDValue Op) {
9226 if (Op.getOpcode() != ISD::XOR)
9227 return false;
9228 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9229 if (N1C && N1C->getAPIntValue() == 1) {
9230 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9231 Op.getOperand(0).hasOneUse();
9232 }
9233 return false;
9234}
9235
Dan Gohmand858e902010-04-17 15:26:15 +00009236SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009237 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009238 SDValue Chain = Op.getOperand(0);
9239 SDValue Cond = Op.getOperand(1);
9240 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009241 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009242 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009243 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009244
Dan Gohman1a492952009-10-20 16:22:37 +00009245 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009246 // Check for setcc([su]{add,sub,mul}o == 0).
9247 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9248 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9249 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9250 Cond.getOperand(0).getResNo() == 1 &&
9251 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9252 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9253 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9254 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9255 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9256 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9257 Inverted = true;
9258 Cond = Cond.getOperand(0);
9259 } else {
9260 SDValue NewCond = LowerSETCC(Cond, DAG);
9261 if (NewCond.getNode())
9262 Cond = NewCond;
9263 }
Dan Gohman1a492952009-10-20 16:22:37 +00009264 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009265#if 0
9266 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009267 else if (Cond.getOpcode() == X86ISD::ADD ||
9268 Cond.getOpcode() == X86ISD::SUB ||
9269 Cond.getOpcode() == X86ISD::SMUL ||
9270 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009271 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009272#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009273
Evan Chengad9c0a32009-12-15 00:53:42 +00009274 // Look pass (and (setcc_carry (cmp ...)), 1).
9275 if (Cond.getOpcode() == ISD::AND &&
9276 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9277 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009278 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009279 Cond = Cond.getOperand(0);
9280 }
9281
Evan Cheng3f41d662007-10-08 22:16:29 +00009282 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9283 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009284 unsigned CondOpcode = Cond.getOpcode();
9285 if (CondOpcode == X86ISD::SETCC ||
9286 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009287 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009288
Dan Gohman475871a2008-07-27 21:46:04 +00009289 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009290 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009291 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009292 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009293 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009294 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009295 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009296 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009297 default: break;
9298 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009299 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009300 // These can only come from an arithmetic instruction with overflow,
9301 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009302 Cond = Cond.getNode()->getOperand(1);
9303 addTest = false;
9304 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009305 }
Evan Cheng0488db92007-09-25 01:57:46 +00009306 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009307 }
9308 CondOpcode = Cond.getOpcode();
9309 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9310 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9311 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9312 Cond.getOperand(0).getValueType() != MVT::i8)) {
9313 SDValue LHS = Cond.getOperand(0);
9314 SDValue RHS = Cond.getOperand(1);
9315 unsigned X86Opcode;
9316 unsigned X86Cond;
9317 SDVTList VTs;
9318 switch (CondOpcode) {
9319 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9320 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9321 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9322 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9323 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9324 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9325 default: llvm_unreachable("unexpected overflowing operator");
9326 }
9327 if (Inverted)
9328 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9329 if (CondOpcode == ISD::UMULO)
9330 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9331 MVT::i32);
9332 else
9333 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9334
9335 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9336
9337 if (CondOpcode == ISD::UMULO)
9338 Cond = X86Op.getValue(2);
9339 else
9340 Cond = X86Op.getValue(1);
9341
9342 CC = DAG.getConstant(X86Cond, MVT::i8);
9343 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009344 } else {
9345 unsigned CondOpc;
9346 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9347 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009348 if (CondOpc == ISD::OR) {
9349 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9350 // two branches instead of an explicit OR instruction with a
9351 // separate test.
9352 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009353 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009354 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009355 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009356 Chain, Dest, CC, Cmp);
9357 CC = Cond.getOperand(1).getOperand(0);
9358 Cond = Cmp;
9359 addTest = false;
9360 }
9361 } else { // ISD::AND
9362 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9363 // two branches instead of an explicit AND instruction with a
9364 // separate test. However, we only do this if this block doesn't
9365 // have a fall-through edge, because this requires an explicit
9366 // jmp when the condition is false.
9367 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009368 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009369 Op.getNode()->hasOneUse()) {
9370 X86::CondCode CCode =
9371 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9372 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009373 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009374 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009375 // Look for an unconditional branch following this conditional branch.
9376 // We need this because we need to reverse the successors in order
9377 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009378 if (User->getOpcode() == ISD::BR) {
9379 SDValue FalseBB = User->getOperand(1);
9380 SDNode *NewBR =
9381 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009382 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009383 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009384 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009385
Dale Johannesene4d209d2009-02-03 20:21:25 +00009386 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009387 Chain, Dest, CC, Cmp);
9388 X86::CondCode CCode =
9389 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9390 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009391 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009392 Cond = Cmp;
9393 addTest = false;
9394 }
9395 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009396 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009397 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9398 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9399 // It should be transformed during dag combiner except when the condition
9400 // is set by a arithmetics with overflow node.
9401 X86::CondCode CCode =
9402 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9403 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009404 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009405 Cond = Cond.getOperand(0).getOperand(1);
9406 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009407 } else if (Cond.getOpcode() == ISD::SETCC &&
9408 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9409 // For FCMP_OEQ, we can emit
9410 // two branches instead of an explicit AND instruction with a
9411 // separate test. However, we only do this if this block doesn't
9412 // have a fall-through edge, because this requires an explicit
9413 // jmp when the condition is false.
9414 if (Op.getNode()->hasOneUse()) {
9415 SDNode *User = *Op.getNode()->use_begin();
9416 // Look for an unconditional branch following this conditional branch.
9417 // We need this because we need to reverse the successors in order
9418 // to implement FCMP_OEQ.
9419 if (User->getOpcode() == ISD::BR) {
9420 SDValue FalseBB = User->getOperand(1);
9421 SDNode *NewBR =
9422 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9423 assert(NewBR == User);
9424 (void)NewBR;
9425 Dest = FalseBB;
9426
9427 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9428 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009429 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009430 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9431 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9432 Chain, Dest, CC, Cmp);
9433 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9434 Cond = Cmp;
9435 addTest = false;
9436 }
9437 }
9438 } else if (Cond.getOpcode() == ISD::SETCC &&
9439 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9440 // For FCMP_UNE, we can emit
9441 // two branches instead of an explicit AND instruction with a
9442 // separate test. However, we only do this if this block doesn't
9443 // have a fall-through edge, because this requires an explicit
9444 // jmp when the condition is false.
9445 if (Op.getNode()->hasOneUse()) {
9446 SDNode *User = *Op.getNode()->use_begin();
9447 // Look for an unconditional branch following this conditional branch.
9448 // We need this because we need to reverse the successors in order
9449 // to implement FCMP_UNE.
9450 if (User->getOpcode() == ISD::BR) {
9451 SDValue FalseBB = User->getOperand(1);
9452 SDNode *NewBR =
9453 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9454 assert(NewBR == User);
9455 (void)NewBR;
9456
9457 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9458 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009459 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009460 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9461 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9462 Chain, Dest, CC, Cmp);
9463 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9464 Cond = Cmp;
9465 addTest = false;
9466 Dest = FalseBB;
9467 }
9468 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009469 }
Evan Cheng0488db92007-09-25 01:57:46 +00009470 }
9471
9472 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009473 // Look pass the truncate if the high bits are known zero.
9474 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9475 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009476
9477 // We know the result of AND is compared against zero. Try to match
9478 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009479 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009480 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9481 if (NewSetCC.getNode()) {
9482 CC = NewSetCC.getOperand(0);
9483 Cond = NewSetCC.getOperand(1);
9484 addTest = false;
9485 }
9486 }
9487 }
9488
9489 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009490 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009491 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009492 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009493 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009494 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009495 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009496}
9497
Anton Korobeynikove060b532007-04-17 19:34:00 +00009498
9499// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9500// Calls to _alloca is needed to probe the stack when allocating more than 4k
9501// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9502// that the guard pages used by the OS virtual memory manager are allocated in
9503// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009504SDValue
9505X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009506 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009507 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009508 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009509 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009510 "are being used");
9511 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009512 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009513
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009514 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009515 SDValue Chain = Op.getOperand(0);
9516 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009517 // FIXME: Ensure alignment here
9518
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009519 bool Is64Bit = Subtarget->is64Bit();
9520 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009521
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009522 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009523 MachineFunction &MF = DAG.getMachineFunction();
9524 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009525
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009526 if (Is64Bit) {
9527 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009528 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009529 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009530
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009531 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009532 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009533 if (I->hasNestAttr())
9534 report_fatal_error("Cannot use segmented stacks with functions that "
9535 "have nested arguments.");
9536 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009537
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009538 const TargetRegisterClass *AddrRegClass =
9539 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9540 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9541 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9542 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9543 DAG.getRegister(Vreg, SPTy));
9544 SDValue Ops1[2] = { Value, Chain };
9545 return DAG.getMergeValues(Ops1, 2, dl);
9546 } else {
9547 SDValue Flag;
9548 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009549
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009550 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9551 Flag = Chain.getValue(1);
9552 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009553
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009554 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9555 Flag = Chain.getValue(1);
9556
9557 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9558
9559 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9560 return DAG.getMergeValues(Ops1, 2, dl);
9561 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009562}
9563
Dan Gohmand858e902010-04-17 15:26:15 +00009564SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009565 MachineFunction &MF = DAG.getMachineFunction();
9566 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9567
Dan Gohman69de1932008-02-06 22:27:42 +00009568 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009569 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009570
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009571 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009572 // vastart just stores the address of the VarArgsFrameIndex slot into the
9573 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009574 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9575 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009576 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9577 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009578 }
9579
9580 // __va_list_tag:
9581 // gp_offset (0 - 6 * 8)
9582 // fp_offset (48 - 48 + 8 * 16)
9583 // overflow_arg_area (point to parameters coming in memory).
9584 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009585 SmallVector<SDValue, 8> MemOps;
9586 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009587 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009588 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009589 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9590 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009591 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009592 MemOps.push_back(Store);
9593
9594 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009595 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009596 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009597 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009598 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9599 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009600 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009601 MemOps.push_back(Store);
9602
9603 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009604 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009605 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009606 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9607 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009608 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9609 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009610 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009611 MemOps.push_back(Store);
9612
9613 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009614 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009615 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009616 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9617 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009618 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9619 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009620 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009621 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009622 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009623}
9624
Dan Gohmand858e902010-04-17 15:26:15 +00009625SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009626 assert(Subtarget->is64Bit() &&
9627 "LowerVAARG only handles 64-bit va_arg!");
9628 assert((Subtarget->isTargetLinux() ||
9629 Subtarget->isTargetDarwin()) &&
9630 "Unhandled target in LowerVAARG");
9631 assert(Op.getNode()->getNumOperands() == 4);
9632 SDValue Chain = Op.getOperand(0);
9633 SDValue SrcPtr = Op.getOperand(1);
9634 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9635 unsigned Align = Op.getConstantOperandVal(3);
9636 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009637
Dan Gohman320afb82010-10-12 18:00:49 +00009638 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009639 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009640 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9641 uint8_t ArgMode;
9642
9643 // Decide which area this value should be read from.
9644 // TODO: Implement the AMD64 ABI in its entirety. This simple
9645 // selection mechanism works only for the basic types.
9646 if (ArgVT == MVT::f80) {
9647 llvm_unreachable("va_arg for f80 not yet implemented");
9648 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9649 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9650 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9651 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9652 } else {
9653 llvm_unreachable("Unhandled argument type in LowerVAARG");
9654 }
9655
9656 if (ArgMode == 2) {
9657 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009658 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009659 !(DAG.getMachineFunction()
9660 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009661 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009662 }
9663
9664 // Insert VAARG_64 node into the DAG
9665 // VAARG_64 returns two values: Variable Argument Address, Chain
9666 SmallVector<SDValue, 11> InstOps;
9667 InstOps.push_back(Chain);
9668 InstOps.push_back(SrcPtr);
9669 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9670 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9671 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9672 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9673 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9674 VTs, &InstOps[0], InstOps.size(),
9675 MVT::i64,
9676 MachinePointerInfo(SV),
9677 /*Align=*/0,
9678 /*Volatile=*/false,
9679 /*ReadMem=*/true,
9680 /*WriteMem=*/true);
9681 Chain = VAARG.getValue(1);
9682
9683 // Load the next argument and return it
9684 return DAG.getLoad(ArgVT, dl,
9685 Chain,
9686 VAARG,
9687 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009688 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009689}
9690
Craig Topper55b24052012-09-11 06:15:32 +00009691static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
9692 SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00009693 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009694 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009695 SDValue Chain = Op.getOperand(0);
9696 SDValue DstPtr = Op.getOperand(1);
9697 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009698 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9699 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009700 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009701
Chris Lattnere72f2022010-09-21 05:40:29 +00009702 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009703 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009704 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009705 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009706}
9707
Craig Topper80e46362012-01-23 06:16:53 +00009708// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9709// may or may not be a constant. Takes immediate version of shift as input.
9710static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9711 SDValue SrcOp, SDValue ShAmt,
9712 SelectionDAG &DAG) {
9713 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9714
9715 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009716 // Constant may be a TargetConstant. Use a regular constant.
9717 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009718 switch (Opc) {
9719 default: llvm_unreachable("Unknown target vector shift node");
9720 case X86ISD::VSHLI:
9721 case X86ISD::VSRLI:
9722 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009723 return DAG.getNode(Opc, dl, VT, SrcOp,
9724 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009725 }
9726 }
9727
9728 // Change opcode to non-immediate version
9729 switch (Opc) {
9730 default: llvm_unreachable("Unknown target vector shift node");
9731 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9732 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9733 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9734 }
9735
9736 // Need to build a vector containing shift amount
9737 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9738 SDValue ShOps[4];
9739 ShOps[0] = ShAmt;
9740 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009741 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009742 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009743
9744 // The return type has to be a 128-bit type with the same element
9745 // type as the input type.
9746 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9747 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9748
9749 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009750 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9751}
9752
Craig Topper55b24052012-09-11 06:15:32 +00009753static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009754 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009755 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009756 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009757 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009758 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009759 case Intrinsic::x86_sse_comieq_ss:
9760 case Intrinsic::x86_sse_comilt_ss:
9761 case Intrinsic::x86_sse_comile_ss:
9762 case Intrinsic::x86_sse_comigt_ss:
9763 case Intrinsic::x86_sse_comige_ss:
9764 case Intrinsic::x86_sse_comineq_ss:
9765 case Intrinsic::x86_sse_ucomieq_ss:
9766 case Intrinsic::x86_sse_ucomilt_ss:
9767 case Intrinsic::x86_sse_ucomile_ss:
9768 case Intrinsic::x86_sse_ucomigt_ss:
9769 case Intrinsic::x86_sse_ucomige_ss:
9770 case Intrinsic::x86_sse_ucomineq_ss:
9771 case Intrinsic::x86_sse2_comieq_sd:
9772 case Intrinsic::x86_sse2_comilt_sd:
9773 case Intrinsic::x86_sse2_comile_sd:
9774 case Intrinsic::x86_sse2_comigt_sd:
9775 case Intrinsic::x86_sse2_comige_sd:
9776 case Intrinsic::x86_sse2_comineq_sd:
9777 case Intrinsic::x86_sse2_ucomieq_sd:
9778 case Intrinsic::x86_sse2_ucomilt_sd:
9779 case Intrinsic::x86_sse2_ucomile_sd:
9780 case Intrinsic::x86_sse2_ucomigt_sd:
9781 case Intrinsic::x86_sse2_ucomige_sd:
9782 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009783 unsigned Opc;
9784 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009785 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009786 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009787 case Intrinsic::x86_sse_comieq_ss:
9788 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009789 Opc = X86ISD::COMI;
9790 CC = ISD::SETEQ;
9791 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009792 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009793 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009794 Opc = X86ISD::COMI;
9795 CC = ISD::SETLT;
9796 break;
9797 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009798 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009799 Opc = X86ISD::COMI;
9800 CC = ISD::SETLE;
9801 break;
9802 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009803 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009804 Opc = X86ISD::COMI;
9805 CC = ISD::SETGT;
9806 break;
9807 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009808 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009809 Opc = X86ISD::COMI;
9810 CC = ISD::SETGE;
9811 break;
9812 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009813 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009814 Opc = X86ISD::COMI;
9815 CC = ISD::SETNE;
9816 break;
9817 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009818 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009819 Opc = X86ISD::UCOMI;
9820 CC = ISD::SETEQ;
9821 break;
9822 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009823 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009824 Opc = X86ISD::UCOMI;
9825 CC = ISD::SETLT;
9826 break;
9827 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009828 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009829 Opc = X86ISD::UCOMI;
9830 CC = ISD::SETLE;
9831 break;
9832 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009833 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009834 Opc = X86ISD::UCOMI;
9835 CC = ISD::SETGT;
9836 break;
9837 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009838 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009839 Opc = X86ISD::UCOMI;
9840 CC = ISD::SETGE;
9841 break;
9842 case Intrinsic::x86_sse_ucomineq_ss:
9843 case Intrinsic::x86_sse2_ucomineq_sd:
9844 Opc = X86ISD::UCOMI;
9845 CC = ISD::SETNE;
9846 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009847 }
Evan Cheng734503b2006-09-11 02:19:56 +00009848
Dan Gohman475871a2008-07-27 21:46:04 +00009849 SDValue LHS = Op.getOperand(1);
9850 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009851 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009852 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009853 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9854 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9855 DAG.getConstant(X86CC, MVT::i8), Cond);
9856 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009857 }
Craig Topper6d688152012-08-14 07:43:25 +00009858
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009859 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009860 case Intrinsic::x86_sse2_pmulu_dq:
9861 case Intrinsic::x86_avx2_pmulu_dq:
9862 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9863 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009864
9865 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009866 case Intrinsic::x86_sse3_hadd_ps:
9867 case Intrinsic::x86_sse3_hadd_pd:
9868 case Intrinsic::x86_avx_hadd_ps_256:
9869 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009870 case Intrinsic::x86_sse3_hsub_ps:
9871 case Intrinsic::x86_sse3_hsub_pd:
9872 case Intrinsic::x86_avx_hsub_ps_256:
9873 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009874 case Intrinsic::x86_ssse3_phadd_w_128:
9875 case Intrinsic::x86_ssse3_phadd_d_128:
9876 case Intrinsic::x86_avx2_phadd_w:
9877 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009878 case Intrinsic::x86_ssse3_phsub_w_128:
9879 case Intrinsic::x86_ssse3_phsub_d_128:
9880 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009881 case Intrinsic::x86_avx2_phsub_d: {
9882 unsigned Opcode;
9883 switch (IntNo) {
9884 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9885 case Intrinsic::x86_sse3_hadd_ps:
9886 case Intrinsic::x86_sse3_hadd_pd:
9887 case Intrinsic::x86_avx_hadd_ps_256:
9888 case Intrinsic::x86_avx_hadd_pd_256:
9889 Opcode = X86ISD::FHADD;
9890 break;
9891 case Intrinsic::x86_sse3_hsub_ps:
9892 case Intrinsic::x86_sse3_hsub_pd:
9893 case Intrinsic::x86_avx_hsub_ps_256:
9894 case Intrinsic::x86_avx_hsub_pd_256:
9895 Opcode = X86ISD::FHSUB;
9896 break;
9897 case Intrinsic::x86_ssse3_phadd_w_128:
9898 case Intrinsic::x86_ssse3_phadd_d_128:
9899 case Intrinsic::x86_avx2_phadd_w:
9900 case Intrinsic::x86_avx2_phadd_d:
9901 Opcode = X86ISD::HADD;
9902 break;
9903 case Intrinsic::x86_ssse3_phsub_w_128:
9904 case Intrinsic::x86_ssse3_phsub_d_128:
9905 case Intrinsic::x86_avx2_phsub_w:
9906 case Intrinsic::x86_avx2_phsub_d:
9907 Opcode = X86ISD::HSUB;
9908 break;
9909 }
9910 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009911 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009912 }
9913
9914 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009915 case Intrinsic::x86_avx2_psllv_d:
9916 case Intrinsic::x86_avx2_psllv_q:
9917 case Intrinsic::x86_avx2_psllv_d_256:
9918 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009919 case Intrinsic::x86_avx2_psrlv_d:
9920 case Intrinsic::x86_avx2_psrlv_q:
9921 case Intrinsic::x86_avx2_psrlv_d_256:
9922 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009923 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009924 case Intrinsic::x86_avx2_psrav_d_256: {
9925 unsigned Opcode;
9926 switch (IntNo) {
9927 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9928 case Intrinsic::x86_avx2_psllv_d:
9929 case Intrinsic::x86_avx2_psllv_q:
9930 case Intrinsic::x86_avx2_psllv_d_256:
9931 case Intrinsic::x86_avx2_psllv_q_256:
9932 Opcode = ISD::SHL;
9933 break;
9934 case Intrinsic::x86_avx2_psrlv_d:
9935 case Intrinsic::x86_avx2_psrlv_q:
9936 case Intrinsic::x86_avx2_psrlv_d_256:
9937 case Intrinsic::x86_avx2_psrlv_q_256:
9938 Opcode = ISD::SRL;
9939 break;
9940 case Intrinsic::x86_avx2_psrav_d:
9941 case Intrinsic::x86_avx2_psrav_d_256:
9942 Opcode = ISD::SRA;
9943 break;
9944 }
9945 return DAG.getNode(Opcode, dl, Op.getValueType(),
9946 Op.getOperand(1), Op.getOperand(2));
9947 }
9948
Craig Topper969ba282012-01-25 06:43:11 +00009949 case Intrinsic::x86_ssse3_pshuf_b_128:
9950 case Intrinsic::x86_avx2_pshuf_b:
9951 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9952 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009953
Craig Topper969ba282012-01-25 06:43:11 +00009954 case Intrinsic::x86_ssse3_psign_b_128:
9955 case Intrinsic::x86_ssse3_psign_w_128:
9956 case Intrinsic::x86_ssse3_psign_d_128:
9957 case Intrinsic::x86_avx2_psign_b:
9958 case Intrinsic::x86_avx2_psign_w:
9959 case Intrinsic::x86_avx2_psign_d:
9960 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9961 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009962
Craig Toppere566cd02012-01-26 07:18:03 +00009963 case Intrinsic::x86_sse41_insertps:
9964 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9965 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009966
Craig Toppere566cd02012-01-26 07:18:03 +00009967 case Intrinsic::x86_avx_vperm2f128_ps_256:
9968 case Intrinsic::x86_avx_vperm2f128_pd_256:
9969 case Intrinsic::x86_avx_vperm2f128_si_256:
9970 case Intrinsic::x86_avx2_vperm2i128:
9971 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9972 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009973
Craig Topperffa6c402012-04-16 07:13:00 +00009974 case Intrinsic::x86_avx2_permd:
9975 case Intrinsic::x86_avx2_permps:
9976 // Operands intentionally swapped. Mask is last operand to intrinsic,
9977 // but second operand for node/intruction.
9978 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9979 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009980
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009981 // ptest and testp intrinsics. The intrinsic these come from are designed to
9982 // return an integer value, not just an instruction so lower it to the ptest
9983 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009984 case Intrinsic::x86_sse41_ptestz:
9985 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009986 case Intrinsic::x86_sse41_ptestnzc:
9987 case Intrinsic::x86_avx_ptestz_256:
9988 case Intrinsic::x86_avx_ptestc_256:
9989 case Intrinsic::x86_avx_ptestnzc_256:
9990 case Intrinsic::x86_avx_vtestz_ps:
9991 case Intrinsic::x86_avx_vtestc_ps:
9992 case Intrinsic::x86_avx_vtestnzc_ps:
9993 case Intrinsic::x86_avx_vtestz_pd:
9994 case Intrinsic::x86_avx_vtestc_pd:
9995 case Intrinsic::x86_avx_vtestnzc_pd:
9996 case Intrinsic::x86_avx_vtestz_ps_256:
9997 case Intrinsic::x86_avx_vtestc_ps_256:
9998 case Intrinsic::x86_avx_vtestnzc_ps_256:
9999 case Intrinsic::x86_avx_vtestz_pd_256:
10000 case Intrinsic::x86_avx_vtestc_pd_256:
10001 case Intrinsic::x86_avx_vtestnzc_pd_256: {
10002 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +000010003 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +000010004 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +000010005 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010006 case Intrinsic::x86_avx_vtestz_ps:
10007 case Intrinsic::x86_avx_vtestz_pd:
10008 case Intrinsic::x86_avx_vtestz_ps_256:
10009 case Intrinsic::x86_avx_vtestz_pd_256:
10010 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010011 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010012 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010013 // ZF = 1
10014 X86CC = X86::COND_E;
10015 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010016 case Intrinsic::x86_avx_vtestc_ps:
10017 case Intrinsic::x86_avx_vtestc_pd:
10018 case Intrinsic::x86_avx_vtestc_ps_256:
10019 case Intrinsic::x86_avx_vtestc_pd_256:
10020 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +000010021 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010022 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010023 // CF = 1
10024 X86CC = X86::COND_B;
10025 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010026 case Intrinsic::x86_avx_vtestnzc_ps:
10027 case Intrinsic::x86_avx_vtestnzc_pd:
10028 case Intrinsic::x86_avx_vtestnzc_ps_256:
10029 case Intrinsic::x86_avx_vtestnzc_pd_256:
10030 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +000010031 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010032 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +000010033 // ZF and CF = 0
10034 X86CC = X86::COND_A;
10035 break;
10036 }
Eric Christopherfd179292009-08-27 18:07:15 +000010037
Eric Christopher71c67532009-07-29 00:28:05 +000010038 SDValue LHS = Op.getOperand(1);
10039 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000010040 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
10041 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +000010042 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
10043 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
10044 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +000010045 }
Evan Cheng5759f972008-05-04 09:15:50 +000010046
Craig Topper80e46362012-01-23 06:16:53 +000010047 // SSE/AVX shift intrinsics
10048 case Intrinsic::x86_sse2_psll_w:
10049 case Intrinsic::x86_sse2_psll_d:
10050 case Intrinsic::x86_sse2_psll_q:
10051 case Intrinsic::x86_avx2_psll_w:
10052 case Intrinsic::x86_avx2_psll_d:
10053 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +000010054 case Intrinsic::x86_sse2_psrl_w:
10055 case Intrinsic::x86_sse2_psrl_d:
10056 case Intrinsic::x86_sse2_psrl_q:
10057 case Intrinsic::x86_avx2_psrl_w:
10058 case Intrinsic::x86_avx2_psrl_d:
10059 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +000010060 case Intrinsic::x86_sse2_psra_w:
10061 case Intrinsic::x86_sse2_psra_d:
10062 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +000010063 case Intrinsic::x86_avx2_psra_d: {
10064 unsigned Opcode;
10065 switch (IntNo) {
10066 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10067 case Intrinsic::x86_sse2_psll_w:
10068 case Intrinsic::x86_sse2_psll_d:
10069 case Intrinsic::x86_sse2_psll_q:
10070 case Intrinsic::x86_avx2_psll_w:
10071 case Intrinsic::x86_avx2_psll_d:
10072 case Intrinsic::x86_avx2_psll_q:
10073 Opcode = X86ISD::VSHL;
10074 break;
10075 case Intrinsic::x86_sse2_psrl_w:
10076 case Intrinsic::x86_sse2_psrl_d:
10077 case Intrinsic::x86_sse2_psrl_q:
10078 case Intrinsic::x86_avx2_psrl_w:
10079 case Intrinsic::x86_avx2_psrl_d:
10080 case Intrinsic::x86_avx2_psrl_q:
10081 Opcode = X86ISD::VSRL;
10082 break;
10083 case Intrinsic::x86_sse2_psra_w:
10084 case Intrinsic::x86_sse2_psra_d:
10085 case Intrinsic::x86_avx2_psra_w:
10086 case Intrinsic::x86_avx2_psra_d:
10087 Opcode = X86ISD::VSRA;
10088 break;
10089 }
10090 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010091 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +000010092 }
10093
10094 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +000010095 case Intrinsic::x86_sse2_pslli_w:
10096 case Intrinsic::x86_sse2_pslli_d:
10097 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010098 case Intrinsic::x86_avx2_pslli_w:
10099 case Intrinsic::x86_avx2_pslli_d:
10100 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010101 case Intrinsic::x86_sse2_psrli_w:
10102 case Intrinsic::x86_sse2_psrli_d:
10103 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +000010104 case Intrinsic::x86_avx2_psrli_w:
10105 case Intrinsic::x86_avx2_psrli_d:
10106 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +000010107 case Intrinsic::x86_sse2_psrai_w:
10108 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +000010109 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +000010110 case Intrinsic::x86_avx2_psrai_d: {
10111 unsigned Opcode;
10112 switch (IntNo) {
10113 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10114 case Intrinsic::x86_sse2_pslli_w:
10115 case Intrinsic::x86_sse2_pslli_d:
10116 case Intrinsic::x86_sse2_pslli_q:
10117 case Intrinsic::x86_avx2_pslli_w:
10118 case Intrinsic::x86_avx2_pslli_d:
10119 case Intrinsic::x86_avx2_pslli_q:
10120 Opcode = X86ISD::VSHLI;
10121 break;
10122 case Intrinsic::x86_sse2_psrli_w:
10123 case Intrinsic::x86_sse2_psrli_d:
10124 case Intrinsic::x86_sse2_psrli_q:
10125 case Intrinsic::x86_avx2_psrli_w:
10126 case Intrinsic::x86_avx2_psrli_d:
10127 case Intrinsic::x86_avx2_psrli_q:
10128 Opcode = X86ISD::VSRLI;
10129 break;
10130 case Intrinsic::x86_sse2_psrai_w:
10131 case Intrinsic::x86_sse2_psrai_d:
10132 case Intrinsic::x86_avx2_psrai_w:
10133 case Intrinsic::x86_avx2_psrai_d:
10134 Opcode = X86ISD::VSRAI;
10135 break;
10136 }
10137 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010138 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010139 }
10140
Craig Topper4feb6472012-08-06 06:22:36 +000010141 case Intrinsic::x86_sse42_pcmpistria128:
10142 case Intrinsic::x86_sse42_pcmpestria128:
10143 case Intrinsic::x86_sse42_pcmpistric128:
10144 case Intrinsic::x86_sse42_pcmpestric128:
10145 case Intrinsic::x86_sse42_pcmpistrio128:
10146 case Intrinsic::x86_sse42_pcmpestrio128:
10147 case Intrinsic::x86_sse42_pcmpistris128:
10148 case Intrinsic::x86_sse42_pcmpestris128:
10149 case Intrinsic::x86_sse42_pcmpistriz128:
10150 case Intrinsic::x86_sse42_pcmpestriz128: {
10151 unsigned Opcode;
10152 unsigned X86CC;
10153 switch (IntNo) {
10154 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10155 case Intrinsic::x86_sse42_pcmpistria128:
10156 Opcode = X86ISD::PCMPISTRI;
10157 X86CC = X86::COND_A;
10158 break;
10159 case Intrinsic::x86_sse42_pcmpestria128:
10160 Opcode = X86ISD::PCMPESTRI;
10161 X86CC = X86::COND_A;
10162 break;
10163 case Intrinsic::x86_sse42_pcmpistric128:
10164 Opcode = X86ISD::PCMPISTRI;
10165 X86CC = X86::COND_B;
10166 break;
10167 case Intrinsic::x86_sse42_pcmpestric128:
10168 Opcode = X86ISD::PCMPESTRI;
10169 X86CC = X86::COND_B;
10170 break;
10171 case Intrinsic::x86_sse42_pcmpistrio128:
10172 Opcode = X86ISD::PCMPISTRI;
10173 X86CC = X86::COND_O;
10174 break;
10175 case Intrinsic::x86_sse42_pcmpestrio128:
10176 Opcode = X86ISD::PCMPESTRI;
10177 X86CC = X86::COND_O;
10178 break;
10179 case Intrinsic::x86_sse42_pcmpistris128:
10180 Opcode = X86ISD::PCMPISTRI;
10181 X86CC = X86::COND_S;
10182 break;
10183 case Intrinsic::x86_sse42_pcmpestris128:
10184 Opcode = X86ISD::PCMPESTRI;
10185 X86CC = X86::COND_S;
10186 break;
10187 case Intrinsic::x86_sse42_pcmpistriz128:
10188 Opcode = X86ISD::PCMPISTRI;
10189 X86CC = X86::COND_E;
10190 break;
10191 case Intrinsic::x86_sse42_pcmpestriz128:
10192 Opcode = X86ISD::PCMPESTRI;
10193 X86CC = X86::COND_E;
10194 break;
10195 }
10196 SmallVector<SDValue, 5> NewOps;
10197 NewOps.append(Op->op_begin()+1, Op->op_end());
10198 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10199 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10200 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10201 DAG.getConstant(X86CC, MVT::i8),
10202 SDValue(PCMP.getNode(), 1));
10203 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10204 }
Craig Topper6d688152012-08-14 07:43:25 +000010205
Craig Topper4feb6472012-08-06 06:22:36 +000010206 case Intrinsic::x86_sse42_pcmpistri128:
10207 case Intrinsic::x86_sse42_pcmpestri128: {
10208 unsigned Opcode;
10209 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10210 Opcode = X86ISD::PCMPISTRI;
10211 else
10212 Opcode = X86ISD::PCMPESTRI;
10213
10214 SmallVector<SDValue, 5> NewOps;
10215 NewOps.append(Op->op_begin()+1, Op->op_end());
10216 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10217 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10218 }
Craig Topper0e292372012-08-24 04:03:22 +000010219 case Intrinsic::x86_fma_vfmadd_ps:
10220 case Intrinsic::x86_fma_vfmadd_pd:
10221 case Intrinsic::x86_fma_vfmsub_ps:
10222 case Intrinsic::x86_fma_vfmsub_pd:
10223 case Intrinsic::x86_fma_vfnmadd_ps:
10224 case Intrinsic::x86_fma_vfnmadd_pd:
10225 case Intrinsic::x86_fma_vfnmsub_ps:
10226 case Intrinsic::x86_fma_vfnmsub_pd:
10227 case Intrinsic::x86_fma_vfmaddsub_ps:
10228 case Intrinsic::x86_fma_vfmaddsub_pd:
10229 case Intrinsic::x86_fma_vfmsubadd_ps:
10230 case Intrinsic::x86_fma_vfmsubadd_pd:
10231 case Intrinsic::x86_fma_vfmadd_ps_256:
10232 case Intrinsic::x86_fma_vfmadd_pd_256:
10233 case Intrinsic::x86_fma_vfmsub_ps_256:
10234 case Intrinsic::x86_fma_vfmsub_pd_256:
10235 case Intrinsic::x86_fma_vfnmadd_ps_256:
10236 case Intrinsic::x86_fma_vfnmadd_pd_256:
10237 case Intrinsic::x86_fma_vfnmsub_ps_256:
10238 case Intrinsic::x86_fma_vfnmsub_pd_256:
10239 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10240 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10241 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10242 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010243 unsigned Opc;
10244 switch (IntNo) {
10245 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10246 case Intrinsic::x86_fma_vfmadd_ps:
10247 case Intrinsic::x86_fma_vfmadd_pd:
10248 case Intrinsic::x86_fma_vfmadd_ps_256:
10249 case Intrinsic::x86_fma_vfmadd_pd_256:
10250 Opc = X86ISD::FMADD;
10251 break;
10252 case Intrinsic::x86_fma_vfmsub_ps:
10253 case Intrinsic::x86_fma_vfmsub_pd:
10254 case Intrinsic::x86_fma_vfmsub_ps_256:
10255 case Intrinsic::x86_fma_vfmsub_pd_256:
10256 Opc = X86ISD::FMSUB;
10257 break;
10258 case Intrinsic::x86_fma_vfnmadd_ps:
10259 case Intrinsic::x86_fma_vfnmadd_pd:
10260 case Intrinsic::x86_fma_vfnmadd_ps_256:
10261 case Intrinsic::x86_fma_vfnmadd_pd_256:
10262 Opc = X86ISD::FNMADD;
10263 break;
10264 case Intrinsic::x86_fma_vfnmsub_ps:
10265 case Intrinsic::x86_fma_vfnmsub_pd:
10266 case Intrinsic::x86_fma_vfnmsub_ps_256:
10267 case Intrinsic::x86_fma_vfnmsub_pd_256:
10268 Opc = X86ISD::FNMSUB;
10269 break;
10270 case Intrinsic::x86_fma_vfmaddsub_ps:
10271 case Intrinsic::x86_fma_vfmaddsub_pd:
10272 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10273 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10274 Opc = X86ISD::FMADDSUB;
10275 break;
10276 case Intrinsic::x86_fma_vfmsubadd_ps:
10277 case Intrinsic::x86_fma_vfmsubadd_pd:
10278 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10279 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10280 Opc = X86ISD::FMSUBADD;
10281 break;
10282 }
10283
10284 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10285 Op.getOperand(2), Op.getOperand(3));
10286 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010288}
Evan Cheng72261582005-12-20 06:22:03 +000010289
Craig Topper55b24052012-09-11 06:15:32 +000010290static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) {
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010291 DebugLoc dl = Op.getDebugLoc();
10292 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10293 switch (IntNo) {
10294 default: return SDValue(); // Don't custom lower most intrinsics.
10295
10296 // RDRAND intrinsics.
10297 case Intrinsic::x86_rdrand_16:
10298 case Intrinsic::x86_rdrand_32:
10299 case Intrinsic::x86_rdrand_64: {
10300 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010301 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10302 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010303
10304 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10305 // return the value from Rand, which is always 0, casted to i32.
10306 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10307 DAG.getConstant(1, Op->getValueType(1)),
10308 DAG.getConstant(X86::COND_B, MVT::i32),
10309 SDValue(Result.getNode(), 1) };
10310 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10311 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10312 Ops, 4);
10313
10314 // Return { result, isValid, chain }.
10315 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010316 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010317 }
10318 }
10319}
10320
Dan Gohmand858e902010-04-17 15:26:15 +000010321SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10322 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010323 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10324 MFI->setReturnAddressIsTaken(true);
10325
Bill Wendling64e87322009-01-16 19:25:27 +000010326 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010327 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010328
10329 if (Depth > 0) {
10330 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10331 SDValue Offset =
10332 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010333 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010334 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010335 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010336 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010337 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010338 }
10339
10340 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010341 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010342 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010343 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010344}
10345
Dan Gohmand858e902010-04-17 15:26:15 +000010346SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010347 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10348 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010349
Owen Andersone50ed302009-08-10 22:56:29 +000010350 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010351 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010352 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10353 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010354 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010355 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010356 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10357 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010358 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010359 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010360}
10361
Dan Gohman475871a2008-07-27 21:46:04 +000010362SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010363 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010364 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010365}
10366
Dan Gohmand858e902010-04-17 15:26:15 +000010367SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010368 SDValue Chain = Op.getOperand(0);
10369 SDValue Offset = Op.getOperand(1);
10370 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010371 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010372
Dan Gohmand8816272010-08-11 18:14:00 +000010373 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10374 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10375 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010376 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010377
Dan Gohmand8816272010-08-11 18:14:00 +000010378 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
10379 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010380 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010381 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10382 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010383 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010384
Dale Johannesene4d209d2009-02-03 20:21:25 +000010385 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010386 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010387 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010388}
10389
Craig Topper55b24052012-09-11 06:15:32 +000010390static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
Duncan Sands4a544a72011-09-06 13:37:06 +000010391 return Op.getOperand(0);
10392}
10393
10394SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10395 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010396 SDValue Root = Op.getOperand(0);
10397 SDValue Trmp = Op.getOperand(1); // trampoline
10398 SDValue FPtr = Op.getOperand(2); // nested function
10399 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010400 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010401
Dan Gohman69de1932008-02-06 22:27:42 +000010402 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010403
10404 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010405 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010406
10407 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010408 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10409 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010410
Evan Cheng0e6a0522011-07-18 20:57:22 +000010411 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10412 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010413
10414 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10415
10416 // Load the pointer to the nested function into R11.
10417 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010418 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010419 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010420 Addr, MachinePointerInfo(TrmpAddr),
10421 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010422
Owen Anderson825b72b2009-08-11 20:47:22 +000010423 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10424 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010425 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10426 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010427 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010428
10429 // Load the 'nest' parameter value into R10.
10430 // R10 is specified in X86CallingConv.td
10431 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010432 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10433 DAG.getConstant(10, MVT::i64));
10434 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010435 Addr, MachinePointerInfo(TrmpAddr, 10),
10436 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010437
Owen Anderson825b72b2009-08-11 20:47:22 +000010438 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10439 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010440 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10441 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010442 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010443
10444 // Jump to the nested function.
10445 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010446 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10447 DAG.getConstant(20, MVT::i64));
10448 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010449 Addr, MachinePointerInfo(TrmpAddr, 20),
10450 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010451
10452 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010453 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10454 DAG.getConstant(22, MVT::i64));
10455 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010456 MachinePointerInfo(TrmpAddr, 22),
10457 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010458
Duncan Sands4a544a72011-09-06 13:37:06 +000010459 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010460 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010461 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010462 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010463 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010464 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010465
10466 switch (CC) {
10467 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010468 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010469 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010470 case CallingConv::X86_StdCall: {
10471 // Pass 'nest' parameter in ECX.
10472 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010473 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010474
10475 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010476 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010477 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010478
Chris Lattner58d74912008-03-12 17:45:29 +000010479 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010480 unsigned InRegCount = 0;
10481 unsigned Idx = 1;
10482
10483 for (FunctionType::param_iterator I = FTy->param_begin(),
10484 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010485 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010486 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010487 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010488
10489 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010490 report_fatal_error("Nest register in use - reduce number of inreg"
10491 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010492 }
10493 }
10494 break;
10495 }
10496 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010497 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010498 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010499 // Pass 'nest' parameter in EAX.
10500 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010501 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010502 break;
10503 }
10504
Dan Gohman475871a2008-07-27 21:46:04 +000010505 SDValue OutChains[4];
10506 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010507
Owen Anderson825b72b2009-08-11 20:47:22 +000010508 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10509 DAG.getConstant(10, MVT::i32));
10510 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010511
Chris Lattnera62fe662010-02-05 19:20:30 +000010512 // This is storing the opcode for MOV32ri.
10513 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010514 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010515 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010516 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010517 Trmp, MachinePointerInfo(TrmpAddr),
10518 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010519
Owen Anderson825b72b2009-08-11 20:47:22 +000010520 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10521 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010522 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10523 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010524 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010525
Chris Lattnera62fe662010-02-05 19:20:30 +000010526 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010527 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10528 DAG.getConstant(5, MVT::i32));
10529 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010530 MachinePointerInfo(TrmpAddr, 5),
10531 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010532
Owen Anderson825b72b2009-08-11 20:47:22 +000010533 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10534 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010535 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10536 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010537 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010538
Duncan Sands4a544a72011-09-06 13:37:06 +000010539 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010540 }
10541}
10542
Dan Gohmand858e902010-04-17 15:26:15 +000010543SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10544 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010545 /*
10546 The rounding mode is in bits 11:10 of FPSR, and has the following
10547 settings:
10548 00 Round to nearest
10549 01 Round to -inf
10550 10 Round to +inf
10551 11 Round to 0
10552
10553 FLT_ROUNDS, on the other hand, expects the following:
10554 -1 Undefined
10555 0 Round to 0
10556 1 Round to nearest
10557 2 Round to +inf
10558 3 Round to -inf
10559
10560 To perform the conversion, we do:
10561 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10562 */
10563
10564 MachineFunction &MF = DAG.getMachineFunction();
10565 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010566 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010567 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010568 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010569 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010570
10571 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010572 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010573 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010574
Michael J. Spencerec38de22010-10-10 22:04:20 +000010575
Chris Lattner2156b792010-09-22 01:11:26 +000010576 MachineMemOperand *MMO =
10577 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10578 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010579
Chris Lattner2156b792010-09-22 01:11:26 +000010580 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10581 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10582 DAG.getVTList(MVT::Other),
10583 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010584
10585 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010586 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010587 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010588
10589 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010590 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010591 DAG.getNode(ISD::SRL, DL, MVT::i16,
10592 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010593 CWD, DAG.getConstant(0x800, MVT::i16)),
10594 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010595 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010596 DAG.getNode(ISD::SRL, DL, MVT::i16,
10597 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010598 CWD, DAG.getConstant(0x400, MVT::i16)),
10599 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010600
Dan Gohman475871a2008-07-27 21:46:04 +000010601 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010602 DAG.getNode(ISD::AND, DL, MVT::i16,
10603 DAG.getNode(ISD::ADD, DL, MVT::i16,
10604 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010605 DAG.getConstant(1, MVT::i16)),
10606 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010607
10608
Duncan Sands83ec4b62008-06-06 12:08:01 +000010609 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010610 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010611}
10612
Craig Topper55b24052012-09-11 06:15:32 +000010613static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010614 EVT VT = Op.getValueType();
10615 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010616 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010617 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010618
10619 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010620 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010621 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010622 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010623 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010624 }
Evan Cheng18efe262007-12-14 02:13:44 +000010625
Evan Cheng152804e2007-12-14 08:30:15 +000010626 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010627 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010628 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010629
10630 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010631 SDValue Ops[] = {
10632 Op,
10633 DAG.getConstant(NumBits+NumBits-1, OpVT),
10634 DAG.getConstant(X86::COND_E, MVT::i8),
10635 Op.getValue(1)
10636 };
10637 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010638
10639 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010640 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010641
Owen Anderson825b72b2009-08-11 20:47:22 +000010642 if (VT == MVT::i8)
10643 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010644 return Op;
10645}
10646
Craig Topper55b24052012-09-11 06:15:32 +000010647static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) {
Chandler Carruthacc068e2011-12-24 10:55:54 +000010648 EVT VT = Op.getValueType();
10649 EVT OpVT = VT;
10650 unsigned NumBits = VT.getSizeInBits();
10651 DebugLoc dl = Op.getDebugLoc();
10652
10653 Op = Op.getOperand(0);
10654 if (VT == MVT::i8) {
10655 // Zero extend to i32 since there is not an i8 bsr.
10656 OpVT = MVT::i32;
10657 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10658 }
10659
10660 // Issue a bsr (scan bits in reverse).
10661 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10662 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10663
10664 // And xor with NumBits-1.
10665 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10666
10667 if (VT == MVT::i8)
10668 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10669 return Op;
10670}
10671
Craig Topper55b24052012-09-11 06:15:32 +000010672static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010673 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010674 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010675 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010676 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010677
10678 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010679 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010680 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010681
10682 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010683 SDValue Ops[] = {
10684 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010685 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010686 DAG.getConstant(X86::COND_E, MVT::i8),
10687 Op.getValue(1)
10688 };
Chandler Carruth77821022011-12-24 12:12:34 +000010689 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010690}
10691
Craig Topper13894fa2011-08-24 06:14:18 +000010692// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10693// ones, and then concatenate the result back.
10694static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010695 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010696
Craig Topper7a9a28b2012-08-12 02:23:29 +000010697 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010698 "Unsupported value type for operation");
10699
Craig Topper66ddd152012-04-27 22:54:43 +000010700 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010701 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010702
10703 // Extract the LHS vectors
10704 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010705 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10706 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010707
10708 // Extract the RHS vectors
10709 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010710 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10711 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010712
10713 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10714 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10715
10716 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10717 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10718 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10719}
10720
Craig Topper55b24052012-09-11 06:15:32 +000010721static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010722 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010723 Op.getValueType().isInteger() &&
10724 "Only handle AVX 256-bit vector integer operation");
10725 return Lower256IntArith(Op, DAG);
10726}
10727
Craig Topper55b24052012-09-11 06:15:32 +000010728static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010729 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010730 Op.getValueType().isInteger() &&
10731 "Only handle AVX 256-bit vector integer operation");
10732 return Lower256IntArith(Op, DAG);
10733}
10734
Craig Topper55b24052012-09-11 06:15:32 +000010735static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
10736 SelectionDAG &DAG) {
Craig Topper13894fa2011-08-24 06:14:18 +000010737 EVT VT = Op.getValueType();
10738
10739 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010740 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010741 return Lower256IntArith(Op, DAG);
10742
Craig Topper5b209e82012-02-05 03:14:49 +000010743 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10744 "Only know how to lower V2I64/V4I64 multiply");
10745
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010746 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010747
Craig Topper5b209e82012-02-05 03:14:49 +000010748 // Ahi = psrlqi(a, 32);
10749 // Bhi = psrlqi(b, 32);
10750 //
10751 // AloBlo = pmuludq(a, b);
10752 // AloBhi = pmuludq(a, Bhi);
10753 // AhiBlo = pmuludq(Ahi, b);
10754
10755 // AloBhi = psllqi(AloBhi, 32);
10756 // AhiBlo = psllqi(AhiBlo, 32);
10757 // return AloBlo + AloBhi + AhiBlo;
10758
Craig Topperaaa643c2011-11-09 07:28:55 +000010759 SDValue A = Op.getOperand(0);
10760 SDValue B = Op.getOperand(1);
10761
Craig Topper5b209e82012-02-05 03:14:49 +000010762 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010763
Craig Topper5b209e82012-02-05 03:14:49 +000010764 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10765 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010766
Craig Topper5b209e82012-02-05 03:14:49 +000010767 // Bit cast to 32-bit vectors for MULUDQ
10768 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10769 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10770 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10771 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10772 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010773
Craig Topper5b209e82012-02-05 03:14:49 +000010774 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10775 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10776 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010777
Craig Topper5b209e82012-02-05 03:14:49 +000010778 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10779 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010780
Dale Johannesene4d209d2009-02-03 20:21:25 +000010781 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010782 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010783}
10784
Nadav Rotem43012222011-05-11 08:12:09 +000010785SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10786
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010787 EVT VT = Op.getValueType();
10788 DebugLoc dl = Op.getDebugLoc();
10789 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010790 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010791 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010792
Craig Topper1accb7e2012-01-10 06:54:16 +000010793 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010794 return SDValue();
10795
Nadav Rotem43012222011-05-11 08:12:09 +000010796 // Optimize shl/srl/sra with constant shift amount.
10797 if (isSplatVector(Amt.getNode())) {
10798 SDValue SclrAmt = Amt->getOperand(0);
10799 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10800 uint64_t ShiftAmt = C->getZExtValue();
10801
Craig Toppered2e13d2012-01-22 19:15:14 +000010802 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10803 (Subtarget->hasAVX2() &&
10804 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10805 if (Op.getOpcode() == ISD::SHL)
10806 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10807 DAG.getConstant(ShiftAmt, MVT::i32));
10808 if (Op.getOpcode() == ISD::SRL)
10809 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10810 DAG.getConstant(ShiftAmt, MVT::i32));
10811 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10812 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10813 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010814 }
10815
Craig Toppered2e13d2012-01-22 19:15:14 +000010816 if (VT == MVT::v16i8) {
10817 if (Op.getOpcode() == ISD::SHL) {
10818 // Make a large shift.
10819 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10820 DAG.getConstant(ShiftAmt, MVT::i32));
10821 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10822 // Zero out the rightmost bits.
10823 SmallVector<SDValue, 16> V(16,
10824 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10825 MVT::i8));
10826 return DAG.getNode(ISD::AND, dl, VT, SHL,
10827 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010828 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010829 if (Op.getOpcode() == ISD::SRL) {
10830 // Make a large shift.
10831 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10832 DAG.getConstant(ShiftAmt, MVT::i32));
10833 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10834 // Zero out the leftmost bits.
10835 SmallVector<SDValue, 16> V(16,
10836 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10837 MVT::i8));
10838 return DAG.getNode(ISD::AND, dl, VT, SRL,
10839 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10840 }
10841 if (Op.getOpcode() == ISD::SRA) {
10842 if (ShiftAmt == 7) {
10843 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010844 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010845 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010846 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010847
Craig Toppered2e13d2012-01-22 19:15:14 +000010848 // R s>> a === ((R u>> a) ^ m) - m
10849 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10850 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10851 MVT::i8));
10852 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10853 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10854 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10855 return Res;
10856 }
Craig Topper731dfd02012-04-23 03:42:40 +000010857 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010858 }
Craig Topper46154eb2011-11-11 07:39:23 +000010859
Craig Topper0d86d462011-11-20 00:12:05 +000010860 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10861 if (Op.getOpcode() == ISD::SHL) {
10862 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010863 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10864 DAG.getConstant(ShiftAmt, MVT::i32));
10865 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010866 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010867 SmallVector<SDValue, 32> V(32,
10868 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10869 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010870 return DAG.getNode(ISD::AND, dl, VT, SHL,
10871 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010872 }
Craig Topper0d86d462011-11-20 00:12:05 +000010873 if (Op.getOpcode() == ISD::SRL) {
10874 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010875 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10876 DAG.getConstant(ShiftAmt, MVT::i32));
10877 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010878 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010879 SmallVector<SDValue, 32> V(32,
10880 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10881 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010882 return DAG.getNode(ISD::AND, dl, VT, SRL,
10883 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10884 }
10885 if (Op.getOpcode() == ISD::SRA) {
10886 if (ShiftAmt == 7) {
10887 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010888 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010889 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010890 }
10891
10892 // R s>> a === ((R u>> a) ^ m) - m
10893 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10894 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10895 MVT::i8));
10896 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10897 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10898 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10899 return Res;
10900 }
Craig Topper731dfd02012-04-23 03:42:40 +000010901 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010902 }
Nadav Rotem43012222011-05-11 08:12:09 +000010903 }
10904 }
10905
10906 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010907 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010908 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10909 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010910
Chris Lattner7302d802012-02-06 21:56:39 +000010911 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10912 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010913 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10914 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010915 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010916 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010917
10918 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010919 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010920 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10921 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10922 }
Nadav Rotem43012222011-05-11 08:12:09 +000010923 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010924 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010925
Nate Begeman51409212010-07-28 00:21:48 +000010926 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010927 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10928 DAG.getConstant(5, MVT::i32));
10929 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010930
Lang Hames8b99c1e2011-12-17 01:08:46 +000010931 // Turn 'a' into a mask suitable for VSELECT
10932 SDValue VSelM = DAG.getConstant(0x80, VT);
10933 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010934 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010935
Lang Hames8b99c1e2011-12-17 01:08:46 +000010936 SDValue CM1 = DAG.getConstant(0x0f, VT);
10937 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010938
Lang Hames8b99c1e2011-12-17 01:08:46 +000010939 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10940 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010941 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10942 DAG.getConstant(4, MVT::i32), DAG);
10943 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010944 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10945
Nate Begeman51409212010-07-28 00:21:48 +000010946 // a += a
10947 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010948 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010949 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010950
Lang Hames8b99c1e2011-12-17 01:08:46 +000010951 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10952 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010953 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10954 DAG.getConstant(2, MVT::i32), DAG);
10955 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010956 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10957
Nate Begeman51409212010-07-28 00:21:48 +000010958 // a += a
10959 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010960 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010961 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010962
Lang Hames8b99c1e2011-12-17 01:08:46 +000010963 // return VSELECT(r, r+r, a);
10964 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010965 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010966 return R;
10967 }
Craig Topper46154eb2011-11-11 07:39:23 +000010968
10969 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010970 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010971 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010972 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10973 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10974
10975 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010976 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10977 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010978
10979 // Recreate the shift amount vectors
10980 SDValue Amt1, Amt2;
10981 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10982 // Constant shift amount
10983 SmallVector<SDValue, 4> Amt1Csts;
10984 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010985 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010986 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010987 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010988 Amt2Csts.push_back(Amt->getOperand(i));
10989
10990 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10991 &Amt1Csts[0], NumElems/2);
10992 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10993 &Amt2Csts[0], NumElems/2);
10994 } else {
10995 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000010996 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
10997 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010998 }
10999
11000 // Issue new vector shifts for the smaller types
11001 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
11002 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
11003
11004 // Concatenate the result back
11005 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
11006 }
11007
Nate Begeman51409212010-07-28 00:21:48 +000011008 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000011009}
Mon P Wangaf9b9522008-12-18 21:42:19 +000011010
Craig Topper55b24052012-09-11 06:15:32 +000011011static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
Bill Wendling74c37652008-12-09 22:08:41 +000011012 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
11013 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000011014 // looks for this combo and may remove the "setcc" instruction if the "setcc"
11015 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000011016 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000011017 SDValue LHS = N->getOperand(0);
11018 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000011019 unsigned BaseOp = 0;
11020 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011021 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000011022 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011023 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000011024 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000011025 // A subtract of one will be selected as a INC. Note that INC doesn't
11026 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011027 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11028 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011029 BaseOp = X86ISD::INC;
11030 Cond = X86::COND_O;
11031 break;
11032 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011033 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000011034 Cond = X86::COND_O;
11035 break;
11036 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011037 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000011038 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011039 break;
11040 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000011041 // A subtract of one will be selected as a DEC. Note that DEC doesn't
11042 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000011043 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
11044 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000011045 BaseOp = X86ISD::DEC;
11046 Cond = X86::COND_O;
11047 break;
11048 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011049 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000011050 Cond = X86::COND_O;
11051 break;
11052 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011053 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000011054 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000011055 break;
11056 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000011057 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000011058 Cond = X86::COND_O;
11059 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011060 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
11061 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
11062 MVT::i32);
11063 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011064
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011065 SDValue SetCC =
11066 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
11067 DAG.getConstant(X86::COND_O, MVT::i32),
11068 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011069
Dan Gohman6e5fda22011-07-22 18:45:15 +000011070 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011071 }
Bill Wendling74c37652008-12-09 22:08:41 +000011072 }
Bill Wendling3fafd932008-11-26 22:37:40 +000011073
Bill Wendling61edeb52008-12-02 01:06:39 +000011074 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000011075 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011076 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000011077
Bill Wendling61edeb52008-12-02 01:06:39 +000011078 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000011079 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
11080 DAG.getConstant(Cond, MVT::i32),
11081 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000011082
Dan Gohman6e5fda22011-07-22 18:45:15 +000011083 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000011084}
11085
Chad Rosier30450e82011-12-22 22:35:21 +000011086SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
11087 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011088 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000011089 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
11090 EVT VT = Op.getValueType();
11091
Craig Toppered2e13d2012-01-22 19:15:14 +000011092 if (!Subtarget->hasSSE2() || !VT.isVector())
11093 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011094
Craig Toppered2e13d2012-01-22 19:15:14 +000011095 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
11096 ExtraVT.getScalarType().getSizeInBits();
11097 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
11098
11099 switch (VT.getSimpleVT().SimpleTy) {
11100 default: return SDValue();
11101 case MVT::v8i32:
11102 case MVT::v16i16:
11103 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011104 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000011105 if (!Subtarget->hasAVX2()) {
11106 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000011107 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000011108
Craig Toppered2e13d2012-01-22 19:15:14 +000011109 // Extract the LHS vectors
11110 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000011111 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
11112 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000011113
Craig Toppered2e13d2012-01-22 19:15:14 +000011114 MVT EltVT = VT.getVectorElementType().getSimpleVT();
11115 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000011116
Craig Toppered2e13d2012-01-22 19:15:14 +000011117 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000011118 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011119 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11120 ExtraNumElems/2);
11121 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011122
Craig Toppered2e13d2012-01-22 19:15:14 +000011123 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11124 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011125
Dmitri Gribenko2de05722012-09-10 21:26:47 +000011126 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);
Craig Toppered2e13d2012-01-22 19:15:14 +000011127 }
11128 // fall through
11129 case MVT::v4i32:
11130 case MVT::v8i16: {
11131 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11132 Op.getOperand(0), ShAmt, DAG);
11133 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011134 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011135 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011136}
11137
11138
Craig Topper55b24052012-09-11 06:15:32 +000011139static SDValue LowerMEMBARRIER(SDValue Op, const X86Subtarget *Subtarget,
11140 SelectionDAG &DAG) {
Eric Christopher9a9d2752010-07-22 02:48:34 +000011141 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011142
Eric Christopher77ed1352011-07-08 00:04:56 +000011143 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11144 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011145 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011146 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011147 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011148 SDValue Ops[] = {
11149 DAG.getRegister(X86::ESP, MVT::i32), // Base
11150 DAG.getTargetConstant(1, MVT::i8), // Scale
11151 DAG.getRegister(0, MVT::i32), // Index
11152 DAG.getTargetConstant(0, MVT::i32), // Disp
11153 DAG.getRegister(0, MVT::i32), // Segment.
11154 Zero,
11155 Chain
11156 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011157 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011158 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11159 array_lengthof(Ops));
11160 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011161 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011162
Eric Christopher9a9d2752010-07-22 02:48:34 +000011163 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011164 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011165 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011166
Chris Lattner132929a2010-08-14 17:26:09 +000011167 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11168 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11169 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11170 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011171
Chris Lattner132929a2010-08-14 17:26:09 +000011172 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11173 if (!Op1 && !Op2 && !Op3 && Op4)
11174 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011175
Chris Lattner132929a2010-08-14 17:26:09 +000011176 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11177 if (Op1 && !Op2 && !Op3 && !Op4)
11178 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011179
11180 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011181 // (MFENCE)>;
11182 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011183}
11184
Craig Topper55b24052012-09-11 06:15:32 +000011185static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
11186 SelectionDAG &DAG) {
Eli Friedman14648462011-07-27 22:21:52 +000011187 DebugLoc dl = Op.getDebugLoc();
11188 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11189 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11190 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11191 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11192
11193 // The only fence that needs an instruction is a sequentially-consistent
11194 // cross-thread fence.
11195 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11196 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11197 // no-sse2). There isn't any reason to disable it if the target processor
11198 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011199 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011200 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11201
11202 SDValue Chain = Op.getOperand(0);
11203 SDValue Zero = DAG.getConstant(0, MVT::i32);
11204 SDValue Ops[] = {
11205 DAG.getRegister(X86::ESP, MVT::i32), // Base
11206 DAG.getTargetConstant(1, MVT::i8), // Scale
11207 DAG.getRegister(0, MVT::i32), // Index
11208 DAG.getTargetConstant(0, MVT::i32), // Disp
11209 DAG.getRegister(0, MVT::i32), // Segment.
11210 Zero,
11211 Chain
11212 };
11213 SDNode *Res =
11214 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11215 array_lengthof(Ops));
11216 return SDValue(Res, 0);
11217 }
11218
11219 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11220 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11221}
11222
11223
Craig Topper55b24052012-09-11 06:15:32 +000011224static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
11225 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000011226 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011227 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011228 unsigned Reg = 0;
11229 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011230 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011231 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011232 case MVT::i8: Reg = X86::AL; size = 1; break;
11233 case MVT::i16: Reg = X86::AX; size = 2; break;
11234 case MVT::i32: Reg = X86::EAX; size = 4; break;
11235 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011236 assert(Subtarget->is64Bit() && "Node not type legal!");
11237 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011238 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011239 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011240 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011241 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011242 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011243 Op.getOperand(1),
11244 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011245 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011246 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011247 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011248 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11249 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11250 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011251 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011252 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011253 return cpOut;
11254}
11255
Craig Topper55b24052012-09-11 06:15:32 +000011256static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
11257 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +000011258 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011259 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011260 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011261 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011262 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011263 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11264 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011265 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011266 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11267 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011268 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011269 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011270 rdx.getValue(1)
11271 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011272 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011273}
11274
Craig Topper55b24052012-09-11 06:15:32 +000011275SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen7d07b482010-05-21 00:52:33 +000011276 EVT SrcVT = Op.getOperand(0).getValueType();
11277 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011278 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011279 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011280 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011281 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011282 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011283 // i64 <=> MMX conversions are Legal.
11284 if (SrcVT==MVT::i64 && DstVT.isVector())
11285 return Op;
11286 if (DstVT==MVT::i64 && SrcVT.isVector())
11287 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011288 // MMX <=> MMX conversions are Legal.
11289 if (SrcVT.isVector() && DstVT.isVector())
11290 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011291 // All other conversions need to be expanded.
11292 return SDValue();
11293}
Chris Lattner5b856542010-12-20 00:59:46 +000011294
Craig Topper55b24052012-09-11 06:15:32 +000011295static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011296 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011297 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011298 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011299 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011300 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011301 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011302 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011303 Node->getOperand(0),
11304 Node->getOperand(1), negOp,
11305 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011306 cast<AtomicSDNode>(Node)->getAlignment(),
11307 cast<AtomicSDNode>(Node)->getOrdering(),
11308 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011309}
11310
Eli Friedman327236c2011-08-24 20:50:09 +000011311static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11312 SDNode *Node = Op.getNode();
11313 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011314 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011315
11316 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011317 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11318 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11319 // (The only way to get a 16-byte store is cmpxchg16b)
11320 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11321 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11322 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011323 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11324 cast<AtomicSDNode>(Node)->getMemoryVT(),
11325 Node->getOperand(0),
11326 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011327 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011328 cast<AtomicSDNode>(Node)->getOrdering(),
11329 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011330 return Swap.getValue(1);
11331 }
11332 // Other atomic stores have a simple pattern.
11333 return Op;
11334}
11335
Chris Lattner5b856542010-12-20 00:59:46 +000011336static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11337 EVT VT = Op.getNode()->getValueType(0);
11338
11339 // Let legalize expand this if it isn't a legal type yet.
11340 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11341 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011342
Chris Lattner5b856542010-12-20 00:59:46 +000011343 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011344
Chris Lattner5b856542010-12-20 00:59:46 +000011345 unsigned Opc;
11346 bool ExtraOp = false;
11347 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011348 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011349 case ISD::ADDC: Opc = X86ISD::ADD; break;
11350 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11351 case ISD::SUBC: Opc = X86ISD::SUB; break;
11352 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11353 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011354
Chris Lattner5b856542010-12-20 00:59:46 +000011355 if (!ExtraOp)
11356 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11357 Op.getOperand(1));
11358 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11359 Op.getOperand(1), Op.getOperand(2));
11360}
11361
Evan Cheng0db9fe62006-04-25 20:13:52 +000011362/// LowerOperation - Provide custom lowering hooks for some operations.
11363///
Dan Gohmand858e902010-04-17 15:26:15 +000011364SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011365 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011366 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011367 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011368 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, Subtarget, DAG);
11369 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG);
11370 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011371 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011372 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011373 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011374 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011375 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11376 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11377 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011378 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
11379 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011380 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11381 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11382 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011383 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011384 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011385 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011386 case ISD::SHL_PARTS:
11387 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011388 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011389 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011390 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011391 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011392 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011393 case ISD::FABS: return LowerFABS(Op, DAG);
11394 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011395 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011396 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011397 case ISD::SETCC: return LowerSETCC(Op, DAG);
11398 case ISD::SELECT: return LowerSELECT(Op, DAG);
11399 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011400 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011401 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011402 case ISD::VAARG: return LowerVAARG(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011403 case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011404 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011405 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011406 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11407 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011408 case ISD::FRAME_TO_ARGS_OFFSET:
11409 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011410 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011411 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011412 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11413 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011414 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011415 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011416 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011417 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011418 case ISD::MUL: return LowerMUL(Op, Subtarget, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011419 case ISD::SRA:
11420 case ISD::SRL:
11421 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011422 case ISD::SADDO:
11423 case ISD::UADDO:
11424 case ISD::SSUBO:
11425 case ISD::USUBO:
11426 case ISD::SMULO:
11427 case ISD::UMULO: return LowerXALUO(Op, DAG);
Craig Topper55b24052012-09-11 06:15:32 +000011428 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011429 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011430 case ISD::ADDC:
11431 case ISD::ADDE:
11432 case ISD::SUBC:
11433 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011434 case ISD::ADD: return LowerADD(Op, DAG);
11435 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011436 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011437}
11438
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011439static void ReplaceATOMIC_LOAD(SDNode *Node,
11440 SmallVectorImpl<SDValue> &Results,
11441 SelectionDAG &DAG) {
11442 DebugLoc dl = Node->getDebugLoc();
11443 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11444
11445 // Convert wide load -> cmpxchg8b/cmpxchg16b
11446 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11447 // (The only way to get a 16-byte load is cmpxchg16b)
11448 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011449 SDValue Zero = DAG.getConstant(0, VT);
11450 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011451 Node->getOperand(0),
11452 Node->getOperand(1), Zero, Zero,
11453 cast<AtomicSDNode>(Node)->getMemOperand(),
11454 cast<AtomicSDNode>(Node)->getOrdering(),
11455 cast<AtomicSDNode>(Node)->getSynchScope());
11456 Results.push_back(Swap.getValue(0));
11457 Results.push_back(Swap.getValue(1));
11458}
11459
Craig Topperc0878702012-08-17 06:55:11 +000011460static void
Duncan Sands1607f052008-12-01 11:39:25 +000011461ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011462 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011463 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011464 assert (Node->getValueType(0) == MVT::i64 &&
11465 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011466
11467 SDValue Chain = Node->getOperand(0);
11468 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011469 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011470 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011471 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011472 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011473 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011474 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011475 SDValue Result =
11476 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11477 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011478 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011479 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011480 Results.push_back(Result.getValue(2));
11481}
11482
Duncan Sands126d9072008-07-04 11:47:58 +000011483/// ReplaceNodeResults - Replace a node with an illegal result type
11484/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011485void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11486 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011487 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011488 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011489 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011490 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011491 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011492 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011493 case ISD::ADDC:
11494 case ISD::ADDE:
11495 case ISD::SUBC:
11496 case ISD::SUBE:
11497 // We don't want to expand or promote these.
11498 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011499 case ISD::FP_TO_SINT:
11500 case ISD::FP_TO_UINT: {
11501 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11502
11503 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11504 return;
11505
Eli Friedman948e95a2009-05-23 09:59:16 +000011506 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011507 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011508 SDValue FIST = Vals.first, StackSlot = Vals.second;
11509 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011510 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011511 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011512 if (StackSlot.getNode() != 0)
11513 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11514 MachinePointerInfo(),
11515 false, false, false, 0));
11516 else
11517 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011518 }
11519 return;
11520 }
11521 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011522 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011523 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011524 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011525 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011526 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011527 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011528 eax.getValue(2));
11529 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11530 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011531 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011532 Results.push_back(edx.getValue(1));
11533 return;
11534 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011535 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011536 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011537 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011538 bool Regs64bit = T == MVT::i128;
11539 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011540 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011541 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11542 DAG.getConstant(0, HalfT));
11543 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11544 DAG.getConstant(1, HalfT));
11545 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11546 Regs64bit ? X86::RAX : X86::EAX,
11547 cpInL, SDValue());
11548 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11549 Regs64bit ? X86::RDX : X86::EDX,
11550 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011551 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011552 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11553 DAG.getConstant(0, HalfT));
11554 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11555 DAG.getConstant(1, HalfT));
11556 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11557 Regs64bit ? X86::RBX : X86::EBX,
11558 swapInL, cpInH.getValue(1));
11559 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011560 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011561 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011562 SDValue Ops[] = { swapInH.getValue(0),
11563 N->getOperand(1),
11564 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011565 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011566 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011567 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11568 X86ISD::LCMPXCHG8_DAG;
11569 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011570 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011571 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11572 Regs64bit ? X86::RAX : X86::EAX,
11573 HalfT, Result.getValue(1));
11574 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11575 Regs64bit ? X86::RDX : X86::EDX,
11576 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011577 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011578 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011579 Results.push_back(cpOutH.getValue(1));
11580 return;
11581 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011582 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011583 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011584 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011585 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011586 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011587 case ISD::ATOMIC_LOAD_XOR:
Craig Topperc0878702012-08-17 06:55:11 +000011588 case ISD::ATOMIC_SWAP: {
11589 unsigned Opc;
11590 switch (N->getOpcode()) {
11591 default: llvm_unreachable("Unexpected opcode");
11592 case ISD::ATOMIC_LOAD_ADD:
11593 Opc = X86ISD::ATOMADD64_DAG;
11594 break;
11595 case ISD::ATOMIC_LOAD_AND:
11596 Opc = X86ISD::ATOMAND64_DAG;
11597 break;
11598 case ISD::ATOMIC_LOAD_NAND:
11599 Opc = X86ISD::ATOMNAND64_DAG;
11600 break;
11601 case ISD::ATOMIC_LOAD_OR:
11602 Opc = X86ISD::ATOMOR64_DAG;
11603 break;
11604 case ISD::ATOMIC_LOAD_SUB:
11605 Opc = X86ISD::ATOMSUB64_DAG;
11606 break;
11607 case ISD::ATOMIC_LOAD_XOR:
11608 Opc = X86ISD::ATOMXOR64_DAG;
11609 break;
11610 case ISD::ATOMIC_SWAP:
11611 Opc = X86ISD::ATOMSWAP64_DAG;
11612 break;
11613 }
11614 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011615 return;
Craig Topperc0878702012-08-17 06:55:11 +000011616 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011617 case ISD::ATOMIC_LOAD:
11618 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011619 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011620}
11621
Evan Cheng72261582005-12-20 06:22:03 +000011622const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11623 switch (Opcode) {
11624 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011625 case X86ISD::BSF: return "X86ISD::BSF";
11626 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011627 case X86ISD::SHLD: return "X86ISD::SHLD";
11628 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011629 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011630 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011631 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011632 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011633 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011634 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011635 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11636 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11637 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011638 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011639 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011640 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011641 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011642 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011643 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011644 case X86ISD::COMI: return "X86ISD::COMI";
11645 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011646 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011647 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011648 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11649 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011650 case X86ISD::CMOV: return "X86ISD::CMOV";
11651 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011652 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011653 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11654 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011655 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011656 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011657 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011658 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011659 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011660 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11661 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011662 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011663 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011664 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011665 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011666 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011667 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11668 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11669 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011670 case X86ISD::HADD: return "X86ISD::HADD";
11671 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011672 case X86ISD::FHADD: return "X86ISD::FHADD";
11673 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011674 case X86ISD::FMAX: return "X86ISD::FMAX";
11675 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011676 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11677 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011678 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11679 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011680 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011681 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011682 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011683 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011684 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011685 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011686 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011687 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11688 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011689 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11690 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11691 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11692 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11693 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11694 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011695 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011696 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011697 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011698 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Craig Toppered2e13d2012-01-22 19:15:14 +000011699 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11700 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011701 case X86ISD::VSHL: return "X86ISD::VSHL";
11702 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011703 case X86ISD::VSRA: return "X86ISD::VSRA";
11704 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11705 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11706 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011707 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011708 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11709 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011710 case X86ISD::ADD: return "X86ISD::ADD";
11711 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011712 case X86ISD::ADC: return "X86ISD::ADC";
11713 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011714 case X86ISD::SMUL: return "X86ISD::SMUL";
11715 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011716 case X86ISD::INC: return "X86ISD::INC";
11717 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011718 case X86ISD::OR: return "X86ISD::OR";
11719 case X86ISD::XOR: return "X86ISD::XOR";
11720 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011721 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011722 case X86ISD::BLSI: return "X86ISD::BLSI";
11723 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11724 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011725 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011726 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011727 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011728 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11729 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11730 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011731 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011732 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011733 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011734 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011735 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011736 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11737 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011738 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11739 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11740 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011741 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11742 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011743 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11744 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011745 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011746 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011747 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011748 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11749 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011750 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011751 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011752 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011753 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011754 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011755 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011756 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011757 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011758 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011759 case X86ISD::FMADD: return "X86ISD::FMADD";
11760 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11761 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11762 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11763 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11764 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011765 }
11766}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011767
Chris Lattnerc9addb72007-03-30 23:15:24 +000011768// isLegalAddressingMode - Return true if the addressing mode represented
11769// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011770bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011771 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011772 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011773 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011774 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011775
Chris Lattnerc9addb72007-03-30 23:15:24 +000011776 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011777 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011778 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011779
Chris Lattnerc9addb72007-03-30 23:15:24 +000011780 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011781 unsigned GVFlags =
11782 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011783
Chris Lattnerdfed4132009-07-10 07:38:24 +000011784 // If a reference to this global requires an extra load, we can't fold it.
11785 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011786 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011787
Chris Lattnerdfed4132009-07-10 07:38:24 +000011788 // If BaseGV requires a register for the PIC base, we cannot also have a
11789 // BaseReg specified.
11790 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011791 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011792
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011793 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011794 if ((M != CodeModel::Small || R != Reloc::Static) &&
11795 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011796 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011797 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011798
Chris Lattnerc9addb72007-03-30 23:15:24 +000011799 switch (AM.Scale) {
11800 case 0:
11801 case 1:
11802 case 2:
11803 case 4:
11804 case 8:
11805 // These scales always work.
11806 break;
11807 case 3:
11808 case 5:
11809 case 9:
11810 // These scales are formed with basereg+scalereg. Only accept if there is
11811 // no basereg yet.
11812 if (AM.HasBaseReg)
11813 return false;
11814 break;
11815 default: // Other stuff never works.
11816 return false;
11817 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011818
Chris Lattnerc9addb72007-03-30 23:15:24 +000011819 return true;
11820}
11821
11822
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011823bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011824 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011825 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011826 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11827 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011828 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011829 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011830 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011831}
11832
Evan Cheng70e10d32012-07-17 06:53:39 +000011833bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11834 return Imm == (int32_t)Imm;
11835}
11836
11837bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011838 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011839 return Imm == (int32_t)Imm;
11840}
11841
Owen Andersone50ed302009-08-10 22:56:29 +000011842bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011843 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011844 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011845 unsigned NumBits1 = VT1.getSizeInBits();
11846 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011847 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011848 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011849 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011850}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011851
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011852bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011853 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011854 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011855}
11856
Owen Andersone50ed302009-08-10 22:56:29 +000011857bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011858 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011859 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011860}
11861
Owen Andersone50ed302009-08-10 22:56:29 +000011862bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011863 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011864 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011865}
11866
Evan Cheng60c07e12006-07-05 22:17:51 +000011867/// isShuffleMaskLegal - Targets can use this to indicate that they only
11868/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11869/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11870/// are assumed to be legal.
11871bool
Eric Christopherfd179292009-08-27 18:07:15 +000011872X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011873 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011874 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011875 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011876 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011877
Nate Begemana09008b2009-10-19 02:17:23 +000011878 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011879 return (VT.getVectorNumElements() == 2 ||
11880 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11881 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011882 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011883 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011884 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11885 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011886 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011887 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11888 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011889 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11890 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011891}
11892
Dan Gohman7d8143f2008-04-09 20:09:42 +000011893bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011894X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011895 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011896 unsigned NumElts = VT.getVectorNumElements();
11897 // FIXME: This collection of masks seems suspect.
11898 if (NumElts == 2)
11899 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011900 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011901 return (isMOVLMask(Mask, VT) ||
11902 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011903 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11904 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011905 }
11906 return false;
11907}
11908
11909//===----------------------------------------------------------------------===//
11910// X86 Scheduler Hooks
11911//===----------------------------------------------------------------------===//
11912
Mon P Wang63307c32008-05-05 19:05:59 +000011913// private utility function
11914MachineBasicBlock *
11915X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11916 MachineBasicBlock *MBB,
11917 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011918 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011919 unsigned LoadOpc,
11920 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011921 unsigned notOpc,
11922 unsigned EAXreg,
Craig Topper44d23822012-02-22 05:59:10 +000011923 const TargetRegisterClass *RC,
Richard Smith42fc29e2012-04-13 22:47:00 +000011924 bool Invert) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011925 // For the atomic bitwise operator, we generate
11926 // thisMBB:
11927 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011928 // ld t1 = [bitinstr.addr]
11929 // op t2 = t1, [bitinstr.val]
Richard Smith42fc29e2012-04-13 22:47:00 +000011930 // not t3 = t2 (if Invert)
Mon P Wangab3e7472008-05-05 22:56:23 +000011931 // mov EAX = t1
Richard Smith42fc29e2012-04-13 22:47:00 +000011932 // lcs dest = [bitinstr.addr], t3 [EAX is implicit]
Mon P Wang63307c32008-05-05 19:05:59 +000011933 // bz newMBB
11934 // fallthrough -->nextMBB
11935 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11936 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011937 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011938 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011939
Mon P Wang63307c32008-05-05 19:05:59 +000011940 /// First build the CFG
11941 MachineFunction *F = MBB->getParent();
11942 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011943 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11944 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11945 F->insert(MBBIter, newMBB);
11946 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011947
Dan Gohman14152b42010-07-06 20:24:04 +000011948 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11949 nextMBB->splice(nextMBB->begin(), thisMBB,
11950 llvm::next(MachineBasicBlock::iterator(bInstr)),
11951 thisMBB->end());
11952 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011953
Mon P Wang63307c32008-05-05 19:05:59 +000011954 // Update thisMBB to fall through to newMBB
11955 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011956
Mon P Wang63307c32008-05-05 19:05:59 +000011957 // newMBB jumps to itself and fall through to nextMBB
11958 newMBB->addSuccessor(nextMBB);
11959 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011960
Mon P Wang63307c32008-05-05 19:05:59 +000011961 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011962 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011963 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011964 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011965 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011966 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011967 int numArgs = bInstr->getNumOperands() - 1;
11968 for (int i=0; i < numArgs; ++i)
11969 argOpers[i] = &bInstr->getOperand(i+1);
11970
11971 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011972 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011973 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011974
Dale Johannesen140be2d2008-08-19 18:47:28 +000011975 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011976 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011977 for (int i=0; i <= lastAddrIndx; ++i)
11978 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011979
Dale Johannesen140be2d2008-08-19 18:47:28 +000011980 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011981 assert((argOpers[valArgIndx]->isReg() ||
11982 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011983 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011984 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011985 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011986 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011987 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Richard Smith42fc29e2012-04-13 22:47:00 +000011988 MIB.addReg(t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011989 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011990
Richard Smith42fc29e2012-04-13 22:47:00 +000011991 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11992 if (Invert) {
11993 MIB = BuildMI(newMBB, dl, TII->get(notOpc), t3).addReg(t2);
11994 }
11995 else
11996 t3 = t2;
11997
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011998 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Richard Smith2c651fe2012-04-16 18:43:53 +000011999 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000012000
Dale Johannesene4d209d2009-02-03 20:21:25 +000012001 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000012002 for (int i=0; i <= lastAddrIndx; ++i)
12003 (*MIB).addOperand(*argOpers[i]);
Richard Smith42fc29e2012-04-13 22:47:00 +000012004 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000012005 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012006 (*MIB).setMemRefs(bInstr->memoperands_begin(),
12007 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000012008
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012009 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000012010 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000012011
Mon P Wang63307c32008-05-05 19:05:59 +000012012 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012013 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012014
Dan Gohman14152b42010-07-06 20:24:04 +000012015 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000012016 return nextMBB;
12017}
12018
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000012019// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000012020MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012021X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
12022 MachineBasicBlock *MBB,
12023 unsigned regOpcL,
12024 unsigned regOpcH,
12025 unsigned immOpcL,
12026 unsigned immOpcH,
Richard Smith42fc29e2012-04-13 22:47:00 +000012027 bool Invert) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012028 // For the atomic bitwise operator, we generate
12029 // thisMBB (instructions are in pairs, except cmpxchg8b)
12030 // ld t1,t2 = [bitinstr.addr]
12031 // newMBB:
12032 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
12033 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000012034 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Richard Smith42fc29e2012-04-13 22:47:00 +000012035 // neg t7, t8 < t5, t6 (if Invert)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012036 // mov ECX, EBX <- t5, t6
12037 // mov EAX, EDX <- t1, t2
12038 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
12039 // mov t3, t4 <- EAX, EDX
12040 // bz newMBB
12041 // result in out1, out2
12042 // fallthrough -->nextMBB
12043
Craig Topperc9099502012-04-20 06:31:50 +000012044 const TargetRegisterClass *RC = &X86::GR32RegClass;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012045 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012046 const unsigned NotOpc = X86::NOT32r;
12047 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12048 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12049 MachineFunction::iterator MBBIter = MBB;
12050 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000012051
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012052 /// First build the CFG
12053 MachineFunction *F = MBB->getParent();
12054 MachineBasicBlock *thisMBB = MBB;
12055 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
12056 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
12057 F->insert(MBBIter, newMBB);
12058 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012059
Dan Gohman14152b42010-07-06 20:24:04 +000012060 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
12061 nextMBB->splice(nextMBB->begin(), thisMBB,
12062 llvm::next(MachineBasicBlock::iterator(bInstr)),
12063 thisMBB->end());
12064 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012065
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012066 // Update thisMBB to fall through to newMBB
12067 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012068
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012069 // newMBB jumps to itself and fall through to nextMBB
12070 newMBB->addSuccessor(nextMBB);
12071 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012072
Dale Johannesene4d209d2009-02-03 20:21:25 +000012073 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012074 // Insert instructions into newMBB based on incoming instruction
12075 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012076 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000012077 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012078 MachineOperand& dest1Oper = bInstr->getOperand(0);
12079 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012080 MachineOperand* argOpers[2 + X86::AddrNumOperands];
12081 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012082 argOpers[i] = &bInstr->getOperand(i+2);
12083
Dan Gohman71ea4e52010-05-14 21:01:44 +000012084 // We use some of the operands multiple times, so conservatively just
12085 // clear any kill flags that might be present.
12086 if (argOpers[i]->isReg() && argOpers[i]->isUse())
12087 argOpers[i]->setIsKill(false);
12088 }
12089
Evan Chengad5b52f2010-01-08 19:14:57 +000012090 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012091 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000012092
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012093 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012094 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012095 for (int i=0; i <= lastAddrIndx; ++i)
12096 (*MIB).addOperand(*argOpers[i]);
12097 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012098 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000012099 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000012100 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012101 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000012102 MachineOperand newOp3 = *(argOpers[3]);
12103 if (newOp3.isImm())
12104 newOp3.setImm(newOp3.getImm()+4);
12105 else
12106 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012107 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000012108 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012109
12110 // t3/4 are defined later, at the bottom of the loop
12111 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
12112 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012113 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012114 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012115 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012116 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
12117
Evan Cheng306b4ca2010-01-08 23:41:50 +000012118 // The subsequent operations should be using the destination registers of
Richard Smith42fc29e2012-04-13 22:47:00 +000012119 // the PHI instructions.
12120 t1 = dest1Oper.getReg();
12121 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012122
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012123 int valArgIndx = lastAddrIndx + 1;
12124 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000012125 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012126 "invalid operand");
12127 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
12128 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012129 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000012130 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012131 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012132 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000012133 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012134 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012135 (*MIB).addOperand(*argOpers[valArgIndx]);
12136 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012137 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012138 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012139 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012140 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000012141 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012142 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012143 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000012144 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012145 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012146 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012147
Richard Smith42fc29e2012-04-13 22:47:00 +000012148 unsigned t7, t8;
12149 if (Invert) {
12150 t7 = F->getRegInfo().createVirtualRegister(RC);
12151 t8 = F->getRegInfo().createVirtualRegister(RC);
12152 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t7).addReg(t5);
12153 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t8).addReg(t6);
12154 } else {
12155 t7 = t5;
12156 t8 = t6;
12157 }
12158
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012159 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012160 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012161 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012162 MIB.addReg(t2);
12163
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012164 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012165 MIB.addReg(t7);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012166 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012167 MIB.addReg(t8);
Scott Michelfdc40a02009-02-17 22:15:04 +000012168
Dale Johannesene4d209d2009-02-03 20:21:25 +000012169 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012170 for (int i=0; i <= lastAddrIndx; ++i)
12171 (*MIB).addOperand(*argOpers[i]);
12172
12173 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012174 (*MIB).setMemRefs(bInstr->memoperands_begin(),
12175 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012176
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012177 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012178 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012179 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012180 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012181
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012182 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012183 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012184
Dan Gohman14152b42010-07-06 20:24:04 +000012185 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012186 return nextMBB;
12187}
12188
12189// private utility function
12190MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000012191X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
12192 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000012193 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000012194 // For the atomic min/max operator, we generate
12195 // thisMBB:
12196 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000012197 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000012198 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000012199 // cmp t1, t2
12200 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000012201 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000012202 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
12203 // bz newMBB
12204 // fallthrough -->nextMBB
12205 //
12206 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12207 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012208 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012209 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000012210
Mon P Wang63307c32008-05-05 19:05:59 +000012211 /// First build the CFG
12212 MachineFunction *F = MBB->getParent();
12213 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012214 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
12215 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
12216 F->insert(MBBIter, newMBB);
12217 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012218
Dan Gohman14152b42010-07-06 20:24:04 +000012219 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
12220 nextMBB->splice(nextMBB->begin(), thisMBB,
12221 llvm::next(MachineBasicBlock::iterator(mInstr)),
12222 thisMBB->end());
12223 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012224
Mon P Wang63307c32008-05-05 19:05:59 +000012225 // Update thisMBB to fall through to newMBB
12226 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012227
Mon P Wang63307c32008-05-05 19:05:59 +000012228 // newMBB jumps to newMBB and fall through to nextMBB
12229 newMBB->addSuccessor(nextMBB);
12230 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012231
Dale Johannesene4d209d2009-02-03 20:21:25 +000012232 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000012233 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012234 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000012235 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000012236 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012237 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000012238 int numArgs = mInstr->getNumOperands() - 1;
12239 for (int i=0; i < numArgs; ++i)
12240 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000012241
Mon P Wang63307c32008-05-05 19:05:59 +000012242 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012243 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012244 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000012245
Craig Topperc9099502012-04-20 06:31:50 +000012246 unsigned t1 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012247 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000012248 for (int i=0; i <= lastAddrIndx; ++i)
12249 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000012250
Mon P Wang63307c32008-05-05 19:05:59 +000012251 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000012252 assert((argOpers[valArgIndx]->isReg() ||
12253 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000012254 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000012255
Craig Topperc9099502012-04-20 06:31:50 +000012256 unsigned t2 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dan Gohmand735b802008-10-03 15:45:36 +000012257 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012258 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000012259 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012260 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000012261 (*MIB).addOperand(*argOpers[valArgIndx]);
12262
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012263 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012264 MIB.addReg(t1);
12265
Dale Johannesene4d209d2009-02-03 20:21:25 +000012266 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000012267 MIB.addReg(t1);
12268 MIB.addReg(t2);
12269
12270 // Generate movc
Craig Topperc9099502012-04-20 06:31:50 +000012271 unsigned t3 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012272 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000012273 MIB.addReg(t2);
12274 MIB.addReg(t1);
12275
12276 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000012277 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000012278 for (int i=0; i <= lastAddrIndx; ++i)
12279 (*MIB).addOperand(*argOpers[i]);
12280 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000012281 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012282 (*MIB).setMemRefs(mInstr->memoperands_begin(),
12283 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000012284
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012285 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000012286 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012287
Mon P Wang63307c32008-05-05 19:05:59 +000012288 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012289 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012290
Dan Gohman14152b42010-07-06 20:24:04 +000012291 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000012292 return nextMBB;
12293}
12294
Eric Christopherf83a5de2009-08-27 18:08:16 +000012295// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012296// or XMM0_V32I8 in AVX all of this code can be replaced with that
12297// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012298MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012299X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012300 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012301 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012302 "Target must have SSE4.2 or AVX features enabled");
12303
Eric Christopherb120ab42009-08-18 22:50:32 +000012304 DebugLoc dl = MI->getDebugLoc();
12305 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012306 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012307 if (!Subtarget->hasAVX()) {
12308 if (memArg)
12309 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12310 else
12311 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12312 } else {
12313 if (memArg)
12314 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12315 else
12316 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12317 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012318
Eric Christopher41c902f2010-11-30 08:20:21 +000012319 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012320 for (unsigned i = 0; i < numArgs; ++i) {
12321 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012322 if (!(Op.isReg() && Op.isImplicit()))
12323 MIB.addOperand(Op);
12324 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012325 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012326 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012327 .addReg(X86::XMM0);
12328
Dan Gohman14152b42010-07-06 20:24:04 +000012329 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012330 return BB;
12331}
12332
12333MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012334X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012335 DebugLoc dl = MI->getDebugLoc();
12336 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012337
Eric Christopher228232b2010-11-30 07:20:12 +000012338 // Address into RAX/EAX, other two args into ECX, EDX.
12339 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12340 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12341 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12342 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012343 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012344
Eric Christopher228232b2010-11-30 07:20:12 +000012345 unsigned ValOps = X86::AddrNumOperands;
12346 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12347 .addReg(MI->getOperand(ValOps).getReg());
12348 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12349 .addReg(MI->getOperand(ValOps+1).getReg());
12350
12351 // The instruction doesn't actually take any operands though.
12352 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012353
Eric Christopher228232b2010-11-30 07:20:12 +000012354 MI->eraseFromParent(); // The pseudo is gone now.
12355 return BB;
12356}
12357
12358MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012359X86TargetLowering::EmitVAARG64WithCustomInserter(
12360 MachineInstr *MI,
12361 MachineBasicBlock *MBB) const {
12362 // Emit va_arg instruction on X86-64.
12363
12364 // Operands to this pseudo-instruction:
12365 // 0 ) Output : destination address (reg)
12366 // 1-5) Input : va_list address (addr, i64mem)
12367 // 6 ) ArgSize : Size (in bytes) of vararg type
12368 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12369 // 8 ) Align : Alignment of type
12370 // 9 ) EFLAGS (implicit-def)
12371
12372 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12373 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12374
12375 unsigned DestReg = MI->getOperand(0).getReg();
12376 MachineOperand &Base = MI->getOperand(1);
12377 MachineOperand &Scale = MI->getOperand(2);
12378 MachineOperand &Index = MI->getOperand(3);
12379 MachineOperand &Disp = MI->getOperand(4);
12380 MachineOperand &Segment = MI->getOperand(5);
12381 unsigned ArgSize = MI->getOperand(6).getImm();
12382 unsigned ArgMode = MI->getOperand(7).getImm();
12383 unsigned Align = MI->getOperand(8).getImm();
12384
12385 // Memory Reference
12386 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12387 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12388 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12389
12390 // Machine Information
12391 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12392 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12393 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12394 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12395 DebugLoc DL = MI->getDebugLoc();
12396
12397 // struct va_list {
12398 // i32 gp_offset
12399 // i32 fp_offset
12400 // i64 overflow_area (address)
12401 // i64 reg_save_area (address)
12402 // }
12403 // sizeof(va_list) = 24
12404 // alignment(va_list) = 8
12405
12406 unsigned TotalNumIntRegs = 6;
12407 unsigned TotalNumXMMRegs = 8;
12408 bool UseGPOffset = (ArgMode == 1);
12409 bool UseFPOffset = (ArgMode == 2);
12410 unsigned MaxOffset = TotalNumIntRegs * 8 +
12411 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12412
12413 /* Align ArgSize to a multiple of 8 */
12414 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12415 bool NeedsAlign = (Align > 8);
12416
12417 MachineBasicBlock *thisMBB = MBB;
12418 MachineBasicBlock *overflowMBB;
12419 MachineBasicBlock *offsetMBB;
12420 MachineBasicBlock *endMBB;
12421
12422 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12423 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12424 unsigned OffsetReg = 0;
12425
12426 if (!UseGPOffset && !UseFPOffset) {
12427 // If we only pull from the overflow region, we don't create a branch.
12428 // We don't need to alter control flow.
12429 OffsetDestReg = 0; // unused
12430 OverflowDestReg = DestReg;
12431
12432 offsetMBB = NULL;
12433 overflowMBB = thisMBB;
12434 endMBB = thisMBB;
12435 } else {
12436 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12437 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12438 // If not, pull from overflow_area. (branch to overflowMBB)
12439 //
12440 // thisMBB
12441 // | .
12442 // | .
12443 // offsetMBB overflowMBB
12444 // | .
12445 // | .
12446 // endMBB
12447
12448 // Registers for the PHI in endMBB
12449 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12450 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12451
12452 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12453 MachineFunction *MF = MBB->getParent();
12454 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12455 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12456 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12457
12458 MachineFunction::iterator MBBIter = MBB;
12459 ++MBBIter;
12460
12461 // Insert the new basic blocks
12462 MF->insert(MBBIter, offsetMBB);
12463 MF->insert(MBBIter, overflowMBB);
12464 MF->insert(MBBIter, endMBB);
12465
12466 // Transfer the remainder of MBB and its successor edges to endMBB.
12467 endMBB->splice(endMBB->begin(), thisMBB,
12468 llvm::next(MachineBasicBlock::iterator(MI)),
12469 thisMBB->end());
12470 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12471
12472 // Make offsetMBB and overflowMBB successors of thisMBB
12473 thisMBB->addSuccessor(offsetMBB);
12474 thisMBB->addSuccessor(overflowMBB);
12475
12476 // endMBB is a successor of both offsetMBB and overflowMBB
12477 offsetMBB->addSuccessor(endMBB);
12478 overflowMBB->addSuccessor(endMBB);
12479
12480 // Load the offset value into a register
12481 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12482 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12483 .addOperand(Base)
12484 .addOperand(Scale)
12485 .addOperand(Index)
12486 .addDisp(Disp, UseFPOffset ? 4 : 0)
12487 .addOperand(Segment)
12488 .setMemRefs(MMOBegin, MMOEnd);
12489
12490 // Check if there is enough room left to pull this argument.
12491 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12492 .addReg(OffsetReg)
12493 .addImm(MaxOffset + 8 - ArgSizeA8);
12494
12495 // Branch to "overflowMBB" if offset >= max
12496 // Fall through to "offsetMBB" otherwise
12497 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12498 .addMBB(overflowMBB);
12499 }
12500
12501 // In offsetMBB, emit code to use the reg_save_area.
12502 if (offsetMBB) {
12503 assert(OffsetReg != 0);
12504
12505 // Read the reg_save_area address.
12506 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12507 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12508 .addOperand(Base)
12509 .addOperand(Scale)
12510 .addOperand(Index)
12511 .addDisp(Disp, 16)
12512 .addOperand(Segment)
12513 .setMemRefs(MMOBegin, MMOEnd);
12514
12515 // Zero-extend the offset
12516 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12517 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12518 .addImm(0)
12519 .addReg(OffsetReg)
12520 .addImm(X86::sub_32bit);
12521
12522 // Add the offset to the reg_save_area to get the final address.
12523 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12524 .addReg(OffsetReg64)
12525 .addReg(RegSaveReg);
12526
12527 // Compute the offset for the next argument
12528 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12529 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12530 .addReg(OffsetReg)
12531 .addImm(UseFPOffset ? 16 : 8);
12532
12533 // Store it back into the va_list.
12534 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12535 .addOperand(Base)
12536 .addOperand(Scale)
12537 .addOperand(Index)
12538 .addDisp(Disp, UseFPOffset ? 4 : 0)
12539 .addOperand(Segment)
12540 .addReg(NextOffsetReg)
12541 .setMemRefs(MMOBegin, MMOEnd);
12542
12543 // Jump to endMBB
12544 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12545 .addMBB(endMBB);
12546 }
12547
12548 //
12549 // Emit code to use overflow area
12550 //
12551
12552 // Load the overflow_area address into a register.
12553 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12554 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12555 .addOperand(Base)
12556 .addOperand(Scale)
12557 .addOperand(Index)
12558 .addDisp(Disp, 8)
12559 .addOperand(Segment)
12560 .setMemRefs(MMOBegin, MMOEnd);
12561
12562 // If we need to align it, do so. Otherwise, just copy the address
12563 // to OverflowDestReg.
12564 if (NeedsAlign) {
12565 // Align the overflow address
12566 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12567 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12568
12569 // aligned_addr = (addr + (align-1)) & ~(align-1)
12570 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12571 .addReg(OverflowAddrReg)
12572 .addImm(Align-1);
12573
12574 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12575 .addReg(TmpReg)
12576 .addImm(~(uint64_t)(Align-1));
12577 } else {
12578 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12579 .addReg(OverflowAddrReg);
12580 }
12581
12582 // Compute the next overflow address after this argument.
12583 // (the overflow address should be kept 8-byte aligned)
12584 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12585 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12586 .addReg(OverflowDestReg)
12587 .addImm(ArgSizeA8);
12588
12589 // Store the new overflow address.
12590 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12591 .addOperand(Base)
12592 .addOperand(Scale)
12593 .addOperand(Index)
12594 .addDisp(Disp, 8)
12595 .addOperand(Segment)
12596 .addReg(NextAddrReg)
12597 .setMemRefs(MMOBegin, MMOEnd);
12598
12599 // If we branched, emit the PHI to the front of endMBB.
12600 if (offsetMBB) {
12601 BuildMI(*endMBB, endMBB->begin(), DL,
12602 TII->get(X86::PHI), DestReg)
12603 .addReg(OffsetDestReg).addMBB(offsetMBB)
12604 .addReg(OverflowDestReg).addMBB(overflowMBB);
12605 }
12606
12607 // Erase the pseudo instruction
12608 MI->eraseFromParent();
12609
12610 return endMBB;
12611}
12612
12613MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012614X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12615 MachineInstr *MI,
12616 MachineBasicBlock *MBB) const {
12617 // Emit code to save XMM registers to the stack. The ABI says that the
12618 // number of registers to save is given in %al, so it's theoretically
12619 // possible to do an indirect jump trick to avoid saving all of them,
12620 // however this code takes a simpler approach and just executes all
12621 // of the stores if %al is non-zero. It's less code, and it's probably
12622 // easier on the hardware branch predictor, and stores aren't all that
12623 // expensive anyway.
12624
12625 // Create the new basic blocks. One block contains all the XMM stores,
12626 // and one block is the final destination regardless of whether any
12627 // stores were performed.
12628 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12629 MachineFunction *F = MBB->getParent();
12630 MachineFunction::iterator MBBIter = MBB;
12631 ++MBBIter;
12632 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12633 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12634 F->insert(MBBIter, XMMSaveMBB);
12635 F->insert(MBBIter, EndMBB);
12636
Dan Gohman14152b42010-07-06 20:24:04 +000012637 // Transfer the remainder of MBB and its successor edges to EndMBB.
12638 EndMBB->splice(EndMBB->begin(), MBB,
12639 llvm::next(MachineBasicBlock::iterator(MI)),
12640 MBB->end());
12641 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12642
Dan Gohmand6708ea2009-08-15 01:38:56 +000012643 // The original block will now fall through to the XMM save block.
12644 MBB->addSuccessor(XMMSaveMBB);
12645 // The XMMSaveMBB will fall through to the end block.
12646 XMMSaveMBB->addSuccessor(EndMBB);
12647
12648 // Now add the instructions.
12649 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12650 DebugLoc DL = MI->getDebugLoc();
12651
12652 unsigned CountReg = MI->getOperand(0).getReg();
12653 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12654 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12655
12656 if (!Subtarget->isTargetWin64()) {
12657 // If %al is 0, branch around the XMM save block.
12658 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012659 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012660 MBB->addSuccessor(EndMBB);
12661 }
12662
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012663 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012664 // In the XMM save block, save all the XMM argument registers.
12665 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12666 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012667 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012668 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012669 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012670 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012671 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012672 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012673 .addFrameIndex(RegSaveFrameIndex)
12674 .addImm(/*Scale=*/1)
12675 .addReg(/*IndexReg=*/0)
12676 .addImm(/*Disp=*/Offset)
12677 .addReg(/*Segment=*/0)
12678 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012679 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012680 }
12681
Dan Gohman14152b42010-07-06 20:24:04 +000012682 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012683
12684 return EndMBB;
12685}
Mon P Wang63307c32008-05-05 19:05:59 +000012686
Lang Hames6e3f7e42012-02-03 01:13:49 +000012687// The EFLAGS operand of SelectItr might be missing a kill marker
12688// because there were multiple uses of EFLAGS, and ISel didn't know
12689// which to mark. Figure out whether SelectItr should have had a
12690// kill marker, and set it if it should. Returns the correct kill
12691// marker value.
12692static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12693 MachineBasicBlock* BB,
12694 const TargetRegisterInfo* TRI) {
12695 // Scan forward through BB for a use/def of EFLAGS.
12696 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12697 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012698 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012699 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012700 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012701 if (mi.definesRegister(X86::EFLAGS))
12702 break; // Should have kill-flag - update below.
12703 }
12704
12705 // If we hit the end of the block, check whether EFLAGS is live into a
12706 // successor.
12707 if (miI == BB->end()) {
12708 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12709 sEnd = BB->succ_end();
12710 sItr != sEnd; ++sItr) {
12711 MachineBasicBlock* succ = *sItr;
12712 if (succ->isLiveIn(X86::EFLAGS))
12713 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012714 }
12715 }
12716
Lang Hames6e3f7e42012-02-03 01:13:49 +000012717 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12718 // out. SelectMI should have a kill flag on EFLAGS.
12719 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012720 return true;
12721}
12722
Evan Cheng60c07e12006-07-05 22:17:51 +000012723MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012724X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012725 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012726 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12727 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012728
Chris Lattner52600972009-09-02 05:57:00 +000012729 // To "insert" a SELECT_CC instruction, we actually have to insert the
12730 // diamond control-flow pattern. The incoming instruction knows the
12731 // destination vreg to set, the condition code register to branch on, the
12732 // true/false values to select between, and a branch opcode to use.
12733 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12734 MachineFunction::iterator It = BB;
12735 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012736
Chris Lattner52600972009-09-02 05:57:00 +000012737 // thisMBB:
12738 // ...
12739 // TrueVal = ...
12740 // cmpTY ccX, r1, r2
12741 // bCC copy1MBB
12742 // fallthrough --> copy0MBB
12743 MachineBasicBlock *thisMBB = BB;
12744 MachineFunction *F = BB->getParent();
12745 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12746 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012747 F->insert(It, copy0MBB);
12748 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012749
Bill Wendling730c07e2010-06-25 20:48:10 +000012750 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12751 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012752 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12753 if (!MI->killsRegister(X86::EFLAGS) &&
12754 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12755 copy0MBB->addLiveIn(X86::EFLAGS);
12756 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012757 }
12758
Dan Gohman14152b42010-07-06 20:24:04 +000012759 // Transfer the remainder of BB and its successor edges to sinkMBB.
12760 sinkMBB->splice(sinkMBB->begin(), BB,
12761 llvm::next(MachineBasicBlock::iterator(MI)),
12762 BB->end());
12763 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12764
12765 // Add the true and fallthrough blocks as its successors.
12766 BB->addSuccessor(copy0MBB);
12767 BB->addSuccessor(sinkMBB);
12768
12769 // Create the conditional branch instruction.
12770 unsigned Opc =
12771 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12772 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12773
Chris Lattner52600972009-09-02 05:57:00 +000012774 // copy0MBB:
12775 // %FalseValue = ...
12776 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012777 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012778
Chris Lattner52600972009-09-02 05:57:00 +000012779 // sinkMBB:
12780 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12781 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012782 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12783 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012784 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12785 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12786
Dan Gohman14152b42010-07-06 20:24:04 +000012787 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012788 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012789}
12790
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012791MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012792X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12793 bool Is64Bit) const {
12794 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12795 DebugLoc DL = MI->getDebugLoc();
12796 MachineFunction *MF = BB->getParent();
12797 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12798
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012799 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012800
12801 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12802 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12803
12804 // BB:
12805 // ... [Till the alloca]
12806 // If stacklet is not large enough, jump to mallocMBB
12807 //
12808 // bumpMBB:
12809 // Allocate by subtracting from RSP
12810 // Jump to continueMBB
12811 //
12812 // mallocMBB:
12813 // Allocate by call to runtime
12814 //
12815 // continueMBB:
12816 // ...
12817 // [rest of original BB]
12818 //
12819
12820 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12821 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12822 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12823
12824 MachineRegisterInfo &MRI = MF->getRegInfo();
12825 const TargetRegisterClass *AddrRegClass =
12826 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12827
12828 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12829 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12830 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012831 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012832 sizeVReg = MI->getOperand(1).getReg(),
12833 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12834
12835 MachineFunction::iterator MBBIter = BB;
12836 ++MBBIter;
12837
12838 MF->insert(MBBIter, bumpMBB);
12839 MF->insert(MBBIter, mallocMBB);
12840 MF->insert(MBBIter, continueMBB);
12841
12842 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12843 (MachineBasicBlock::iterator(MI)), BB->end());
12844 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12845
12846 // Add code to the main basic block to check if the stack limit has been hit,
12847 // and if so, jump to mallocMBB otherwise to bumpMBB.
12848 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012849 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012850 .addReg(tmpSPVReg).addReg(sizeVReg);
12851 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012852 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012853 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012854 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12855
12856 // bumpMBB simply decreases the stack pointer, since we know the current
12857 // stacklet has enough space.
12858 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012859 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012860 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012861 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012862 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12863
12864 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012865 const uint32_t *RegMask =
12866 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012867 if (Is64Bit) {
12868 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12869 .addReg(sizeVReg);
12870 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012871 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012872 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012873 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012874 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012875 } else {
12876 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12877 .addImm(12);
12878 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12879 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012880 .addExternalSymbol("__morestack_allocate_stack_space")
12881 .addRegMask(RegMask)
12882 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012883 }
12884
12885 if (!Is64Bit)
12886 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12887 .addImm(16);
12888
12889 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12890 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12891 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12892
12893 // Set up the CFG correctly.
12894 BB->addSuccessor(bumpMBB);
12895 BB->addSuccessor(mallocMBB);
12896 mallocMBB->addSuccessor(continueMBB);
12897 bumpMBB->addSuccessor(continueMBB);
12898
12899 // Take care of the PHI nodes.
12900 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12901 MI->getOperand(0).getReg())
12902 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12903 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12904
12905 // Delete the original pseudo instruction.
12906 MI->eraseFromParent();
12907
12908 // And we're done.
12909 return continueMBB;
12910}
12911
12912MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012913X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012914 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012915 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12916 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012917
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012918 assert(!Subtarget->isTargetEnvMacho());
12919
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012920 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12921 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012922
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012923 if (Subtarget->isTargetWin64()) {
12924 if (Subtarget->isTargetCygMing()) {
12925 // ___chkstk(Mingw64):
12926 // Clobbers R10, R11, RAX and EFLAGS.
12927 // Updates RSP.
12928 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12929 .addExternalSymbol("___chkstk")
12930 .addReg(X86::RAX, RegState::Implicit)
12931 .addReg(X86::RSP, RegState::Implicit)
12932 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12933 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12934 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12935 } else {
12936 // __chkstk(MSVCRT): does not update stack pointer.
12937 // Clobbers R10, R11 and EFLAGS.
12938 // FIXME: RAX(allocated size) might be reused and not killed.
12939 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12940 .addExternalSymbol("__chkstk")
12941 .addReg(X86::RAX, RegState::Implicit)
12942 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12943 // RAX has the offset to subtracted from RSP.
12944 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12945 .addReg(X86::RSP)
12946 .addReg(X86::RAX);
12947 }
12948 } else {
12949 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012950 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12951
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012952 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12953 .addExternalSymbol(StackProbeSymbol)
12954 .addReg(X86::EAX, RegState::Implicit)
12955 .addReg(X86::ESP, RegState::Implicit)
12956 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12957 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12958 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12959 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012960
Dan Gohman14152b42010-07-06 20:24:04 +000012961 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012962 return BB;
12963}
Chris Lattner52600972009-09-02 05:57:00 +000012964
12965MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012966X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12967 MachineBasicBlock *BB) const {
12968 // This is pretty easy. We're taking the value that we received from
12969 // our load from the relocation, sticking it in either RDI (x86-64)
12970 // or EAX and doing an indirect call. The return value will then
12971 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012972 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012973 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012974 DebugLoc DL = MI->getDebugLoc();
12975 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012976
12977 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012978 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012979
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012980 // Get a register mask for the lowered call.
12981 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
12982 // proper register mask.
12983 const uint32_t *RegMask =
12984 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012985 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012986 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12987 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012988 .addReg(X86::RIP)
12989 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012990 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012991 MI->getOperand(3).getTargetFlags())
12992 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012993 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012994 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012995 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000012996 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012997 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12998 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012999 .addReg(0)
13000 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013001 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000013002 MI->getOperand(3).getTargetFlags())
13003 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013004 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013005 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013006 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013007 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000013008 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
13009 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000013010 .addReg(TII->getGlobalBaseReg(F))
13011 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000013012 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000013013 MI->getOperand(3).getTargetFlags())
13014 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000013015 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000013016 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000013017 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013018 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000013019
Dan Gohman14152b42010-07-06 20:24:04 +000013020 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000013021 return BB;
13022}
13023
13024MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000013025X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013026 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000013027 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000013028 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013029 case X86::TAILJMPd64:
13030 case X86::TAILJMPr64:
13031 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000013032 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013033 case X86::TCRETURNdi64:
13034 case X86::TCRETURNri64:
13035 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000013036 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000013037 case X86::WIN_ALLOCA:
13038 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000013039 case X86::SEG_ALLOCA_32:
13040 return EmitLoweredSegAlloca(MI, BB, false);
13041 case X86::SEG_ALLOCA_64:
13042 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000013043 case X86::TLSCall_32:
13044 case X86::TLSCall_64:
13045 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000013046 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000013047 case X86::CMOV_FR32:
13048 case X86::CMOV_FR64:
13049 case X86::CMOV_V4F32:
13050 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000013051 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000013052 case X86::CMOV_V8F32:
13053 case X86::CMOV_V4F64:
13054 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000013055 case X86::CMOV_GR16:
13056 case X86::CMOV_GR32:
13057 case X86::CMOV_RFP32:
13058 case X86::CMOV_RFP64:
13059 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000013060 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013061
Dale Johannesen849f2142007-07-03 00:53:03 +000013062 case X86::FP32_TO_INT16_IN_MEM:
13063 case X86::FP32_TO_INT32_IN_MEM:
13064 case X86::FP32_TO_INT64_IN_MEM:
13065 case X86::FP64_TO_INT16_IN_MEM:
13066 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000013067 case X86::FP64_TO_INT64_IN_MEM:
13068 case X86::FP80_TO_INT16_IN_MEM:
13069 case X86::FP80_TO_INT32_IN_MEM:
13070 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000013071 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
13072 DebugLoc DL = MI->getDebugLoc();
13073
Evan Cheng60c07e12006-07-05 22:17:51 +000013074 // Change the floating point control register to use "round towards zero"
13075 // mode when truncating to an integer value.
13076 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000013077 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000013078 addFrameReference(BuildMI(*BB, MI, DL,
13079 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013080
13081 // Load the old value of the high byte of the control word...
13082 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000013083 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000013084 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000013085 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013086
13087 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000013088 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013089 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000013090
13091 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000013092 addFrameReference(BuildMI(*BB, MI, DL,
13093 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013094
13095 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000013096 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000013097 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000013098
13099 // Get the X86 opcode to use.
13100 unsigned Opc;
13101 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000013102 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000013103 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
13104 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
13105 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
13106 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
13107 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
13108 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000013109 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
13110 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
13111 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000013112 }
13113
13114 X86AddressMode AM;
13115 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000013116 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013117 AM.BaseType = X86AddressMode::RegBase;
13118 AM.Base.Reg = Op.getReg();
13119 } else {
13120 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013121 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013122 }
13123 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013124 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013125 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013126 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013127 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013128 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013129 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013130 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013131 AM.GV = Op.getGlobal();
13132 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013133 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013134 }
Dan Gohman14152b42010-07-06 20:24:04 +000013135 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013136 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013137
13138 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013139 addFrameReference(BuildMI(*BB, MI, DL,
13140 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013141
Dan Gohman14152b42010-07-06 20:24:04 +000013142 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013143 return BB;
13144 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013145 // String/text processing lowering.
13146 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013147 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013148 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013149 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013150 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013151 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013152 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013153 case X86::VPCMPESTRM128MEM: {
13154 unsigned NumArgs;
13155 bool MemArg;
13156 switch (MI->getOpcode()) {
13157 default: llvm_unreachable("illegal opcode!");
13158 case X86::PCMPISTRM128REG:
13159 case X86::VPCMPISTRM128REG:
13160 NumArgs = 3; MemArg = false; break;
13161 case X86::PCMPISTRM128MEM:
13162 case X86::VPCMPISTRM128MEM:
13163 NumArgs = 3; MemArg = true; break;
13164 case X86::PCMPESTRM128REG:
13165 case X86::VPCMPESTRM128REG:
13166 NumArgs = 5; MemArg = false; break;
13167 case X86::PCMPESTRM128MEM:
13168 case X86::VPCMPESTRM128MEM:
13169 NumArgs = 5; MemArg = true; break;
13170 }
13171 return EmitPCMP(MI, BB, NumArgs, MemArg);
13172 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013173
Eric Christopher228232b2010-11-30 07:20:12 +000013174 // Thread synchronization.
13175 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013176 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013177
Eric Christopherb120ab42009-08-18 22:50:32 +000013178 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000013179 case X86::ATOMMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013180 case X86::ATOMMAX32:
Mon P Wang63307c32008-05-05 19:05:59 +000013181 case X86::ATOMUMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013182 case X86::ATOMUMAX32:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013183 case X86::ATOMMIN16:
13184 case X86::ATOMMAX16:
13185 case X86::ATOMUMIN16:
13186 case X86::ATOMUMAX16:
13187 case X86::ATOMMIN64:
13188 case X86::ATOMMAX64:
13189 case X86::ATOMUMIN64:
13190 case X86::ATOMUMAX64: {
13191 unsigned Opc;
13192 switch (MI->getOpcode()) {
13193 default: llvm_unreachable("illegal opcode!");
13194 case X86::ATOMMIN32: Opc = X86::CMOVL32rr; break;
13195 case X86::ATOMMAX32: Opc = X86::CMOVG32rr; break;
13196 case X86::ATOMUMIN32: Opc = X86::CMOVB32rr; break;
13197 case X86::ATOMUMAX32: Opc = X86::CMOVA32rr; break;
13198 case X86::ATOMMIN16: Opc = X86::CMOVL16rr; break;
13199 case X86::ATOMMAX16: Opc = X86::CMOVG16rr; break;
13200 case X86::ATOMUMIN16: Opc = X86::CMOVB16rr; break;
13201 case X86::ATOMUMAX16: Opc = X86::CMOVA16rr; break;
13202 case X86::ATOMMIN64: Opc = X86::CMOVL64rr; break;
13203 case X86::ATOMMAX64: Opc = X86::CMOVG64rr; break;
13204 case X86::ATOMUMIN64: Opc = X86::CMOVB64rr; break;
13205 case X86::ATOMUMAX64: Opc = X86::CMOVA64rr; break;
13206 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
13207 }
13208 return EmitAtomicMinMaxWithCustomInserter(MI, BB, Opc);
13209 }
13210
13211 case X86::ATOMAND32:
13212 case X86::ATOMOR32:
13213 case X86::ATOMXOR32:
13214 case X86::ATOMNAND32: {
13215 bool Invert = false;
13216 unsigned RegOpc, ImmOpc;
13217 switch (MI->getOpcode()) {
13218 default: llvm_unreachable("illegal opcode!");
13219 case X86::ATOMAND32:
13220 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; break;
13221 case X86::ATOMOR32:
13222 RegOpc = X86::OR32rr; ImmOpc = X86::OR32ri; break;
13223 case X86::ATOMXOR32:
13224 RegOpc = X86::XOR32rr; ImmOpc = X86::XOR32ri; break;
13225 case X86::ATOMNAND32:
13226 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; Invert = true; break;
13227 }
13228 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13229 X86::MOV32rm, X86::LCMPXCHG32,
13230 X86::NOT32r, X86::EAX,
13231 &X86::GR32RegClass, Invert);
13232 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013233
13234 case X86::ATOMAND16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013235 case X86::ATOMOR16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013236 case X86::ATOMXOR16:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013237 case X86::ATOMNAND16: {
13238 bool Invert = false;
13239 unsigned RegOpc, ImmOpc;
13240 switch (MI->getOpcode()) {
13241 default: llvm_unreachable("illegal opcode!");
13242 case X86::ATOMAND16:
13243 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; break;
13244 case X86::ATOMOR16:
13245 RegOpc = X86::OR16rr; ImmOpc = X86::OR16ri; break;
13246 case X86::ATOMXOR16:
13247 RegOpc = X86::XOR16rr; ImmOpc = X86::XOR16ri; break;
13248 case X86::ATOMNAND16:
13249 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; Invert = true; break;
13250 }
13251 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13252 X86::MOV16rm, X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013253 X86::NOT16r, X86::AX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013254 &X86::GR16RegClass, Invert);
13255 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013256
13257 case X86::ATOMAND8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013258 case X86::ATOMOR8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013259 case X86::ATOMXOR8:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013260 case X86::ATOMNAND8: {
13261 bool Invert = false;
13262 unsigned RegOpc, ImmOpc;
13263 switch (MI->getOpcode()) {
13264 default: llvm_unreachable("illegal opcode!");
13265 case X86::ATOMAND8:
13266 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; break;
13267 case X86::ATOMOR8:
13268 RegOpc = X86::OR8rr; ImmOpc = X86::OR8ri; break;
13269 case X86::ATOMXOR8:
13270 RegOpc = X86::XOR8rr; ImmOpc = X86::XOR8ri; break;
13271 case X86::ATOMNAND8:
13272 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; Invert = true; break;
13273 }
13274 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13275 X86::MOV8rm, X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013276 X86::NOT8r, X86::AL,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013277 &X86::GR8RegClass, Invert);
13278 }
13279
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013280 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000013281 case X86::ATOMAND64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013282 case X86::ATOMOR64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013283 case X86::ATOMXOR64:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013284 case X86::ATOMNAND64: {
13285 bool Invert = false;
13286 unsigned RegOpc, ImmOpc;
13287 switch (MI->getOpcode()) {
13288 default: llvm_unreachable("illegal opcode!");
13289 case X86::ATOMAND64:
13290 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; break;
13291 case X86::ATOMOR64:
13292 RegOpc = X86::OR64rr; ImmOpc = X86::OR64ri32; break;
13293 case X86::ATOMXOR64:
13294 RegOpc = X86::XOR64rr; ImmOpc = X86::XOR64ri32; break;
13295 case X86::ATOMNAND64:
13296 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; Invert = true; break;
13297 }
13298 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13299 X86::MOV64rm, X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013300 X86::NOT64r, X86::RAX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013301 &X86::GR64RegClass, Invert);
13302 }
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013303
13304 // This group does 64-bit operations on a 32-bit host.
13305 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013306 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013307 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013308 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013309 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013310 case X86::ATOMSUB6432:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013311 case X86::ATOMSWAP6432: {
13312 bool Invert = false;
13313 unsigned RegOpcL, RegOpcH, ImmOpcL, ImmOpcH;
13314 switch (MI->getOpcode()) {
13315 default: llvm_unreachable("illegal opcode!");
13316 case X86::ATOMAND6432:
13317 RegOpcL = RegOpcH = X86::AND32rr;
13318 ImmOpcL = ImmOpcH = X86::AND32ri;
13319 break;
13320 case X86::ATOMOR6432:
13321 RegOpcL = RegOpcH = X86::OR32rr;
13322 ImmOpcL = ImmOpcH = X86::OR32ri;
13323 break;
13324 case X86::ATOMXOR6432:
13325 RegOpcL = RegOpcH = X86::XOR32rr;
13326 ImmOpcL = ImmOpcH = X86::XOR32ri;
13327 break;
13328 case X86::ATOMNAND6432:
13329 RegOpcL = RegOpcH = X86::AND32rr;
13330 ImmOpcL = ImmOpcH = X86::AND32ri;
13331 Invert = true;
13332 break;
13333 case X86::ATOMADD6432:
13334 RegOpcL = X86::ADD32rr; RegOpcH = X86::ADC32rr;
13335 ImmOpcL = X86::ADD32ri; ImmOpcH = X86::ADC32ri;
13336 break;
13337 case X86::ATOMSUB6432:
13338 RegOpcL = X86::SUB32rr; RegOpcH = X86::SBB32rr;
13339 ImmOpcL = X86::SUB32ri; ImmOpcH = X86::SBB32ri;
13340 break;
13341 case X86::ATOMSWAP6432:
13342 RegOpcL = RegOpcH = X86::MOV32rr;
13343 ImmOpcL = ImmOpcH = X86::MOV32ri;
13344 break;
13345 }
13346 return EmitAtomicBit6432WithCustomInserter(MI, BB, RegOpcL, RegOpcH,
13347 ImmOpcL, ImmOpcH, Invert);
13348 }
13349
Dan Gohmand6708ea2009-08-15 01:38:56 +000013350 case X86::VASTART_SAVE_XMM_REGS:
13351 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013352
13353 case X86::VAARG_64:
13354 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013355 }
13356}
13357
13358//===----------------------------------------------------------------------===//
13359// X86 Optimization Hooks
13360//===----------------------------------------------------------------------===//
13361
Dan Gohman475871a2008-07-27 21:46:04 +000013362void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013363 APInt &KnownZero,
13364 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013365 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013366 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013367 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013368 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013369 assert((Opc >= ISD::BUILTIN_OP_END ||
13370 Opc == ISD::INTRINSIC_WO_CHAIN ||
13371 Opc == ISD::INTRINSIC_W_CHAIN ||
13372 Opc == ISD::INTRINSIC_VOID) &&
13373 "Should use MaskedValueIsZero if you don't know whether Op"
13374 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013375
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013376 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013377 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013378 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013379 case X86ISD::ADD:
13380 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013381 case X86ISD::ADC:
13382 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013383 case X86ISD::SMUL:
13384 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013385 case X86ISD::INC:
13386 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013387 case X86ISD::OR:
13388 case X86ISD::XOR:
13389 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013390 // These nodes' second result is a boolean.
13391 if (Op.getResNo() == 0)
13392 break;
13393 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013394 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013395 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013396 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013397 case ISD::INTRINSIC_WO_CHAIN: {
13398 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13399 unsigned NumLoBits = 0;
13400 switch (IntId) {
13401 default: break;
13402 case Intrinsic::x86_sse_movmsk_ps:
13403 case Intrinsic::x86_avx_movmsk_ps_256:
13404 case Intrinsic::x86_sse2_movmsk_pd:
13405 case Intrinsic::x86_avx_movmsk_pd_256:
13406 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013407 case Intrinsic::x86_sse2_pmovmskb_128:
13408 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013409 // High bits of movmskp{s|d}, pmovmskb are known zero.
13410 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013411 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013412 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13413 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13414 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13415 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13416 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13417 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013418 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013419 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013420 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013421 break;
13422 }
13423 }
13424 break;
13425 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013426 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013427}
Chris Lattner259e97c2006-01-31 19:43:35 +000013428
Owen Andersonbc146b02010-09-21 20:42:50 +000013429unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13430 unsigned Depth) const {
13431 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13432 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13433 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013434
Owen Andersonbc146b02010-09-21 20:42:50 +000013435 // Fallback case.
13436 return 1;
13437}
13438
Evan Cheng206ee9d2006-07-07 08:33:52 +000013439/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013440/// node is a GlobalAddress + offset.
13441bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013442 const GlobalValue* &GA,
13443 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013444 if (N->getOpcode() == X86ISD::Wrapper) {
13445 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013446 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013447 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013448 return true;
13449 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013450 }
Evan Chengad4196b2008-05-12 19:56:52 +000013451 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013452}
13453
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013454/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13455/// same as extracting the high 128-bit part of 256-bit vector and then
13456/// inserting the result into the low part of a new 256-bit vector
13457static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13458 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013459 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013460
13461 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013462 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013463 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13464 SVOp->getMaskElt(j) >= 0)
13465 return false;
13466
13467 return true;
13468}
13469
13470/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13471/// same as extracting the low 128-bit part of 256-bit vector and then
13472/// inserting the result into the high part of a new 256-bit vector
13473static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13474 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013475 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013476
13477 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013478 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013479 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13480 SVOp->getMaskElt(j) >= 0)
13481 return false;
13482
13483 return true;
13484}
13485
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013486/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13487static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013488 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013489 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013490 DebugLoc dl = N->getDebugLoc();
13491 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13492 SDValue V1 = SVOp->getOperand(0);
13493 SDValue V2 = SVOp->getOperand(1);
13494 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013495 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013496
13497 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13498 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13499 //
13500 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013501 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013502 // V UNDEF BUILD_VECTOR UNDEF
13503 // \ / \ /
13504 // CONCAT_VECTOR CONCAT_VECTOR
13505 // \ /
13506 // \ /
13507 // RESULT: V + zero extended
13508 //
13509 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13510 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13511 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13512 return SDValue();
13513
13514 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13515 return SDValue();
13516
13517 // To match the shuffle mask, the first half of the mask should
13518 // be exactly the first vector, and all the rest a splat with the
13519 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013520 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013521 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13522 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13523 return SDValue();
13524
Chad Rosier3d1161e2012-01-03 21:05:52 +000013525 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13526 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013527 if (Ld->hasNUsesOfValue(1, 0)) {
13528 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13529 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13530 SDValue ResNode =
13531 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13532 Ld->getMemoryVT(),
13533 Ld->getPointerInfo(),
13534 Ld->getAlignment(),
13535 false/*isVolatile*/, true/*ReadMem*/,
13536 false/*WriteMem*/);
13537 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13538 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013539 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013540
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013541 // Emit a zeroed vector and insert the desired subvector on its
13542 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013543 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013544 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013545 return DCI.CombineTo(N, InsV);
13546 }
13547
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013548 //===--------------------------------------------------------------------===//
13549 // Combine some shuffles into subvector extracts and inserts:
13550 //
13551
13552 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13553 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013554 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13555 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013556 return DCI.CombineTo(N, InsV);
13557 }
13558
13559 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13560 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013561 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13562 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013563 return DCI.CombineTo(N, InsV);
13564 }
13565
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013566 return SDValue();
13567}
13568
13569/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013570static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013571 TargetLowering::DAGCombinerInfo &DCI,
13572 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013573 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013574 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013575
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013576 // Don't create instructions with illegal types after legalize types has run.
13577 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13578 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13579 return SDValue();
13580
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013581 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013582 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013583 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013584 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013585
13586 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013587 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013588 return SDValue();
13589
13590 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13591 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13592 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013593 SmallVector<SDValue, 16> Elts;
13594 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013595 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013596
Nate Begemanfdea31a2010-03-24 20:49:50 +000013597 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013598}
Evan Chengd880b972008-05-09 21:53:03 +000013599
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013600
Craig Topper55b24052012-09-11 06:15:32 +000013601/// PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013602/// a sequence of vector shuffle operations.
13603/// It is possible when we truncate 256-bit vector to 128-bit vector
Craig Topper55b24052012-09-11 06:15:32 +000013604static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
13605 TargetLowering::DAGCombinerInfo &DCI,
13606 const X86Subtarget *Subtarget) {
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013607 if (!DCI.isBeforeLegalizeOps())
13608 return SDValue();
13609
Craig Topper3ef43cf2012-04-24 06:36:35 +000013610 if (!Subtarget->hasAVX())
13611 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013612
13613 EVT VT = N->getValueType(0);
13614 SDValue Op = N->getOperand(0);
13615 EVT OpVT = Op.getValueType();
13616 DebugLoc dl = N->getDebugLoc();
13617
13618 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13619
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013620 if (Subtarget->hasAVX2()) {
13621 // AVX2: v4i64 -> v4i32
13622
13623 // VPERMD
13624 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13625
13626 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13627 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13628 ShufMask);
13629
Craig Topperd63fa652012-04-22 18:51:37 +000013630 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13631 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013632 }
13633
13634 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013635 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013636 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013637
13638 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013639 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013640
13641 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13642 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13643
13644 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013645 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013646
Craig Toppercacafd42012-08-14 08:18:43 +000013647 SDValue Undef = DAG.getUNDEF(VT);
13648 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13649 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013650
13651 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013652 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013653
Elena Demikhovsky73252572012-02-01 10:33:05 +000013654 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013655 }
Craig Topperd63fa652012-04-22 18:51:37 +000013656
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013657 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13658
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013659 if (Subtarget->hasAVX2()) {
13660 // AVX2: v8i32 -> v8i16
13661
13662 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013663
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013664 // PSHUFB
13665 SmallVector<SDValue,32> pshufbMask;
13666 for (unsigned i = 0; i < 2; ++i) {
13667 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13668 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13669 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13670 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13671 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13672 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13673 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13674 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13675 for (unsigned j = 0; j < 8; ++j)
13676 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13677 }
Craig Topperd63fa652012-04-22 18:51:37 +000013678 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13679 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013680 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13681
13682 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13683
13684 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013685 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013686 &ShufMask[0]);
13687
Craig Topperd63fa652012-04-22 18:51:37 +000013688 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13689 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013690
13691 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13692 }
13693
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013694 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013695 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013696
13697 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013698 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013699
13700 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13701 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13702
13703 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013704 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13705 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013706
Craig Toppercacafd42012-08-14 08:18:43 +000013707 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13708 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13709 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013710
13711 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13712 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13713
13714 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013715 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013716
Elena Demikhovsky73252572012-02-01 10:33:05 +000013717 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013718 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013719 }
13720
13721 return SDValue();
13722}
13723
Craig Topper89f4e662012-03-20 07:17:59 +000013724/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13725/// specific shuffle of a load can be folded into a single element load.
13726/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13727/// shuffles have been customed lowered so we need to handle those here.
13728static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13729 TargetLowering::DAGCombinerInfo &DCI) {
13730 if (DCI.isBeforeLegalizeOps())
13731 return SDValue();
13732
13733 SDValue InVec = N->getOperand(0);
13734 SDValue EltNo = N->getOperand(1);
13735
13736 if (!isa<ConstantSDNode>(EltNo))
13737 return SDValue();
13738
13739 EVT VT = InVec.getValueType();
13740
13741 bool HasShuffleIntoBitcast = false;
13742 if (InVec.getOpcode() == ISD::BITCAST) {
13743 // Don't duplicate a load with other uses.
13744 if (!InVec.hasOneUse())
13745 return SDValue();
13746 EVT BCVT = InVec.getOperand(0).getValueType();
13747 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13748 return SDValue();
13749 InVec = InVec.getOperand(0);
13750 HasShuffleIntoBitcast = true;
13751 }
13752
13753 if (!isTargetShuffle(InVec.getOpcode()))
13754 return SDValue();
13755
13756 // Don't duplicate a load with other uses.
13757 if (!InVec.hasOneUse())
13758 return SDValue();
13759
13760 SmallVector<int, 16> ShuffleMask;
13761 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013762 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13763 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013764 return SDValue();
13765
13766 // Select the input vector, guarding against out of range extract vector.
13767 unsigned NumElems = VT.getVectorNumElements();
13768 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13769 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13770 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13771 : InVec.getOperand(1);
13772
13773 // If inputs to shuffle are the same for both ops, then allow 2 uses
13774 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13775
13776 if (LdNode.getOpcode() == ISD::BITCAST) {
13777 // Don't duplicate a load with other uses.
13778 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13779 return SDValue();
13780
13781 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13782 LdNode = LdNode.getOperand(0);
13783 }
13784
13785 if (!ISD::isNormalLoad(LdNode.getNode()))
13786 return SDValue();
13787
13788 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13789
13790 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13791 return SDValue();
13792
13793 if (HasShuffleIntoBitcast) {
13794 // If there's a bitcast before the shuffle, check if the load type and
13795 // alignment is valid.
13796 unsigned Align = LN0->getAlignment();
13797 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13798 unsigned NewAlign = TLI.getTargetData()->
13799 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13800
13801 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13802 return SDValue();
13803 }
13804
13805 // All checks match so transform back to vector_shuffle so that DAG combiner
13806 // can finish the job
13807 DebugLoc dl = N->getDebugLoc();
13808
13809 // Create shuffle node taking into account the case that its a unary shuffle
13810 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13811 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13812 InVec.getOperand(0), Shuffle,
13813 &ShuffleMask[0]);
13814 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13815 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13816 EltNo);
13817}
13818
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013819/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13820/// generation and convert it from being a bunch of shuffles and extracts
13821/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013822static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013823 TargetLowering::DAGCombinerInfo &DCI) {
13824 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13825 if (NewOp.getNode())
13826 return NewOp;
13827
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013828 SDValue InputVector = N->getOperand(0);
13829
13830 // Only operate on vectors of 4 elements, where the alternative shuffling
13831 // gets to be more expensive.
13832 if (InputVector.getValueType() != MVT::v4i32)
13833 return SDValue();
13834
13835 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13836 // single use which is a sign-extend or zero-extend, and all elements are
13837 // used.
13838 SmallVector<SDNode *, 4> Uses;
13839 unsigned ExtractedElements = 0;
13840 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13841 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13842 if (UI.getUse().getResNo() != InputVector.getResNo())
13843 return SDValue();
13844
13845 SDNode *Extract = *UI;
13846 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13847 return SDValue();
13848
13849 if (Extract->getValueType(0) != MVT::i32)
13850 return SDValue();
13851 if (!Extract->hasOneUse())
13852 return SDValue();
13853 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13854 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13855 return SDValue();
13856 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13857 return SDValue();
13858
13859 // Record which element was extracted.
13860 ExtractedElements |=
13861 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13862
13863 Uses.push_back(Extract);
13864 }
13865
13866 // If not all the elements were used, this may not be worthwhile.
13867 if (ExtractedElements != 15)
13868 return SDValue();
13869
13870 // Ok, we've now decided to do the transformation.
13871 DebugLoc dl = InputVector.getDebugLoc();
13872
13873 // Store the value to a temporary stack slot.
13874 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013875 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13876 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013877
13878 // Replace each use (extract) with a load of the appropriate element.
13879 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13880 UE = Uses.end(); UI != UE; ++UI) {
13881 SDNode *Extract = *UI;
13882
Nadav Rotem86694292011-05-17 08:31:57 +000013883 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013884 SDValue Idx = Extract->getOperand(1);
13885 unsigned EltSize =
13886 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13887 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013888 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013889 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13890
Nadav Rotem86694292011-05-17 08:31:57 +000013891 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013892 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013893
13894 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013895 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013896 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013897 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013898
13899 // Replace the exact with the load.
13900 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13901 }
13902
13903 // The replacement was made in place; don't return anything.
13904 return SDValue();
13905}
13906
Duncan Sands6bcd2192011-09-17 16:49:39 +000013907/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13908/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013909static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013910 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013911 const X86Subtarget *Subtarget) {
13912 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013913 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013914 // Get the LHS/RHS of the select.
13915 SDValue LHS = N->getOperand(1);
13916 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013917 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013918
Dan Gohman670e5392009-09-21 18:03:22 +000013919 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013920 // instructions match the semantics of the common C idiom x<y?x:y but not
13921 // x<=y?x:y, because of how they handle negative zero (which can be
13922 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013923 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13924 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013925 (Subtarget->hasSSE2() ||
13926 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013927 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013928
Chris Lattner47b4ce82009-03-11 05:48:52 +000013929 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013930 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013931 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13932 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013933 switch (CC) {
13934 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013935 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013936 // Converting this to a min would handle NaNs incorrectly, and swapping
13937 // the operands would cause it to handle comparisons between positive
13938 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013939 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013940 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013941 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13942 break;
13943 std::swap(LHS, RHS);
13944 }
Dan Gohman670e5392009-09-21 18:03:22 +000013945 Opcode = X86ISD::FMIN;
13946 break;
13947 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013948 // Converting this to a min would handle comparisons between positive
13949 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013950 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013951 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13952 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013953 Opcode = X86ISD::FMIN;
13954 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013955 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013956 // Converting this to a min would handle both negative zeros and NaNs
13957 // incorrectly, but we can swap the operands to fix both.
13958 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013959 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013960 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013961 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013962 Opcode = X86ISD::FMIN;
13963 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013964
Dan Gohman670e5392009-09-21 18:03:22 +000013965 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013966 // Converting this to a max would handle comparisons between positive
13967 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013968 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013969 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013970 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013971 Opcode = X86ISD::FMAX;
13972 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013973 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013974 // Converting this to a max would handle NaNs incorrectly, and swapping
13975 // the operands would cause it to handle comparisons between positive
13976 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013977 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013978 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013979 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13980 break;
13981 std::swap(LHS, RHS);
13982 }
Dan Gohman670e5392009-09-21 18:03:22 +000013983 Opcode = X86ISD::FMAX;
13984 break;
13985 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013986 // Converting this to a max would handle both negative zeros and NaNs
13987 // incorrectly, but we can swap the operands to fix both.
13988 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013989 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013990 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013991 case ISD::SETGE:
13992 Opcode = X86ISD::FMAX;
13993 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013994 }
Dan Gohman670e5392009-09-21 18:03:22 +000013995 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013996 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13997 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013998 switch (CC) {
13999 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000014000 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014001 // Converting this to a min would handle comparisons between positive
14002 // and negative zero incorrectly, and swapping the operands would
14003 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014004 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014005 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000014006 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014007 break;
14008 std::swap(LHS, RHS);
14009 }
Dan Gohman670e5392009-09-21 18:03:22 +000014010 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000014011 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014012 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000014013 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014014 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014015 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
14016 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014017 Opcode = X86ISD::FMIN;
14018 break;
14019 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000014020 // Converting this to a min would handle both negative zeros and NaNs
14021 // incorrectly, but we can swap the operands to fix both.
14022 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014023 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014024 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014025 case ISD::SETGE:
14026 Opcode = X86ISD::FMIN;
14027 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014028
Dan Gohman670e5392009-09-21 18:03:22 +000014029 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000014030 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000014031 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014032 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014033 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000014034 break;
Dan Gohman670e5392009-09-21 18:03:22 +000014035 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000014036 // Converting this to a max would handle comparisons between positive
14037 // and negative zero incorrectly, and swapping the operands would
14038 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000014039 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000014040 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000014041 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000014042 break;
14043 std::swap(LHS, RHS);
14044 }
Dan Gohman670e5392009-09-21 18:03:22 +000014045 Opcode = X86ISD::FMAX;
14046 break;
14047 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000014048 // Converting this to a max would handle both negative zeros and NaNs
14049 // incorrectly, but we can swap the operands to fix both.
14050 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000014051 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014052 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000014053 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000014054 Opcode = X86ISD::FMAX;
14055 break;
14056 }
Chris Lattner83e6c992006-10-04 06:57:07 +000014057 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000014058
Chris Lattner47b4ce82009-03-11 05:48:52 +000014059 if (Opcode)
14060 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000014061 }
Eric Christopherfd179292009-08-27 18:07:15 +000014062
Chris Lattnerd1980a52009-03-12 06:52:53 +000014063 // If this is a select between two integer constants, try to do some
14064 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000014065 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
14066 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000014067 // Don't do this for crazy integer types.
14068 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
14069 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000014070 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014071 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000014072
Chris Lattnercee56e72009-03-13 05:53:31 +000014073 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000014074 // Efficiently invertible.
14075 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
14076 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
14077 isa<ConstantSDNode>(Cond.getOperand(1))))) {
14078 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000014079 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000014080 }
Eric Christopherfd179292009-08-27 18:07:15 +000014081
Chris Lattnerd1980a52009-03-12 06:52:53 +000014082 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014083 if (FalseC->getAPIntValue() == 0 &&
14084 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014085 if (NeedsCondInvert) // Invert the condition if needed.
14086 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14087 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014088
Chris Lattnerd1980a52009-03-12 06:52:53 +000014089 // Zero extend the condition if needed.
14090 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014091
Chris Lattnercee56e72009-03-13 05:53:31 +000014092 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000014093 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014094 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014095 }
Eric Christopherfd179292009-08-27 18:07:15 +000014096
Chris Lattner97a29a52009-03-13 05:22:11 +000014097 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000014098 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000014099 if (NeedsCondInvert) // Invert the condition if needed.
14100 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14101 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014102
Chris Lattner97a29a52009-03-13 05:22:11 +000014103 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014104 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14105 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014106 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000014107 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000014108 }
Eric Christopherfd179292009-08-27 18:07:15 +000014109
Chris Lattnercee56e72009-03-13 05:53:31 +000014110 // Optimize cases that will turn into an LEA instruction. This requires
14111 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014112 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014113 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014114 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014115
Chris Lattnercee56e72009-03-13 05:53:31 +000014116 bool isFastMultiplier = false;
14117 if (Diff < 10) {
14118 switch ((unsigned char)Diff) {
14119 default: break;
14120 case 1: // result = add base, cond
14121 case 2: // result = lea base( , cond*2)
14122 case 3: // result = lea base(cond, cond*2)
14123 case 4: // result = lea base( , cond*4)
14124 case 5: // result = lea base(cond, cond*4)
14125 case 8: // result = lea base( , cond*8)
14126 case 9: // result = lea base(cond, cond*8)
14127 isFastMultiplier = true;
14128 break;
14129 }
14130 }
Eric Christopherfd179292009-08-27 18:07:15 +000014131
Chris Lattnercee56e72009-03-13 05:53:31 +000014132 if (isFastMultiplier) {
14133 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14134 if (NeedsCondInvert) // Invert the condition if needed.
14135 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14136 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014137
Chris Lattnercee56e72009-03-13 05:53:31 +000014138 // Zero extend the condition if needed.
14139 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14140 Cond);
14141 // Scale the condition by the difference.
14142 if (Diff != 1)
14143 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14144 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014145
Chris Lattnercee56e72009-03-13 05:53:31 +000014146 // Add the base if non-zero.
14147 if (FalseC->getAPIntValue() != 0)
14148 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14149 SDValue(FalseC, 0));
14150 return Cond;
14151 }
Eric Christopherfd179292009-08-27 18:07:15 +000014152 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014153 }
14154 }
Eric Christopherfd179292009-08-27 18:07:15 +000014155
Evan Cheng56f582d2012-01-04 01:41:39 +000014156 // Canonicalize max and min:
14157 // (x > y) ? x : y -> (x >= y) ? x : y
14158 // (x < y) ? x : y -> (x <= y) ? x : y
14159 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14160 // the need for an extra compare
14161 // against zero. e.g.
14162 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14163 // subl %esi, %edi
14164 // testl %edi, %edi
14165 // movl $0, %eax
14166 // cmovgl %edi, %eax
14167 // =>
14168 // xorl %eax, %eax
14169 // subl %esi, $edi
14170 // cmovsl %eax, %edi
14171 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14172 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14173 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14174 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14175 switch (CC) {
14176 default: break;
14177 case ISD::SETLT:
14178 case ISD::SETGT: {
14179 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14180 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14181 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14182 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14183 }
14184 }
14185 }
14186
Nadav Rotemcc616562012-01-15 19:27:55 +000014187 // If we know that this node is legal then we know that it is going to be
14188 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14189 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14190 // to simplify previous instructions.
14191 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14192 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014193 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014194 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014195
14196 // Don't optimize vector selects that map to mask-registers.
14197 if (BitWidth == 1)
14198 return SDValue();
14199
Nadav Rotemcc616562012-01-15 19:27:55 +000014200 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14201 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14202
14203 APInt KnownZero, KnownOne;
14204 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14205 DCI.isBeforeLegalizeOps());
14206 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14207 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14208 DCI.CommitTargetLoweringOpt(TLO);
14209 }
14210
Dan Gohman475871a2008-07-27 21:46:04 +000014211 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014212}
14213
Michael Liao2a33cec2012-08-10 19:58:13 +000014214// Check whether a boolean test is testing a boolean value generated by
14215// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14216// code.
14217//
14218// Simplify the following patterns:
14219// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14220// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14221// to (Op EFLAGS Cond)
14222//
14223// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14224// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14225// to (Op EFLAGS !Cond)
14226//
14227// where Op could be BRCOND or CMOV.
14228//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014229static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014230 // Quit if not CMP and SUB with its value result used.
14231 if (Cmp.getOpcode() != X86ISD::CMP &&
14232 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14233 return SDValue();
14234
14235 // Quit if not used as a boolean value.
14236 if (CC != X86::COND_E && CC != X86::COND_NE)
14237 return SDValue();
14238
14239 // Check CMP operands. One of them should be 0 or 1 and the other should be
14240 // an SetCC or extended from it.
14241 SDValue Op1 = Cmp.getOperand(0);
14242 SDValue Op2 = Cmp.getOperand(1);
14243
14244 SDValue SetCC;
14245 const ConstantSDNode* C = 0;
14246 bool needOppositeCond = (CC == X86::COND_E);
14247
14248 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14249 SetCC = Op2;
14250 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14251 SetCC = Op1;
14252 else // Quit if all operands are not constants.
14253 return SDValue();
14254
14255 if (C->getZExtValue() == 1)
14256 needOppositeCond = !needOppositeCond;
14257 else if (C->getZExtValue() != 0)
14258 // Quit if the constant is neither 0 or 1.
14259 return SDValue();
14260
14261 // Skip 'zext' node.
14262 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14263 SetCC = SetCC.getOperand(0);
14264
Michael Liao7fdc66b2012-09-10 16:36:16 +000014265 switch (SetCC.getOpcode()) {
14266 case X86ISD::SETCC:
14267 // Set the condition code or opposite one if necessary.
14268 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14269 if (needOppositeCond)
14270 CC = X86::GetOppositeBranchCondition(CC);
14271 return SetCC.getOperand(1);
14272 case X86ISD::CMOV: {
14273 // Check whether false/true value has canonical one, i.e. 0 or 1.
14274 ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
14275 ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
14276 // Quit if true value is not a constant.
14277 if (!TVal)
14278 return SDValue();
14279 // Quit if false value is not a constant.
14280 if (!FVal) {
14281 // A special case for rdrand, where 0 is set if false cond is found.
14282 SDValue Op = SetCC.getOperand(0);
14283 if (Op.getOpcode() != X86ISD::RDRAND)
14284 return SDValue();
14285 }
14286 // Quit if false value is not the constant 0 or 1.
14287 bool FValIsFalse = true;
14288 if (FVal && FVal->getZExtValue() != 0) {
14289 if (FVal->getZExtValue() != 1)
14290 return SDValue();
14291 // If FVal is 1, opposite cond is needed.
14292 needOppositeCond = !needOppositeCond;
14293 FValIsFalse = false;
14294 }
14295 // Quit if TVal is not the constant opposite of FVal.
14296 if (FValIsFalse && TVal->getZExtValue() != 1)
14297 return SDValue();
14298 if (!FValIsFalse && TVal->getZExtValue() != 0)
14299 return SDValue();
14300 CC = X86::CondCode(SetCC.getConstantOperandVal(2));
14301 if (needOppositeCond)
14302 CC = X86::GetOppositeBranchCondition(CC);
14303 return SetCC.getOperand(3);
14304 }
14305 }
Michael Liao2a33cec2012-08-10 19:58:13 +000014306
Michael Liao7fdc66b2012-09-10 16:36:16 +000014307 return SDValue();
Michael Liao2a33cec2012-08-10 19:58:13 +000014308}
14309
Chris Lattnerd1980a52009-03-12 06:52:53 +000014310/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14311static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014312 TargetLowering::DAGCombinerInfo &DCI,
14313 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014314 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014315
Chris Lattnerd1980a52009-03-12 06:52:53 +000014316 // If the flag operand isn't dead, don't touch this CMOV.
14317 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14318 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014319
Evan Chengb5a55d92011-05-24 01:48:22 +000014320 SDValue FalseOp = N->getOperand(0);
14321 SDValue TrueOp = N->getOperand(1);
14322 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14323 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014324
Evan Chengb5a55d92011-05-24 01:48:22 +000014325 if (CC == X86::COND_E || CC == X86::COND_NE) {
14326 switch (Cond.getOpcode()) {
14327 default: break;
14328 case X86ISD::BSR:
14329 case X86ISD::BSF:
14330 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14331 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14332 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14333 }
14334 }
14335
Michael Liao2a33cec2012-08-10 19:58:13 +000014336 SDValue Flags;
14337
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014338 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014339 if (Flags.getNode() &&
14340 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014341 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014342 SDValue Ops[] = { FalseOp, TrueOp,
14343 DAG.getConstant(CC, MVT::i8), Flags };
14344 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14345 Ops, array_lengthof(Ops));
14346 }
14347
Chris Lattnerd1980a52009-03-12 06:52:53 +000014348 // If this is a select between two integer constants, try to do some
14349 // optimizations. Note that the operands are ordered the opposite of SELECT
14350 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014351 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14352 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014353 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14354 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014355 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14356 CC = X86::GetOppositeBranchCondition(CC);
14357 std::swap(TrueC, FalseC);
14358 }
Eric Christopherfd179292009-08-27 18:07:15 +000014359
Chris Lattnerd1980a52009-03-12 06:52:53 +000014360 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014361 // This is efficient for any integer data type (including i8/i16) and
14362 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014363 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014364 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14365 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014366
Chris Lattnerd1980a52009-03-12 06:52:53 +000014367 // Zero extend the condition if needed.
14368 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014369
Chris Lattnerd1980a52009-03-12 06:52:53 +000014370 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14371 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014372 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014373 if (N->getNumValues() == 2) // Dead flag value?
14374 return DCI.CombineTo(N, Cond, SDValue());
14375 return Cond;
14376 }
Eric Christopherfd179292009-08-27 18:07:15 +000014377
Chris Lattnercee56e72009-03-13 05:53:31 +000014378 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14379 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014380 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014381 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14382 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014383
Chris Lattner97a29a52009-03-13 05:22:11 +000014384 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014385 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14386 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014387 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14388 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014389
Chris Lattner97a29a52009-03-13 05:22:11 +000014390 if (N->getNumValues() == 2) // Dead flag value?
14391 return DCI.CombineTo(N, Cond, SDValue());
14392 return Cond;
14393 }
Eric Christopherfd179292009-08-27 18:07:15 +000014394
Chris Lattnercee56e72009-03-13 05:53:31 +000014395 // Optimize cases that will turn into an LEA instruction. This requires
14396 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014397 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014398 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014399 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014400
Chris Lattnercee56e72009-03-13 05:53:31 +000014401 bool isFastMultiplier = false;
14402 if (Diff < 10) {
14403 switch ((unsigned char)Diff) {
14404 default: break;
14405 case 1: // result = add base, cond
14406 case 2: // result = lea base( , cond*2)
14407 case 3: // result = lea base(cond, cond*2)
14408 case 4: // result = lea base( , cond*4)
14409 case 5: // result = lea base(cond, cond*4)
14410 case 8: // result = lea base( , cond*8)
14411 case 9: // result = lea base(cond, cond*8)
14412 isFastMultiplier = true;
14413 break;
14414 }
14415 }
Eric Christopherfd179292009-08-27 18:07:15 +000014416
Chris Lattnercee56e72009-03-13 05:53:31 +000014417 if (isFastMultiplier) {
14418 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014419 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14420 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014421 // Zero extend the condition if needed.
14422 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14423 Cond);
14424 // Scale the condition by the difference.
14425 if (Diff != 1)
14426 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14427 DAG.getConstant(Diff, Cond.getValueType()));
14428
14429 // Add the base if non-zero.
14430 if (FalseC->getAPIntValue() != 0)
14431 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14432 SDValue(FalseC, 0));
14433 if (N->getNumValues() == 2) // Dead flag value?
14434 return DCI.CombineTo(N, Cond, SDValue());
14435 return Cond;
14436 }
Eric Christopherfd179292009-08-27 18:07:15 +000014437 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014438 }
14439 }
14440 return SDValue();
14441}
14442
14443
Evan Cheng0b0cd912009-03-28 05:57:29 +000014444/// PerformMulCombine - Optimize a single multiply with constant into two
14445/// in order to implement it with two cheaper instructions, e.g.
14446/// LEA + SHL, LEA + LEA.
14447static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14448 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014449 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14450 return SDValue();
14451
Owen Andersone50ed302009-08-10 22:56:29 +000014452 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014453 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014454 return SDValue();
14455
14456 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14457 if (!C)
14458 return SDValue();
14459 uint64_t MulAmt = C->getZExtValue();
14460 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14461 return SDValue();
14462
14463 uint64_t MulAmt1 = 0;
14464 uint64_t MulAmt2 = 0;
14465 if ((MulAmt % 9) == 0) {
14466 MulAmt1 = 9;
14467 MulAmt2 = MulAmt / 9;
14468 } else if ((MulAmt % 5) == 0) {
14469 MulAmt1 = 5;
14470 MulAmt2 = MulAmt / 5;
14471 } else if ((MulAmt % 3) == 0) {
14472 MulAmt1 = 3;
14473 MulAmt2 = MulAmt / 3;
14474 }
14475 if (MulAmt2 &&
14476 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14477 DebugLoc DL = N->getDebugLoc();
14478
14479 if (isPowerOf2_64(MulAmt2) &&
14480 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14481 // If second multiplifer is pow2, issue it first. We want the multiply by
14482 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14483 // is an add.
14484 std::swap(MulAmt1, MulAmt2);
14485
14486 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014487 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014488 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014489 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014490 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014491 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014492 DAG.getConstant(MulAmt1, VT));
14493
Eric Christopherfd179292009-08-27 18:07:15 +000014494 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014495 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014496 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014497 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014498 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014499 DAG.getConstant(MulAmt2, VT));
14500
14501 // Do not add new nodes to DAG combiner worklist.
14502 DCI.CombineTo(N, NewMul, false);
14503 }
14504 return SDValue();
14505}
14506
Evan Chengad9c0a32009-12-15 00:53:42 +000014507static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14508 SDValue N0 = N->getOperand(0);
14509 SDValue N1 = N->getOperand(1);
14510 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14511 EVT VT = N0.getValueType();
14512
14513 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14514 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014515 if (VT.isInteger() && !VT.isVector() &&
14516 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014517 N0.getOperand(1).getOpcode() == ISD::Constant) {
14518 SDValue N00 = N0.getOperand(0);
14519 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14520 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14521 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14522 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14523 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14524 APInt ShAmt = N1C->getAPIntValue();
14525 Mask = Mask.shl(ShAmt);
14526 if (Mask != 0)
14527 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14528 N00, DAG.getConstant(Mask, VT));
14529 }
14530 }
14531
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014532
14533 // Hardware support for vector shifts is sparse which makes us scalarize the
14534 // vector operations in many cases. Also, on sandybridge ADD is faster than
14535 // shl.
14536 // (shl V, 1) -> add V,V
14537 if (isSplatVector(N1.getNode())) {
14538 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14539 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14540 // We shift all of the values by one. In many cases we do not have
14541 // hardware support for this operation. This is better expressed as an ADD
14542 // of two values.
14543 if (N1C && (1 == N1C->getZExtValue())) {
14544 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14545 }
14546 }
14547
Evan Chengad9c0a32009-12-15 00:53:42 +000014548 return SDValue();
14549}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014550
Nate Begeman740ab032009-01-26 00:52:55 +000014551/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14552/// when possible.
14553static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014554 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014555 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014556 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014557 if (N->getOpcode() == ISD::SHL) {
14558 SDValue V = PerformSHLCombine(N, DAG);
14559 if (V.getNode()) return V;
14560 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014561
Nate Begeman740ab032009-01-26 00:52:55 +000014562 // On X86 with SSE2 support, we can transform this to a vector shift if
14563 // all elements are shifted by the same amount. We can't do this in legalize
14564 // because the a constant vector is typically transformed to a constant pool
14565 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014566 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014567 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014568
Craig Topper7be5dfd2011-11-12 09:58:49 +000014569 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14570 (!Subtarget->hasAVX2() ||
14571 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014572 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014573
Mon P Wang3becd092009-01-28 08:12:05 +000014574 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014575 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014576 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014577 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014578 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14579 unsigned NumElts = VT.getVectorNumElements();
14580 unsigned i = 0;
14581 for (; i != NumElts; ++i) {
14582 SDValue Arg = ShAmtOp.getOperand(i);
14583 if (Arg.getOpcode() == ISD::UNDEF) continue;
14584 BaseShAmt = Arg;
14585 break;
14586 }
Craig Topper37c26772012-01-17 04:44:50 +000014587 // Handle the case where the build_vector is all undef
14588 // FIXME: Should DAG allow this?
14589 if (i == NumElts)
14590 return SDValue();
14591
Mon P Wang3becd092009-01-28 08:12:05 +000014592 for (; i != NumElts; ++i) {
14593 SDValue Arg = ShAmtOp.getOperand(i);
14594 if (Arg.getOpcode() == ISD::UNDEF) continue;
14595 if (Arg != BaseShAmt) {
14596 return SDValue();
14597 }
14598 }
14599 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014600 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014601 SDValue InVec = ShAmtOp.getOperand(0);
14602 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14603 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14604 unsigned i = 0;
14605 for (; i != NumElts; ++i) {
14606 SDValue Arg = InVec.getOperand(i);
14607 if (Arg.getOpcode() == ISD::UNDEF) continue;
14608 BaseShAmt = Arg;
14609 break;
14610 }
14611 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14612 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014613 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014614 if (C->getZExtValue() == SplatIdx)
14615 BaseShAmt = InVec.getOperand(1);
14616 }
14617 }
Mon P Wang845b1892012-02-01 22:15:20 +000014618 if (BaseShAmt.getNode() == 0) {
14619 // Don't create instructions with illegal types after legalize
14620 // types has run.
14621 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14622 !DCI.isBeforeLegalize())
14623 return SDValue();
14624
Mon P Wangefa42202009-09-03 19:56:25 +000014625 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14626 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014627 }
Mon P Wang3becd092009-01-28 08:12:05 +000014628 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014629 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014630
Mon P Wangefa42202009-09-03 19:56:25 +000014631 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014632 if (EltVT.bitsGT(MVT::i32))
14633 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14634 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014635 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014636
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014637 // The shift amount is identical so we can do a vector shift.
14638 SDValue ValOp = N->getOperand(0);
14639 switch (N->getOpcode()) {
14640 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014641 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014642 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014643 switch (VT.getSimpleVT().SimpleTy) {
14644 default: return SDValue();
14645 case MVT::v2i64:
14646 case MVT::v4i32:
14647 case MVT::v8i16:
14648 case MVT::v4i64:
14649 case MVT::v8i32:
14650 case MVT::v16i16:
14651 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14652 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014653 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014654 switch (VT.getSimpleVT().SimpleTy) {
14655 default: return SDValue();
14656 case MVT::v4i32:
14657 case MVT::v8i16:
14658 case MVT::v8i32:
14659 case MVT::v16i16:
14660 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14661 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014662 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014663 switch (VT.getSimpleVT().SimpleTy) {
14664 default: return SDValue();
14665 case MVT::v2i64:
14666 case MVT::v4i32:
14667 case MVT::v8i16:
14668 case MVT::v4i64:
14669 case MVT::v8i32:
14670 case MVT::v16i16:
14671 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14672 }
Nate Begeman740ab032009-01-26 00:52:55 +000014673 }
Nate Begeman740ab032009-01-26 00:52:55 +000014674}
14675
Nate Begemanb65c1752010-12-17 22:55:37 +000014676
Stuart Hastings865f0932011-06-03 23:53:54 +000014677// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14678// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14679// and friends. Likewise for OR -> CMPNEQSS.
14680static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14681 TargetLowering::DAGCombinerInfo &DCI,
14682 const X86Subtarget *Subtarget) {
14683 unsigned opcode;
14684
14685 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14686 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014687 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014688 SDValue N0 = N->getOperand(0);
14689 SDValue N1 = N->getOperand(1);
14690 SDValue CMP0 = N0->getOperand(1);
14691 SDValue CMP1 = N1->getOperand(1);
14692 DebugLoc DL = N->getDebugLoc();
14693
14694 // The SETCCs should both refer to the same CMP.
14695 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14696 return SDValue();
14697
14698 SDValue CMP00 = CMP0->getOperand(0);
14699 SDValue CMP01 = CMP0->getOperand(1);
14700 EVT VT = CMP00.getValueType();
14701
14702 if (VT == MVT::f32 || VT == MVT::f64) {
14703 bool ExpectingFlags = false;
14704 // Check for any users that want flags:
14705 for (SDNode::use_iterator UI = N->use_begin(),
14706 UE = N->use_end();
14707 !ExpectingFlags && UI != UE; ++UI)
14708 switch (UI->getOpcode()) {
14709 default:
14710 case ISD::BR_CC:
14711 case ISD::BRCOND:
14712 case ISD::SELECT:
14713 ExpectingFlags = true;
14714 break;
14715 case ISD::CopyToReg:
14716 case ISD::SIGN_EXTEND:
14717 case ISD::ZERO_EXTEND:
14718 case ISD::ANY_EXTEND:
14719 break;
14720 }
14721
14722 if (!ExpectingFlags) {
14723 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14724 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14725
14726 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14727 X86::CondCode tmp = cc0;
14728 cc0 = cc1;
14729 cc1 = tmp;
14730 }
14731
14732 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14733 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14734 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14735 X86ISD::NodeType NTOperator = is64BitFP ?
14736 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14737 // FIXME: need symbolic constants for these magic numbers.
14738 // See X86ATTInstPrinter.cpp:printSSECC().
14739 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14740 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14741 DAG.getConstant(x86cc, MVT::i8));
14742 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14743 OnesOrZeroesF);
14744 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14745 DAG.getConstant(1, MVT::i32));
14746 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14747 return OneBitOfTruth;
14748 }
14749 }
14750 }
14751 }
14752 return SDValue();
14753}
14754
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014755/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14756/// so it can be folded inside ANDNP.
14757static bool CanFoldXORWithAllOnes(const SDNode *N) {
14758 EVT VT = N->getValueType(0);
14759
14760 // Match direct AllOnes for 128 and 256-bit vectors
14761 if (ISD::isBuildVectorAllOnes(N))
14762 return true;
14763
14764 // Look through a bit convert.
14765 if (N->getOpcode() == ISD::BITCAST)
14766 N = N->getOperand(0).getNode();
14767
14768 // Sometimes the operand may come from a insert_subvector building a 256-bit
14769 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014770 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014771 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14772 SDValue V1 = N->getOperand(0);
14773 SDValue V2 = N->getOperand(1);
14774
14775 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14776 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14777 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14778 ISD::isBuildVectorAllOnes(V2.getNode()))
14779 return true;
14780 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014781
14782 return false;
14783}
14784
Nate Begemanb65c1752010-12-17 22:55:37 +000014785static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14786 TargetLowering::DAGCombinerInfo &DCI,
14787 const X86Subtarget *Subtarget) {
14788 if (DCI.isBeforeLegalizeOps())
14789 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014790
Stuart Hastings865f0932011-06-03 23:53:54 +000014791 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14792 if (R.getNode())
14793 return R;
14794
Craig Topper54a11172011-10-14 07:06:56 +000014795 EVT VT = N->getValueType(0);
14796
Craig Topperb4c94572011-10-21 06:55:01 +000014797 // Create ANDN, BLSI, and BLSR instructions
14798 // BLSI is X & (-X)
14799 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014800 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14801 SDValue N0 = N->getOperand(0);
14802 SDValue N1 = N->getOperand(1);
14803 DebugLoc DL = N->getDebugLoc();
14804
14805 // Check LHS for not
14806 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14807 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14808 // Check RHS for not
14809 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14810 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14811
Craig Topperb4c94572011-10-21 06:55:01 +000014812 // Check LHS for neg
14813 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14814 isZero(N0.getOperand(0)))
14815 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14816
14817 // Check RHS for neg
14818 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14819 isZero(N1.getOperand(0)))
14820 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14821
14822 // Check LHS for X-1
14823 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14824 isAllOnes(N0.getOperand(1)))
14825 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14826
14827 // Check RHS for X-1
14828 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14829 isAllOnes(N1.getOperand(1)))
14830 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14831
Craig Topper54a11172011-10-14 07:06:56 +000014832 return SDValue();
14833 }
14834
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014835 // Want to form ANDNP nodes:
14836 // 1) In the hopes of then easily combining them with OR and AND nodes
14837 // to form PBLEND/PSIGN.
14838 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014839 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014840 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014841
Nate Begemanb65c1752010-12-17 22:55:37 +000014842 SDValue N0 = N->getOperand(0);
14843 SDValue N1 = N->getOperand(1);
14844 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014845
Nate Begemanb65c1752010-12-17 22:55:37 +000014846 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014847 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014848 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14849 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014850 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014851
14852 // Check RHS for vnot
14853 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014854 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14855 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014856 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014857
Nate Begemanb65c1752010-12-17 22:55:37 +000014858 return SDValue();
14859}
14860
Evan Cheng760d1942010-01-04 21:22:48 +000014861static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014862 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014863 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014864 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014865 return SDValue();
14866
Stuart Hastings865f0932011-06-03 23:53:54 +000014867 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14868 if (R.getNode())
14869 return R;
14870
Evan Cheng760d1942010-01-04 21:22:48 +000014871 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014872
Evan Cheng760d1942010-01-04 21:22:48 +000014873 SDValue N0 = N->getOperand(0);
14874 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014875
Nate Begemanb65c1752010-12-17 22:55:37 +000014876 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014877 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014878 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014879 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14880 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014881
Craig Topper1666cb62011-11-19 07:07:26 +000014882 // Canonicalize pandn to RHS
14883 if (N0.getOpcode() == X86ISD::ANDNP)
14884 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014885 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014886 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14887 SDValue Mask = N1.getOperand(0);
14888 SDValue X = N1.getOperand(1);
14889 SDValue Y;
14890 if (N0.getOperand(0) == Mask)
14891 Y = N0.getOperand(1);
14892 if (N0.getOperand(1) == Mask)
14893 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014894
Craig Topper1666cb62011-11-19 07:07:26 +000014895 // Check to see if the mask appeared in both the AND and ANDNP and
14896 if (!Y.getNode())
14897 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014898
Craig Topper1666cb62011-11-19 07:07:26 +000014899 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014900 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014901 if (Mask.getOpcode() == ISD::BITCAST)
14902 Mask = Mask.getOperand(0);
14903 if (X.getOpcode() == ISD::BITCAST)
14904 X = X.getOperand(0);
14905 if (Y.getOpcode() == ISD::BITCAST)
14906 Y = Y.getOperand(0);
14907
Craig Topper1666cb62011-11-19 07:07:26 +000014908 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014909
Craig Toppered2e13d2012-01-22 19:15:14 +000014910 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014911 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14912 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014913 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014914 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014915
14916 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014917 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014918 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14919 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14920 if ((SraAmt + 1) != EltBits)
14921 return SDValue();
14922
14923 DebugLoc DL = N->getDebugLoc();
14924
14925 // Now we know we at least have a plendvb with the mask val. See if
14926 // we can form a psignb/w/d.
14927 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000014928 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14929 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000014930 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
14931 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
14932 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014933 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000014934 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000014935 }
14936 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000014937 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000014938 return SDValue();
14939
14940 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14941
14942 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14943 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14944 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000014945 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000014946 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014947 }
14948 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014949
Craig Topper1666cb62011-11-19 07:07:26 +000014950 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14951 return SDValue();
14952
Nate Begemanb65c1752010-12-17 22:55:37 +000014953 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014954 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14955 std::swap(N0, N1);
14956 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14957 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014958 if (!N0.hasOneUse() || !N1.hasOneUse())
14959 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014960
14961 SDValue ShAmt0 = N0.getOperand(1);
14962 if (ShAmt0.getValueType() != MVT::i8)
14963 return SDValue();
14964 SDValue ShAmt1 = N1.getOperand(1);
14965 if (ShAmt1.getValueType() != MVT::i8)
14966 return SDValue();
14967 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14968 ShAmt0 = ShAmt0.getOperand(0);
14969 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14970 ShAmt1 = ShAmt1.getOperand(0);
14971
14972 DebugLoc DL = N->getDebugLoc();
14973 unsigned Opc = X86ISD::SHLD;
14974 SDValue Op0 = N0.getOperand(0);
14975 SDValue Op1 = N1.getOperand(0);
14976 if (ShAmt0.getOpcode() == ISD::SUB) {
14977 Opc = X86ISD::SHRD;
14978 std::swap(Op0, Op1);
14979 std::swap(ShAmt0, ShAmt1);
14980 }
14981
Evan Cheng8b1190a2010-04-28 01:18:01 +000014982 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014983 if (ShAmt1.getOpcode() == ISD::SUB) {
14984 SDValue Sum = ShAmt1.getOperand(0);
14985 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014986 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14987 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14988 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14989 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014990 return DAG.getNode(Opc, DL, VT,
14991 Op0, Op1,
14992 DAG.getNode(ISD::TRUNCATE, DL,
14993 MVT::i8, ShAmt0));
14994 }
14995 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14996 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14997 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014998 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014999 return DAG.getNode(Opc, DL, VT,
15000 N0.getOperand(0), N1.getOperand(0),
15001 DAG.getNode(ISD::TRUNCATE, DL,
15002 MVT::i8, ShAmt0));
15003 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015004
Evan Cheng760d1942010-01-04 21:22:48 +000015005 return SDValue();
15006}
15007
Manman Ren92363622012-06-07 22:39:10 +000015008// Generate NEG and CMOV for integer abs.
15009static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
15010 EVT VT = N->getValueType(0);
15011
15012 // Since X86 does not have CMOV for 8-bit integer, we don't convert
15013 // 8-bit integer abs to NEG and CMOV.
15014 if (VT.isInteger() && VT.getSizeInBits() == 8)
15015 return SDValue();
15016
15017 SDValue N0 = N->getOperand(0);
15018 SDValue N1 = N->getOperand(1);
15019 DebugLoc DL = N->getDebugLoc();
15020
15021 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
15022 // and change it to SUB and CMOV.
15023 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
15024 N0.getOpcode() == ISD::ADD &&
15025 N0.getOperand(1) == N1 &&
15026 N1.getOpcode() == ISD::SRA &&
15027 N1.getOperand(0) == N0.getOperand(0))
15028 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
15029 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
15030 // Generate SUB & CMOV.
15031 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
15032 DAG.getConstant(0, VT), N0.getOperand(0));
15033
15034 SDValue Ops[] = { N0.getOperand(0), Neg,
15035 DAG.getConstant(X86::COND_GE, MVT::i8),
15036 SDValue(Neg.getNode(), 1) };
15037 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
15038 Ops, array_lengthof(Ops));
15039 }
15040 return SDValue();
15041}
15042
Craig Topper3738ccd2011-12-27 06:27:23 +000015043// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000015044static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
15045 TargetLowering::DAGCombinerInfo &DCI,
15046 const X86Subtarget *Subtarget) {
15047 if (DCI.isBeforeLegalizeOps())
15048 return SDValue();
15049
Manman Ren45d53b82012-06-08 18:58:26 +000015050 if (Subtarget->hasCMov()) {
15051 SDValue RV = performIntegerAbsCombine(N, DAG);
15052 if (RV.getNode())
15053 return RV;
15054 }
Manman Ren92363622012-06-07 22:39:10 +000015055
15056 // Try forming BMI if it is available.
15057 if (!Subtarget->hasBMI())
15058 return SDValue();
15059
Craig Topperb4c94572011-10-21 06:55:01 +000015060 EVT VT = N->getValueType(0);
15061
15062 if (VT != MVT::i32 && VT != MVT::i64)
15063 return SDValue();
15064
Craig Topper3738ccd2011-12-27 06:27:23 +000015065 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15066
Craig Topperb4c94572011-10-21 06:55:01 +000015067 // Create BLSMSK instructions by finding X ^ (X-1)
15068 SDValue N0 = N->getOperand(0);
15069 SDValue N1 = N->getOperand(1);
15070 DebugLoc DL = N->getDebugLoc();
15071
15072 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15073 isAllOnes(N0.getOperand(1)))
15074 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15075
15076 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15077 isAllOnes(N1.getOperand(1)))
15078 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15079
15080 return SDValue();
15081}
15082
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015083/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15084static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015085 TargetLowering::DAGCombinerInfo &DCI,
15086 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015087 LoadSDNode *Ld = cast<LoadSDNode>(N);
15088 EVT RegVT = Ld->getValueType(0);
15089 EVT MemVT = Ld->getMemoryVT();
15090 DebugLoc dl = Ld->getDebugLoc();
15091 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15092
15093 ISD::LoadExtType Ext = Ld->getExtensionType();
15094
Nadav Rotemca6f2962011-09-18 19:00:23 +000015095 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015096 // shuffle. We need SSE4 for the shuffles.
15097 // TODO: It is possible to support ZExt by zeroing the undef values
15098 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015099 if (RegVT.isVector() && RegVT.isInteger() &&
15100 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015101 assert(MemVT != RegVT && "Cannot extend to the same type");
15102 assert(MemVT.isVector() && "Must load a vector from memory");
15103
15104 unsigned NumElems = RegVT.getVectorNumElements();
15105 unsigned RegSz = RegVT.getSizeInBits();
15106 unsigned MemSz = MemVT.getSizeInBits();
15107 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015108
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015109 // All sizes must be a power of two.
15110 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15111 return SDValue();
15112
15113 // Attempt to load the original value using scalar loads.
15114 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015115 MVT SclrLoadTy = MVT::i8;
15116 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15117 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15118 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015119 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015120 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015121 }
15122 }
15123
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015124 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15125 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15126 (64 <= MemSz))
15127 SclrLoadTy = MVT::f64;
15128
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015129 // Calculate the number of scalar loads that we need to perform
15130 // in order to load our vector from memory.
15131 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015132
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015133 // Represent our vector as a sequence of elements which are the
15134 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015135 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15136 RegSz/SclrLoadTy.getSizeInBits());
15137
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015138 // Represent the data using the same element type that is stored in
15139 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015140 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15141 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015142
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015143 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15144 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015145
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015146 // We can't shuffle using an illegal type.
15147 if (!TLI.isTypeLegal(WideVecVT))
15148 return SDValue();
15149
15150 SmallVector<SDValue, 8> Chains;
15151 SDValue Ptr = Ld->getBasePtr();
15152 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15153 TLI.getPointerTy());
15154 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15155
15156 for (unsigned i = 0; i < NumLoads; ++i) {
15157 // Perform a single load.
15158 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15159 Ptr, Ld->getPointerInfo(),
15160 Ld->isVolatile(), Ld->isNonTemporal(),
15161 Ld->isInvariant(), Ld->getAlignment());
15162 Chains.push_back(ScalarLoad.getValue(1));
15163 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15164 // another round of DAGCombining.
15165 if (i == 0)
15166 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15167 else
15168 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15169 ScalarLoad, DAG.getIntPtrConstant(i));
15170
15171 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15172 }
15173
15174 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15175 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015176
15177 // Bitcast the loaded value to a vector of the original element type, in
15178 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015179 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015180 unsigned SizeRatio = RegSz/MemSz;
15181
15182 // Redistribute the loaded elements into the different locations.
15183 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015184 for (unsigned i = 0; i != NumElems; ++i)
15185 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015186
15187 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015188 DAG.getUNDEF(WideVecVT),
15189 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015190
15191 // Bitcast to the requested type.
15192 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15193 // Replace the original load with the new sequence
15194 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015195 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015196 }
15197
15198 return SDValue();
15199}
15200
Chris Lattner149a4e52008-02-22 02:09:43 +000015201/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015202static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015203 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015204 StoreSDNode *St = cast<StoreSDNode>(N);
15205 EVT VT = St->getValue().getValueType();
15206 EVT StVT = St->getMemoryVT();
15207 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015208 SDValue StoredVal = St->getOperand(1);
15209 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15210
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015211 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015212 // On Sandy Bridge, 256-bit memory operations are executed by two
15213 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15214 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015215 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015216 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15217 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015218 SDValue Value0 = StoredVal.getOperand(0);
15219 SDValue Value1 = StoredVal.getOperand(1);
15220
15221 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15222 SDValue Ptr0 = St->getBasePtr();
15223 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15224
15225 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15226 St->getPointerInfo(), St->isVolatile(),
15227 St->isNonTemporal(), St->getAlignment());
15228 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15229 St->getPointerInfo(), St->isVolatile(),
15230 St->isNonTemporal(), St->getAlignment());
15231 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15232 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015233
15234 // Optimize trunc store (of multiple scalars) to shuffle and store.
15235 // First, pack all of the elements in one place. Next, store to memory
15236 // in fewer chunks.
15237 if (St->isTruncatingStore() && VT.isVector()) {
15238 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15239 unsigned NumElems = VT.getVectorNumElements();
15240 assert(StVT != VT && "Cannot truncate to the same type");
15241 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15242 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15243
15244 // From, To sizes and ElemCount must be pow of two
15245 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015246 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015247 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015248 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015249
Nadav Rotem614061b2011-08-10 19:30:14 +000015250 unsigned SizeRatio = FromSz / ToSz;
15251
15252 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15253
15254 // Create a type on which we perform the shuffle
15255 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15256 StVT.getScalarType(), NumElems*SizeRatio);
15257
15258 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15259
15260 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15261 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015262 for (unsigned i = 0; i != NumElems; ++i)
15263 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015264
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015265 // Can't shuffle using an illegal type.
15266 if (!TLI.isTypeLegal(WideVecVT))
15267 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015268
15269 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015270 DAG.getUNDEF(WideVecVT),
15271 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015272 // At this point all of the data is stored at the bottom of the
15273 // register. We now need to save it to mem.
15274
15275 // Find the largest store unit
15276 MVT StoreType = MVT::i8;
15277 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15278 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15279 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015280 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015281 StoreType = Tp;
15282 }
15283
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015284 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15285 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15286 (64 <= NumElems * ToSz))
15287 StoreType = MVT::f64;
15288
Nadav Rotem614061b2011-08-10 19:30:14 +000015289 // Bitcast the original vector into a vector of store-size units
15290 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015291 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015292 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15293 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15294 SmallVector<SDValue, 8> Chains;
15295 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15296 TLI.getPointerTy());
15297 SDValue Ptr = St->getBasePtr();
15298
15299 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015300 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015301 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15302 StoreType, ShuffWide,
15303 DAG.getIntPtrConstant(i));
15304 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15305 St->getPointerInfo(), St->isVolatile(),
15306 St->isNonTemporal(), St->getAlignment());
15307 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15308 Chains.push_back(Ch);
15309 }
15310
15311 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15312 Chains.size());
15313 }
15314
15315
Chris Lattner149a4e52008-02-22 02:09:43 +000015316 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15317 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015318 // A preferable solution to the general problem is to figure out the right
15319 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015320
15321 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015322 if (VT.getSizeInBits() != 64)
15323 return SDValue();
15324
Devang Patel578efa92009-06-05 21:57:13 +000015325 const Function *F = DAG.getMachineFunction().getFunction();
15326 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015327 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015328 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015329 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015330 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015331 isa<LoadSDNode>(St->getValue()) &&
15332 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15333 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015334 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015335 LoadSDNode *Ld = 0;
15336 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015337 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015338 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015339 // Must be a store of a load. We currently handle two cases: the load
15340 // is a direct child, and it's under an intervening TokenFactor. It is
15341 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015342 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015343 Ld = cast<LoadSDNode>(St->getChain());
15344 else if (St->getValue().hasOneUse() &&
15345 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015346 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015347 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015348 TokenFactorIndex = i;
15349 Ld = cast<LoadSDNode>(St->getValue());
15350 } else
15351 Ops.push_back(ChainVal->getOperand(i));
15352 }
15353 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015354
Evan Cheng536e6672009-03-12 05:59:15 +000015355 if (!Ld || !ISD::isNormalLoad(Ld))
15356 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015357
Evan Cheng536e6672009-03-12 05:59:15 +000015358 // If this is not the MMX case, i.e. we are just turning i64 load/store
15359 // into f64 load/store, avoid the transformation if there are multiple
15360 // uses of the loaded value.
15361 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15362 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015363
Evan Cheng536e6672009-03-12 05:59:15 +000015364 DebugLoc LdDL = Ld->getDebugLoc();
15365 DebugLoc StDL = N->getDebugLoc();
15366 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15367 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15368 // pair instead.
15369 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015370 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015371 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15372 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015373 Ld->isNonTemporal(), Ld->isInvariant(),
15374 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015375 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015376 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015377 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015378 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015379 Ops.size());
15380 }
Evan Cheng536e6672009-03-12 05:59:15 +000015381 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015382 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015383 St->isVolatile(), St->isNonTemporal(),
15384 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015385 }
Evan Cheng536e6672009-03-12 05:59:15 +000015386
15387 // Otherwise, lower to two pairs of 32-bit loads / stores.
15388 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015389 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15390 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015391
Owen Anderson825b72b2009-08-11 20:47:22 +000015392 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015393 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015394 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015395 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015396 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015397 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015398 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015399 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015400 MinAlign(Ld->getAlignment(), 4));
15401
15402 SDValue NewChain = LoLd.getValue(1);
15403 if (TokenFactorIndex != -1) {
15404 Ops.push_back(LoLd);
15405 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015406 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015407 Ops.size());
15408 }
15409
15410 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015411 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15412 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015413
15414 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015415 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015416 St->isVolatile(), St->isNonTemporal(),
15417 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015418 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015419 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015420 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015421 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015422 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015423 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015424 }
Dan Gohman475871a2008-07-27 21:46:04 +000015425 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015426}
15427
Duncan Sands17470be2011-09-22 20:15:48 +000015428/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15429/// and return the operands for the horizontal operation in LHS and RHS. A
15430/// horizontal operation performs the binary operation on successive elements
15431/// of its first operand, then on successive elements of its second operand,
15432/// returning the resulting values in a vector. For example, if
15433/// A = < float a0, float a1, float a2, float a3 >
15434/// and
15435/// B = < float b0, float b1, float b2, float b3 >
15436/// then the result of doing a horizontal operation on A and B is
15437/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15438/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15439/// A horizontal-op B, for some already available A and B, and if so then LHS is
15440/// set to A, RHS to B, and the routine returns 'true'.
15441/// Note that the binary operation should have the property that if one of the
15442/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015443static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015444 // Look for the following pattern: if
15445 // A = < float a0, float a1, float a2, float a3 >
15446 // B = < float b0, float b1, float b2, float b3 >
15447 // and
15448 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15449 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15450 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15451 // which is A horizontal-op B.
15452
15453 // At least one of the operands should be a vector shuffle.
15454 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15455 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15456 return false;
15457
15458 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015459
15460 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15461 "Unsupported vector type for horizontal add/sub");
15462
15463 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15464 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015465 unsigned NumElts = VT.getVectorNumElements();
15466 unsigned NumLanes = VT.getSizeInBits()/128;
15467 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015468 assert((NumLaneElts % 2 == 0) &&
15469 "Vector type should have an even number of elements in each lane");
15470 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015471
15472 // View LHS in the form
15473 // LHS = VECTOR_SHUFFLE A, B, LMask
15474 // If LHS is not a shuffle then pretend it is the shuffle
15475 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15476 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15477 // type VT.
15478 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015479 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015480 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15481 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15482 A = LHS.getOperand(0);
15483 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15484 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015485 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15486 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015487 } else {
15488 if (LHS.getOpcode() != ISD::UNDEF)
15489 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015490 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015491 LMask[i] = i;
15492 }
15493
15494 // Likewise, view RHS in the form
15495 // RHS = VECTOR_SHUFFLE C, D, RMask
15496 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015497 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015498 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15499 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15500 C = RHS.getOperand(0);
15501 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15502 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015503 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15504 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015505 } else {
15506 if (RHS.getOpcode() != ISD::UNDEF)
15507 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015508 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015509 RMask[i] = i;
15510 }
15511
15512 // Check that the shuffles are both shuffling the same vectors.
15513 if (!(A == C && B == D) && !(A == D && B == C))
15514 return false;
15515
15516 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15517 if (!A.getNode() && !B.getNode())
15518 return false;
15519
15520 // If A and B occur in reverse order in RHS, then "swap" them (which means
15521 // rewriting the mask).
15522 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015523 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015524
15525 // At this point LHS and RHS are equivalent to
15526 // LHS = VECTOR_SHUFFLE A, B, LMask
15527 // RHS = VECTOR_SHUFFLE A, B, RMask
15528 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015529 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015530 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015531
Craig Topperf8363302011-12-02 08:18:41 +000015532 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015533 if (LIdx < 0 || RIdx < 0 ||
15534 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15535 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015536 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015537
Craig Topperf8363302011-12-02 08:18:41 +000015538 // Check that successive elements are being operated on. If not, this is
15539 // not a horizontal operation.
15540 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15541 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015542 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015543 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015544 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015545 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015546 }
15547
15548 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15549 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15550 return true;
15551}
15552
15553/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15554static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15555 const X86Subtarget *Subtarget) {
15556 EVT VT = N->getValueType(0);
15557 SDValue LHS = N->getOperand(0);
15558 SDValue RHS = N->getOperand(1);
15559
15560 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015561 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015562 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015563 isHorizontalBinOp(LHS, RHS, true))
15564 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15565 return SDValue();
15566}
15567
15568/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15569static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15570 const X86Subtarget *Subtarget) {
15571 EVT VT = N->getValueType(0);
15572 SDValue LHS = N->getOperand(0);
15573 SDValue RHS = N->getOperand(1);
15574
15575 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015576 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015577 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015578 isHorizontalBinOp(LHS, RHS, false))
15579 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15580 return SDValue();
15581}
15582
Chris Lattner6cf73262008-01-25 06:14:17 +000015583/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15584/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015585static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015586 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15587 // F[X]OR(0.0, x) -> x
15588 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015589 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15590 if (C->getValueAPF().isPosZero())
15591 return N->getOperand(1);
15592 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15593 if (C->getValueAPF().isPosZero())
15594 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015595 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015596}
15597
Nadav Rotemd60cb112012-08-19 13:06:16 +000015598/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15599/// X86ISD::FMAX nodes.
15600static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15601 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15602
15603 // Only perform optimizations if UnsafeMath is used.
15604 if (!DAG.getTarget().Options.UnsafeFPMath)
15605 return SDValue();
15606
15607 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015608 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015609 unsigned NewOp = 0;
15610 switch (N->getOpcode()) {
15611 default: llvm_unreachable("unknown opcode");
15612 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15613 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15614 }
15615
15616 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
15617 N->getOperand(0), N->getOperand(1));
15618}
15619
15620
Chris Lattneraf723b92008-01-25 05:46:26 +000015621/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015622static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015623 // FAND(0.0, x) -> 0.0
15624 // FAND(x, 0.0) -> 0.0
15625 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15626 if (C->getValueAPF().isPosZero())
15627 return N->getOperand(0);
15628 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15629 if (C->getValueAPF().isPosZero())
15630 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015631 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015632}
15633
Dan Gohmane5af2d32009-01-29 01:59:02 +000015634static SDValue PerformBTCombine(SDNode *N,
15635 SelectionDAG &DAG,
15636 TargetLowering::DAGCombinerInfo &DCI) {
15637 // BT ignores high bits in the bit index operand.
15638 SDValue Op1 = N->getOperand(1);
15639 if (Op1.hasOneUse()) {
15640 unsigned BitWidth = Op1.getValueSizeInBits();
15641 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15642 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015643 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15644 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015645 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015646 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15647 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15648 DCI.CommitTargetLoweringOpt(TLO);
15649 }
15650 return SDValue();
15651}
Chris Lattner83e6c992006-10-04 06:57:07 +000015652
Eli Friedman7a5e5552009-06-07 06:52:44 +000015653static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15654 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015655 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015656 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015657 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015658 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015659 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015660 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015661 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015662 }
15663 return SDValue();
15664}
15665
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015666static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15667 TargetLowering::DAGCombinerInfo &DCI,
15668 const X86Subtarget *Subtarget) {
15669 if (!DCI.isBeforeLegalizeOps())
15670 return SDValue();
15671
Craig Topper3ef43cf2012-04-24 06:36:35 +000015672 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015673 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015674
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015675 EVT VT = N->getValueType(0);
15676 SDValue Op = N->getOperand(0);
15677 EVT OpVT = Op.getValueType();
15678 DebugLoc dl = N->getDebugLoc();
15679
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015680 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15681 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015682
Craig Topper3ef43cf2012-04-24 06:36:35 +000015683 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015684 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015685
15686 // Optimize vectors in AVX mode
15687 // Sign extend v8i16 to v8i32 and
15688 // v4i32 to v4i64
15689 //
15690 // Divide input vector into two parts
15691 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15692 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15693 // concat the vectors to original VT
15694
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015695 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015696 SDValue Undef = DAG.getUNDEF(OpVT);
15697
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015698 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015699 for (unsigned i = 0; i != NumElems/2; ++i)
15700 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015701
Craig Toppercacafd42012-08-14 08:18:43 +000015702 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015703
15704 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015705 for (unsigned i = 0; i != NumElems/2; ++i)
15706 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015707
Craig Toppercacafd42012-08-14 08:18:43 +000015708 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015709
Craig Topper3ef43cf2012-04-24 06:36:35 +000015710 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015711 VT.getVectorNumElements()/2);
15712
Craig Topper3ef43cf2012-04-24 06:36:35 +000015713 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015714 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15715
15716 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15717 }
15718 return SDValue();
15719}
15720
Michael Liaof6c24ee2012-08-10 14:39:24 +000015721static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015722 const X86Subtarget* Subtarget) {
15723 DebugLoc dl = N->getDebugLoc();
15724 EVT VT = N->getValueType(0);
15725
Craig Topperb1bdd7d2012-08-30 06:56:15 +000015726 // Let legalize expand this if it isn't a legal type yet.
15727 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
15728 return SDValue();
15729
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015730 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000015731 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
15732 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015733 return SDValue();
15734
15735 SDValue A = N->getOperand(0);
15736 SDValue B = N->getOperand(1);
15737 SDValue C = N->getOperand(2);
15738
15739 bool NegA = (A.getOpcode() == ISD::FNEG);
15740 bool NegB = (B.getOpcode() == ISD::FNEG);
15741 bool NegC = (C.getOpcode() == ISD::FNEG);
15742
Michael Liaof6c24ee2012-08-10 14:39:24 +000015743 // Negative multiplication when NegA xor NegB
15744 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015745 if (NegA)
15746 A = A.getOperand(0);
15747 if (NegB)
15748 B = B.getOperand(0);
15749 if (NegC)
15750 C = C.getOperand(0);
15751
15752 unsigned Opcode;
15753 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000015754 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015755 else
Craig Topperbf404372012-08-31 15:40:30 +000015756 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
15757
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015758 return DAG.getNode(Opcode, dl, VT, A, B, C);
15759}
15760
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015761static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015762 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015763 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015764 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15765 // (and (i32 x86isd::setcc_carry), 1)
15766 // This eliminates the zext. This transformation is necessary because
15767 // ISD::SETCC is always legalized to i8.
15768 DebugLoc dl = N->getDebugLoc();
15769 SDValue N0 = N->getOperand(0);
15770 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015771 EVT OpVT = N0.getValueType();
15772
Evan Cheng2e489c42009-12-16 00:53:11 +000015773 if (N0.getOpcode() == ISD::AND &&
15774 N0.hasOneUse() &&
15775 N0.getOperand(0).hasOneUse()) {
15776 SDValue N00 = N0.getOperand(0);
15777 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15778 return SDValue();
15779 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15780 if (!C || C->getZExtValue() != 1)
15781 return SDValue();
15782 return DAG.getNode(ISD::AND, dl, VT,
15783 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15784 N00.getOperand(0), N00.getOperand(1)),
15785 DAG.getConstant(1, VT));
15786 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015787
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015788 // Optimize vectors in AVX mode:
15789 //
15790 // v8i16 -> v8i32
15791 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15792 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15793 // Concat upper and lower parts.
15794 //
15795 // v4i32 -> v4i64
15796 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15797 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15798 // Concat upper and lower parts.
15799 //
Craig Topperc16f8512012-04-25 06:39:39 +000015800 if (!DCI.isBeforeLegalizeOps())
15801 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015802
Craig Topperc16f8512012-04-25 06:39:39 +000015803 if (!Subtarget->hasAVX())
15804 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015805
Craig Topperc16f8512012-04-25 06:39:39 +000015806 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15807 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015808
Craig Topperc16f8512012-04-25 06:39:39 +000015809 if (Subtarget->hasAVX2())
15810 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015811
Craig Topperc16f8512012-04-25 06:39:39 +000015812 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15813 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15814 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015815
Craig Topperc16f8512012-04-25 06:39:39 +000015816 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15817 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015818
Craig Topperc16f8512012-04-25 06:39:39 +000015819 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15820 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15821
15822 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015823 }
15824
Evan Cheng2e489c42009-12-16 00:53:11 +000015825 return SDValue();
15826}
15827
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015828// Optimize x == -y --> x+y == 0
15829// x != -y --> x+y != 0
15830static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15831 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15832 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015833 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015834
15835 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15836 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15837 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15838 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15839 LHS.getValueType(), RHS, LHS.getOperand(1));
15840 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15841 addV, DAG.getConstant(0, addV.getValueType()), CC);
15842 }
15843 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15844 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15845 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15846 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15847 RHS.getValueType(), LHS, RHS.getOperand(1));
15848 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15849 addV, DAG.getConstant(0, addV.getValueType()), CC);
15850 }
15851 return SDValue();
15852}
15853
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015854// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015855static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
15856 TargetLowering::DAGCombinerInfo &DCI,
15857 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015858 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015859 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15860 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015861
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015862 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15863 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15864 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015865 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015866 return DAG.getNode(ISD::AND, DL, MVT::i8,
15867 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015868 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015869 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015870
Michael Liao2a33cec2012-08-10 19:58:13 +000015871 SDValue Flags;
15872
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015873 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15874 if (Flags.getNode()) {
15875 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15876 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15877 }
15878
Michael Liao2a33cec2012-08-10 19:58:13 +000015879 return SDValue();
15880}
15881
15882// Optimize branch condition evaluation.
15883//
15884static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15885 TargetLowering::DAGCombinerInfo &DCI,
15886 const X86Subtarget *Subtarget) {
15887 DebugLoc DL = N->getDebugLoc();
15888 SDValue Chain = N->getOperand(0);
15889 SDValue Dest = N->getOperand(1);
15890 SDValue EFLAGS = N->getOperand(3);
15891 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15892
15893 SDValue Flags;
15894
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015895 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15896 if (Flags.getNode()) {
15897 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15898 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15899 Flags);
15900 }
15901
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015902 return SDValue();
15903}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015904
Craig Topper7fd5e162012-04-24 06:02:29 +000015905static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015906 SDValue Op0 = N->getOperand(0);
15907 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015908
15909 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015910 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015911 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015912 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015913 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15914 // Notice that we use SINT_TO_FP because we know that the high bits
15915 // are zero and SINT_TO_FP is better supported by the hardware.
15916 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15917 }
15918
15919 return SDValue();
15920}
15921
Benjamin Kramer1396c402011-06-18 11:09:41 +000015922static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15923 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015924 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015925 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015926
15927 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015928 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015929 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015930 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015931 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
15932 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15933 }
15934
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015935 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
15936 // a 32-bit target where SSE doesn't support i64->FP operations.
15937 if (Op0.getOpcode() == ISD::LOAD) {
15938 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
15939 EVT VT = Ld->getValueType(0);
15940 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
15941 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
15942 !XTLI->getSubtarget()->is64Bit() &&
15943 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000015944 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
15945 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015946 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
15947 return FILDChain;
15948 }
15949 }
15950 return SDValue();
15951}
15952
Craig Topper7fd5e162012-04-24 06:02:29 +000015953static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
15954 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015955
15956 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000015957 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
15958 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015959 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015960 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
15961 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
15962 }
15963
15964 return SDValue();
15965}
15966
Chris Lattner23a01992010-12-20 01:37:09 +000015967// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
15968static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
15969 X86TargetLowering::DAGCombinerInfo &DCI) {
15970 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
15971 // the result is either zero or one (depending on the input carry bit).
15972 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
15973 if (X86::isZeroNode(N->getOperand(0)) &&
15974 X86::isZeroNode(N->getOperand(1)) &&
15975 // We don't have a good way to replace an EFLAGS use, so only do this when
15976 // dead right now.
15977 SDValue(N, 1).use_empty()) {
15978 DebugLoc DL = N->getDebugLoc();
15979 EVT VT = N->getValueType(0);
15980 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
15981 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
15982 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
15983 DAG.getConstant(X86::COND_B,MVT::i8),
15984 N->getOperand(2)),
15985 DAG.getConstant(1, VT));
15986 return DCI.CombineTo(N, Res1, CarryOut);
15987 }
15988
15989 return SDValue();
15990}
15991
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015992// fold (add Y, (sete X, 0)) -> adc 0, Y
15993// (add Y, (setne X, 0)) -> sbb -1, Y
15994// (sub (sete X, 0), Y) -> sbb 0, Y
15995// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015996static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015997 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015998
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015999 // Look through ZExts.
16000 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
16001 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
16002 return SDValue();
16003
16004 SDValue SetCC = Ext.getOperand(0);
16005 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
16006 return SDValue();
16007
16008 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
16009 if (CC != X86::COND_E && CC != X86::COND_NE)
16010 return SDValue();
16011
16012 SDValue Cmp = SetCC.getOperand(1);
16013 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000016014 !X86::isZeroNode(Cmp.getOperand(1)) ||
16015 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000016016 return SDValue();
16017
16018 SDValue CmpOp0 = Cmp.getOperand(0);
16019 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
16020 DAG.getConstant(1, CmpOp0.getValueType()));
16021
16022 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
16023 if (CC == X86::COND_NE)
16024 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
16025 DL, OtherVal.getValueType(), OtherVal,
16026 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
16027 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
16028 DL, OtherVal.getValueType(), OtherVal,
16029 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
16030}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000016031
Craig Topper54f952a2011-11-19 09:02:40 +000016032/// PerformADDCombine - Do target-specific dag combines on integer adds.
16033static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
16034 const X86Subtarget *Subtarget) {
16035 EVT VT = N->getValueType(0);
16036 SDValue Op0 = N->getOperand(0);
16037 SDValue Op1 = N->getOperand(1);
16038
16039 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000016040 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000016041 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000016042 isHorizontalBinOp(Op0, Op1, true))
16043 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
16044
16045 return OptimizeConditionalInDecrement(N, DAG);
16046}
16047
16048static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
16049 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016050 SDValue Op0 = N->getOperand(0);
16051 SDValue Op1 = N->getOperand(1);
16052
16053 // X86 can't encode an immediate LHS of a sub. See if we can push the
16054 // negation into a preceding instruction.
16055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016056 // If the RHS of the sub is a XOR with one use and a constant, invert the
16057 // immediate. Then add one to the LHS of the sub so we can turn
16058 // X-Y -> X+~Y+1, saving one register.
16059 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16060 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016061 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016062 EVT VT = Op0.getValueType();
16063 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16064 Op1.getOperand(0),
16065 DAG.getConstant(~XorC, VT));
16066 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016067 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016068 }
16069 }
16070
Craig Topper54f952a2011-11-19 09:02:40 +000016071 // Try to synthesize horizontal adds from adds of shuffles.
16072 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016073 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016074 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16075 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016076 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16077
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016078 return OptimizeConditionalInDecrement(N, DAG);
16079}
16080
Dan Gohman475871a2008-07-27 21:46:04 +000016081SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016082 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016083 SelectionDAG &DAG = DCI.DAG;
16084 switch (N->getOpcode()) {
16085 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016086 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016087 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016088 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016089 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016090 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016091 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16092 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016093 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016094 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016095 case ISD::SHL:
16096 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016097 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016098 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016099 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016100 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016101 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016102 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016103 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016104 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000016105 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000016106 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16107 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016108 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016109 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016110 case X86ISD::FMIN:
16111 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016112 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016113 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016114 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016115 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016116 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016117 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Craig Topper55b24052012-09-11 06:15:32 +000016118 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016119 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016120 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016121 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016122 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016123 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016124 case X86ISD::UNPCKH:
16125 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016126 case X86ISD::MOVHLPS:
16127 case X86ISD::MOVLHPS:
16128 case X86ISD::PSHUFD:
16129 case X86ISD::PSHUFHW:
16130 case X86ISD::PSHUFLW:
16131 case X86ISD::MOVSS:
16132 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016133 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016134 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016135 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016136 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016137 }
16138
Dan Gohman475871a2008-07-27 21:46:04 +000016139 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016140}
16141
Evan Chenge5b51ac2010-04-17 06:13:15 +000016142/// isTypeDesirableForOp - Return true if the target has native support for
16143/// the specified value type and it is 'desirable' to use the type for the
16144/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16145/// instruction encodings are longer and some i16 instructions are slow.
16146bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16147 if (!isTypeLegal(VT))
16148 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016149 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016150 return true;
16151
16152 switch (Opc) {
16153 default:
16154 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016155 case ISD::LOAD:
16156 case ISD::SIGN_EXTEND:
16157 case ISD::ZERO_EXTEND:
16158 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016159 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016160 case ISD::SRL:
16161 case ISD::SUB:
16162 case ISD::ADD:
16163 case ISD::MUL:
16164 case ISD::AND:
16165 case ISD::OR:
16166 case ISD::XOR:
16167 return false;
16168 }
16169}
16170
16171/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016172/// beneficial for dag combiner to promote the specified node. If true, it
16173/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016174bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016175 EVT VT = Op.getValueType();
16176 if (VT != MVT::i16)
16177 return false;
16178
Evan Cheng4c26e932010-04-19 19:29:22 +000016179 bool Promote = false;
16180 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016181 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016182 default: break;
16183 case ISD::LOAD: {
16184 LoadSDNode *LD = cast<LoadSDNode>(Op);
16185 // If the non-extending load has a single use and it's not live out, then it
16186 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016187 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16188 Op.hasOneUse()*/) {
16189 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16190 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16191 // The only case where we'd want to promote LOAD (rather then it being
16192 // promoted as an operand is when it's only use is liveout.
16193 if (UI->getOpcode() != ISD::CopyToReg)
16194 return false;
16195 }
16196 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016197 Promote = true;
16198 break;
16199 }
16200 case ISD::SIGN_EXTEND:
16201 case ISD::ZERO_EXTEND:
16202 case ISD::ANY_EXTEND:
16203 Promote = true;
16204 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016205 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016206 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016207 SDValue N0 = Op.getOperand(0);
16208 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016209 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016210 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016211 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016212 break;
16213 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016214 case ISD::ADD:
16215 case ISD::MUL:
16216 case ISD::AND:
16217 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016218 case ISD::XOR:
16219 Commute = true;
16220 // fallthrough
16221 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016222 SDValue N0 = Op.getOperand(0);
16223 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016224 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016225 return false;
16226 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016227 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016228 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016229 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016230 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016231 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016232 }
16233 }
16234
16235 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016236 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016237}
16238
Evan Cheng60c07e12006-07-05 22:17:51 +000016239//===----------------------------------------------------------------------===//
16240// X86 Inline Assembly Support
16241//===----------------------------------------------------------------------===//
16242
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016243namespace {
16244 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016245 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016246 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016247
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016248 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016249 StringRef piece(*args[i]);
16250 if (!s.startswith(piece)) // Check if the piece matches.
16251 return false;
16252
16253 s = s.substr(piece.size());
16254 StringRef::size_type pos = s.find_first_not_of(" \t");
16255 if (pos == 0) // We matched a prefix.
16256 return false;
16257
16258 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016259 }
16260
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016261 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016262 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016263 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016264}
16265
Chris Lattnerb8105652009-07-20 17:51:36 +000016266bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16267 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016268
16269 std::string AsmStr = IA->getAsmString();
16270
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016271 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16272 if (!Ty || Ty->getBitWidth() % 16 != 0)
16273 return false;
16274
Chris Lattnerb8105652009-07-20 17:51:36 +000016275 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016276 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016277 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016278
16279 switch (AsmPieces.size()) {
16280 default: return false;
16281 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016282 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016283 // we will turn this bswap into something that will be lowered to logical
16284 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16285 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016286 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016287 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16288 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16289 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16290 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16291 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16292 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016293 // No need to check constraints, nothing other than the equivalent of
16294 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016295 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016296 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016297
Chris Lattnerb8105652009-07-20 17:51:36 +000016298 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016299 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016300 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016301 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16302 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016303 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016304 const std::string &ConstraintsStr = IA->getConstraintString();
16305 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016306 std::sort(AsmPieces.begin(), AsmPieces.end());
16307 if (AsmPieces.size() == 4 &&
16308 AsmPieces[0] == "~{cc}" &&
16309 AsmPieces[1] == "~{dirflag}" &&
16310 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016311 AsmPieces[3] == "~{fpsr}")
16312 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016313 }
16314 break;
16315 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016316 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016317 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016318 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16319 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16320 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016321 AsmPieces.clear();
16322 const std::string &ConstraintsStr = IA->getConstraintString();
16323 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16324 std::sort(AsmPieces.begin(), AsmPieces.end());
16325 if (AsmPieces.size() == 4 &&
16326 AsmPieces[0] == "~{cc}" &&
16327 AsmPieces[1] == "~{dirflag}" &&
16328 AsmPieces[2] == "~{flags}" &&
16329 AsmPieces[3] == "~{fpsr}")
16330 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016331 }
Evan Cheng55d42002011-01-08 01:24:27 +000016332
16333 if (CI->getType()->isIntegerTy(64)) {
16334 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16335 if (Constraints.size() >= 2 &&
16336 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16337 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16338 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016339 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16340 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16341 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016342 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016343 }
16344 }
16345 break;
16346 }
16347 return false;
16348}
16349
16350
16351
Chris Lattnerf4dff842006-07-11 02:54:03 +000016352/// getConstraintType - Given a constraint letter, return the type of
16353/// constraint it is for this target.
16354X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016355X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16356 if (Constraint.size() == 1) {
16357 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016358 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016359 case 'q':
16360 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016361 case 'f':
16362 case 't':
16363 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016364 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016365 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016366 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016367 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016368 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016369 case 'a':
16370 case 'b':
16371 case 'c':
16372 case 'd':
16373 case 'S':
16374 case 'D':
16375 case 'A':
16376 return C_Register;
16377 case 'I':
16378 case 'J':
16379 case 'K':
16380 case 'L':
16381 case 'M':
16382 case 'N':
16383 case 'G':
16384 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016385 case 'e':
16386 case 'Z':
16387 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016388 default:
16389 break;
16390 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016391 }
Chris Lattner4234f572007-03-25 02:14:49 +000016392 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016393}
16394
John Thompson44ab89e2010-10-29 17:29:13 +000016395/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016396/// This object must already have been set up with the operand type
16397/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016398TargetLowering::ConstraintWeight
16399 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016400 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016401 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016402 Value *CallOperandVal = info.CallOperandVal;
16403 // If we don't have a value, we can't do a match,
16404 // but allow it at the lowest weight.
16405 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016406 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016407 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016408 // Look at the constraint type.
16409 switch (*constraint) {
16410 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016411 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16412 case 'R':
16413 case 'q':
16414 case 'Q':
16415 case 'a':
16416 case 'b':
16417 case 'c':
16418 case 'd':
16419 case 'S':
16420 case 'D':
16421 case 'A':
16422 if (CallOperandVal->getType()->isIntegerTy())
16423 weight = CW_SpecificReg;
16424 break;
16425 case 'f':
16426 case 't':
16427 case 'u':
16428 if (type->isFloatingPointTy())
16429 weight = CW_SpecificReg;
16430 break;
16431 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016432 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016433 weight = CW_SpecificReg;
16434 break;
16435 case 'x':
16436 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016437 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016438 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016439 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016440 break;
16441 case 'I':
16442 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16443 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016444 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016445 }
16446 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016447 case 'J':
16448 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16449 if (C->getZExtValue() <= 63)
16450 weight = CW_Constant;
16451 }
16452 break;
16453 case 'K':
16454 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16455 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16456 weight = CW_Constant;
16457 }
16458 break;
16459 case 'L':
16460 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16461 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16462 weight = CW_Constant;
16463 }
16464 break;
16465 case 'M':
16466 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16467 if (C->getZExtValue() <= 3)
16468 weight = CW_Constant;
16469 }
16470 break;
16471 case 'N':
16472 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16473 if (C->getZExtValue() <= 0xff)
16474 weight = CW_Constant;
16475 }
16476 break;
16477 case 'G':
16478 case 'C':
16479 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16480 weight = CW_Constant;
16481 }
16482 break;
16483 case 'e':
16484 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16485 if ((C->getSExtValue() >= -0x80000000LL) &&
16486 (C->getSExtValue() <= 0x7fffffffLL))
16487 weight = CW_Constant;
16488 }
16489 break;
16490 case 'Z':
16491 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16492 if (C->getZExtValue() <= 0xffffffff)
16493 weight = CW_Constant;
16494 }
16495 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016496 }
16497 return weight;
16498}
16499
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016500/// LowerXConstraint - try to replace an X constraint, which matches anything,
16501/// with another that has more specific requirements based on the type of the
16502/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016503const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016504LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016505 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16506 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016507 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016508 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016509 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016510 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016511 return "x";
16512 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016513
Chris Lattner5e764232008-04-26 23:02:14 +000016514 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016515}
16516
Chris Lattner48884cd2007-08-25 00:47:38 +000016517/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16518/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016519void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016520 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016521 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016522 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016523 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016524
Eric Christopher100c8332011-06-02 23:16:42 +000016525 // Only support length 1 constraints for now.
16526 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016527
Eric Christopher100c8332011-06-02 23:16:42 +000016528 char ConstraintLetter = Constraint[0];
16529 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016530 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016531 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016532 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016533 if (C->getZExtValue() <= 31) {
16534 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016535 break;
16536 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016537 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016538 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016539 case 'J':
16540 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016541 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016542 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16543 break;
16544 }
16545 }
16546 return;
16547 case 'K':
16548 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016549 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016550 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16551 break;
16552 }
16553 }
16554 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016555 case 'N':
16556 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016557 if (C->getZExtValue() <= 255) {
16558 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016559 break;
16560 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016561 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016562 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016563 case 'e': {
16564 // 32-bit signed value
16565 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016566 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16567 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016568 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016569 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016570 break;
16571 }
16572 // FIXME gcc accepts some relocatable values here too, but only in certain
16573 // memory models; it's complicated.
16574 }
16575 return;
16576 }
16577 case 'Z': {
16578 // 32-bit unsigned value
16579 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016580 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16581 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016582 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16583 break;
16584 }
16585 }
16586 // FIXME gcc accepts some relocatable values here too, but only in certain
16587 // memory models; it's complicated.
16588 return;
16589 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016590 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016591 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016592 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016593 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016594 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016595 break;
16596 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016597
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016598 // In any sort of PIC mode addresses need to be computed at runtime by
16599 // adding in a register or some sort of table lookup. These can't
16600 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016601 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016602 return;
16603
Chris Lattnerdc43a882007-05-03 16:52:29 +000016604 // If we are in non-pic codegen mode, we allow the address of a global (with
16605 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016606 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016607 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016608
Chris Lattner49921962009-05-08 18:23:14 +000016609 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16610 while (1) {
16611 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16612 Offset += GA->getOffset();
16613 break;
16614 } else if (Op.getOpcode() == ISD::ADD) {
16615 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16616 Offset += C->getZExtValue();
16617 Op = Op.getOperand(0);
16618 continue;
16619 }
16620 } else if (Op.getOpcode() == ISD::SUB) {
16621 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16622 Offset += -C->getZExtValue();
16623 Op = Op.getOperand(0);
16624 continue;
16625 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016626 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016627
Chris Lattner49921962009-05-08 18:23:14 +000016628 // Otherwise, this isn't something we can handle, reject it.
16629 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016630 }
Eric Christopherfd179292009-08-27 18:07:15 +000016631
Dan Gohman46510a72010-04-15 01:51:59 +000016632 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016633 // If we require an extra load to get this address, as in PIC mode, we
16634 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016635 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16636 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016637 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016638
Devang Patel0d881da2010-07-06 22:08:15 +000016639 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16640 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016641 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016642 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016643 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016644
Gabor Greifba36cb52008-08-28 21:40:38 +000016645 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016646 Ops.push_back(Result);
16647 return;
16648 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016649 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016650}
16651
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016652std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016653X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016654 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016655 // First, see if this is a constraint that directly corresponds to an LLVM
16656 // register class.
16657 if (Constraint.size() == 1) {
16658 // GCC Constraint Letters
16659 switch (Constraint[0]) {
16660 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016661 // TODO: Slight differences here in allocation order and leaving
16662 // RIP in the class. Do they matter any more here than they do
16663 // in the normal allocation?
16664 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16665 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016666 if (VT == MVT::i32 || VT == MVT::f32)
16667 return std::make_pair(0U, &X86::GR32RegClass);
16668 if (VT == MVT::i16)
16669 return std::make_pair(0U, &X86::GR16RegClass);
16670 if (VT == MVT::i8 || VT == MVT::i1)
16671 return std::make_pair(0U, &X86::GR8RegClass);
16672 if (VT == MVT::i64 || VT == MVT::f64)
16673 return std::make_pair(0U, &X86::GR64RegClass);
16674 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016675 }
16676 // 32-bit fallthrough
16677 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016678 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016679 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16680 if (VT == MVT::i16)
16681 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16682 if (VT == MVT::i8 || VT == MVT::i1)
16683 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16684 if (VT == MVT::i64)
16685 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016686 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016687 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016688 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016689 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016690 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016691 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016692 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016693 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016694 return std::make_pair(0U, &X86::GR32RegClass);
16695 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016696 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016697 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016698 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016699 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016700 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016701 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016702 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16703 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016704 case 'f': // FP Stack registers.
16705 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16706 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016707 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016708 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016709 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016710 return std::make_pair(0U, &X86::RFP64RegClass);
16711 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016712 case 'y': // MMX_REGS if MMX allowed.
16713 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016714 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016715 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016716 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016717 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016718 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016719 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016720
Owen Anderson825b72b2009-08-11 20:47:22 +000016721 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016722 default: break;
16723 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016724 case MVT::f32:
16725 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016726 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016727 case MVT::f64:
16728 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016729 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016730 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016731 case MVT::v16i8:
16732 case MVT::v8i16:
16733 case MVT::v4i32:
16734 case MVT::v2i64:
16735 case MVT::v4f32:
16736 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016737 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016738 // AVX types.
16739 case MVT::v32i8:
16740 case MVT::v16i16:
16741 case MVT::v8i32:
16742 case MVT::v4i64:
16743 case MVT::v8f32:
16744 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016745 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016746 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016747 break;
16748 }
16749 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016750
Chris Lattnerf76d1802006-07-31 23:26:50 +000016751 // Use the default implementation in TargetLowering to convert the register
16752 // constraint into a member of a register class.
16753 std::pair<unsigned, const TargetRegisterClass*> Res;
16754 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016755
16756 // Not found as a standard register?
16757 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016758 // Map st(0) -> st(7) -> ST0
16759 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16760 tolower(Constraint[1]) == 's' &&
16761 tolower(Constraint[2]) == 't' &&
16762 Constraint[3] == '(' &&
16763 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16764 Constraint[5] == ')' &&
16765 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016766
Chris Lattner56d77c72009-09-13 22:41:48 +000016767 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016768 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016769 return Res;
16770 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016771
Chris Lattner56d77c72009-09-13 22:41:48 +000016772 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016773 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016774 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016775 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016776 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016777 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016778
16779 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016780 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016781 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016782 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016783 return Res;
16784 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016785
Dale Johannesen330169f2008-11-13 21:52:36 +000016786 // 'A' means EAX + EDX.
16787 if (Constraint == "A") {
16788 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016789 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016790 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016791 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016792 return Res;
16793 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016794
Chris Lattnerf76d1802006-07-31 23:26:50 +000016795 // Otherwise, check to see if this is a register class of the wrong value
16796 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16797 // turn into {ax},{dx}.
16798 if (Res.second->hasType(VT))
16799 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016800
Chris Lattnerf76d1802006-07-31 23:26:50 +000016801 // All of the single-register GCC register classes map their values onto
16802 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16803 // really want an 8-bit or 32-bit register, map to the appropriate register
16804 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016805 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016806 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016807 unsigned DestReg = 0;
16808 switch (Res.first) {
16809 default: break;
16810 case X86::AX: DestReg = X86::AL; break;
16811 case X86::DX: DestReg = X86::DL; break;
16812 case X86::CX: DestReg = X86::CL; break;
16813 case X86::BX: DestReg = X86::BL; break;
16814 }
16815 if (DestReg) {
16816 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016817 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016818 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016819 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016820 unsigned DestReg = 0;
16821 switch (Res.first) {
16822 default: break;
16823 case X86::AX: DestReg = X86::EAX; break;
16824 case X86::DX: DestReg = X86::EDX; break;
16825 case X86::CX: DestReg = X86::ECX; break;
16826 case X86::BX: DestReg = X86::EBX; break;
16827 case X86::SI: DestReg = X86::ESI; break;
16828 case X86::DI: DestReg = X86::EDI; break;
16829 case X86::BP: DestReg = X86::EBP; break;
16830 case X86::SP: DestReg = X86::ESP; break;
16831 }
16832 if (DestReg) {
16833 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016834 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016835 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016836 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016837 unsigned DestReg = 0;
16838 switch (Res.first) {
16839 default: break;
16840 case X86::AX: DestReg = X86::RAX; break;
16841 case X86::DX: DestReg = X86::RDX; break;
16842 case X86::CX: DestReg = X86::RCX; break;
16843 case X86::BX: DestReg = X86::RBX; break;
16844 case X86::SI: DestReg = X86::RSI; break;
16845 case X86::DI: DestReg = X86::RDI; break;
16846 case X86::BP: DestReg = X86::RBP; break;
16847 case X86::SP: DestReg = X86::RSP; break;
16848 }
16849 if (DestReg) {
16850 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016851 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016852 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016853 }
Craig Topperc9099502012-04-20 06:31:50 +000016854 } else if (Res.second == &X86::FR32RegClass ||
16855 Res.second == &X86::FR64RegClass ||
16856 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016857 // Handle references to XMM physical registers that got mapped into the
16858 // wrong class. This can happen with constraints like {xmm0} where the
16859 // target independent register mapper will just pick the first match it can
16860 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016861
16862 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016863 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016864 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016865 Res.second = &X86::FR64RegClass;
16866 else if (X86::VR128RegClass.hasType(VT))
16867 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016868 else if (X86::VR256RegClass.hasType(VT))
16869 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016870 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016871
Chris Lattnerf76d1802006-07-31 23:26:50 +000016872 return Res;
16873}