blob: 59d52339ef4547b2a9a44576123686b095dd6441 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Craig Topper1bf724b2012-02-19 07:15:48 +000016#include "X86ISelLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000018#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
David Greene583b68f2011-02-17 19:18:59 +000021#include "Utils/X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000040#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000045#include "llvm/ADT/VariadicFunction.h"
Evan Cheng485fafc2011-03-21 01:19:09 +000046#include "llvm/Support/CallSite.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
48#include "llvm/Support/ErrorHandling.h"
49#include "llvm/Support/MathExtras.h"
Rafael Espindola151ab3e2011-08-30 19:47:04 +000050#include "llvm/Target/TargetOptions.h"
Benjamin Kramer9c683542012-01-30 15:16:21 +000051#include <bitset>
Joerg Sonnenberger78cab942012-08-10 10:53:56 +000052#include <cctype>
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
54
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Evan Cheng10e86422008-04-25 19:11:04 +000057// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000058static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000059 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000060
David Greenea5f26012011-02-07 19:36:54 +000061/// Generate a DAG to grab 128-bits from a vector > 128 bits. This
62/// sets things up to match to an AVX VEXTRACTF128 instruction or a
David Greene74a579d2011-02-10 16:57:36 +000063/// simple subregister reference. Idx is an index in the 128 bits we
64/// want. It need not be aligned to a 128-bit bounday. That makes
65/// lowering EXTRACT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +000066static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
67 SelectionDAG &DAG, DebugLoc dl) {
David Greenea5f26012011-02-07 19:36:54 +000068 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +000069 assert(VT.is256BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +000070 EVT ElVT = VT.getVectorElementType();
Craig Topper66ddd152012-04-27 22:54:43 +000071 unsigned Factor = VT.getSizeInBits()/128;
Bruno Cardoso Lopes67727ca2011-07-21 01:55:27 +000072 EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
73 VT.getVectorNumElements()/Factor);
David Greenea5f26012011-02-07 19:36:54 +000074
75 // Extract from UNDEF is UNDEF.
76 if (Vec.getOpcode() == ISD::UNDEF)
Craig Topper767b4f62012-04-22 19:29:34 +000077 return DAG.getUNDEF(ResultVT);
David Greenea5f26012011-02-07 19:36:54 +000078
Craig Topperb14940a2012-04-22 20:55:18 +000079 // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR
80 // we can match to VEXTRACTF128.
81 unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +000082
Craig Topperb14940a2012-04-22 20:55:18 +000083 // This is the index of the first element of the 128-bit chunk
84 // we want.
85 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128)
86 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +000087
Craig Topperb8d9da12012-09-06 06:09:01 +000088 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topperb14940a2012-04-22 20:55:18 +000089 SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec,
90 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +000091
Craig Topperb14940a2012-04-22 20:55:18 +000092 return Result;
David Greenea5f26012011-02-07 19:36:54 +000093}
94
95/// Generate a DAG to put 128-bits into a vector > 128 bits. This
96/// sets things up to match to an AVX VINSERTF128 instruction or a
David Greene6b381262011-02-09 15:32:06 +000097/// simple superregister reference. Idx is an index in the 128 bits
98/// we want. It need not be aligned to a 128-bit bounday. That makes
99/// lowering INSERT_VECTOR_ELT operations easier.
Craig Topperb14940a2012-04-22 20:55:18 +0000100static SDValue Insert128BitVector(SDValue Result, SDValue Vec,
101 unsigned IdxVal, SelectionDAG &DAG,
David Greenea5f26012011-02-07 19:36:54 +0000102 DebugLoc dl) {
Craig Topper703c38b2012-06-20 05:39:26 +0000103 // Inserting UNDEF is Result
104 if (Vec.getOpcode() == ISD::UNDEF)
105 return Result;
106
Craig Topperb14940a2012-04-22 20:55:18 +0000107 EVT VT = Vec.getValueType();
Craig Topper7a9a28b2012-08-12 02:23:29 +0000108 assert(VT.is128BitVector() && "Unexpected vector size!");
David Greenea5f26012011-02-07 19:36:54 +0000109
Craig Topperb14940a2012-04-22 20:55:18 +0000110 EVT ElVT = VT.getVectorElementType();
111 EVT ResultVT = Result.getValueType();
David Greenea5f26012011-02-07 19:36:54 +0000112
Craig Topperb14940a2012-04-22 20:55:18 +0000113 // Insert the relevant 128 bits.
114 unsigned ElemsPerChunk = 128/ElVT.getSizeInBits();
David Greenea5f26012011-02-07 19:36:54 +0000115
Craig Topperb14940a2012-04-22 20:55:18 +0000116 // This is the index of the first element of the 128-bit chunk
117 // we want.
118 unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128)
119 * ElemsPerChunk);
David Greenea5f26012011-02-07 19:36:54 +0000120
Craig Topperb8d9da12012-09-06 06:09:01 +0000121 SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal);
Craig Topper703c38b2012-06-20 05:39:26 +0000122 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec,
123 VecIdx);
David Greenea5f26012011-02-07 19:36:54 +0000124}
125
Craig Topper4c7972d2012-04-22 18:15:59 +0000126/// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
127/// instructions. This is used because creating CONCAT_VECTOR nodes of
128/// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
129/// large BUILD_VECTORS.
130static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
131 unsigned NumElems, SelectionDAG &DAG,
132 DebugLoc dl) {
Craig Topperb14940a2012-04-22 20:55:18 +0000133 SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
134 return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
Craig Topper4c7972d2012-04-22 18:15:59 +0000135}
136
Chris Lattnerf0144122009-07-28 03:13:23 +0000137static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +0000138 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139 bool is64Bit = Subtarget->is64Bit();
NAKAMURA Takumi27635382011-02-05 15:10:54 +0000140
Evan Cheng2bffee22011-02-01 01:14:13 +0000141 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000142 if (is64Bit)
Bill Wendlinga44489d2012-06-26 10:05:06 +0000143 return new X86_64MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000144 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +0000145 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000146
Rafael Espindolad6b43a32012-06-19 00:48:28 +0000147 if (Subtarget->isTargetLinux())
148 return new X86LinuxTargetObjectFile();
Evan Cheng203576a2011-07-20 19:50:42 +0000149 if (Subtarget->isTargetELF())
150 return new TargetLoweringObjectFileELF();
Evan Cheng2bffee22011-02-01 01:14:13 +0000151 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +0000152 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +0000153 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +0000154}
155
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000156X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000158 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +0000159 X86ScalarSSEf64 = Subtarget->hasSSE2();
160 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +0000161 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000162
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000163 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000164 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000165
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166 // Set up the TargetLowering object.
Craig Topper9e401f22012-04-21 18:58:38 +0000167 static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000168
169 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Duncan Sands03228082008-11-23 15:47:28 +0000170 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000171 // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
172 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Eric Christopher471e4222011-06-08 23:55:35 +0000173
Eric Christopherde5e1012011-03-11 01:05:58 +0000174 // For 64-bit since we have so many registers use the ILP scheduler, for
175 // 32-bit code use the register pressure specific scheduling.
Preston Gurdc0f0a932012-05-02 22:02:02 +0000176 // For Atom, always use ILP scheduling.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000177 if (Subtarget->isAtom())
Eric Christopherde5e1012011-03-11 01:05:58 +0000178 setSchedulingPreference(Sched::ILP);
Preston Gurdc0f0a932012-05-02 22:02:02 +0000179 else if (Subtarget->is64Bit())
180 setSchedulingPreference(Sched::ILP);
Eric Christopherde5e1012011-03-11 01:05:58 +0000181 else
182 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000183 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000184
Preston Gurd2e2efd92012-09-04 18:22:17 +0000185 // Bypass i32 with i8 on Atom when compiling with O2
186 if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default)
187 addBypassSlowDivType(Type::getInt32Ty(getGlobalContext()), Type::getInt8Ty(getGlobalContext()));
188
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000189 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000190 // Setup Windows compiler runtime calls.
191 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000192 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
Julien Lerougef2960822011-07-08 21:40:25 +0000193 setLibcallName(RTLIB::SREM_I64, "_allrem");
194 setLibcallName(RTLIB::UREM_I64, "_aullrem");
195 setLibcallName(RTLIB::MUL_I64, "_allmul");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000196 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000197 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Julien Lerougef2960822011-07-08 21:40:25 +0000198 setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
199 setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
200 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000201
202 // The _ftol2 runtime function has an unusual calling conv, which
203 // is modeled by a special pseudo-instruction.
204 setLibcallName(RTLIB::FPTOUINT_F64_I64, 0);
205 setLibcallName(RTLIB::FPTOUINT_F32_I64, 0);
206 setLibcallName(RTLIB::FPTOUINT_F64_I32, 0);
207 setLibcallName(RTLIB::FPTOUINT_F32_I32, 0);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000208 }
209
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000210 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000211 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000212 setUseUnderscoreSetJmp(false);
213 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000214 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000215 // MS runtime is weird: it exports _setjmp, but longjmp!
216 setUseUnderscoreSetJmp(true);
217 setUseUnderscoreLongJmp(false);
218 } else {
219 setUseUnderscoreSetJmp(true);
220 setUseUnderscoreLongJmp(true);
221 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000222
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000224 addRegisterClass(MVT::i8, &X86::GR8RegClass);
225 addRegisterClass(MVT::i16, &X86::GR16RegClass);
226 addRegisterClass(MVT::i32, &X86::GR32RegClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 if (Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +0000228 addRegisterClass(MVT::i64, &X86::GR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000231
Scott Michelfdc40a02009-02-17 22:15:04 +0000232 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000234 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000236 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
238 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000239
240 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
242 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
243 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
244 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
246 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000247
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000248 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
249 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
251 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
252 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000253
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling397ae212012-01-05 02:13:20 +0000256 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000257 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000258 // We have an algorithm for SSE2->double, and we turn this into a
259 // 64-bit FILD followed by conditional FADD for other targets.
260 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000261 // We have an algorithm for SSE2, and we turn this into a 64-bit
262 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000263 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000264 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265
266 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
267 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
269 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000270
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000271 if (!TM.Options.UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000272 // SSE has no i16 to fp conversion, only i32
273 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000275 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000277 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
279 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000280 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000281 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
283 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000284 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000285
Dale Johannesen73328d12007-09-19 23:55:34 +0000286 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
287 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
289 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000290
Evan Cheng02568ff2006-01-30 22:13:22 +0000291 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
292 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
294 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000295
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000296 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000298 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000300 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
302 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000303 }
304
305 // Handle FP_TO_UINT by promoting the destination to a larger signed
306 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
308 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
309 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310
Evan Cheng25ab6902006-09-08 06:48:29 +0000311 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
313 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000314 } else if (!TM.Options.UseSoftFloat) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +0000315 // Since AVX is a superset of SSE3, only check for SSE here.
316 if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 // Expand FP_TO_UINT into a select.
318 // FIXME: We would like to use a Custom expander here eventually to do
319 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000322 // With SSE3 we can use fisttpll to convert to a signed i64; without
323 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000325 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000326
Michael J. Spencer1a2d0612012-02-24 19:01:22 +0000327 if (isTargetFTOL()) {
328 // Use the _ftol2 runtime function, which has a pseudo-instruction
329 // to handle its weird calling convention.
330 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom);
331 }
332
Chris Lattner399610a2006-12-05 18:22:22 +0000333 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000334 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
336 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000337 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000338 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000339 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000340 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000341 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000342 }
Chris Lattner21f66852005-12-23 05:15:23 +0000343
Dan Gohmanb00ee212008-02-18 19:34:53 +0000344 // Scalar integer divide and remainder are lowered to use operations that
345 // produce two results, to match the available instructions. This exposes
346 // the two-result form to trivial CSE, which is able to combine x/y and x%y
347 // into a single instruction.
348 //
349 // Scalar integer multiply-high is also lowered to use two-result
350 // operations, to match the available instructions. However, plain multiply
351 // (low) operations are left as Legal, as there are single-result
352 // instructions for this in x86. Using the two-result multiply instructions
353 // when both high and low results are needed must be arranged by dagcombine.
Craig Topper9e401f22012-04-21 18:58:38 +0000354 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000355 MVT VT = IntVTs[i];
356 setOperationAction(ISD::MULHS, VT, Expand);
357 setOperationAction(ISD::MULHU, VT, Expand);
358 setOperationAction(ISD::SDIV, VT, Expand);
359 setOperationAction(ISD::UDIV, VT, Expand);
360 setOperationAction(ISD::SREM, VT, Expand);
361 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000362
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000363 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000364 setOperationAction(ISD::ADDC, VT, Custom);
365 setOperationAction(ISD::ADDE, VT, Custom);
366 setOperationAction(ISD::SUBC, VT, Custom);
367 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000368 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000369
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
371 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
372 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
373 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000374 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
378 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
379 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
380 setOperationAction(ISD::FREM , MVT::f32 , Expand);
381 setOperationAction(ISD::FREM , MVT::f64 , Expand);
382 setOperationAction(ISD::FREM , MVT::f80 , Expand);
383 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000384
Chandler Carruth77821022011-12-24 12:12:34 +0000385 // Promote the i8 variants and force them on up to i32 which has a shorter
386 // encoding.
387 setOperationAction(ISD::CTTZ , MVT::i8 , Promote);
388 AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32);
389 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote);
390 AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32);
Craig Topper909652f2011-10-14 03:21:46 +0000391 if (Subtarget->hasBMI()) {
Chandler Carruthd873a4b2011-12-24 11:11:38 +0000392 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand);
393 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand);
394 if (Subtarget->is64Bit())
395 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper909652f2011-10-14 03:21:46 +0000396 } else {
Craig Topper909652f2011-10-14 03:21:46 +0000397 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
398 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
399 if (Subtarget->is64Bit())
400 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
401 }
Craig Topper37f21672011-10-11 06:44:02 +0000402
403 if (Subtarget->hasLZCNT()) {
Chandler Carruth77821022011-12-24 12:12:34 +0000404 // When promoting the i8 variants, force them to i32 for a shorter
405 // encoding.
Craig Topper37f21672011-10-11 06:44:02 +0000406 setOperationAction(ISD::CTLZ , MVT::i8 , Promote);
Chandler Carruth77821022011-12-24 12:12:34 +0000407 AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32);
408 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote);
409 AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand);
411 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand);
412 if (Subtarget->is64Bit())
413 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Craig Topper37f21672011-10-11 06:44:02 +0000414 } else {
415 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
416 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
417 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000418 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom);
419 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom);
420 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom);
421 if (Subtarget->is64Bit()) {
Craig Topper37f21672011-10-11 06:44:02 +0000422 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Chandler Carruthacc068e2011-12-24 10:55:54 +0000423 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
424 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000425 }
426
Benjamin Kramer1292c222010-12-04 20:32:23 +0000427 if (Subtarget->hasPOPCNT()) {
428 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
429 } else {
430 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
431 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
432 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
433 if (Subtarget->is64Bit())
434 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
435 }
436
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
438 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000439
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000440 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000441 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000442 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000443 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000444 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
446 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
447 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
448 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
449 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000450 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
452 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
453 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
454 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000455 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
Andrew Trickf6c39412011-03-23 23:11:02 +0000457 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000458 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000460
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000461 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
463 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
464 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
465 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000466 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
468 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000469 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000470 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
472 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
473 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
474 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000475 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000476 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000477 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
479 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
480 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000481 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
483 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
484 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000485 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000486
Craig Topper1accb7e2012-01-10 06:54:16 +0000487 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000489
Eric Christopher9a9d2752010-07-22 02:48:34 +0000490 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000491 setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000492
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000493 // On X86 and X86-64, atomic operations are lowered to locked instructions.
494 // Locked instructions, in turn, have implicit fence semantics (all memory
495 // operations are flushed before issuing the locked instruction, and they
496 // are not buffered), so we can fold away the common pattern of
497 // fence-atomic-fence.
498 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000499
Mon P Wang63307c32008-05-05 19:05:59 +0000500 // Expand certain atomics
Craig Topper9e401f22012-04-21 18:58:38 +0000501 for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) {
Chris Lattnere019ec12010-12-19 20:07:10 +0000502 MVT VT = IntVTs[i];
503 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
504 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
Eli Friedman327236c2011-08-24 20:50:09 +0000505 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000506 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000507
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000508 if (!Subtarget->is64Bit()) {
Eli Friedmanf8f90f02011-08-24 22:33:28 +0000509 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
511 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
512 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
513 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
514 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
515 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
516 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000517 }
518
Eli Friedman43f51ae2011-08-26 21:21:21 +0000519 if (Subtarget->hasCmpxchg16b()) {
520 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
521 }
522
Evan Cheng3c992d22006-03-07 02:02:57 +0000523 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000524 if (!Subtarget->isTargetDarwin() &&
525 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000526 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000527 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000528 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000529
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
531 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
532 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
533 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000534 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000535 setExceptionPointerRegister(X86::RAX);
536 setExceptionSelectorRegister(X86::RDX);
537 } else {
538 setExceptionPointerRegister(X86::EAX);
539 setExceptionSelectorRegister(X86::EDX);
540 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
542 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000543
Duncan Sands4a544a72011-09-06 13:37:06 +0000544 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
545 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000546
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000548
Nate Begemanacc398c2006-01-25 18:21:52 +0000549 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::VASTART , MVT::Other, Custom);
551 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000552 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::VAARG , MVT::Other, Custom);
554 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000555 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 setOperationAction(ISD::VAARG , MVT::Other, Expand);
557 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000558 }
Evan Chengae642192007-03-02 23:16:35 +0000559
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
561 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eric Christopherc967ad82011-08-31 04:17:21 +0000562
563 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
564 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
565 MVT::i64 : MVT::i32, Custom);
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000566 else if (TM.Options.EnableSegmentedStacks)
Eric Christopherc967ad82011-08-31 04:17:21 +0000567 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
568 MVT::i64 : MVT::i32, Custom);
569 else
570 setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ?
571 MVT::i64 : MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000572
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000573 if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000574 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000575 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000576 addRegisterClass(MVT::f32, &X86::FR32RegClass);
577 addRegisterClass(MVT::f64, &X86::FR64RegClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000578
Evan Cheng223547a2006-01-31 22:28:30 +0000579 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::FABS , MVT::f64, Custom);
581 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000582
583 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000584 setOperationAction(ISD::FNEG , MVT::f64, Custom);
585 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000586
Evan Cheng68c47cb2007-01-05 07:55:56 +0000587 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000588 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
589 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000590
Stuart Hastings4fd0dee2011-06-01 04:39:42 +0000591 // Lower this to FGETSIGNx86 plus an AND.
592 setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
593 setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
594
Evan Chengd25e9e82006-02-02 00:28:23 +0000595 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::FSIN , MVT::f64, Expand);
597 setOperationAction(ISD::FCOS , MVT::f64, Expand);
598 setOperationAction(ISD::FSIN , MVT::f32, Expand);
599 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000600
Chris Lattnera54aa942006-01-29 06:26:08 +0000601 // Expand FP immediates into loads from the stack, except for the special
602 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000603 addLegalFPImmediate(APFloat(+0.0)); // xorpd
604 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000605 } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000606 // Use SSE for f32, x87 for f64.
607 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000608 addRegisterClass(MVT::f32, &X86::FR32RegClass);
609 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000610
611 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000613
614 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000616
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000618
619 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000622
623 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f32, Expand);
625 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000626
Nate Begemane1795842008-02-14 08:57:00 +0000627 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000628 addLegalFPImmediate(APFloat(+0.0f)); // xorps
629 addLegalFPImmediate(APFloat(+0.0)); // FLD0
630 addLegalFPImmediate(APFloat(+1.0)); // FLD1
631 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
632 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
633
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000634 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
636 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000637 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000638 } else if (!TM.Options.UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000639 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000640 // Set up the FP register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000641 addRegisterClass(MVT::f64, &X86::RFP64RegClass);
642 addRegisterClass(MVT::f32, &X86::RFP32RegClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000643
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
645 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
646 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
647 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000648
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000649 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
651 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000652 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000653 addLegalFPImmediate(APFloat(+0.0)); // FLD0
654 addLegalFPImmediate(APFloat(+1.0)); // FLD1
655 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
656 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000657 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
658 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
659 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
660 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000661 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000662
Cameron Zwarich33390842011-07-08 21:39:21 +0000663 // We don't support FMA.
664 setOperationAction(ISD::FMA, MVT::f64, Expand);
665 setOperationAction(ISD::FMA, MVT::f32, Expand);
666
Dale Johannesen59a58732007-08-05 18:49:15 +0000667 // Long double always uses X87.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000668 if (!TM.Options.UseSoftFloat) {
Craig Topperc9099502012-04-20 06:31:50 +0000669 addRegisterClass(MVT::f80, &X86::RFP80RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
671 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000672 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000673 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000674 addLegalFPImmediate(TmpFlt); // FLD0
675 TmpFlt.changeSign();
676 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000677
678 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000679 APFloat TmpFlt2(+1.0);
680 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
681 &ignored);
682 addLegalFPImmediate(TmpFlt2); // FLD1
683 TmpFlt2.changeSign();
684 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
685 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000686
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000687 if (!TM.Options.UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
689 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000690 }
Cameron Zwarich33390842011-07-08 21:39:21 +0000691
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000692 setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
693 setOperationAction(ISD::FCEIL, MVT::f80, Expand);
694 setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
695 setOperationAction(ISD::FRINT, MVT::f80, Expand);
696 setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000697 setOperationAction(ISD::FMA, MVT::f80, Expand);
Dale Johannesen2f429012007-09-26 21:10:55 +0000698 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000699
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000700 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
702 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
703 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000704
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::FLOG, MVT::f80, Expand);
706 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
707 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
708 setOperationAction(ISD::FEXP, MVT::f80, Expand);
709 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000710
Mon P Wangf007a8b2008-11-06 05:31:54 +0000711 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000712 // (for widening) or expand (for scalarization). Then we will selectively
713 // turn on ones that can be effectively codegen'd.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000714 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
715 VT <= MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
717 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
718 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
719 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
720 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
721 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
722 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
723 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
724 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
725 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
726 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
727 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
728 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
729 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
730 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000732 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
733 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000734 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
735 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
736 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
737 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +0000738 setOperationAction(ISD::FMA, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
740 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
741 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
Craig Toppera1fb1d22012-09-08 04:58:43 +0000742 setOperationAction(ISD::FFLOOR, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
744 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
745 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
746 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
747 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
748 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
749 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000750 setOperationAction(ISD::CTTZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000751 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000752 setOperationAction(ISD::CTLZ_ZERO_UNDEF, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
754 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
755 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
756 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
757 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
758 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Duncan Sands28b77e92011-09-06 19:07:46 +0000759 setOperationAction(ISD::SETCC, (MVT::SimpleValueType)VT, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
761 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
762 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
763 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
764 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
765 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
766 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
767 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
768 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000769 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000770 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
771 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
772 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
773 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
Nadav Rotemaec58612011-09-13 19:17:42 +0000774 setOperationAction(ISD::VSELECT, (MVT::SimpleValueType)VT, Expand);
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000775 for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE;
776 InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
Dan Gohman2e141d72009-12-14 23:40:38 +0000777 setTruncStoreAction((MVT::SimpleValueType)VT,
778 (MVT::SimpleValueType)InnerVT, Expand);
779 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
780 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
781 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000782 }
783
Evan Chengc7ce29b2009-02-13 22:36:38 +0000784 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
785 // with -msoft-float, disable use of MMX as well.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000786 if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) {
Craig Topperc9099502012-04-20 06:31:50 +0000787 addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000788 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000789 }
790
Dale Johannesen0488fb62010-09-30 23:57:10 +0000791 // MMX-sized vectors (other than x86mmx) are expected to be expanded
792 // into smaller operations.
793 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
794 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
795 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
796 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
797 setOperationAction(ISD::AND, MVT::v8i8, Expand);
798 setOperationAction(ISD::AND, MVT::v4i16, Expand);
799 setOperationAction(ISD::AND, MVT::v2i32, Expand);
800 setOperationAction(ISD::AND, MVT::v1i64, Expand);
801 setOperationAction(ISD::OR, MVT::v8i8, Expand);
802 setOperationAction(ISD::OR, MVT::v4i16, Expand);
803 setOperationAction(ISD::OR, MVT::v2i32, Expand);
804 setOperationAction(ISD::OR, MVT::v1i64, Expand);
805 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
806 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
807 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
808 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
809 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
810 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
811 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
812 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
813 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
814 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
815 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
816 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
817 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000818 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
819 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
820 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
821 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000822
Craig Topper1accb7e2012-01-10 06:54:16 +0000823 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) {
Craig Topperc9099502012-04-20 06:31:50 +0000824 addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000825
Owen Anderson825b72b2009-08-11 20:47:22 +0000826 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
827 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
828 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
829 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
830 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
831 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000832 setOperationAction(ISD::FABS, MVT::v4f32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
834 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
835 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
836 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
837 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000838 }
839
Craig Topper1accb7e2012-01-10 06:54:16 +0000840 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000841 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000842
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000843 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
844 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000845 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
846 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
847 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
848 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000849
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
851 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
852 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
853 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
854 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
855 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
856 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
857 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
858 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
859 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
860 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
861 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
862 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
863 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
864 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
865 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +0000866 setOperationAction(ISD::FABS, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000867
Nadav Rotem354efd82011-09-18 14:57:03 +0000868 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000869 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
870 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
871 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000872
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
874 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
876 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
877 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000878
Evan Cheng2c3ae372006-04-12 21:21:57 +0000879 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000880 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000881 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000882 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000883 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000884 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000885 // Do not attempt to custom lower non-128-bit vectors
886 if (!VT.is128BitVector())
887 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000888 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
889 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
890 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000891 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000892
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
894 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
895 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
896 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
898 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000899
Nate Begemancdd1eec2008-02-12 22:51:28 +0000900 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
902 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000903 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000904
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000905 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000906 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000907 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000908
909 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000910 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000911 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000912
Craig Topper0d1f1762012-08-12 00:34:56 +0000913 setOperationAction(ISD::AND, VT, Promote);
914 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
915 setOperationAction(ISD::OR, VT, Promote);
916 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
917 setOperationAction(ISD::XOR, VT, Promote);
918 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
919 setOperationAction(ISD::LOAD, VT, Promote);
920 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
921 setOperationAction(ISD::SELECT, VT, Promote);
922 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000923 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000924
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000926
Evan Cheng2c3ae372006-04-12 21:21:57 +0000927 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
929 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
930 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
931 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000932
Owen Anderson825b72b2009-08-11 20:47:22 +0000933 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
934 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000935 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000936
Craig Topperd0a31172012-01-10 06:37:29 +0000937 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000938 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
939 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
940 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
941 setOperationAction(ISD::FRINT, MVT::f32, Legal);
942 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
943 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
944 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
945 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
946 setOperationAction(ISD::FRINT, MVT::f64, Legal);
947 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
948
Nate Begeman14d12ca2008-02-11 04:19:36 +0000949 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000951
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000952 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
953 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
954 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
955 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
956 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000957
Nate Begeman14d12ca2008-02-11 04:19:36 +0000958 // i8 and i16 vectors are custom , because the source register and source
959 // source memory operand types are not the same width. f32 vectors are
960 // custom since the immediate controlling the insert encodes additional
961 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
963 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
964 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
965 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000966
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
968 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
969 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
970 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000971
Pete Coopera77214a2011-11-14 19:38:42 +0000972 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000973 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000974 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000975 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
976 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000977 }
978 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000979
Craig Topper1accb7e2012-01-10 06:54:16 +0000980 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000981 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000982 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000983
Nadav Rotem43012222011-05-11 08:12:09 +0000984 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000985 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000986
Nadav Rotem43012222011-05-11 08:12:09 +0000987 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000988 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000989
990 if (Subtarget->hasAVX2()) {
991 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
992 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
993
994 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
995 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
996
997 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
998 } else {
999 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
1000 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
1001
1002 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1003 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1004
1005 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1006 }
Nadav Rotem43012222011-05-11 08:12:09 +00001007 }
1008
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001009 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001010 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1011 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1012 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1013 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1014 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1015 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001016
Owen Anderson825b72b2009-08-11 20:47:22 +00001017 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001018 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1019 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001020
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1022 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1023 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1024 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1025 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1026 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001027 setOperationAction(ISD::FABS, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001028
Owen Anderson825b72b2009-08-11 20:47:22 +00001029 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1030 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1031 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1032 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1033 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1034 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
Craig Topper43620672012-09-08 07:31:51 +00001035 setOperationAction(ISD::FABS, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001036
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001037 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1038 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001039 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001040
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001041 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1042 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1043
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001044 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1045 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1046
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001047 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001048 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001049
Duncan Sands28b77e92011-09-06 19:07:46 +00001050 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1051 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1052 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1053 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001054
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001055 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1056 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1057 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1058
Craig Topperaaa643c2011-11-09 07:28:55 +00001059 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1060 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1061 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1062 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001063
Craig Topperbf404372012-08-31 15:40:30 +00001064 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001065 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1066 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1067 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1068 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1069 setOperationAction(ISD::FMA, MVT::f32, Custom);
1070 setOperationAction(ISD::FMA, MVT::f64, Custom);
1071 }
Craig Topper880ef452012-08-11 22:34:26 +00001072
Craig Topperaaa643c2011-11-09 07:28:55 +00001073 if (Subtarget->hasAVX2()) {
1074 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1075 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1076 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1077 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001078
Craig Topperaaa643c2011-11-09 07:28:55 +00001079 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1080 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1081 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1082 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001083
Craig Topperaaa643c2011-11-09 07:28:55 +00001084 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1085 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1086 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001087 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001088
1089 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001090
1091 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1092 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1093
1094 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1095 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1096
1097 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001098 } else {
1099 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1100 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1101 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1102 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1103
1104 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1105 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1106 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1107 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1108
1109 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1110 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1111 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1112 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001113
1114 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1115 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1116
1117 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1118 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1119
1120 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001121 }
Craig Topper13894fa2011-08-24 06:14:18 +00001122
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001123 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001124 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1125 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001126 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001127
1128 // Extract subvector is special because the value type
1129 // (result) is 128-bit but the source is 256-bit wide.
1130 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001131 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001132
1133 // Do not attempt to custom lower other non-256-bit vectors
1134 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001135 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001136
Craig Topper0d1f1762012-08-12 00:34:56 +00001137 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1138 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1139 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1140 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1141 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1142 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1143 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001144 }
1145
David Greene54d8eba2011-01-27 22:38:56 +00001146 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001147 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001148 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001149
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001150 // Do not attempt to promote non-256-bit vectors
1151 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001152 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001153
Craig Topper0d1f1762012-08-12 00:34:56 +00001154 setOperationAction(ISD::AND, VT, Promote);
1155 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1156 setOperationAction(ISD::OR, VT, Promote);
1157 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1158 setOperationAction(ISD::XOR, VT, Promote);
1159 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1160 setOperationAction(ISD::LOAD, VT, Promote);
1161 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1162 setOperationAction(ISD::SELECT, VT, Promote);
1163 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001164 }
David Greene9b9838d2009-06-29 16:47:10 +00001165 }
1166
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001167 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1168 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001169 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1170 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001171 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1172 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001173 }
1174
Evan Cheng6be2c582006-04-05 23:38:46 +00001175 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001176 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001177 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001178
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001179
Eli Friedman962f5492010-06-02 19:35:46 +00001180 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1181 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001182 //
Eli Friedman962f5492010-06-02 19:35:46 +00001183 // FIXME: We really should do custom legalization for addition and
1184 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1185 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001186 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1187 // Add/Sub/Mul with overflow operations are custom lowered.
1188 MVT VT = IntVTs[i];
1189 setOperationAction(ISD::SADDO, VT, Custom);
1190 setOperationAction(ISD::UADDO, VT, Custom);
1191 setOperationAction(ISD::SSUBO, VT, Custom);
1192 setOperationAction(ISD::USUBO, VT, Custom);
1193 setOperationAction(ISD::SMULO, VT, Custom);
1194 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001195 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001196
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001197 // There are no 8-bit 3-address imul/mul instructions
1198 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1199 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001200
Evan Chengd54f2d52009-03-31 19:38:51 +00001201 if (!Subtarget->is64Bit()) {
1202 // These libcalls are not available in 32-bit.
1203 setLibcallName(RTLIB::SHL_I128, 0);
1204 setLibcallName(RTLIB::SRL_I128, 0);
1205 setLibcallName(RTLIB::SRA_I128, 0);
1206 }
1207
Evan Cheng206ee9d2006-07-07 08:33:52 +00001208 // We have target-specific dag combine patterns for the following nodes:
1209 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001210 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001211 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001212 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001213 setTargetDAGCombine(ISD::SHL);
1214 setTargetDAGCombine(ISD::SRA);
1215 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001216 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001217 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001218 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001219 setTargetDAGCombine(ISD::FADD);
1220 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001221 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001222 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001223 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001224 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001225 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001226 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001227 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001228 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001229 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001230 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001231 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001232 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001233 if (Subtarget->is64Bit())
1234 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001235 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001236
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001237 computeRegisterProperties();
1238
Evan Cheng05219282011-01-06 06:52:41 +00001239 // On Darwin, -Os means optimize for size without hurting performance,
1240 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001241 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001242 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001243 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001244 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1245 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1246 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001247 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001248 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001249
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001250 // Predictable cmov don't hurt on atom because it's in-order.
1251 predictableSelectIsExpensive = !Subtarget->isAtom();
1252
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001253 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001254}
1255
Scott Michel5b8f82e2008-03-10 15:42:14 +00001256
Duncan Sands28b77e92011-09-06 19:07:46 +00001257EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1258 if (!VT.isVector()) return MVT::i8;
1259 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001260}
1261
1262
Evan Cheng29286502008-01-23 23:17:41 +00001263/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1264/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001265static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001266 if (MaxAlign == 16)
1267 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001268 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001269 if (VTy->getBitWidth() == 128)
1270 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001271 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001272 unsigned EltAlign = 0;
1273 getMaxByValAlign(ATy->getElementType(), EltAlign);
1274 if (EltAlign > MaxAlign)
1275 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001276 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001277 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1278 unsigned EltAlign = 0;
1279 getMaxByValAlign(STy->getElementType(i), EltAlign);
1280 if (EltAlign > MaxAlign)
1281 MaxAlign = EltAlign;
1282 if (MaxAlign == 16)
1283 break;
1284 }
1285 }
Evan Cheng29286502008-01-23 23:17:41 +00001286}
1287
1288/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1289/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001290/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1291/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001292unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001293 if (Subtarget->is64Bit()) {
1294 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001295 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001296 if (TyAlign > 8)
1297 return TyAlign;
1298 return 8;
1299 }
1300
Evan Cheng29286502008-01-23 23:17:41 +00001301 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001302 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001303 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001304 return Align;
1305}
Chris Lattner2b02a442007-02-25 08:29:00 +00001306
Evan Chengf0df0312008-05-15 08:39:06 +00001307/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001308/// and store operations as a result of memset, memcpy, and memmove
1309/// lowering. If DstAlign is zero that means it's safe to destination
1310/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1311/// means there isn't a need to check it against alignment requirement,
1312/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001313/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001314/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1315/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1316/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001317/// It returns EVT::Other if the type should be determined using generic
1318/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001319EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001320X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1321 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001322 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001323 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001324 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001325 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1326 // linux. This is because the stack realignment code can't handle certain
1327 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001328 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001329 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001330 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001331 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001332 (Subtarget->isUnalignedMemAccessFast() ||
1333 ((DstAlign == 0 || DstAlign >= 16) &&
1334 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001335 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001336 if (Subtarget->getStackAlignment() >= 32) {
1337 if (Subtarget->hasAVX2())
1338 return MVT::v8i32;
1339 if (Subtarget->hasAVX())
1340 return MVT::v8f32;
1341 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001342 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001343 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001344 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001345 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001346 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001347 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001348 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001349 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001350 // Do not use f64 to lower memcpy if source is string constant. It's
1351 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001352 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001353 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001354 }
Evan Chengf0df0312008-05-15 08:39:06 +00001355 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001356 return MVT::i64;
1357 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001358}
1359
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001360/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1361/// current function. The returned value is a member of the
1362/// MachineJumpTableInfo::JTEntryKind enum.
1363unsigned X86TargetLowering::getJumpTableEncoding() const {
1364 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1365 // symbol.
1366 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1367 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001368 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001369
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001370 // Otherwise, use the normal jump table encoding heuristics.
1371 return TargetLowering::getJumpTableEncoding();
1372}
1373
Chris Lattnerc64daab2010-01-26 05:02:42 +00001374const MCExpr *
1375X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1376 const MachineBasicBlock *MBB,
1377 unsigned uid,MCContext &Ctx) const{
1378 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1379 Subtarget->isPICStyleGOT());
1380 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1381 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001382 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1383 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001384}
1385
Evan Chengcc415862007-11-09 01:32:10 +00001386/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1387/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001388SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001389 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001390 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001391 // This doesn't have DebugLoc associated with it, but is not really the
1392 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001393 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001394 return Table;
1395}
1396
Chris Lattner589c6f62010-01-26 06:28:43 +00001397/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1398/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1399/// MCExpr.
1400const MCExpr *X86TargetLowering::
1401getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1402 MCContext &Ctx) const {
1403 // X86-64 uses RIP relative addressing based on the jump table label.
1404 if (Subtarget->isPICStyleRIPRel())
1405 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1406
1407 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001408 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001409}
1410
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001411// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001412std::pair<const TargetRegisterClass*, uint8_t>
1413X86TargetLowering::findRepresentativeClass(EVT VT) const{
1414 const TargetRegisterClass *RRC = 0;
1415 uint8_t Cost = 1;
1416 switch (VT.getSimpleVT().SimpleTy) {
1417 default:
1418 return TargetLowering::findRepresentativeClass(VT);
1419 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001420 RRC = Subtarget->is64Bit() ?
1421 (const TargetRegisterClass*)&X86::GR64RegClass :
1422 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001423 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001424 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001425 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001426 break;
1427 case MVT::f32: case MVT::f64:
1428 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1429 case MVT::v4f32: case MVT::v2f64:
1430 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1431 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001432 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001433 break;
1434 }
1435 return std::make_pair(RRC, Cost);
1436}
1437
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001438bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1439 unsigned &Offset) const {
1440 if (!Subtarget->isTargetLinux())
1441 return false;
1442
1443 if (Subtarget->is64Bit()) {
1444 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1445 Offset = 0x28;
1446 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1447 AddressSpace = 256;
1448 else
1449 AddressSpace = 257;
1450 } else {
1451 // %gs:0x14 on i386
1452 Offset = 0x14;
1453 AddressSpace = 256;
1454 }
1455 return true;
1456}
1457
1458
Chris Lattner2b02a442007-02-25 08:29:00 +00001459//===----------------------------------------------------------------------===//
1460// Return Value Calling Convention Implementation
1461//===----------------------------------------------------------------------===//
1462
Chris Lattner59ed56b2007-02-28 04:55:35 +00001463#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001464
Michael J. Spencerec38de22010-10-10 22:04:20 +00001465bool
Eric Christopher471e4222011-06-08 23:55:35 +00001466X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001467 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001468 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001469 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001470 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001471 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001472 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001473 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001474}
1475
Dan Gohman98ca4f22009-08-05 01:29:28 +00001476SDValue
1477X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001478 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001479 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001480 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001481 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001482 MachineFunction &MF = DAG.getMachineFunction();
1483 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Chris Lattner9774c912007-02-27 05:28:59 +00001485 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001486 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487 RVLocs, *DAG.getContext());
1488 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001489
Evan Chengdcea1632010-02-04 02:40:39 +00001490 // Add the regs to the liveout set for the function.
1491 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1492 for (unsigned i = 0; i != RVLocs.size(); ++i)
1493 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1494 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001495
Dan Gohman475871a2008-07-27 21:46:04 +00001496 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001497
Dan Gohman475871a2008-07-27 21:46:04 +00001498 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001499 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1500 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001501 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1502 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001503
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001504 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001505 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1506 CCValAssign &VA = RVLocs[i];
1507 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001508 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001509 EVT ValVT = ValToCopy.getValueType();
1510
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001511 // Promote values to the appropriate types
1512 if (VA.getLocInfo() == CCValAssign::SExt)
1513 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1514 else if (VA.getLocInfo() == CCValAssign::ZExt)
1515 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1516 else if (VA.getLocInfo() == CCValAssign::AExt)
1517 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1518 else if (VA.getLocInfo() == CCValAssign::BCvt)
1519 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1520
Dale Johannesenc4510512010-09-24 19:05:48 +00001521 // If this is x86-64, and we disabled SSE, we can't return FP values,
1522 // or SSE or MMX vectors.
1523 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1524 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001525 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001526 report_fatal_error("SSE register return with SSE disabled");
1527 }
1528 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1529 // llvm-gcc has never done it right and no one has noticed, so this
1530 // should be OK for now.
1531 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001532 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001533 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001534
Chris Lattner447ff682008-03-11 03:23:40 +00001535 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1536 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001537 if (VA.getLocReg() == X86::ST0 ||
1538 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001539 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1540 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001541 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001542 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001543 RetOps.push_back(ValToCopy);
1544 // Don't emit a copytoreg.
1545 continue;
1546 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001547
Evan Cheng242b38b2009-02-23 09:03:22 +00001548 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1549 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001550 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001551 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001552 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001553 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001554 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1555 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001556 // If we don't have SSE2 available, convert to v4f32 so the generated
1557 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001558 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001559 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001560 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001561 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001562 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001563
Dale Johannesendd64c412009-02-04 00:33:20 +00001564 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001565 Flag = Chain.getValue(1);
1566 }
Dan Gohman61a92132008-04-21 23:59:07 +00001567
1568 // The x86-64 ABI for returning structs by value requires that we copy
1569 // the sret argument into %rax for the return. We saved the argument into
1570 // a virtual register in the entry block, so now we copy the value out
1571 // and into %rax.
1572 if (Subtarget->is64Bit() &&
1573 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1574 MachineFunction &MF = DAG.getMachineFunction();
1575 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1576 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001577 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001578 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001579 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001580
Dale Johannesendd64c412009-02-04 00:33:20 +00001581 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001582 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001583
1584 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001585 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001586 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001587
Chris Lattner447ff682008-03-11 03:23:40 +00001588 RetOps[0] = Chain; // Update chain.
1589
1590 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001591 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001592 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001593
1594 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001595 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001596}
1597
Evan Chengbf010eb2012-04-10 01:51:00 +00001598bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001599 if (N->getNumValues() != 1)
1600 return false;
1601 if (!N->hasNUsesOfValue(1, 0))
1602 return false;
1603
Evan Chengbf010eb2012-04-10 01:51:00 +00001604 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001605 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001606 if (Copy->getOpcode() == ISD::CopyToReg) {
1607 // If the copy has a glue operand, we conservatively assume it isn't safe to
1608 // perform a tail call.
1609 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1610 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001611 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001612 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001613 return false;
1614
Evan Cheng1bf891a2010-12-01 22:59:46 +00001615 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001616 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001617 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001618 if (UI->getOpcode() != X86ISD::RET_FLAG)
1619 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001620 HasRet = true;
1621 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001622
Evan Chengbf010eb2012-04-10 01:51:00 +00001623 if (!HasRet)
1624 return false;
1625
1626 Chain = TCChain;
1627 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001628}
1629
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001630EVT
1631X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001632 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001633 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001634 // TODO: Is this also valid on 32-bit?
1635 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001636 ReturnMVT = MVT::i8;
1637 else
1638 ReturnMVT = MVT::i32;
1639
1640 EVT MinVT = getRegisterType(Context, ReturnMVT);
1641 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001642}
1643
Dan Gohman98ca4f22009-08-05 01:29:28 +00001644/// LowerCallResult - Lower the result values of a call into the
1645/// appropriate copies out of appropriate physical registers.
1646///
1647SDValue
1648X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001649 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001650 const SmallVectorImpl<ISD::InputArg> &Ins,
1651 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001652 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001653
Chris Lattnere32bbf62007-02-28 07:09:55 +00001654 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001655 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001656 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001657 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001658 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001659 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001660
Chris Lattner3085e152007-02-25 08:59:22 +00001661 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001662 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001663 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001664 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001665
Torok Edwin3f142c32009-02-01 18:15:56 +00001666 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001667 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001668 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001669 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001670 }
1671
Evan Cheng79fb3b42009-02-20 20:43:02 +00001672 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001673
1674 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001675 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001676 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001677 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001678 // instead.
1679 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1680 // If we prefer to use the value in xmm registers, copy it out as f80 and
1681 // use a truncate to move it from fp stack reg to xmm reg.
1682 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001683 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001684 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1685 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001686 Val = Chain.getValue(0);
1687
1688 // Round the f80 to the right size, which also moves it to the appropriate
1689 // xmm register.
1690 if (CopyVT != VA.getValVT())
1691 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1692 // This truncation won't change the value.
1693 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001694 } else {
1695 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1696 CopyVT, InFlag).getValue(1);
1697 Val = Chain.getValue(0);
1698 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001699 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001700 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001701 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001702
Dan Gohman98ca4f22009-08-05 01:29:28 +00001703 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001704}
1705
1706
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001707//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001708// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001709//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001710// StdCall calling convention seems to be standard for many Windows' API
1711// routines and around. It differs from C calling convention just a little:
1712// callee should clean up the stack, not caller. Symbols should be also
1713// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001714// For info on fast calling convention see Fast Calling Convention (tail call)
1715// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001716
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001718/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001719enum StructReturnType {
1720 NotStructReturn,
1721 RegStructReturn,
1722 StackStructReturn
1723};
1724static StructReturnType
1725callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001726 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001727 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001728
Rafael Espindola1cee7102012-07-25 13:41:10 +00001729 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1730 if (!Flags.isSRet())
1731 return NotStructReturn;
1732 if (Flags.isInReg())
1733 return RegStructReturn;
1734 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001735}
1736
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001737/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001738/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001739static StructReturnType
1740argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001742 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001743
Rafael Espindola1cee7102012-07-25 13:41:10 +00001744 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1745 if (!Flags.isSRet())
1746 return NotStructReturn;
1747 if (Flags.isInReg())
1748 return RegStructReturn;
1749 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001750}
1751
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001752/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1753/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001754/// the specific parameter attribute. The copy will be passed as a byval
1755/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001756static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001757CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001758 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1759 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001760 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001761
Dale Johannesendd64c412009-02-04 00:33:20 +00001762 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001763 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001764 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001765}
1766
Chris Lattner29689432010-03-11 00:22:57 +00001767/// IsTailCallConvention - Return true if the calling convention is one that
1768/// supports tail call optimization.
1769static bool IsTailCallConvention(CallingConv::ID CC) {
1770 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1771}
1772
Evan Cheng485fafc2011-03-21 01:19:09 +00001773bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001774 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001775 return false;
1776
1777 CallSite CS(CI);
1778 CallingConv::ID CalleeCC = CS.getCallingConv();
1779 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1780 return false;
1781
1782 return true;
1783}
1784
Evan Cheng0c439eb2010-01-27 00:07:07 +00001785/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1786/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001787static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1788 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001789 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001790}
1791
Dan Gohman98ca4f22009-08-05 01:29:28 +00001792SDValue
1793X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001794 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001795 const SmallVectorImpl<ISD::InputArg> &Ins,
1796 DebugLoc dl, SelectionDAG &DAG,
1797 const CCValAssign &VA,
1798 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001799 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001800 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001801 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001802 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1803 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001804 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001805 EVT ValVT;
1806
1807 // If value is passed by pointer we have address passed instead of the value
1808 // itself.
1809 if (VA.getLocInfo() == CCValAssign::Indirect)
1810 ValVT = VA.getLocVT();
1811 else
1812 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001813
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001814 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001815 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001816 // In case of tail call optimization mark all arguments mutable. Since they
1817 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001818 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001819 unsigned Bytes = Flags.getByValSize();
1820 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1821 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001822 return DAG.getFrameIndex(FI, getPointerTy());
1823 } else {
1824 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001825 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001826 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1827 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001828 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001829 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001830 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001831}
1832
Dan Gohman475871a2008-07-27 21:46:04 +00001833SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001834X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001835 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001836 bool isVarArg,
1837 const SmallVectorImpl<ISD::InputArg> &Ins,
1838 DebugLoc dl,
1839 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001840 SmallVectorImpl<SDValue> &InVals)
1841 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001842 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001843 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001844
Gordon Henriksen86737662008-01-05 16:56:59 +00001845 const Function* Fn = MF.getFunction();
1846 if (Fn->hasExternalLinkage() &&
1847 Subtarget->isTargetCygMing() &&
1848 Fn->getName() == "main")
1849 FuncInfo->setForceFramePointer(true);
1850
Evan Cheng1bc78042006-04-26 01:20:17 +00001851 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001852 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001853 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001854 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001855
Chris Lattner29689432010-03-11 00:22:57 +00001856 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1857 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001858
Chris Lattner638402b2007-02-28 07:00:42 +00001859 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001860 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001861 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001862 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001863
1864 // Allocate shadow area for Win64
1865 if (IsWin64) {
1866 CCInfo.AllocateStack(32, 8);
1867 }
1868
Duncan Sands45907662010-10-31 13:21:44 +00001869 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001870
Chris Lattnerf39f7712007-02-28 05:46:49 +00001871 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001872 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001873 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1874 CCValAssign &VA = ArgLocs[i];
1875 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1876 // places.
1877 assert(VA.getValNo() != LastVal &&
1878 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001879 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001880 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001881
Chris Lattnerf39f7712007-02-28 05:46:49 +00001882 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001883 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001884 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001885 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001886 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001887 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001888 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001889 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001890 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001891 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001892 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001893 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001894 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001895 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001896 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001897 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001898 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001899 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001900 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001901
Devang Patel68e6bee2011-02-21 23:21:26 +00001902 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001904
Chris Lattnerf39f7712007-02-28 05:46:49 +00001905 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1906 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1907 // right size.
1908 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001909 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001910 DAG.getValueType(VA.getValVT()));
1911 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001912 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001913 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001914 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001915 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001916
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001917 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001918 // Handle MMX values passed in XMM regs.
1919 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001920 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1921 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001922 } else
1923 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001924 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001925 } else {
1926 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001927 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001928 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001929
1930 // If value is passed via pointer - do a load.
1931 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001932 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001933 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001934
Dan Gohman98ca4f22009-08-05 01:29:28 +00001935 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001936 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001937
Dan Gohman61a92132008-04-21 23:59:07 +00001938 // The x86-64 ABI for returning structs by value requires that we copy
1939 // the sret argument into %rax for the return. Save the argument into
1940 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001941 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001942 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1943 unsigned Reg = FuncInfo->getSRetReturnReg();
1944 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001946 FuncInfo->setSRetReturnReg(Reg);
1947 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001948 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001950 }
1951
Chris Lattnerf39f7712007-02-28 05:46:49 +00001952 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001953 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001954 if (FuncIsMadeTailCallSafe(CallConv,
1955 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001956 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001957
Evan Cheng1bc78042006-04-26 01:20:17 +00001958 // If the function takes variable number of arguments, make a frame index for
1959 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001960 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001961 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1962 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001963 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001964 }
1965 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001966 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1967
1968 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001969 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001970 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001971 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001972 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001973 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1974 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001975 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001976 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1977 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1978 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001979 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001980 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001981
1982 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001983 // The XMM registers which might contain var arg parameters are shadowed
1984 // in their paired GPR. So we only need to save the GPR to their home
1985 // slots.
1986 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001987 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001988 } else {
1989 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1990 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001991
Chad Rosier30450e82011-12-22 22:35:21 +00001992 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
1993 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001994 }
1995 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1996 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001997
Devang Patel578efa92009-06-05 21:57:13 +00001998 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00001999 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00002000 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002001 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
2002 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00002003 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002004 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002005 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002006 // Kernel mode asks for SSE to be disabled, so don't push them
2007 // on the stack.
2008 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002009
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002010 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002011 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002012 // Get to the caller-allocated home save location. Add 8 to account
2013 // for the return address.
2014 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002015 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002016 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002017 // Fixup to set vararg frame on shadow area (4 x i64).
2018 if (NumIntRegs < 4)
2019 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002020 } else {
2021 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002022 // registers, then we must store them to their spots on the stack so
2023 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002024 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2025 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2026 FuncInfo->setRegSaveFrameIndex(
2027 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002028 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002029 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002030
Gordon Henriksen86737662008-01-05 16:56:59 +00002031 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002032 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002033 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2034 getPointerTy());
2035 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002036 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002037 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2038 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002039 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002040 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002041 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002042 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002043 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002044 MachinePointerInfo::getFixedStack(
2045 FuncInfo->getRegSaveFrameIndex(), Offset),
2046 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002047 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002048 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002049 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002050
Dan Gohmanface41a2009-08-16 21:24:25 +00002051 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2052 // Now store the XMM (fp + vector) parameter registers.
2053 SmallVector<SDValue, 11> SaveXMMOps;
2054 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002055
Craig Topperc9099502012-04-20 06:31:50 +00002056 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002057 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2058 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002059
Dan Gohman1e93df62010-04-17 14:41:14 +00002060 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2061 FuncInfo->getRegSaveFrameIndex()));
2062 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2063 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002064
Dan Gohmanface41a2009-08-16 21:24:25 +00002065 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002066 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002067 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002068 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2069 SaveXMMOps.push_back(Val);
2070 }
2071 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2072 MVT::Other,
2073 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002074 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002075
2076 if (!MemOps.empty())
2077 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2078 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002079 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002080 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002081
Gordon Henriksen86737662008-01-05 16:56:59 +00002082 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002083 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2084 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002085 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002086 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002087 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002088 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002089 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002090 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002091 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002092 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002093
Gordon Henriksen86737662008-01-05 16:56:59 +00002094 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002095 // RegSaveFrameIndex is X86-64 only.
2096 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002097 if (CallConv == CallingConv::X86_FastCall ||
2098 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002099 // fastcc functions can't have varargs.
2100 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002101 }
Evan Cheng25caf632006-05-23 21:06:34 +00002102
Rafael Espindola76927d752011-08-30 19:39:58 +00002103 FuncInfo->setArgumentStackSize(StackSize);
2104
Dan Gohman98ca4f22009-08-05 01:29:28 +00002105 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002106}
2107
Dan Gohman475871a2008-07-27 21:46:04 +00002108SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002109X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2110 SDValue StackPtr, SDValue Arg,
2111 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002112 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002113 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002114 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002115 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002116 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002117 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002118 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002119
2120 return DAG.getStore(Chain, dl, Arg, PtrOff,
2121 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002122 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002123}
2124
Bill Wendling64e87322009-01-16 19:25:27 +00002125/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002126/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002127SDValue
2128X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002129 SDValue &OutRetAddr, SDValue Chain,
2130 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002131 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002132 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002133 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002134 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002135
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002136 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002137 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002138 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002139 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002140}
2141
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002142/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002143/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002144static SDValue
2145EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002146 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002147 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002148 // Store the return address to the appropriate stack slot.
2149 if (!FPDiff) return Chain;
2150 // Calculate the new stack slot for the return address.
2151 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002152 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002153 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002154 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002155 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002156 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002157 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002158 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002159 return Chain;
2160}
2161
Dan Gohman98ca4f22009-08-05 01:29:28 +00002162SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002163X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002164 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002165 SelectionDAG &DAG = CLI.DAG;
2166 DebugLoc &dl = CLI.DL;
2167 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2168 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2169 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2170 SDValue Chain = CLI.Chain;
2171 SDValue Callee = CLI.Callee;
2172 CallingConv::ID CallConv = CLI.CallConv;
2173 bool &isTailCall = CLI.IsTailCall;
2174 bool isVarArg = CLI.IsVarArg;
2175
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176 MachineFunction &MF = DAG.getMachineFunction();
2177 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002178 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002179 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002180 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002181 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002182
Nick Lewycky22de16d2012-01-19 00:34:10 +00002183 if (MF.getTarget().Options.DisableTailCalls)
2184 isTailCall = false;
2185
Evan Cheng5f941932010-02-05 02:21:12 +00002186 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002187 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002188 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002189 isVarArg, SR != NotStructReturn,
2190 MF.getFunction()->hasStructRetAttr(),
2191 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002192
2193 // Sibcalls are automatically detected tailcalls which do not require
2194 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002195 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002196 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002197
2198 if (isTailCall)
2199 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002200 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002201
Chris Lattner29689432010-03-11 00:22:57 +00002202 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2203 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002204
Chris Lattner638402b2007-02-28 07:00:42 +00002205 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002206 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002207 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002208 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002209
2210 // Allocate shadow area for Win64
2211 if (IsWin64) {
2212 CCInfo.AllocateStack(32, 8);
2213 }
2214
Duncan Sands45907662010-10-31 13:21:44 +00002215 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002216
Chris Lattner423c5f42007-02-28 05:31:48 +00002217 // Get a count of how many bytes are to be pushed on the stack.
2218 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002219 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002220 // This is a sibcall. The memory operands are available in caller's
2221 // own caller's stack.
2222 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002223 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2224 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002225 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002226
Gordon Henriksen86737662008-01-05 16:56:59 +00002227 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002228 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002229 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002230 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002231 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2232 FPDiff = NumBytesCallerPushed - NumBytes;
2233
2234 // Set the delta of movement of the returnaddr stackslot.
2235 // But only set if delta is greater than previous delta.
2236 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2237 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2238 }
2239
Evan Chengf22f9b32010-02-06 03:28:46 +00002240 if (!IsSibcall)
2241 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002242
Dan Gohman475871a2008-07-27 21:46:04 +00002243 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002244 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002245 if (isTailCall && FPDiff)
2246 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2247 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002248
Dan Gohman475871a2008-07-27 21:46:04 +00002249 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2250 SmallVector<SDValue, 8> MemOpChains;
2251 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002252
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002253 // Walk the register/memloc assignments, inserting copies/loads. In the case
2254 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002255 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2256 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002257 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002258 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002259 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002260 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002261
Chris Lattner423c5f42007-02-28 05:31:48 +00002262 // Promote the value if needed.
2263 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002264 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002265 case CCValAssign::Full: break;
2266 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002267 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002268 break;
2269 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002270 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002271 break;
2272 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002273 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002274 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002275 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002276 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2277 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002278 } else
2279 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2280 break;
2281 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002282 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002283 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002284 case CCValAssign::Indirect: {
2285 // Store the argument.
2286 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002287 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002288 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002289 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002290 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002291 Arg = SpillSlot;
2292 break;
2293 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002294 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002295
Chris Lattner423c5f42007-02-28 05:31:48 +00002296 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002297 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2298 if (isVarArg && IsWin64) {
2299 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2300 // shadow reg if callee is a varargs function.
2301 unsigned ShadowReg = 0;
2302 switch (VA.getLocReg()) {
2303 case X86::XMM0: ShadowReg = X86::RCX; break;
2304 case X86::XMM1: ShadowReg = X86::RDX; break;
2305 case X86::XMM2: ShadowReg = X86::R8; break;
2306 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002307 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002308 if (ShadowReg)
2309 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002310 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002311 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002312 assert(VA.isMemLoc());
2313 if (StackPtr.getNode() == 0)
2314 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2315 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2316 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002317 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002318 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002319
Evan Cheng32fe1032006-05-25 00:59:30 +00002320 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002321 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002322 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002323
Chris Lattner88e1fd52009-07-09 04:24:46 +00002324 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002325 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2326 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002327 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002328 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2329 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002330 } else {
2331 // If we are tail calling and generating PIC/GOT style code load the
2332 // address of the callee into ECX. The value in ecx is used as target of
2333 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2334 // for tail calls on PIC/GOT architectures. Normally we would just put the
2335 // address of GOT into ebx and then call target@PLT. But for tail calls
2336 // ebx would be restored (since ebx is callee saved) before jumping to the
2337 // target@PLT.
2338
2339 // Note: The actual moving to ECX is done further down.
2340 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2341 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2342 !G->getGlobal()->hasProtectedVisibility())
2343 Callee = LowerGlobalAddress(Callee, DAG);
2344 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002345 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002346 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002347 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002348
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002349 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002350 // From AMD64 ABI document:
2351 // For calls that may call functions that use varargs or stdargs
2352 // (prototype-less calls or calls to functions containing ellipsis (...) in
2353 // the declaration) %al is used as hidden argument to specify the number
2354 // of SSE registers used. The contents of %al do not need to match exactly
2355 // the number of registers, but must be an ubound on the number of SSE
2356 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002357
Gordon Henriksen86737662008-01-05 16:56:59 +00002358 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002359 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002360 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2361 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2362 };
2363 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002364 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002365 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002366
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002367 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2368 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002369 }
2370
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002371 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002372 if (isTailCall) {
2373 // Force all the incoming stack arguments to be loaded from the stack
2374 // before any new outgoing arguments are stored to the stack, because the
2375 // outgoing stack slots may alias the incoming argument stack slots, and
2376 // the alias isn't otherwise explicit. This is slightly more conservative
2377 // than necessary, because it means that each store effectively depends
2378 // on every argument instead of just those arguments it would clobber.
2379 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2380
Dan Gohman475871a2008-07-27 21:46:04 +00002381 SmallVector<SDValue, 8> MemOpChains2;
2382 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002383 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002384 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002385 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2386 CCValAssign &VA = ArgLocs[i];
2387 if (VA.isRegLoc())
2388 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002389 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002390 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002391 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002392 // Create frame index.
2393 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002394 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002395 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002396 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002397
Duncan Sands276dcbd2008-03-21 09:14:45 +00002398 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002399 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002400 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002401 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002402 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002403 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002404 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002405
Dan Gohman98ca4f22009-08-05 01:29:28 +00002406 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2407 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002408 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002409 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002410 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002411 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002412 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002413 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002414 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002415 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002416 }
2417 }
2418
2419 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002420 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002421 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002422
2423 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002424 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002425 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002426 }
2427
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002428 // Build a sequence of copy-to-reg nodes chained together with token chain
2429 // and flag operands which copy the outgoing args into registers.
2430 SDValue InFlag;
2431 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2432 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2433 RegsToPass[i].second, InFlag);
2434 InFlag = Chain.getValue(1);
2435 }
2436
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002437 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2438 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2439 // In the 64-bit large code model, we have to make all calls
2440 // through a register, since the call instruction's 32-bit
2441 // pc-relative offset may not be large enough to hold the whole
2442 // address.
2443 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002444 // If the callee is a GlobalAddress node (quite common, every direct call
2445 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2446 // it.
2447
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002448 // We should use extra load for direct calls to dllimported functions in
2449 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002450 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002451 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002452 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002453 bool ExtraLoad = false;
2454 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002455
Chris Lattner48a7d022009-07-09 05:02:21 +00002456 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2457 // external symbols most go through the PLT in PIC mode. If the symbol
2458 // has hidden or protected visibility, or if it is static or local, then
2459 // we don't need to use the PLT - we can directly call it.
2460 if (Subtarget->isTargetELF() &&
2461 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002462 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002463 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002464 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002465 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002466 (!Subtarget->getTargetTriple().isMacOSX() ||
2467 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002468 // PC-relative references to external symbols should go through $stub,
2469 // unless we're building with the leopard linker or later, which
2470 // automatically synthesizes these stubs.
2471 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002472 } else if (Subtarget->isPICStyleRIPRel() &&
2473 isa<Function>(GV) &&
2474 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2475 // If the function is marked as non-lazy, generate an indirect call
2476 // which loads from the GOT directly. This avoids runtime overhead
2477 // at the cost of eager binding (and one extra byte of encoding).
2478 OpFlags = X86II::MO_GOTPCREL;
2479 WrapperKind = X86ISD::WrapperRIP;
2480 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002481 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002482
Devang Patel0d881da2010-07-06 22:08:15 +00002483 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002484 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002485
2486 // Add a wrapper if needed.
2487 if (WrapperKind != ISD::DELETED_NODE)
2488 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2489 // Add extra indirection if needed.
2490 if (ExtraLoad)
2491 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2492 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002493 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002494 }
Bill Wendling056292f2008-09-16 21:48:12 +00002495 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002496 unsigned char OpFlags = 0;
2497
Evan Cheng1bf891a2010-12-01 22:59:46 +00002498 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2499 // external symbols should go through the PLT.
2500 if (Subtarget->isTargetELF() &&
2501 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2502 OpFlags = X86II::MO_PLT;
2503 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002504 (!Subtarget->getTargetTriple().isMacOSX() ||
2505 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002506 // PC-relative references to external symbols should go through $stub,
2507 // unless we're building with the leopard linker or later, which
2508 // automatically synthesizes these stubs.
2509 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002510 }
Eric Christopherfd179292009-08-27 18:07:15 +00002511
Chris Lattner48a7d022009-07-09 05:02:21 +00002512 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2513 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002514 }
2515
Chris Lattnerd96d0722007-02-25 06:40:16 +00002516 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002517 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002518 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002519
Evan Chengf22f9b32010-02-06 03:28:46 +00002520 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002521 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2522 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002523 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002524 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002525
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002526 Ops.push_back(Chain);
2527 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002528
Dan Gohman98ca4f22009-08-05 01:29:28 +00002529 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002530 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002531
Gordon Henriksen86737662008-01-05 16:56:59 +00002532 // Add argument registers to the end of the list so that they are known live
2533 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002534 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2535 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2536 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002537
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002538 // Add a register mask operand representing the call-preserved registers.
2539 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2540 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2541 assert(Mask && "Missing call preserved mask for calling convention");
2542 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002543
Gabor Greifba36cb52008-08-28 21:40:38 +00002544 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002545 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002546
Dan Gohman98ca4f22009-08-05 01:29:28 +00002547 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002548 // We used to do:
2549 //// If this is the first return lowered for this function, add the regs
2550 //// to the liveout set for the function.
2551 // This isn't right, although it's probably harmless on x86; liveouts
2552 // should be computed from returns not tail calls. Consider a void
2553 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002554 return DAG.getNode(X86ISD::TC_RETURN, dl,
2555 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002556 }
2557
Dale Johannesenace16102009-02-03 19:33:06 +00002558 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002559 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002560
Chris Lattner2d297092006-05-23 18:50:38 +00002561 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002562 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002563 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2564 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002565 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002566 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002567 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002568 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002569 // pops the hidden struct pointer, so we have to push it back.
2570 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002571 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002572 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002573 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002574 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002575
Gordon Henriksenae636f82008-01-03 16:47:34 +00002576 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002577 if (!IsSibcall) {
2578 Chain = DAG.getCALLSEQ_END(Chain,
2579 DAG.getIntPtrConstant(NumBytes, true),
2580 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2581 true),
2582 InFlag);
2583 InFlag = Chain.getValue(1);
2584 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002585
Chris Lattner3085e152007-02-25 08:59:22 +00002586 // Handle result values, copying them out of physregs into vregs that we
2587 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002588 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2589 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002590}
2591
Evan Cheng25ab6902006-09-08 06:48:29 +00002592
2593//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002594// Fast Calling Convention (tail call) implementation
2595//===----------------------------------------------------------------------===//
2596
2597// Like std call, callee cleans arguments, convention except that ECX is
2598// reserved for storing the tail called function address. Only 2 registers are
2599// free for argument passing (inreg). Tail call optimization is performed
2600// provided:
2601// * tailcallopt is enabled
2602// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002603// On X86_64 architecture with GOT-style position independent code only local
2604// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002605// To keep the stack aligned according to platform abi the function
2606// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2607// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002608// If a tail called function callee has more arguments than the caller the
2609// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002610// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002611// original REtADDR, but before the saved framepointer or the spilled registers
2612// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2613// stack layout:
2614// arg1
2615// arg2
2616// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002617// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002618// move area ]
2619// (possible EBP)
2620// ESI
2621// EDI
2622// local1 ..
2623
2624/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2625/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002626unsigned
2627X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2628 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002629 MachineFunction &MF = DAG.getMachineFunction();
2630 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002631 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002632 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002633 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002634 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002635 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002636 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2637 // Number smaller than 12 so just add the difference.
2638 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2639 } else {
2640 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002641 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002642 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002643 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002644 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002645}
2646
Evan Cheng5f941932010-02-05 02:21:12 +00002647/// MatchingStackOffset - Return true if the given stack call argument is
2648/// already available in the same position (relatively) of the caller's
2649/// incoming argument stack.
2650static
2651bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2652 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2653 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002654 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2655 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002656 if (Arg.getOpcode() == ISD::CopyFromReg) {
2657 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002658 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002659 return false;
2660 MachineInstr *Def = MRI->getVRegDef(VR);
2661 if (!Def)
2662 return false;
2663 if (!Flags.isByVal()) {
2664 if (!TII->isLoadFromStackSlot(Def, FI))
2665 return false;
2666 } else {
2667 unsigned Opcode = Def->getOpcode();
2668 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2669 Def->getOperand(1).isFI()) {
2670 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002671 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002672 } else
2673 return false;
2674 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002675 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2676 if (Flags.isByVal())
2677 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002678 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002679 // define @foo(%struct.X* %A) {
2680 // tail call @bar(%struct.X* byval %A)
2681 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002682 return false;
2683 SDValue Ptr = Ld->getBasePtr();
2684 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2685 if (!FINode)
2686 return false;
2687 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002688 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002689 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002690 FI = FINode->getIndex();
2691 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002692 } else
2693 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002694
Evan Cheng4cae1332010-03-05 08:38:04 +00002695 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002696 if (!MFI->isFixedObjectIndex(FI))
2697 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002698 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002699}
2700
Dan Gohman98ca4f22009-08-05 01:29:28 +00002701/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2702/// for tail call optimization. Targets which want to do tail call
2703/// optimization should implement this function.
2704bool
2705X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002706 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002707 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002708 bool isCalleeStructRet,
2709 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002710 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002711 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002712 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002713 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002714 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002715 CalleeCC != CallingConv::C)
2716 return false;
2717
Evan Cheng7096ae42010-01-29 06:45:59 +00002718 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002719 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002720 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002721 CallingConv::ID CallerCC = CallerF->getCallingConv();
2722 bool CCMatch = CallerCC == CalleeCC;
2723
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002724 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002725 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002726 return true;
2727 return false;
2728 }
2729
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002730 // Look for obvious safe cases to perform tail call optimization that do not
2731 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002732
Evan Cheng2c12cb42010-03-26 16:26:03 +00002733 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2734 // emit a special epilogue.
2735 if (RegInfo->needsStackRealignment(MF))
2736 return false;
2737
Evan Chenga375d472010-03-15 18:54:48 +00002738 // Also avoid sibcall optimization if either caller or callee uses struct
2739 // return semantics.
2740 if (isCalleeStructRet || isCallerStructRet)
2741 return false;
2742
Chad Rosier2416da32011-06-24 21:15:36 +00002743 // An stdcall caller is expected to clean up its arguments; the callee
2744 // isn't going to do that.
2745 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2746 return false;
2747
Chad Rosier871f6642011-05-18 19:59:50 +00002748 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002749 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002750 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002751
2752 // Optimizing for varargs on Win64 is unlikely to be safe without
2753 // additional testing.
2754 if (Subtarget->isTargetWin64())
2755 return false;
2756
Chad Rosier871f6642011-05-18 19:59:50 +00002757 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002758 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002759 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002760
Chad Rosier871f6642011-05-18 19:59:50 +00002761 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2762 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2763 if (!ArgLocs[i].isRegLoc())
2764 return false;
2765 }
2766
Chad Rosier30450e82011-12-22 22:35:21 +00002767 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2768 // stack. Therefore, if it's not used by the call it is not safe to optimize
2769 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002770 bool Unused = false;
2771 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2772 if (!Ins[i].Used) {
2773 Unused = true;
2774 break;
2775 }
2776 }
2777 if (Unused) {
2778 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002779 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002780 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002781 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002782 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002783 CCValAssign &VA = RVLocs[i];
2784 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2785 return false;
2786 }
2787 }
2788
Evan Cheng13617962010-04-30 01:12:32 +00002789 // If the calling conventions do not match, then we'd better make sure the
2790 // results are returned in the same way as what the caller expects.
2791 if (!CCMatch) {
2792 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002793 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002794 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002795 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2796
2797 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002798 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002799 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002800 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2801
2802 if (RVLocs1.size() != RVLocs2.size())
2803 return false;
2804 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2805 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2806 return false;
2807 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2808 return false;
2809 if (RVLocs1[i].isRegLoc()) {
2810 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2811 return false;
2812 } else {
2813 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2814 return false;
2815 }
2816 }
2817 }
2818
Evan Chenga6bff982010-01-30 01:22:00 +00002819 // If the callee takes no arguments then go on to check the results of the
2820 // call.
2821 if (!Outs.empty()) {
2822 // Check if stack adjustment is needed. For now, do not do this if any
2823 // argument is passed on the stack.
2824 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002825 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002826 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002827
2828 // Allocate shadow area for Win64
2829 if (Subtarget->isTargetWin64()) {
2830 CCInfo.AllocateStack(32, 8);
2831 }
2832
Duncan Sands45907662010-10-31 13:21:44 +00002833 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002834 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002835 MachineFunction &MF = DAG.getMachineFunction();
2836 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2837 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002838
2839 // Check if the arguments are already laid out in the right way as
2840 // the caller's fixed stack objects.
2841 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002842 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2843 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002844 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002845 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2846 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002847 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002848 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002849 if (VA.getLocInfo() == CCValAssign::Indirect)
2850 return false;
2851 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002852 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2853 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002854 return false;
2855 }
2856 }
2857 }
Evan Cheng9c044672010-05-29 01:35:22 +00002858
2859 // If the tailcall address may be in a register, then make sure it's
2860 // possible to register allocate for it. In 32-bit, the call address can
2861 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002862 // callee-saved registers are restored. These happen to be the same
2863 // registers used to pass 'inreg' arguments so watch out for those.
2864 if (!Subtarget->is64Bit() &&
2865 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002866 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002867 unsigned NumInRegs = 0;
2868 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2869 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002870 if (!VA.isRegLoc())
2871 continue;
2872 unsigned Reg = VA.getLocReg();
2873 switch (Reg) {
2874 default: break;
2875 case X86::EAX: case X86::EDX: case X86::ECX:
2876 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002877 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002878 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002879 }
2880 }
2881 }
Evan Chenga6bff982010-01-30 01:22:00 +00002882 }
Evan Chengb1712452010-01-27 06:25:16 +00002883
Evan Cheng86809cc2010-02-03 03:28:02 +00002884 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002885}
2886
Dan Gohman3df24e62008-09-03 23:12:08 +00002887FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002888X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2889 const TargetLibraryInfo *libInfo) const {
2890 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002891}
2892
2893
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002894//===----------------------------------------------------------------------===//
2895// Other Lowering Hooks
2896//===----------------------------------------------------------------------===//
2897
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002898static bool MayFoldLoad(SDValue Op) {
2899 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2900}
2901
2902static bool MayFoldIntoStore(SDValue Op) {
2903 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2904}
2905
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002906static bool isTargetShuffle(unsigned Opcode) {
2907 switch(Opcode) {
2908 default: return false;
2909 case X86ISD::PSHUFD:
2910 case X86ISD::PSHUFHW:
2911 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002912 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002913 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002914 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002915 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002916 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002917 case X86ISD::MOVLPS:
2918 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002919 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002920 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002921 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002922 case X86ISD::MOVSS:
2923 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002924 case X86ISD::UNPCKL:
2925 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002926 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002927 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002928 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002929 return true;
2930 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002931}
2932
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002933static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002934 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002935 switch(Opc) {
2936 default: llvm_unreachable("Unknown x86 shuffle node");
2937 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002938 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002939 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002940 return DAG.getNode(Opc, dl, VT, V1);
2941 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002942}
2943
2944static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002945 SDValue V1, unsigned TargetMask,
2946 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002947 switch(Opc) {
2948 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002949 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002950 case X86ISD::PSHUFHW:
2951 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002952 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002953 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002954 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2955 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002956}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002957
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002958static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002959 SDValue V1, SDValue V2, unsigned TargetMask,
2960 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002961 switch(Opc) {
2962 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002963 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002964 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002965 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002966 return DAG.getNode(Opc, dl, VT, V1, V2,
2967 DAG.getConstant(TargetMask, MVT::i8));
2968 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002969}
2970
2971static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2972 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2973 switch(Opc) {
2974 default: llvm_unreachable("Unknown x86 shuffle node");
2975 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002976 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002977 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002978 case X86ISD::MOVLPS:
2979 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002980 case X86ISD::MOVSS:
2981 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002982 case X86ISD::UNPCKL:
2983 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002984 return DAG.getNode(Opc, dl, VT, V1, V2);
2985 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002986}
2987
Dan Gohmand858e902010-04-17 15:26:15 +00002988SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002989 MachineFunction &MF = DAG.getMachineFunction();
2990 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2991 int ReturnAddrIndex = FuncInfo->getRAIndex();
2992
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002993 if (ReturnAddrIndex == 0) {
2994 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002995 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002996 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002997 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002998 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002999 }
3000
Evan Cheng25ab6902006-09-08 06:48:29 +00003001 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003002}
3003
3004
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003005bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3006 bool hasSymbolicDisplacement) {
3007 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003008 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003009 return false;
3010
3011 // If we don't have a symbolic displacement - we don't have any extra
3012 // restrictions.
3013 if (!hasSymbolicDisplacement)
3014 return true;
3015
3016 // FIXME: Some tweaks might be needed for medium code model.
3017 if (M != CodeModel::Small && M != CodeModel::Kernel)
3018 return false;
3019
3020 // For small code model we assume that latest object is 16MB before end of 31
3021 // bits boundary. We may also accept pretty large negative constants knowing
3022 // that all objects are in the positive half of address space.
3023 if (M == CodeModel::Small && Offset < 16*1024*1024)
3024 return true;
3025
3026 // For kernel code model we know that all object resist in the negative half
3027 // of 32bits address space. We may not accept negative offsets, since they may
3028 // be just off and we may accept pretty large positive ones.
3029 if (M == CodeModel::Kernel && Offset > 0)
3030 return true;
3031
3032 return false;
3033}
3034
Evan Chengef41ff62011-06-23 17:54:54 +00003035/// isCalleePop - Determines whether the callee is required to pop its
3036/// own arguments. Callee pop is necessary to support tail calls.
3037bool X86::isCalleePop(CallingConv::ID CallingConv,
3038 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3039 if (IsVarArg)
3040 return false;
3041
3042 switch (CallingConv) {
3043 default:
3044 return false;
3045 case CallingConv::X86_StdCall:
3046 return !is64Bit;
3047 case CallingConv::X86_FastCall:
3048 return !is64Bit;
3049 case CallingConv::X86_ThisCall:
3050 return !is64Bit;
3051 case CallingConv::Fast:
3052 return TailCallOpt;
3053 case CallingConv::GHC:
3054 return TailCallOpt;
3055 }
3056}
3057
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003058/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3059/// specific condition code, returning the condition code and the LHS/RHS of the
3060/// comparison to make.
3061static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3062 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003063 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003064 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3065 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3066 // X > -1 -> X == 0, jump !sign.
3067 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003068 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003069 }
3070 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003071 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003072 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003073 }
3074 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003075 // X < 1 -> X <= 0
3076 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003077 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003078 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003079 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003080
Evan Chengd9558e02006-01-06 00:43:03 +00003081 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003082 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003083 case ISD::SETEQ: return X86::COND_E;
3084 case ISD::SETGT: return X86::COND_G;
3085 case ISD::SETGE: return X86::COND_GE;
3086 case ISD::SETLT: return X86::COND_L;
3087 case ISD::SETLE: return X86::COND_LE;
3088 case ISD::SETNE: return X86::COND_NE;
3089 case ISD::SETULT: return X86::COND_B;
3090 case ISD::SETUGT: return X86::COND_A;
3091 case ISD::SETULE: return X86::COND_BE;
3092 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003093 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003094 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003095
Chris Lattner4c78e022008-12-23 23:42:27 +00003096 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003097
Chris Lattner4c78e022008-12-23 23:42:27 +00003098 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003099 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3100 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003101 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3102 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003103 }
3104
Chris Lattner4c78e022008-12-23 23:42:27 +00003105 switch (SetCCOpcode) {
3106 default: break;
3107 case ISD::SETOLT:
3108 case ISD::SETOLE:
3109 case ISD::SETUGT:
3110 case ISD::SETUGE:
3111 std::swap(LHS, RHS);
3112 break;
3113 }
3114
3115 // On a floating point condition, the flags are set as follows:
3116 // ZF PF CF op
3117 // 0 | 0 | 0 | X > Y
3118 // 0 | 0 | 1 | X < Y
3119 // 1 | 0 | 0 | X == Y
3120 // 1 | 1 | 1 | unordered
3121 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003122 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003123 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003124 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003125 case ISD::SETOLT: // flipped
3126 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003127 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003128 case ISD::SETOLE: // flipped
3129 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003130 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003131 case ISD::SETUGT: // flipped
3132 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003133 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003134 case ISD::SETUGE: // flipped
3135 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003136 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003137 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003138 case ISD::SETNE: return X86::COND_NE;
3139 case ISD::SETUO: return X86::COND_P;
3140 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003141 case ISD::SETOEQ:
3142 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003143 }
Evan Chengd9558e02006-01-06 00:43:03 +00003144}
3145
Evan Cheng4a460802006-01-11 00:33:36 +00003146/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3147/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003148/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003149static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003150 switch (X86CC) {
3151 default:
3152 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003153 case X86::COND_B:
3154 case X86::COND_BE:
3155 case X86::COND_E:
3156 case X86::COND_P:
3157 case X86::COND_A:
3158 case X86::COND_AE:
3159 case X86::COND_NE:
3160 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003161 return true;
3162 }
3163}
3164
Evan Chengeb2f9692009-10-27 19:56:55 +00003165/// isFPImmLegal - Returns true if the target can instruction select the
3166/// specified FP immediate natively. If false, the legalizer will
3167/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003168bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003169 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3170 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3171 return true;
3172 }
3173 return false;
3174}
3175
Nate Begeman9008ca62009-04-27 18:41:29 +00003176/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3177/// the specified range (L, H].
3178static bool isUndefOrInRange(int Val, int Low, int Hi) {
3179 return (Val < 0) || (Val >= Low && Val < Hi);
3180}
3181
3182/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3183/// specified value.
3184static bool isUndefOrEqual(int Val, int CmpVal) {
3185 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003186 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003188}
3189
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003190/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003191/// from position Pos and ending in Pos+Size, falls within the specified
3192/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003193static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003194 unsigned Pos, unsigned Size, int Low) {
3195 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003196 if (!isUndefOrEqual(Mask[i], Low))
3197 return false;
3198 return true;
3199}
3200
Nate Begeman9008ca62009-04-27 18:41:29 +00003201/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3202/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3203/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003204static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003205 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003206 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003207 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003208 return (Mask[0] < 2 && Mask[1] < 2);
3209 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003210}
3211
Nate Begeman9008ca62009-04-27 18:41:29 +00003212/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3213/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003214static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3215 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003216 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003217
Nate Begeman9008ca62009-04-27 18:41:29 +00003218 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003219 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3220 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003221
Evan Cheng506d3df2006-03-29 23:07:14 +00003222 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003223 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003224 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003225 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003226
Craig Toppera9a568a2012-05-02 08:03:44 +00003227 if (VT == MVT::v16i16) {
3228 // Lower quadword copied in order or undef.
3229 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3230 return false;
3231
3232 // Upper quadword shuffled.
3233 for (unsigned i = 12; i != 16; ++i)
3234 if (!isUndefOrInRange(Mask[i], 12, 16))
3235 return false;
3236 }
3237
Evan Cheng506d3df2006-03-29 23:07:14 +00003238 return true;
3239}
3240
Nate Begeman9008ca62009-04-27 18:41:29 +00003241/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3242/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003243static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3244 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003245 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003246
Rafael Espindola15684b22009-04-24 12:40:33 +00003247 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003248 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3249 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003250
Rafael Espindola15684b22009-04-24 12:40:33 +00003251 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003252 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003253 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003254 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003255
Craig Toppera9a568a2012-05-02 08:03:44 +00003256 if (VT == MVT::v16i16) {
3257 // Upper quadword copied in order.
3258 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3259 return false;
3260
3261 // Lower quadword shuffled.
3262 for (unsigned i = 8; i != 12; ++i)
3263 if (!isUndefOrInRange(Mask[i], 8, 12))
3264 return false;
3265 }
3266
Rafael Espindola15684b22009-04-24 12:40:33 +00003267 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003268}
3269
Nate Begemana09008b2009-10-19 02:17:23 +00003270/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3271/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003272static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3273 const X86Subtarget *Subtarget) {
3274 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3275 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003276 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003277
Craig Topper0e2037b2012-01-20 05:53:00 +00003278 unsigned NumElts = VT.getVectorNumElements();
3279 unsigned NumLanes = VT.getSizeInBits()/128;
3280 unsigned NumLaneElts = NumElts/NumLanes;
3281
3282 // Do not handle 64-bit element shuffles with palignr.
3283 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003284 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003285
Craig Topper0e2037b2012-01-20 05:53:00 +00003286 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3287 unsigned i;
3288 for (i = 0; i != NumLaneElts; ++i) {
3289 if (Mask[i+l] >= 0)
3290 break;
3291 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003292
Craig Topper0e2037b2012-01-20 05:53:00 +00003293 // Lane is all undef, go to next lane
3294 if (i == NumLaneElts)
3295 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003296
Craig Topper0e2037b2012-01-20 05:53:00 +00003297 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003298
Craig Topper0e2037b2012-01-20 05:53:00 +00003299 // Make sure its in this lane in one of the sources
3300 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3301 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003302 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003303
3304 // If not lane 0, then we must match lane 0
3305 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3306 return false;
3307
3308 // Correct second source to be contiguous with first source
3309 if (Start >= (int)NumElts)
3310 Start -= NumElts - NumLaneElts;
3311
3312 // Make sure we're shifting in the right direction.
3313 if (Start <= (int)(i+l))
3314 return false;
3315
3316 Start -= i;
3317
3318 // Check the rest of the elements to see if they are consecutive.
3319 for (++i; i != NumLaneElts; ++i) {
3320 int Idx = Mask[i+l];
3321
3322 // Make sure its in this lane
3323 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3324 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3325 return false;
3326
3327 // If not lane 0, then we must match lane 0
3328 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3329 return false;
3330
3331 if (Idx >= (int)NumElts)
3332 Idx -= NumElts - NumLaneElts;
3333
3334 if (!isUndefOrEqual(Idx, Start+i))
3335 return false;
3336
3337 }
Nate Begemana09008b2009-10-19 02:17:23 +00003338 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003339
Nate Begemana09008b2009-10-19 02:17:23 +00003340 return true;
3341}
3342
Craig Topper1a7700a2012-01-19 08:19:12 +00003343/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3344/// the two vector operands have swapped position.
3345static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3346 unsigned NumElems) {
3347 for (unsigned i = 0; i != NumElems; ++i) {
3348 int idx = Mask[i];
3349 if (idx < 0)
3350 continue;
3351 else if (idx < (int)NumElems)
3352 Mask[i] = idx + NumElems;
3353 else
3354 Mask[i] = idx - NumElems;
3355 }
3356}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003357
Craig Topper1a7700a2012-01-19 08:19:12 +00003358/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3359/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3360/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3361/// reverse of what x86 shuffles want.
3362static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3363 bool Commuted = false) {
3364 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003365 return false;
3366
Craig Topper1a7700a2012-01-19 08:19:12 +00003367 unsigned NumElems = VT.getVectorNumElements();
3368 unsigned NumLanes = VT.getSizeInBits()/128;
3369 unsigned NumLaneElems = NumElems/NumLanes;
3370
3371 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003372 return false;
3373
3374 // VSHUFPSY divides the resulting vector into 4 chunks.
3375 // The sources are also splitted into 4 chunks, and each destination
3376 // chunk must come from a different source chunk.
3377 //
3378 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3379 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3380 //
3381 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3382 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3383 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003384 // VSHUFPDY divides the resulting vector into 4 chunks.
3385 // The sources are also splitted into 4 chunks, and each destination
3386 // chunk must come from a different source chunk.
3387 //
3388 // SRC1 => X3 X2 X1 X0
3389 // SRC2 => Y3 Y2 Y1 Y0
3390 //
3391 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3392 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003393 unsigned HalfLaneElems = NumLaneElems/2;
3394 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3395 for (unsigned i = 0; i != NumLaneElems; ++i) {
3396 int Idx = Mask[i+l];
3397 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3398 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3399 return false;
3400 // For VSHUFPSY, the mask of the second half must be the same as the
3401 // first but with the appropriate offsets. This works in the same way as
3402 // VPERMILPS works with masks.
3403 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3404 continue;
3405 if (!isUndefOrEqual(Idx, Mask[i]+l))
3406 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003407 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003408 }
3409
3410 return true;
3411}
3412
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003413/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3414/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003415static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003416 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003417 return false;
3418
Craig Topper7a9a28b2012-08-12 02:23:29 +00003419 unsigned NumElems = VT.getVectorNumElements();
3420
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003421 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003422 return false;
3423
Evan Cheng2064a2b2006-03-28 06:50:32 +00003424 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003425 return isUndefOrEqual(Mask[0], 6) &&
3426 isUndefOrEqual(Mask[1], 7) &&
3427 isUndefOrEqual(Mask[2], 2) &&
3428 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003429}
3430
Nate Begeman0b10b912009-11-07 23:17:15 +00003431/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3432/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3433/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003434static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003435 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003436 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003437
Craig Topper7a9a28b2012-08-12 02:23:29 +00003438 unsigned NumElems = VT.getVectorNumElements();
3439
Nate Begeman0b10b912009-11-07 23:17:15 +00003440 if (NumElems != 4)
3441 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003442
Craig Topperdd637ae2012-02-19 05:41:45 +00003443 return isUndefOrEqual(Mask[0], 2) &&
3444 isUndefOrEqual(Mask[1], 3) &&
3445 isUndefOrEqual(Mask[2], 2) &&
3446 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003447}
3448
Evan Cheng5ced1d82006-04-06 23:23:56 +00003449/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3450/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003451static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003452 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003453 return false;
3454
Craig Topperdd637ae2012-02-19 05:41:45 +00003455 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003456
Evan Cheng5ced1d82006-04-06 23:23:56 +00003457 if (NumElems != 2 && NumElems != 4)
3458 return false;
3459
Chad Rosier238ae312012-04-30 17:47:15 +00003460 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003461 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003462 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003463
Chad Rosier238ae312012-04-30 17:47:15 +00003464 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003465 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003466 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003467
3468 return true;
3469}
3470
Nate Begeman0b10b912009-11-07 23:17:15 +00003471/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3472/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003473static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003474 if (!VT.is128BitVector())
3475 return false;
3476
Craig Topperdd637ae2012-02-19 05:41:45 +00003477 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003478
Craig Topper7a9a28b2012-08-12 02:23:29 +00003479 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003480 return false;
3481
Chad Rosier238ae312012-04-30 17:47:15 +00003482 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003483 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003484 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003485
Chad Rosier238ae312012-04-30 17:47:15 +00003486 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3487 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003488 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003489
3490 return true;
3491}
3492
Elena Demikhovsky15963732012-06-26 08:04:10 +00003493//
3494// Some special combinations that can be optimized.
3495//
3496static
3497SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3498 SelectionDAG &DAG) {
3499 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003500 DebugLoc dl = SVOp->getDebugLoc();
3501
3502 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3503 return SDValue();
3504
3505 ArrayRef<int> Mask = SVOp->getMask();
3506
3507 // These are the special masks that may be optimized.
3508 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3509 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3510 bool MatchEvenMask = true;
3511 bool MatchOddMask = true;
3512 for (int i=0; i<8; ++i) {
3513 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3514 MatchEvenMask = false;
3515 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3516 MatchOddMask = false;
3517 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003518
Elena Demikhovsky32510202012-09-04 12:49:02 +00003519 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003520 return SDValue();
Elena Demikhovsky32510202012-09-04 12:49:02 +00003521
Elena Demikhovsky15963732012-06-26 08:04:10 +00003522 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3523
Elena Demikhovsky32510202012-09-04 12:49:02 +00003524 SDValue Op0 = SVOp->getOperand(0);
3525 SDValue Op1 = SVOp->getOperand(1);
3526
3527 if (MatchEvenMask) {
3528 // Shift the second operand right to 32 bits.
3529 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3530 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3531 } else {
3532 // Shift the first operand left to 32 bits.
3533 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3534 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3535 }
3536 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3537 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003538}
3539
Evan Cheng0038e592006-03-28 00:39:58 +00003540/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3541/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003542static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003543 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003544 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003545
3546 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3547 "Unsupported vector type for unpckh");
3548
Craig Topper6347e862011-11-21 06:57:39 +00003549 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003550 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003551 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003552
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003553 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3554 // independently on 128-bit lanes.
3555 unsigned NumLanes = VT.getSizeInBits()/128;
3556 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003557
Craig Topper94438ba2011-12-16 08:06:31 +00003558 for (unsigned l = 0; l != NumLanes; ++l) {
3559 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3560 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003561 i += 2, ++j) {
3562 int BitI = Mask[i];
3563 int BitI1 = Mask[i+1];
3564 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003565 return false;
David Greenea20244d2011-03-02 17:23:43 +00003566 if (V2IsSplat) {
3567 if (!isUndefOrEqual(BitI1, NumElts))
3568 return false;
3569 } else {
3570 if (!isUndefOrEqual(BitI1, j + NumElts))
3571 return false;
3572 }
Evan Cheng39623da2006-04-20 08:58:49 +00003573 }
Evan Cheng0038e592006-03-28 00:39:58 +00003574 }
David Greenea20244d2011-03-02 17:23:43 +00003575
Evan Cheng0038e592006-03-28 00:39:58 +00003576 return true;
3577}
3578
Evan Cheng4fcb9222006-03-28 02:43:26 +00003579/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3580/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003581static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003582 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003583 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003584
3585 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3586 "Unsupported vector type for unpckh");
3587
Craig Topper6347e862011-11-21 06:57:39 +00003588 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003589 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003590 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003591
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003592 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3593 // independently on 128-bit lanes.
3594 unsigned NumLanes = VT.getSizeInBits()/128;
3595 unsigned NumLaneElts = NumElts/NumLanes;
3596
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003597 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003598 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3599 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003600 int BitI = Mask[i];
3601 int BitI1 = Mask[i+1];
3602 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003603 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003604 if (V2IsSplat) {
3605 if (isUndefOrEqual(BitI1, NumElts))
3606 return false;
3607 } else {
3608 if (!isUndefOrEqual(BitI1, j+NumElts))
3609 return false;
3610 }
Evan Cheng39623da2006-04-20 08:58:49 +00003611 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003612 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003613 return true;
3614}
3615
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003616/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3617/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3618/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003619static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003620 bool HasAVX2) {
3621 unsigned NumElts = VT.getVectorNumElements();
3622
3623 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3624 "Unsupported vector type for unpckh");
3625
3626 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3627 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003628 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003629
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003630 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3631 // FIXME: Need a better way to get rid of this, there's no latency difference
3632 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3633 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003634 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003635 return false;
3636
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003637 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3638 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003639 unsigned NumLanes = VT.getSizeInBits()/128;
3640 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003641
Craig Topper94438ba2011-12-16 08:06:31 +00003642 for (unsigned l = 0; l != NumLanes; ++l) {
3643 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3644 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003645 i += 2, ++j) {
3646 int BitI = Mask[i];
3647 int BitI1 = Mask[i+1];
3648
3649 if (!isUndefOrEqual(BitI, j))
3650 return false;
3651 if (!isUndefOrEqual(BitI1, j))
3652 return false;
3653 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003654 }
David Greenea20244d2011-03-02 17:23:43 +00003655
Rafael Espindola15684b22009-04-24 12:40:33 +00003656 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003657}
3658
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003659/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3660/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3661/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003662static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003663 unsigned NumElts = VT.getVectorNumElements();
3664
3665 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3666 "Unsupported vector type for unpckh");
3667
3668 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3669 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003670 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003671
Craig Topper94438ba2011-12-16 08:06:31 +00003672 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3673 // independently on 128-bit lanes.
3674 unsigned NumLanes = VT.getSizeInBits()/128;
3675 unsigned NumLaneElts = NumElts/NumLanes;
3676
3677 for (unsigned l = 0; l != NumLanes; ++l) {
3678 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3679 i != (l+1)*NumLaneElts; i += 2, ++j) {
3680 int BitI = Mask[i];
3681 int BitI1 = Mask[i+1];
3682 if (!isUndefOrEqual(BitI, j))
3683 return false;
3684 if (!isUndefOrEqual(BitI1, j))
3685 return false;
3686 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003687 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003688 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003689}
3690
Evan Cheng017dcc62006-04-21 01:05:10 +00003691/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3692/// specifies a shuffle of elements that is suitable for input to MOVSS,
3693/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003694static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003695 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003696 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003697 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003698 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003699
Craig Topperc612d792012-01-02 09:17:37 +00003700 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003701
Nate Begeman9008ca62009-04-27 18:41:29 +00003702 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003703 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003704
Craig Topperc612d792012-01-02 09:17:37 +00003705 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003706 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003707 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003708
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003709 return true;
3710}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003711
Craig Topper70b883b2011-11-28 10:14:51 +00003712/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003713/// as permutations between 128-bit chunks or halves. As an example: this
3714/// shuffle bellow:
3715/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3716/// The first half comes from the second half of V1 and the second half from the
3717/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003718static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003719 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003720 return false;
3721
3722 // The shuffle result is divided into half A and half B. In total the two
3723 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3724 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003725 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003726 bool MatchA = false, MatchB = false;
3727
3728 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003729 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003730 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3731 MatchA = true;
3732 break;
3733 }
3734 }
3735
3736 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003737 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003738 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3739 MatchB = true;
3740 break;
3741 }
3742 }
3743
3744 return MatchA && MatchB;
3745}
3746
Craig Topper70b883b2011-11-28 10:14:51 +00003747/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3748/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003749static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003750 EVT VT = SVOp->getValueType(0);
3751
Craig Topperc612d792012-01-02 09:17:37 +00003752 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003753
Craig Topperc612d792012-01-02 09:17:37 +00003754 unsigned FstHalf = 0, SndHalf = 0;
3755 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003756 if (SVOp->getMaskElt(i) > 0) {
3757 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3758 break;
3759 }
3760 }
Craig Topperc612d792012-01-02 09:17:37 +00003761 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003762 if (SVOp->getMaskElt(i) > 0) {
3763 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3764 break;
3765 }
3766 }
3767
3768 return (FstHalf | (SndHalf << 4));
3769}
3770
Craig Topper70b883b2011-11-28 10:14:51 +00003771/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003772/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3773/// Note that VPERMIL mask matching is different depending whether theunderlying
3774/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3775/// to the same elements of the low, but to the higher half of the source.
3776/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003777/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003778static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003779 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003780 return false;
3781
Craig Topperc612d792012-01-02 09:17:37 +00003782 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003783 // Only match 256-bit with 32/64-bit types
3784 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003785 return false;
3786
Craig Topperc612d792012-01-02 09:17:37 +00003787 unsigned NumLanes = VT.getSizeInBits()/128;
3788 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003789 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003790 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003791 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003792 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003793 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003794 continue;
3795 // VPERMILPS handling
3796 if (Mask[i] < 0)
3797 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003798 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003799 return false;
3800 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003801 }
3802
3803 return true;
3804}
3805
Craig Topper5aaffa82012-02-19 02:53:47 +00003806/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003807/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003808/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003809static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003810 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003811 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003812 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003813
3814 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003815 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003816 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003817
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003819 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003820
Craig Topperc612d792012-01-02 09:17:37 +00003821 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003822 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3823 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3824 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003825 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003826
Evan Cheng39623da2006-04-20 08:58:49 +00003827 return true;
3828}
3829
Evan Chengd9539472006-04-14 21:59:03 +00003830/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3831/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003832/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003833static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003834 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003835 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003836 return false;
3837
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003838 unsigned NumElems = VT.getVectorNumElements();
3839
3840 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3841 (VT.getSizeInBits() == 256 && NumElems != 8))
3842 return false;
3843
3844 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003845 for (unsigned i = 0; i != NumElems; i += 2)
3846 if (!isUndefOrEqual(Mask[i], i+1) ||
3847 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003849
3850 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003851}
3852
3853/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3854/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003855/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003856static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003857 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003858 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003859 return false;
3860
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003861 unsigned NumElems = VT.getVectorNumElements();
3862
3863 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3864 (VT.getSizeInBits() == 256 && NumElems != 8))
3865 return false;
3866
3867 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003868 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003869 if (!isUndefOrEqual(Mask[i], i) ||
3870 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003872
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003873 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003874}
3875
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003876/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3877/// specifies a shuffle of elements that is suitable for input to 256-bit
3878/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003879static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003880 if (!HasAVX || !VT.is256BitVector())
3881 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003882
Craig Topper7a9a28b2012-08-12 02:23:29 +00003883 unsigned NumElts = VT.getVectorNumElements();
3884 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003885 return false;
3886
Craig Topperc612d792012-01-02 09:17:37 +00003887 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003888 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003889 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003890 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003891 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003892 return false;
3893 return true;
3894}
3895
Evan Cheng0b457f02008-09-25 20:50:48 +00003896/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003897/// specifies a shuffle of elements that is suitable for input to 128-bit
3898/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003899static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003900 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003901 return false;
3902
Craig Topperc612d792012-01-02 09:17:37 +00003903 unsigned e = VT.getVectorNumElements() / 2;
3904 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003905 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003906 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003907 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003908 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003909 return false;
3910 return true;
3911}
3912
David Greenec38a03e2011-02-03 15:50:00 +00003913/// isVEXTRACTF128Index - Return true if the specified
3914/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3915/// suitable for input to VEXTRACTF128.
3916bool X86::isVEXTRACTF128Index(SDNode *N) {
3917 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3918 return false;
3919
3920 // The index should be aligned on a 128-bit boundary.
3921 uint64_t Index =
3922 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3923
3924 unsigned VL = N->getValueType(0).getVectorNumElements();
3925 unsigned VBits = N->getValueType(0).getSizeInBits();
3926 unsigned ElSize = VBits / VL;
3927 bool Result = (Index * ElSize) % 128 == 0;
3928
3929 return Result;
3930}
3931
David Greeneccacdc12011-02-04 16:08:29 +00003932/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3933/// operand specifies a subvector insert that is suitable for input to
3934/// VINSERTF128.
3935bool X86::isVINSERTF128Index(SDNode *N) {
3936 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3937 return false;
3938
3939 // The index should be aligned on a 128-bit boundary.
3940 uint64_t Index =
3941 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3942
3943 unsigned VL = N->getValueType(0).getVectorNumElements();
3944 unsigned VBits = N->getValueType(0).getSizeInBits();
3945 unsigned ElSize = VBits / VL;
3946 bool Result = (Index * ElSize) % 128 == 0;
3947
3948 return Result;
3949}
3950
Evan Cheng63d33002006-03-22 08:01:21 +00003951/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003952/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003953/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003954static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003955 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003956
Craig Topper1a7700a2012-01-19 08:19:12 +00003957 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3958 "Unsupported vector type for PSHUF/SHUFP");
3959
3960 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3961 // independently on 128-bit lanes.
3962 unsigned NumElts = VT.getVectorNumElements();
3963 unsigned NumLanes = VT.getSizeInBits()/128;
3964 unsigned NumLaneElts = NumElts/NumLanes;
3965
3966 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3967 "Only supports 2 or 4 elements per lane");
3968
3969 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003970 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003971 for (unsigned i = 0; i != NumElts; ++i) {
3972 int Elt = N->getMaskElt(i);
3973 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003974 Elt &= NumLaneElts - 1;
3975 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003976 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003977 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003978
Evan Cheng63d33002006-03-22 08:01:21 +00003979 return Mask;
3980}
3981
Evan Cheng506d3df2006-03-29 23:07:14 +00003982/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003983/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00003984static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00003985 EVT VT = N->getValueType(0);
3986
3987 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
3988 "Unsupported vector type for PSHUFHW");
3989
3990 unsigned NumElts = VT.getVectorNumElements();
3991
Evan Cheng506d3df2006-03-29 23:07:14 +00003992 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00003993 for (unsigned l = 0; l != NumElts; l += 8) {
3994 // 8 nodes per lane, but we only care about the last 4.
3995 for (unsigned i = 0; i < 4; ++i) {
3996 int Elt = N->getMaskElt(l+i+4);
3997 if (Elt < 0) continue;
3998 Elt &= 0x3; // only 2-bits.
3999 Mask |= Elt << (i * 2);
4000 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004001 }
Craig Topper6b28d352012-05-03 07:12:59 +00004002
Evan Cheng506d3df2006-03-29 23:07:14 +00004003 return Mask;
4004}
4005
4006/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004007/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004008static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004009 EVT VT = N->getValueType(0);
4010
4011 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4012 "Unsupported vector type for PSHUFHW");
4013
4014 unsigned NumElts = VT.getVectorNumElements();
4015
Evan Cheng506d3df2006-03-29 23:07:14 +00004016 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004017 for (unsigned l = 0; l != NumElts; l += 8) {
4018 // 8 nodes per lane, but we only care about the first 4.
4019 for (unsigned i = 0; i < 4; ++i) {
4020 int Elt = N->getMaskElt(l+i);
4021 if (Elt < 0) continue;
4022 Elt &= 0x3; // only 2-bits
4023 Mask |= Elt << (i * 2);
4024 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004025 }
Craig Topper6b28d352012-05-03 07:12:59 +00004026
Evan Cheng506d3df2006-03-29 23:07:14 +00004027 return Mask;
4028}
4029
Nate Begemana09008b2009-10-19 02:17:23 +00004030/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4031/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004032static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4033 EVT VT = SVOp->getValueType(0);
4034 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004035
Craig Topper0e2037b2012-01-20 05:53:00 +00004036 unsigned NumElts = VT.getVectorNumElements();
4037 unsigned NumLanes = VT.getSizeInBits()/128;
4038 unsigned NumLaneElts = NumElts/NumLanes;
4039
4040 int Val = 0;
4041 unsigned i;
4042 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004043 Val = SVOp->getMaskElt(i);
4044 if (Val >= 0)
4045 break;
4046 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004047 if (Val >= (int)NumElts)
4048 Val -= NumElts - NumLaneElts;
4049
Eli Friedman63f8dde2011-07-25 21:36:45 +00004050 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004051 return (Val - i) * EltSize;
4052}
4053
David Greenec38a03e2011-02-03 15:50:00 +00004054/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4055/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4056/// instructions.
4057unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4058 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4059 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4060
4061 uint64_t Index =
4062 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4063
4064 EVT VecVT = N->getOperand(0).getValueType();
4065 EVT ElVT = VecVT.getVectorElementType();
4066
4067 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004068 return Index / NumElemsPerChunk;
4069}
4070
David Greeneccacdc12011-02-04 16:08:29 +00004071/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4072/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4073/// instructions.
4074unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4075 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4076 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4077
4078 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004079 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004080
4081 EVT VecVT = N->getValueType(0);
4082 EVT ElVT = VecVT.getVectorElementType();
4083
4084 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004085 return Index / NumElemsPerChunk;
4086}
4087
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004088/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4089/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4090/// Handles 256-bit.
4091static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4092 EVT VT = N->getValueType(0);
4093
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004094 unsigned NumElts = VT.getVectorNumElements();
4095
Craig Topper095c5282012-04-15 23:48:57 +00004096 assert((VT.is256BitVector() && NumElts == 4) &&
4097 "Unsupported vector type for VPERMQ/VPERMPD");
4098
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004099 unsigned Mask = 0;
4100 for (unsigned i = 0; i != NumElts; ++i) {
4101 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004102 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004103 continue;
4104 Mask |= Elt << (i*2);
4105 }
4106
4107 return Mask;
4108}
Evan Cheng37b73872009-07-30 08:33:02 +00004109/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4110/// constant +0.0.
4111bool X86::isZeroNode(SDValue Elt) {
4112 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004113 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004114 (isa<ConstantFPSDNode>(Elt) &&
4115 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4116}
4117
Nate Begeman9008ca62009-04-27 18:41:29 +00004118/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4119/// their permute mask.
4120static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4121 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004122 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004123 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004124 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004125
Nate Begeman5a5ca152009-04-29 05:20:52 +00004126 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004127 int Idx = SVOp->getMaskElt(i);
4128 if (Idx >= 0) {
4129 if (Idx < (int)NumElems)
4130 Idx += NumElems;
4131 else
4132 Idx -= NumElems;
4133 }
4134 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004135 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004136 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4137 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004138}
4139
Evan Cheng533a0aa2006-04-19 20:35:22 +00004140/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4141/// match movhlps. The lower half elements should come from upper half of
4142/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004143/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004144static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004145 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004146 return false;
4147 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004148 return false;
4149 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004150 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004151 return false;
4152 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004153 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004154 return false;
4155 return true;
4156}
4157
Evan Cheng5ced1d82006-04-06 23:23:56 +00004158/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004159/// is promoted to a vector. It also returns the LoadSDNode by reference if
4160/// required.
4161static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004162 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4163 return false;
4164 N = N->getOperand(0).getNode();
4165 if (!ISD::isNON_EXTLoad(N))
4166 return false;
4167 if (LD)
4168 *LD = cast<LoadSDNode>(N);
4169 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004170}
4171
Dan Gohman65fd6562011-11-03 21:49:52 +00004172// Test whether the given value is a vector value which will be legalized
4173// into a load.
4174static bool WillBeConstantPoolLoad(SDNode *N) {
4175 if (N->getOpcode() != ISD::BUILD_VECTOR)
4176 return false;
4177
4178 // Check for any non-constant elements.
4179 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4180 switch (N->getOperand(i).getNode()->getOpcode()) {
4181 case ISD::UNDEF:
4182 case ISD::ConstantFP:
4183 case ISD::Constant:
4184 break;
4185 default:
4186 return false;
4187 }
4188
4189 // Vectors of all-zeros and all-ones are materialized with special
4190 // instructions rather than being loaded.
4191 return !ISD::isBuildVectorAllZeros(N) &&
4192 !ISD::isBuildVectorAllOnes(N);
4193}
4194
Evan Cheng533a0aa2006-04-19 20:35:22 +00004195/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4196/// match movlp{s|d}. The lower half elements should come from lower half of
4197/// V1 (and in order), and the upper half elements should come from the upper
4198/// half of V2 (and in order). And since V1 will become the source of the
4199/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004200static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004201 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004202 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004203 return false;
4204
Evan Cheng466685d2006-10-09 20:57:25 +00004205 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004206 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004207 // Is V2 is a vector load, don't do this transformation. We will try to use
4208 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004209 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004210 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004211
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004212 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004213
Evan Cheng533a0aa2006-04-19 20:35:22 +00004214 if (NumElems != 2 && NumElems != 4)
4215 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004216 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004217 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004218 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004219 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004220 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004221 return false;
4222 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004223}
4224
Evan Cheng39623da2006-04-20 08:58:49 +00004225/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4226/// all the same.
4227static bool isSplatVector(SDNode *N) {
4228 if (N->getOpcode() != ISD::BUILD_VECTOR)
4229 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004230
Dan Gohman475871a2008-07-27 21:46:04 +00004231 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004232 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4233 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004234 return false;
4235 return true;
4236}
4237
Evan Cheng213d2cf2007-05-17 18:45:50 +00004238/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004239/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004240/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004241static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004242 SDValue V1 = N->getOperand(0);
4243 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004244 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4245 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004246 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004247 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004248 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004249 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4250 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004251 if (Opc != ISD::BUILD_VECTOR ||
4252 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004253 return false;
4254 } else if (Idx >= 0) {
4255 unsigned Opc = V1.getOpcode();
4256 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4257 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004258 if (Opc != ISD::BUILD_VECTOR ||
4259 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004260 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004261 }
4262 }
4263 return true;
4264}
4265
4266/// getZeroVector - Returns a vector of specified type with all zero elements.
4267///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004268static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004269 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004270 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004271 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004272
Dale Johannesen0488fb62010-09-30 23:57:10 +00004273 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004274 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004275 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004276 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004277 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004278 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4279 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4280 } else { // SSE1
4281 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4282 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4283 }
Craig Topper9d352402012-04-23 07:24:41 +00004284 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004285 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004286 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4287 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4288 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4289 } else {
4290 // 256-bit logic and arithmetic instructions in AVX are all
4291 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4292 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4293 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4294 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4295 }
Craig Topper9d352402012-04-23 07:24:41 +00004296 } else
4297 llvm_unreachable("Unexpected vector type");
4298
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004299 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004300}
4301
Chris Lattner8a594482007-11-25 00:24:49 +00004302/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004303/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4304/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4305/// Then bitcast to their original type, ensuring they get CSE'd.
4306static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4307 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004308 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004309 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004310
Owen Anderson825b72b2009-08-11 20:47:22 +00004311 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004312 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004313 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004314 if (HasAVX2) { // AVX2
4315 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4316 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4317 } else { // AVX
4318 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004319 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004320 }
Craig Topper9d352402012-04-23 07:24:41 +00004321 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004322 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004323 } else
4324 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004325
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004326 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004327}
4328
Evan Cheng39623da2006-04-20 08:58:49 +00004329/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4330/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004331static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004332 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004333 if (Mask[i] > (int)NumElems) {
4334 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004335 }
Evan Cheng39623da2006-04-20 08:58:49 +00004336 }
Evan Cheng39623da2006-04-20 08:58:49 +00004337}
4338
Evan Cheng017dcc62006-04-21 01:05:10 +00004339/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4340/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004341static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004342 SDValue V2) {
4343 unsigned NumElems = VT.getVectorNumElements();
4344 SmallVector<int, 8> Mask;
4345 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004346 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004347 Mask.push_back(i);
4348 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004349}
4350
Nate Begeman9008ca62009-04-27 18:41:29 +00004351/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004352static SDValue getUnpackl(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;
Evan Chengc575ca22006-04-17 20:43:08 +00004356 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004357 Mask.push_back(i);
4358 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004359 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004360 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004361}
4362
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004363/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004364static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 SDValue V2) {
4366 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004367 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004368 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004369 Mask.push_back(i + Half);
4370 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004371 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004372 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004373}
4374
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004375// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004376// a generic shuffle instruction because the target has no such instructions.
4377// Generate shuffles which repeat i16 and i8 several times until they can be
4378// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004379static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004380 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004382 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004383
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 while (NumElems > 4) {
4385 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004386 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004387 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004388 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 EltNo -= NumElems/2;
4390 }
4391 NumElems >>= 1;
4392 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004393 return V;
4394}
Eric Christopherfd179292009-08-27 18:07:15 +00004395
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004396/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4397static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4398 EVT VT = V.getValueType();
4399 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004400 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004401
Craig Topper9d352402012-04-23 07:24:41 +00004402 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004403 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004404 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004405 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4406 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004407 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004408 // To use VPERMILPS to splat scalars, the second half of indicies must
4409 // refer to the higher part, which is a duplication of the lower one,
4410 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004411 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4412 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004413
4414 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4415 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4416 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004417 } else
4418 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004419
4420 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4421}
4422
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004423/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004424static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4425 EVT SrcVT = SV->getValueType(0);
4426 SDValue V1 = SV->getOperand(0);
4427 DebugLoc dl = SV->getDebugLoc();
4428
4429 int EltNo = SV->getSplatIndex();
4430 int NumElems = SrcVT.getVectorNumElements();
4431 unsigned Size = SrcVT.getSizeInBits();
4432
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004433 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4434 "Unknown how to promote splat for type");
4435
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004436 // Extract the 128-bit part containing the splat element and update
4437 // the splat element index when it refers to the higher register.
4438 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004439 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4440 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004441 EltNo -= NumElems/2;
4442 }
4443
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004444 // All i16 and i8 vector types can't be used directly by a generic shuffle
4445 // instruction because the target has no such instruction. Generate shuffles
4446 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004447 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004448 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004449 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004450 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004451
4452 // Recreate the 256-bit vector and place the same 128-bit vector
4453 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004454 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004455 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004456 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004457 }
4458
4459 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004460}
4461
Evan Chengba05f722006-04-21 23:03:30 +00004462/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004463/// vector of zero or undef vector. This produces a shuffle where the low
4464/// element of V2 is swizzled into the zero/undef vector, landing at element
4465/// 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 +00004466static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004467 bool IsZero,
4468 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004469 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004470 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004471 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004472 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 unsigned NumElems = VT.getVectorNumElements();
4474 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004475 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004476 // If this is the insertion idx, put the low elt of V2 here.
4477 MaskVec.push_back(i == Idx ? NumElems : i);
4478 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004479}
4480
Craig Toppera1ffc682012-03-20 06:42:26 +00004481/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4482/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004483/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004484static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004485 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004486 unsigned NumElems = VT.getVectorNumElements();
4487 SDValue ImmN;
4488
Craig Topper89f4e662012-03-20 07:17:59 +00004489 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004490 switch(N->getOpcode()) {
4491 case X86ISD::SHUFP:
4492 ImmN = N->getOperand(N->getNumOperands()-1);
4493 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4494 break;
4495 case X86ISD::UNPCKH:
4496 DecodeUNPCKHMask(VT, Mask);
4497 break;
4498 case X86ISD::UNPCKL:
4499 DecodeUNPCKLMask(VT, Mask);
4500 break;
4501 case X86ISD::MOVHLPS:
4502 DecodeMOVHLPSMask(NumElems, Mask);
4503 break;
4504 case X86ISD::MOVLHPS:
4505 DecodeMOVLHPSMask(NumElems, Mask);
4506 break;
4507 case X86ISD::PSHUFD:
4508 case X86ISD::VPERMILP:
4509 ImmN = N->getOperand(N->getNumOperands()-1);
4510 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004511 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004512 break;
4513 case X86ISD::PSHUFHW:
4514 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004515 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004516 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004517 break;
4518 case X86ISD::PSHUFLW:
4519 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004520 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004521 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004522 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004523 case X86ISD::VPERMI:
4524 ImmN = N->getOperand(N->getNumOperands()-1);
4525 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4526 IsUnary = true;
4527 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004528 case X86ISD::MOVSS:
4529 case X86ISD::MOVSD: {
4530 // The index 0 always comes from the first element of the second source,
4531 // this is why MOVSS and MOVSD are used in the first place. The other
4532 // elements come from the other positions of the first source vector
4533 Mask.push_back(NumElems);
4534 for (unsigned i = 1; i != NumElems; ++i) {
4535 Mask.push_back(i);
4536 }
4537 break;
4538 }
4539 case X86ISD::VPERM2X128:
4540 ImmN = N->getOperand(N->getNumOperands()-1);
4541 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004542 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004543 break;
4544 case X86ISD::MOVDDUP:
4545 case X86ISD::MOVLHPD:
4546 case X86ISD::MOVLPD:
4547 case X86ISD::MOVLPS:
4548 case X86ISD::MOVSHDUP:
4549 case X86ISD::MOVSLDUP:
4550 case X86ISD::PALIGN:
4551 // Not yet implemented
4552 return false;
4553 default: llvm_unreachable("unknown target shuffle node");
4554 }
4555
4556 return true;
4557}
4558
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004559/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4560/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004561static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004562 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004563 if (Depth == 6)
4564 return SDValue(); // Limit search depth.
4565
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004566 SDValue V = SDValue(N, 0);
4567 EVT VT = V.getValueType();
4568 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004569
4570 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4571 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004572 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004573
Craig Topper3d092db2012-03-21 02:14:01 +00004574 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004575 return DAG.getUNDEF(VT.getVectorElementType());
4576
Craig Topperd156dc12012-02-06 07:17:51 +00004577 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004578 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4579 : SV->getOperand(1);
4580 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004581 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004582
4583 // Recurse into target specific vector shuffles to find scalars.
4584 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004585 MVT ShufVT = V.getValueType().getSimpleVT();
4586 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004587 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004588 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004589 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004590
Craig Topperd978c542012-05-06 19:46:21 +00004591 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004592 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004593
Craig Topper3d092db2012-03-21 02:14:01 +00004594 int Elt = ShuffleMask[Index];
4595 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004596 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004597
Craig Topper3d092db2012-03-21 02:14:01 +00004598 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004599 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004600 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004601 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004602 }
4603
4604 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004605 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004606 V = V.getOperand(0);
4607 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004608 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004609
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004610 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004611 return SDValue();
4612 }
4613
4614 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4615 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004616 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004617
4618 if (V.getOpcode() == ISD::BUILD_VECTOR)
4619 return V.getOperand(Index);
4620
4621 return SDValue();
4622}
4623
4624/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4625/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004626/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004627static
Craig Topper3d092db2012-03-21 02:14:01 +00004628unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004629 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004630 unsigned i;
4631 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004632 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004633 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004634 if (!(Elt.getNode() &&
4635 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4636 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004637 }
4638
4639 return i;
4640}
4641
Craig Topper3d092db2012-03-21 02:14:01 +00004642/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4643/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004644/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4645static
Craig Topper3d092db2012-03-21 02:14:01 +00004646bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4647 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4648 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649 bool SeenV1 = false;
4650 bool SeenV2 = false;
4651
Craig Topper3d092db2012-03-21 02:14:01 +00004652 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004653 int Idx = SVOp->getMaskElt(i);
4654 // Ignore undef indicies
4655 if (Idx < 0)
4656 continue;
4657
Craig Topper3d092db2012-03-21 02:14:01 +00004658 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004659 SeenV1 = true;
4660 else
4661 SeenV2 = true;
4662
4663 // Only accept consecutive elements from the same vector
4664 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4665 return false;
4666 }
4667
4668 OpNum = SeenV1 ? 0 : 1;
4669 return true;
4670}
4671
4672/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4673/// logical left shift of a vector.
4674static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4675 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4676 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4677 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4678 false /* check zeros from right */, DAG);
4679 unsigned OpSrc;
4680
4681 if (!NumZeros)
4682 return false;
4683
4684 // Considering the elements in the mask that are not consecutive zeros,
4685 // check if they consecutively come from only one of the source vectors.
4686 //
4687 // V1 = {X, A, B, C} 0
4688 // \ \ \ /
4689 // vector_shuffle V1, V2 <1, 2, 3, X>
4690 //
4691 if (!isShuffleMaskConsecutive(SVOp,
4692 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004693 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004694 NumZeros, // Where to start looking in the src vector
4695 NumElems, // Number of elements in vector
4696 OpSrc)) // Which source operand ?
4697 return false;
4698
4699 isLeft = false;
4700 ShAmt = NumZeros;
4701 ShVal = SVOp->getOperand(OpSrc);
4702 return true;
4703}
4704
4705/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4706/// logical left shift of a vector.
4707static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4708 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4709 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4710 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4711 true /* check zeros from left */, DAG);
4712 unsigned OpSrc;
4713
4714 if (!NumZeros)
4715 return false;
4716
4717 // Considering the elements in the mask that are not consecutive zeros,
4718 // check if they consecutively come from only one of the source vectors.
4719 //
4720 // 0 { A, B, X, X } = V2
4721 // / \ / /
4722 // vector_shuffle V1, V2 <X, X, 4, 5>
4723 //
4724 if (!isShuffleMaskConsecutive(SVOp,
4725 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004726 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004727 0, // Where to start looking in the src vector
4728 NumElems, // Number of elements in vector
4729 OpSrc)) // Which source operand ?
4730 return false;
4731
4732 isLeft = true;
4733 ShAmt = NumZeros;
4734 ShVal = SVOp->getOperand(OpSrc);
4735 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004736}
4737
4738/// isVectorShift - Returns true if the shuffle can be implemented as a
4739/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004740static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004741 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004742 // Although the logic below support any bitwidth size, there are no
4743 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004744 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004745 return false;
4746
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004747 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4748 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4749 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004750
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004751 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004752}
4753
Evan Chengc78d3b42006-04-24 18:01:45 +00004754/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4755///
Dan Gohman475871a2008-07-27 21:46:04 +00004756static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004757 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004758 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004759 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004760 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004761 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004762 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004763
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004764 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004765 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004766 bool First = true;
4767 for (unsigned i = 0; i < 16; ++i) {
4768 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4769 if (ThisIsNonZero && First) {
4770 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004771 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004772 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004773 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004774 First = false;
4775 }
4776
4777 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004778 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004779 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4780 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004781 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004782 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004783 }
4784 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004785 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4786 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4787 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004788 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004789 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004790 } else
4791 ThisElt = LastElt;
4792
Gabor Greifba36cb52008-08-28 21:40:38 +00004793 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004794 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004795 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004796 }
4797 }
4798
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004799 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004800}
4801
Bill Wendlinga348c562007-03-22 18:42:45 +00004802/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004803///
Dan Gohman475871a2008-07-27 21:46:04 +00004804static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004805 unsigned NumNonZero, unsigned NumZero,
4806 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004807 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004808 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004809 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004810 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004811
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004812 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004813 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004814 bool First = true;
4815 for (unsigned i = 0; i < 8; ++i) {
4816 bool isNonZero = (NonZeros & (1 << i)) != 0;
4817 if (isNonZero) {
4818 if (First) {
4819 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004820 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004821 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004822 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004823 First = false;
4824 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004825 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004827 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004828 }
4829 }
4830
4831 return V;
4832}
4833
Evan Chengf26ffe92008-05-29 08:22:04 +00004834/// getVShift - Return a vector logical shift node.
4835///
Owen Andersone50ed302009-08-10 22:56:29 +00004836static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004837 unsigned NumBits, SelectionDAG &DAG,
4838 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004839 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004840 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004841 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004842 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4843 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004844 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004845 DAG.getConstant(NumBits,
4846 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004847}
4848
Dan Gohman475871a2008-07-27 21:46:04 +00004849SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004850X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004851 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004852
Evan Chengc3630942009-12-09 21:00:30 +00004853 // Check if the scalar load can be widened into a vector load. And if
4854 // the address is "base + cst" see if the cst can be "absorbed" into
4855 // the shuffle mask.
4856 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4857 SDValue Ptr = LD->getBasePtr();
4858 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4859 return SDValue();
4860 EVT PVT = LD->getValueType(0);
4861 if (PVT != MVT::i32 && PVT != MVT::f32)
4862 return SDValue();
4863
4864 int FI = -1;
4865 int64_t Offset = 0;
4866 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4867 FI = FINode->getIndex();
4868 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004869 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004870 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4871 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4872 Offset = Ptr.getConstantOperandVal(1);
4873 Ptr = Ptr.getOperand(0);
4874 } else {
4875 return SDValue();
4876 }
4877
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004878 // FIXME: 256-bit vector instructions don't require a strict alignment,
4879 // improve this code to support it better.
4880 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004881 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004882 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004883 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004884 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004885 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004886 // Can't change the alignment. FIXME: It's possible to compute
4887 // the exact stack offset and reference FI + adjust offset instead.
4888 // If someone *really* cares about this. That's the way to implement it.
4889 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004890 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004891 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004892 }
4893 }
4894
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004895 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004896 // Ptr + (Offset & ~15).
4897 if (Offset < 0)
4898 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004899 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004900 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004901 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004902 if (StartOffset)
4903 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4904 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4905
4906 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004907 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004908
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004909 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4910 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004911 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004912 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004913
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004914 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004915 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004916 Mask.push_back(EltNo);
4917
Craig Toppercc3000632012-01-30 07:50:31 +00004918 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004919 }
4920
4921 return SDValue();
4922}
4923
Michael J. Spencerec38de22010-10-10 22:04:20 +00004924/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4925/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004926/// load which has the same value as a build_vector whose operands are 'elts'.
4927///
4928/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004929///
Nate Begeman1449f292010-03-24 22:19:06 +00004930/// FIXME: we'd also like to handle the case where the last elements are zero
4931/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4932/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004933static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004934 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004935 EVT EltVT = VT.getVectorElementType();
4936 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004937
Nate Begemanfdea31a2010-03-24 20:49:50 +00004938 LoadSDNode *LDBase = NULL;
4939 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004940
Nate Begeman1449f292010-03-24 22:19:06 +00004941 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004942 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004943 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004944 for (unsigned i = 0; i < NumElems; ++i) {
4945 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004946
Nate Begemanfdea31a2010-03-24 20:49:50 +00004947 if (!Elt.getNode() ||
4948 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4949 return SDValue();
4950 if (!LDBase) {
4951 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4952 return SDValue();
4953 LDBase = cast<LoadSDNode>(Elt.getNode());
4954 LastLoadedElt = i;
4955 continue;
4956 }
4957 if (Elt.getOpcode() == ISD::UNDEF)
4958 continue;
4959
4960 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4961 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4962 return SDValue();
4963 LastLoadedElt = i;
4964 }
Nate Begeman1449f292010-03-24 22:19:06 +00004965
4966 // If we have found an entire vector of loads and undefs, then return a large
4967 // load of the entire vector width starting at the base pointer. If we found
4968 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004969 if (LastLoadedElt == NumElems - 1) {
4970 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004971 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004972 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004973 LDBase->isVolatile(), LDBase->isNonTemporal(),
4974 LDBase->isInvariant(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004975 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004976 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004977 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004978 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00004979 }
4980 if (NumElems == 4 && LastLoadedElt == 1 &&
4981 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004982 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4983 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004984 SDValue ResNode =
4985 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4986 LDBase->getPointerInfo(),
4987 LDBase->getAlignment(),
4988 false/*isVolatile*/, true/*ReadMem*/,
4989 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00004990
4991 // Make sure the newly-created LOAD is in the same position as LDBase in
4992 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
4993 // update uses of LDBase's output chain to use the TokenFactor.
4994 if (LDBase->hasAnyUseOfValue(1)) {
4995 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
4996 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
4997 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
4998 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
4999 SDValue(ResNode.getNode(), 1));
5000 }
5001
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005002 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00005003 }
5004 return SDValue();
5005}
5006
Nadav Rotem9d68b062012-04-08 12:54:54 +00005007/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5008/// to generate a splat value for the following cases:
5009/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005010/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005011/// a scalar load, or a constant.
5012/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005013/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005014SDValue
5015X86TargetLowering::LowerVectorBroadcast(SDValue &Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005016 if (!Subtarget->hasAVX())
5017 return SDValue();
5018
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005019 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005020 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005021
Craig Topper5da8a802012-05-04 05:49:51 +00005022 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5023 "Unsupported vector type for broadcast.");
5024
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005025 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005026 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005027
Nadav Rotem9d68b062012-04-08 12:54:54 +00005028 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005029 default:
5030 // Unknown pattern found.
5031 return SDValue();
5032
5033 case ISD::BUILD_VECTOR: {
5034 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005035 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005036 return SDValue();
5037
Nadav Rotem9d68b062012-04-08 12:54:54 +00005038 Ld = Op.getOperand(0);
5039 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5040 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005041
5042 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005043 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005044 // Constants may have multiple users.
5045 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005046 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005047 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005048 }
5049
5050 case ISD::VECTOR_SHUFFLE: {
5051 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5052
5053 // Shuffles must have a splat mask where the first element is
5054 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005055 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005056 return SDValue();
5057
5058 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005059 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005060 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5061
5062 if (!Subtarget->hasAVX2())
5063 return SDValue();
5064
5065 // Use the register form of the broadcast instruction available on AVX2.
5066 if (VT.is256BitVector())
5067 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5068 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5069 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005070
5071 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005072 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005073 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005074
5075 // The scalar_to_vector node and the suspected
5076 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005077 // Constants may have multiple users.
5078 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005079 return SDValue();
5080 break;
5081 }
5082 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005083
Craig Topper7a9a28b2012-08-12 02:23:29 +00005084 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005085
5086 // Handle the broadcasting a single constant scalar from the constant pool
5087 // into a vector. On Sandybridge it is still better to load a constant vector
5088 // from the constant pool and not to broadcast it from a scalar.
5089 if (ConstSplatVal && Subtarget->hasAVX2()) {
5090 EVT CVT = Ld.getValueType();
5091 assert(!CVT.isVector() && "Must not broadcast a vector type");
5092 unsigned ScalarSize = CVT.getSizeInBits();
5093
Craig Topper5da8a802012-05-04 05:49:51 +00005094 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005095 const Constant *C = 0;
5096 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5097 C = CI->getConstantIntValue();
5098 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5099 C = CF->getConstantFPValue();
5100
5101 assert(C && "Invalid constant type");
5102
Nadav Rotem154819d2012-04-09 07:45:58 +00005103 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005104 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005105 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005106 MachinePointerInfo::getConstantPool(),
5107 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005108
Nadav Rotem9d68b062012-04-08 12:54:54 +00005109 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5110 }
5111 }
5112
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005113 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005114 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5115
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005116 // Handle AVX2 in-register broadcasts.
5117 if (!IsLoad && Subtarget->hasAVX2() &&
5118 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5119 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5120
5121 // The scalar source must be a normal load.
5122 if (!IsLoad)
5123 return SDValue();
5124
Craig Topper5da8a802012-05-04 05:49:51 +00005125 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005126 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005127
Craig Toppera9376332012-01-10 08:23:59 +00005128 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005129 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005130 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005131 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005132 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005133 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005134
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005135 // Unsupported broadcast.
5136 return SDValue();
5137}
5138
Michael Liao7091b242012-08-14 21:24:47 +00005139// LowerVectorFpExtend - Recognize the scalarized FP_EXTEND from v2f32 to v2f64
5140// and convert it into X86ISD::VFPEXT due to the current ISD::FP_EXTEND has the
5141// constraint of matching input/output vector elements.
5142SDValue
5143X86TargetLowering::LowerVectorFpExtend(SDValue &Op, SelectionDAG &DAG) const {
5144 DebugLoc DL = Op.getDebugLoc();
5145 SDNode *N = Op.getNode();
5146 EVT VT = Op.getValueType();
5147 unsigned NumElts = Op.getNumOperands();
5148
5149 // Check supported types and sub-targets.
5150 //
5151 // Only v2f32 -> v2f64 needs special handling.
5152 if (VT != MVT::v2f64 || !Subtarget->hasSSE2())
5153 return SDValue();
5154
5155 SDValue VecIn;
5156 EVT VecInVT;
5157 SmallVector<int, 8> Mask;
5158 EVT SrcVT = MVT::Other;
5159
5160 // Check the patterns could be translated into X86vfpext.
5161 for (unsigned i = 0; i < NumElts; ++i) {
5162 SDValue In = N->getOperand(i);
5163 unsigned Opcode = In.getOpcode();
5164
5165 // Skip if the element is undefined.
5166 if (Opcode == ISD::UNDEF) {
5167 Mask.push_back(-1);
5168 continue;
5169 }
5170
5171 // Quit if one of the elements is not defined from 'fpext'.
5172 if (Opcode != ISD::FP_EXTEND)
5173 return SDValue();
5174
5175 // Check how the source of 'fpext' is defined.
5176 SDValue L2In = In.getOperand(0);
5177 EVT L2InVT = L2In.getValueType();
5178
5179 // Check the original type
5180 if (SrcVT == MVT::Other)
5181 SrcVT = L2InVT;
5182 else if (SrcVT != L2InVT) // Quit if non-homogenous typed.
5183 return SDValue();
5184
5185 // Check whether the value being 'fpext'ed is extracted from the same
5186 // source.
5187 Opcode = L2In.getOpcode();
5188
5189 // Quit if it's not extracted with a constant index.
5190 if (Opcode != ISD::EXTRACT_VECTOR_ELT ||
5191 !isa<ConstantSDNode>(L2In.getOperand(1)))
5192 return SDValue();
5193
5194 SDValue ExtractedFromVec = L2In.getOperand(0);
5195
5196 if (VecIn.getNode() == 0) {
5197 VecIn = ExtractedFromVec;
5198 VecInVT = ExtractedFromVec.getValueType();
5199 } else if (VecIn != ExtractedFromVec) // Quit if built from more than 1 vec.
5200 return SDValue();
5201
5202 Mask.push_back(cast<ConstantSDNode>(L2In.getOperand(1))->getZExtValue());
5203 }
5204
Michael Liao24438b82012-08-20 17:59:18 +00005205 // Quit if all operands of BUILD_VECTOR are undefined.
5206 if (!VecIn.getNode())
5207 return SDValue();
5208
Michael Liao7091b242012-08-14 21:24:47 +00005209 // Fill the remaining mask as undef.
5210 for (unsigned i = NumElts; i < VecInVT.getVectorNumElements(); ++i)
5211 Mask.push_back(-1);
5212
5213 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
5214 DAG.getVectorShuffle(VecInVT, DL,
5215 VecIn, DAG.getUNDEF(VecInVT),
5216 &Mask[0]));
5217}
5218
Evan Chengc3630942009-12-09 21:00:30 +00005219SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005220X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005221 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005222
David Greenef125a292011-02-08 19:04:41 +00005223 EVT VT = Op.getValueType();
5224 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005225 unsigned NumElems = Op.getNumOperands();
5226
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005227 // Vectors containing all zeros can be matched by pxor and xorps later
5228 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5229 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5230 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005231 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005232 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005233
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005234 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005235 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005236
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005237 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005238 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5239 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005240 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005241 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005242 return Op;
5243
Craig Topper07a27622012-01-22 03:07:48 +00005244 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005245 }
5246
Nadav Rotem154819d2012-04-09 07:45:58 +00005247 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005248 if (Broadcast.getNode())
5249 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005250
Michael Liao7091b242012-08-14 21:24:47 +00005251 SDValue FpExt = LowerVectorFpExtend(Op, DAG);
5252 if (FpExt.getNode())
5253 return FpExt;
5254
Owen Andersone50ed302009-08-10 22:56:29 +00005255 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005256
Evan Cheng0db9fe62006-04-25 20:13:52 +00005257 unsigned NumZero = 0;
5258 unsigned NumNonZero = 0;
5259 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005260 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005261 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005262 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005263 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005264 if (Elt.getOpcode() == ISD::UNDEF)
5265 continue;
5266 Values.insert(Elt);
5267 if (Elt.getOpcode() != ISD::Constant &&
5268 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005269 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005270 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005271 NumZero++;
5272 else {
5273 NonZeros |= (1 << i);
5274 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005275 }
5276 }
5277
Chris Lattner97a2a562010-08-26 05:24:29 +00005278 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5279 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005280 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005281
Chris Lattner67f453a2008-03-09 05:42:06 +00005282 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005283 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005284 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005285 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005286
Chris Lattner62098042008-03-09 01:05:04 +00005287 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5288 // the value are obviously zero, truncate the value to i32 and do the
5289 // insertion that way. Only do this if the value is non-constant or if the
5290 // value is a constant being inserted into element 0. It is cheaper to do
5291 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005292 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005293 (!IsAllConstants || Idx == 0)) {
5294 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005295 // Handle SSE only.
5296 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5297 EVT VecVT = MVT::v4i32;
5298 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005299
Chris Lattner62098042008-03-09 01:05:04 +00005300 // Truncate the value (which may itself be a constant) to i32, and
5301 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005302 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005303 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005304 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005305
Chris Lattner62098042008-03-09 01:05:04 +00005306 // Now we have our 32-bit value zero extended in the low element of
5307 // a vector. If Idx != 0, swizzle it into place.
5308 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005309 SmallVector<int, 4> Mask;
5310 Mask.push_back(Idx);
5311 for (unsigned i = 1; i != VecElts; ++i)
5312 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005313 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005314 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005315 }
Craig Topper07a27622012-01-22 03:07:48 +00005316 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005317 }
5318 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005319
Chris Lattner19f79692008-03-08 22:59:52 +00005320 // If we have a constant or non-constant insertion into the low element of
5321 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5322 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005323 // depending on what the source datatype is.
5324 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005325 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005326 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005327
5328 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005329 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005330 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005331 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005332 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5333 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005334 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005335 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005336 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5337 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005338 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005339 }
5340
5341 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005342 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005343 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005344 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005345 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005346 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005347 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005348 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005349 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005350 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005351 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005352 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005353 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005354
5355 // Is it a vector logical left shift?
5356 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005357 X86::isZeroNode(Op.getOperand(0)) &&
5358 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005359 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005360 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005361 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005362 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005363 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005364 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005365
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005366 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005367 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005368
Chris Lattner19f79692008-03-08 22:59:52 +00005369 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5370 // is a non-constant being inserted into an element other than the low one,
5371 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5372 // movd/movss) to move this into the low element, then shuffle it into
5373 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005374 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005375 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005376
Evan Cheng0db9fe62006-04-25 20:13:52 +00005377 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005378 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005379 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005380 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005381 MaskVec.push_back(i == Idx ? 0 : 1);
5382 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005383 }
5384 }
5385
Chris Lattner67f453a2008-03-09 05:42:06 +00005386 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005387 if (Values.size() == 1) {
5388 if (EVTBits == 32) {
5389 // Instead of a shuffle like this:
5390 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5391 // Check if it's possible to issue this instead.
5392 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5393 unsigned Idx = CountTrailingZeros_32(NonZeros);
5394 SDValue Item = Op.getOperand(Idx);
5395 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5396 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5397 }
Dan Gohman475871a2008-07-27 21:46:04 +00005398 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005399 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005400
Dan Gohmana3941172007-07-24 22:55:08 +00005401 // A vector full of immediates; various special cases are already
5402 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005403 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005404 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005405
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005406 // For AVX-length vectors, build the individual 128-bit pieces and use
5407 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005408 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005409 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005410 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005411 V.push_back(Op.getOperand(i));
5412
5413 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5414
5415 // Build both the lower and upper subvector.
5416 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5417 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5418 NumElems/2);
5419
5420 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005421 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005422 }
5423
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005424 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005425 if (EVTBits == 64) {
5426 if (NumNonZero == 1) {
5427 // One half is zero or undef.
5428 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005429 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005430 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005431 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005432 }
Dan Gohman475871a2008-07-27 21:46:04 +00005433 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005434 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005435
5436 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005437 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005438 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005439 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005440 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005441 }
5442
Bill Wendling826f36f2007-03-28 00:57:11 +00005443 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005444 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005445 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005446 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005447 }
5448
5449 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005450 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005451 if (NumElems == 4 && NumZero > 0) {
5452 for (unsigned i = 0; i < 4; ++i) {
5453 bool isZero = !(NonZeros & (1 << i));
5454 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005455 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005456 else
Dale Johannesenace16102009-02-03 19:33:06 +00005457 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005458 }
5459
5460 for (unsigned i = 0; i < 2; ++i) {
5461 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5462 default: break;
5463 case 0:
5464 V[i] = V[i*2]; // Must be a zero vector.
5465 break;
5466 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005467 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005468 break;
5469 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005470 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005471 break;
5472 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005473 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005474 break;
5475 }
5476 }
5477
Benjamin Kramer9c683542012-01-30 15:16:21 +00005478 bool Reverse1 = (NonZeros & 0x3) == 2;
5479 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5480 int MaskVec[] = {
5481 Reverse1 ? 1 : 0,
5482 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005483 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5484 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005485 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005486 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487 }
5488
Craig Topper7a9a28b2012-08-12 02:23:29 +00005489 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005490 // Check for a build vector of consecutive loads.
5491 for (unsigned i = 0; i < NumElems; ++i)
5492 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005493
Nate Begemanfdea31a2010-03-24 20:49:50 +00005494 // Check for elements which are consecutive loads.
5495 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5496 if (LD.getNode())
5497 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005498
5499 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005500 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005501 SDValue Result;
5502 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5503 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5504 else
5505 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005506
Chris Lattner24faf612010-08-28 17:59:08 +00005507 for (unsigned i = 1; i < NumElems; ++i) {
5508 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5509 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005510 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005511 }
5512 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005513 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005514
Chris Lattner6e80e442010-08-28 17:15:43 +00005515 // Otherwise, expand into a number of unpckl*, start by extending each of
5516 // our (non-undef) elements to the full vector width with the element in the
5517 // bottom slot of the vector (which generates no code for SSE).
5518 for (unsigned i = 0; i < NumElems; ++i) {
5519 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5520 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5521 else
5522 V[i] = DAG.getUNDEF(VT);
5523 }
5524
5525 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005526 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5527 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5528 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005529 unsigned EltStride = NumElems >> 1;
5530 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005531 for (unsigned i = 0; i < EltStride; ++i) {
5532 // If V[i+EltStride] is undef and this is the first round of mixing,
5533 // then it is safe to just drop this shuffle: V[i] is already in the
5534 // right place, the one element (since it's the first round) being
5535 // inserted as undef can be dropped. This isn't safe for successive
5536 // rounds because they will permute elements within both vectors.
5537 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5538 EltStride == NumElems/2)
5539 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005540
Chris Lattner6e80e442010-08-28 17:15:43 +00005541 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005542 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005543 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005544 }
5545 return V[0];
5546 }
Dan Gohman475871a2008-07-27 21:46:04 +00005547 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005548}
5549
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005550// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5551// to create 256-bit vectors from two other 128-bit ones.
5552static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5553 DebugLoc dl = Op.getDebugLoc();
5554 EVT ResVT = Op.getValueType();
5555
Craig Topper7a9a28b2012-08-12 02:23:29 +00005556 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005557
5558 SDValue V1 = Op.getOperand(0);
5559 SDValue V2 = Op.getOperand(1);
5560 unsigned NumElems = ResVT.getVectorNumElements();
5561
Craig Topper4c7972d2012-04-22 18:15:59 +00005562 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005563}
5564
5565SDValue
5566X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005567 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005568
5569 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5570 // from two other 128-bit ones.
5571 return LowerAVXCONCAT_VECTORS(Op, DAG);
5572}
5573
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005574// Try to lower a shuffle node into a simple blend instruction.
Craig Topper1842ba02012-04-23 06:38:28 +00005575static SDValue LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005576 const X86Subtarget *Subtarget,
Nadav Rotem91794872012-04-11 11:05:21 +00005577 SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005578 SDValue V1 = SVOp->getOperand(0);
5579 SDValue V2 = SVOp->getOperand(1);
5580 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005581 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005582 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005583
Nadav Roteme6113782012-04-11 06:40:27 +00005584 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005585 return SDValue();
5586
Craig Topper1842ba02012-04-23 06:38:28 +00005587 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005588 MVT OpTy;
5589
Craig Topper708e44f2012-04-23 07:36:33 +00005590 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005591 default: return SDValue();
5592 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005593 ISDNo = X86ISD::BLENDPW;
5594 OpTy = MVT::v8i16;
5595 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005596 case MVT::v4i32:
5597 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005598 ISDNo = X86ISD::BLENDPS;
5599 OpTy = MVT::v4f32;
5600 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005601 case MVT::v2i64:
5602 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005603 ISDNo = X86ISD::BLENDPD;
5604 OpTy = MVT::v2f64;
5605 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005606 case MVT::v8i32:
5607 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005608 if (!Subtarget->hasAVX())
5609 return SDValue();
5610 ISDNo = X86ISD::BLENDPS;
5611 OpTy = MVT::v8f32;
5612 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005613 case MVT::v4i64:
5614 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005615 if (!Subtarget->hasAVX())
5616 return SDValue();
5617 ISDNo = X86ISD::BLENDPD;
5618 OpTy = MVT::v4f64;
5619 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005620 }
5621 assert(ISDNo && "Invalid Op Number");
5622
5623 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005624
Craig Topper1842ba02012-04-23 06:38:28 +00005625 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005626 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005627 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005628 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005629 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005630 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005631 else
5632 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005633 }
5634
Nadav Roteme6113782012-04-11 06:40:27 +00005635 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5636 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5637 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5638 DAG.getConstant(MaskVals, MVT::i32));
5639 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005640}
5641
Nate Begemanb9a47b82009-02-23 08:49:38 +00005642// v8i16 shuffles - Prefer shuffles in the following order:
5643// 1. [all] pshuflw, pshufhw, optional move
5644// 2. [ssse3] 1 x pshufb
5645// 3. [ssse3] 2 x pshufb + 1 x por
5646// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005647SDValue
5648X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5649 SelectionDAG &DAG) const {
5650 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005651 SDValue V1 = SVOp->getOperand(0);
5652 SDValue V2 = SVOp->getOperand(1);
5653 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005654 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005655
Nate Begemanb9a47b82009-02-23 08:49:38 +00005656 // Determine if more than 1 of the words in each of the low and high quadwords
5657 // of the result come from the same quadword of one of the two inputs. Undef
5658 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005659 unsigned LoQuad[] = { 0, 0, 0, 0 };
5660 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005661 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005662 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005663 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005664 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005665 MaskVals.push_back(EltIdx);
5666 if (EltIdx < 0) {
5667 ++Quad[0];
5668 ++Quad[1];
5669 ++Quad[2];
5670 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005671 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005672 }
5673 ++Quad[EltIdx / 4];
5674 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005675 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005676
Nate Begemanb9a47b82009-02-23 08:49:38 +00005677 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005678 unsigned MaxQuad = 1;
5679 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005680 if (LoQuad[i] > MaxQuad) {
5681 BestLoQuad = i;
5682 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005683 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005684 }
5685
Nate Begemanb9a47b82009-02-23 08:49:38 +00005686 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005687 MaxQuad = 1;
5688 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005689 if (HiQuad[i] > MaxQuad) {
5690 BestHiQuad = i;
5691 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005692 }
5693 }
5694
Nate Begemanb9a47b82009-02-23 08:49:38 +00005695 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005696 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005697 // single pshufb instruction is necessary. If There are more than 2 input
5698 // quads, disable the next transformation since it does not help SSSE3.
5699 bool V1Used = InputQuads[0] || InputQuads[1];
5700 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005701 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005702 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005703 BestLoQuad = InputQuads[0] ? 0 : 1;
5704 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005705 }
5706 if (InputQuads.count() > 2) {
5707 BestLoQuad = -1;
5708 BestHiQuad = -1;
5709 }
5710 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005711
Nate Begemanb9a47b82009-02-23 08:49:38 +00005712 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5713 // the shuffle mask. If a quad is scored as -1, that means that it contains
5714 // words from all 4 input quadwords.
5715 SDValue NewV;
5716 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005717 int MaskV[] = {
5718 BestLoQuad < 0 ? 0 : BestLoQuad,
5719 BestHiQuad < 0 ? 1 : BestHiQuad
5720 };
Eric Christopherfd179292009-08-27 18:07:15 +00005721 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005722 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5723 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5724 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005725
Nate Begemanb9a47b82009-02-23 08:49:38 +00005726 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5727 // source words for the shuffle, to aid later transformations.
5728 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005729 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005730 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005731 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005732 if (idx != (int)i)
5733 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005734 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005735 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 AllWordsInNewV = false;
5737 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005738 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005739
Nate Begemanb9a47b82009-02-23 08:49:38 +00005740 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5741 if (AllWordsInNewV) {
5742 for (int i = 0; i != 8; ++i) {
5743 int idx = MaskVals[i];
5744 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005745 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005746 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005747 if ((idx != i) && idx < 4)
5748 pshufhw = false;
5749 if ((idx != i) && idx > 3)
5750 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005751 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005752 V1 = NewV;
5753 V2Used = false;
5754 BestLoQuad = 0;
5755 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005756 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005757
Nate Begemanb9a47b82009-02-23 08:49:38 +00005758 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5759 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005760 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005761 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5762 unsigned TargetMask = 0;
5763 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005764 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005765 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5766 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5767 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005768 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005769 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005770 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005771 }
Eric Christopherfd179292009-08-27 18:07:15 +00005772
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 // If we have SSSE3, and all words of the result are from 1 input vector,
5774 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5775 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005776 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005777 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005778
Nate Begemanb9a47b82009-02-23 08:49:38 +00005779 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005780 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005781 // mask, and elements that come from V1 in the V2 mask, so that the two
5782 // results can be OR'd together.
5783 bool TwoInputs = V1Used && V2Used;
5784 for (unsigned i = 0; i != 8; ++i) {
5785 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005786 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5787 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5788 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5789 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005790 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005791 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005792 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005793 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005794 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005795 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005796 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005797
Nate Begemanb9a47b82009-02-23 08:49:38 +00005798 // Calculate the shuffle mask for the second input, shuffle it, and
5799 // OR it with the first shuffled input.
5800 pshufbMask.clear();
5801 for (unsigned i = 0; i != 8; ++i) {
5802 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005803 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5804 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5805 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5806 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005807 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005808 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005809 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005810 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005811 MVT::v16i8, &pshufbMask[0], 16));
5812 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005813 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005814 }
5815
5816 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5817 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005818 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005819 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005820 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005821 for (int i = 0; i != 4; ++i) {
5822 int idx = MaskVals[i];
5823 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 InOrder.set(i);
5825 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005826 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005827 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005828 }
5829 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005831 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005832
Craig Topperdd637ae2012-02-19 05:41:45 +00005833 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5834 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005835 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005836 NewV.getOperand(0),
5837 getShufflePSHUFLWImmediate(SVOp), DAG);
5838 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005839 }
Eric Christopherfd179292009-08-27 18:07:15 +00005840
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5842 // and update MaskVals with the new element order.
5843 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005844 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005845 for (unsigned i = 4; i != 8; ++i) {
5846 int idx = MaskVals[i];
5847 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 InOrder.set(i);
5849 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005850 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005851 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005852 }
5853 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005854 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005855 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005856
Craig Topperdd637ae2012-02-19 05:41:45 +00005857 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5858 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005859 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005860 NewV.getOperand(0),
5861 getShufflePSHUFHWImmediate(SVOp), DAG);
5862 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005863 }
Eric Christopherfd179292009-08-27 18:07:15 +00005864
Nate Begemanb9a47b82009-02-23 08:49:38 +00005865 // In case BestHi & BestLo were both -1, which means each quadword has a word
5866 // from each of the four input quadwords, calculate the InOrder bitvector now
5867 // before falling through to the insert/extract cleanup.
5868 if (BestLoQuad == -1 && BestHiQuad == -1) {
5869 NewV = V1;
5870 for (int i = 0; i != 8; ++i)
5871 if (MaskVals[i] < 0 || MaskVals[i] == i)
5872 InOrder.set(i);
5873 }
Eric Christopherfd179292009-08-27 18:07:15 +00005874
Nate Begemanb9a47b82009-02-23 08:49:38 +00005875 // The other elements are put in the right place using pextrw and pinsrw.
5876 for (unsigned i = 0; i != 8; ++i) {
5877 if (InOrder[i])
5878 continue;
5879 int EltIdx = MaskVals[i];
5880 if (EltIdx < 0)
5881 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005882 SDValue ExtOp = (EltIdx < 8) ?
5883 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5884 DAG.getIntPtrConstant(EltIdx)) :
5885 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005886 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005888 DAG.getIntPtrConstant(i));
5889 }
5890 return NewV;
5891}
5892
5893// v16i8 shuffles - Prefer shuffles in the following order:
5894// 1. [ssse3] 1 x pshufb
5895// 2. [ssse3] 2 x pshufb + 1 x por
5896// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5897static
Nate Begeman9008ca62009-04-27 18:41:29 +00005898SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005899 SelectionDAG &DAG,
5900 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005901 SDValue V1 = SVOp->getOperand(0);
5902 SDValue V2 = SVOp->getOperand(1);
5903 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005904 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005905
Nate Begemanb9a47b82009-02-23 08:49:38 +00005906 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005907 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005909
Nate Begemanb9a47b82009-02-23 08:49:38 +00005910 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005911 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005912 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005913
Nate Begemanb9a47b82009-02-23 08:49:38 +00005914 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005915 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005916 //
5917 // Otherwise, we have elements from both input vectors, and must zero out
5918 // elements that come from V2 in the first mask, and V1 in the second mask
5919 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005920 for (unsigned i = 0; i != 16; ++i) {
5921 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005922 if (EltIdx < 0 || EltIdx >= 16)
5923 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005924 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005925 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005926 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005927 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005928 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005929
5930 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5931 // the 2nd operand if it's undefined or zero.
5932 if (V2.getOpcode() == ISD::UNDEF ||
5933 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005934 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005935
Nate Begemanb9a47b82009-02-23 08:49:38 +00005936 // Calculate the shuffle mask for the second input, shuffle it, and
5937 // OR it with the first shuffled input.
5938 pshufbMask.clear();
5939 for (unsigned i = 0; i != 16; ++i) {
5940 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005941 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005942 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005943 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005944 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005945 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005946 MVT::v16i8, &pshufbMask[0], 16));
5947 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005948 }
Eric Christopherfd179292009-08-27 18:07:15 +00005949
Nate Begemanb9a47b82009-02-23 08:49:38 +00005950 // No SSSE3 - Calculate in place words and then fix all out of place words
5951 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5952 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005953 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5954 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005955 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005956 for (int i = 0; i != 8; ++i) {
5957 int Elt0 = MaskVals[i*2];
5958 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005959
Nate Begemanb9a47b82009-02-23 08:49:38 +00005960 // This word of the result is all undef, skip it.
5961 if (Elt0 < 0 && Elt1 < 0)
5962 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005963
Nate Begemanb9a47b82009-02-23 08:49:38 +00005964 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005965 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005966 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005967
Nate Begemanb9a47b82009-02-23 08:49:38 +00005968 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5969 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5970 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005971
5972 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5973 // using a single extract together, load it and store it.
5974 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005975 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005976 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005977 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005978 DAG.getIntPtrConstant(i));
5979 continue;
5980 }
5981
Nate Begemanb9a47b82009-02-23 08:49:38 +00005982 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005983 // source byte is not also odd, shift the extracted word left 8 bits
5984 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005985 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005986 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005987 DAG.getIntPtrConstant(Elt1 / 2));
5988 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005989 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005990 DAG.getConstant(8,
5991 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005992 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005993 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5994 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005995 }
5996 // If Elt0 is defined, extract it from the appropriate source. If the
5997 // source byte is not also even, shift the extracted word right 8 bits. If
5998 // Elt1 was also defined, OR the extracted values together before
5999 // inserting them in the result.
6000 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006002 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
6003 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006005 DAG.getConstant(8,
6006 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006007 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006008 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6009 DAG.getConstant(0x00FF, MVT::i16));
6010 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006011 : InsElt0;
6012 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006013 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006014 DAG.getIntPtrConstant(i));
6015 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006016 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006017}
6018
Elena Demikhovsky41789462012-09-06 12:42:01 +00006019// v32i8 shuffles - Translate to VPSHUFB if possible.
6020static
6021SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
6022 SelectionDAG &DAG,
6023 const X86TargetLowering &TLI) {
6024 EVT VT = SVOp->getValueType(0);
6025 SDValue V1 = SVOp->getOperand(0);
6026 SDValue V2 = SVOp->getOperand(1);
6027 DebugLoc dl = SVOp->getDebugLoc();
6028 ArrayRef<int> MaskVals = SVOp->getMask();
6029
6030 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
6031
6032 if (VT != MVT::v32i8 || !TLI.getSubtarget()->hasAVX2() || !V2IsUndef)
6033 return SDValue();
6034
6035 SmallVector<SDValue,32> pshufbMask;
6036 for (unsigned i = 0; i != 32; i++) {
6037 int EltIdx = MaskVals[i];
6038 if (EltIdx < 0 || EltIdx >= 32)
6039 EltIdx = 0x80;
6040 else {
6041 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6042 // Cross lane is not allowed.
6043 return SDValue();
6044 EltIdx &= 0xf;
6045 }
6046 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6047 }
6048 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6049 DAG.getNode(ISD::BUILD_VECTOR, dl,
6050 MVT::v32i8, &pshufbMask[0], 32));
6051}
6052
Evan Cheng7a831ce2007-12-15 03:00:47 +00006053/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006054/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006055/// done when every pair / quad of shuffle mask elements point to elements in
6056/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006057/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006058static
Nate Begeman9008ca62009-04-27 18:41:29 +00006059SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006060 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006061 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006062 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006063 MVT NewVT;
6064 unsigned Scale;
6065 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006066 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006067 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6068 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6069 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6070 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6071 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6072 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006073 }
6074
Nate Begeman9008ca62009-04-27 18:41:29 +00006075 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006076 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006077 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006078 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006079 int EltIdx = SVOp->getMaskElt(i+j);
6080 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006081 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006082 if (StartIdx < 0)
6083 StartIdx = (EltIdx / Scale);
6084 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006085 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006086 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006087 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006088 }
6089
Craig Topper11ac1f82012-05-04 04:08:44 +00006090 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6091 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006092 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006093}
6094
Evan Chengd880b972008-05-09 21:53:03 +00006095/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006096///
Owen Andersone50ed302009-08-10 22:56:29 +00006097static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006098 SDValue SrcOp, SelectionDAG &DAG,
6099 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006100 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006101 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006102 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006103 LD = dyn_cast<LoadSDNode>(SrcOp);
6104 if (!LD) {
6105 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6106 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006107 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006108 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006109 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006110 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006111 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006112 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006113 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006114 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006115 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6116 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6117 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006118 SrcOp.getOperand(0)
6119 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006120 }
6121 }
6122 }
6123
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006124 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006125 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006126 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006127 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006128}
6129
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006130/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6131/// which could not be matched by any known target speficic shuffle
6132static SDValue
6133LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006134
6135 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6136 if (NewOp.getNode())
6137 return NewOp;
6138
Craig Topper8f35c132012-01-20 09:29:03 +00006139 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006140
Craig Topper8f35c132012-01-20 09:29:03 +00006141 unsigned NumElems = VT.getVectorNumElements();
6142 unsigned NumLaneElems = NumElems / 2;
6143
Craig Topper8f35c132012-01-20 09:29:03 +00006144 DebugLoc dl = SVOp->getDebugLoc();
6145 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006146 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006147 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006148
Craig Topper9a2b6e12012-04-06 07:45:23 +00006149 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006150 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006151 // Build a shuffle mask for the output, discovering on the fly which
6152 // input vectors to use as shuffle operands (recorded in InputUsed).
6153 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006154 // out with UseBuildVector set.
6155 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006156 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006157 unsigned LaneStart = l * NumLaneElems;
6158 for (unsigned i = 0; i != NumLaneElems; ++i) {
6159 // The mask element. This indexes into the input.
6160 int Idx = SVOp->getMaskElt(i+LaneStart);
6161 if (Idx < 0) {
6162 // the mask element does not index into any input vector.
6163 Mask.push_back(-1);
6164 continue;
6165 }
Craig Topper8f35c132012-01-20 09:29:03 +00006166
Craig Topper9a2b6e12012-04-06 07:45:23 +00006167 // The input vector this mask element indexes into.
6168 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006169
Craig Topper9a2b6e12012-04-06 07:45:23 +00006170 // Turn the index into an offset from the start of the input vector.
6171 Idx -= Input * NumLaneElems;
6172
6173 // Find or create a shuffle vector operand to hold this input.
6174 unsigned OpNo;
6175 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6176 if (InputUsed[OpNo] == Input)
6177 // This input vector is already an operand.
6178 break;
6179 if (InputUsed[OpNo] < 0) {
6180 // Create a new operand for this input vector.
6181 InputUsed[OpNo] = Input;
6182 break;
6183 }
6184 }
6185
6186 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006187 // More than two input vectors used! Give up on trying to create a
6188 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6189 UseBuildVector = true;
6190 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006191 }
6192
6193 // Add the mask index for the new shuffle vector.
6194 Mask.push_back(Idx + OpNo * NumLaneElems);
6195 }
6196
Craig Topper8ae97ba2012-05-21 06:40:16 +00006197 if (UseBuildVector) {
6198 SmallVector<SDValue, 16> SVOps;
6199 for (unsigned i = 0; i != NumLaneElems; ++i) {
6200 // The mask element. This indexes into the input.
6201 int Idx = SVOp->getMaskElt(i+LaneStart);
6202 if (Idx < 0) {
6203 SVOps.push_back(DAG.getUNDEF(EltVT));
6204 continue;
6205 }
6206
6207 // The input vector this mask element indexes into.
6208 int Input = Idx / NumElems;
6209
6210 // Turn the index into an offset from the start of the input vector.
6211 Idx -= Input * NumElems;
6212
6213 // Extract the vector element by hand.
6214 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6215 SVOp->getOperand(Input),
6216 DAG.getIntPtrConstant(Idx)));
6217 }
6218
6219 // Construct the output using a BUILD_VECTOR.
6220 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6221 SVOps.size());
6222 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006223 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006224 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006225 } else {
6226 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006227 (InputUsed[0] % 2) * NumLaneElems,
6228 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006229 // If only one input was used, use an undefined vector for the other.
6230 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6231 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006232 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006233 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006234 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006235 }
6236
6237 Mask.clear();
6238 }
Craig Topper8f35c132012-01-20 09:29:03 +00006239
6240 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006241 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006242}
6243
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006244/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6245/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006246static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006247LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006248 SDValue V1 = SVOp->getOperand(0);
6249 SDValue V2 = SVOp->getOperand(1);
6250 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006251 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006252
Craig Topper7a9a28b2012-08-12 02:23:29 +00006253 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006254
Benjamin Kramer9c683542012-01-30 15:16:21 +00006255 std::pair<int, int> Locs[4];
6256 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006257 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006258
Evan Chengace3c172008-07-22 21:13:36 +00006259 unsigned NumHi = 0;
6260 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006261 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006262 int Idx = PermMask[i];
6263 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006264 Locs[i] = std::make_pair(-1, -1);
6265 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006266 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6267 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006268 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006269 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006270 NumLo++;
6271 } else {
6272 Locs[i] = std::make_pair(1, NumHi);
6273 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006274 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006275 NumHi++;
6276 }
6277 }
6278 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006279
Evan Chengace3c172008-07-22 21:13:36 +00006280 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006281 // If no more than two elements come from either vector. This can be
6282 // implemented with two shuffles. First shuffle gather the elements.
6283 // The second shuffle, which takes the first shuffle as both of its
6284 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006285 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006286
Benjamin Kramer9c683542012-01-30 15:16:21 +00006287 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006288
Benjamin Kramer9c683542012-01-30 15:16:21 +00006289 for (unsigned i = 0; i != 4; ++i)
6290 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006291 unsigned Idx = (i < 2) ? 0 : 4;
6292 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006293 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006294 }
Evan Chengace3c172008-07-22 21:13:36 +00006295
Nate Begeman9008ca62009-04-27 18:41:29 +00006296 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006297 }
6298
6299 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006300 // Otherwise, we must have three elements from one vector, call it X, and
6301 // one element from the other, call it Y. First, use a shufps to build an
6302 // intermediate vector with the one element from Y and the element from X
6303 // that will be in the same half in the final destination (the indexes don't
6304 // matter). Then, use a shufps to build the final vector, taking the half
6305 // containing the element from Y from the intermediate, and the other half
6306 // from X.
6307 if (NumHi == 3) {
6308 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006309 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006310 std::swap(V1, V2);
6311 }
6312
6313 // Find the element from V2.
6314 unsigned HiIndex;
6315 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006316 int Val = PermMask[HiIndex];
6317 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006318 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006319 if (Val >= 4)
6320 break;
6321 }
6322
Nate Begeman9008ca62009-04-27 18:41:29 +00006323 Mask1[0] = PermMask[HiIndex];
6324 Mask1[1] = -1;
6325 Mask1[2] = PermMask[HiIndex^1];
6326 Mask1[3] = -1;
6327 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006328
6329 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006330 Mask1[0] = PermMask[0];
6331 Mask1[1] = PermMask[1];
6332 Mask1[2] = HiIndex & 1 ? 6 : 4;
6333 Mask1[3] = HiIndex & 1 ? 4 : 6;
6334 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006335 }
Craig Topper69947b92012-04-23 06:57:04 +00006336
6337 Mask1[0] = HiIndex & 1 ? 2 : 0;
6338 Mask1[1] = HiIndex & 1 ? 0 : 2;
6339 Mask1[2] = PermMask[2];
6340 Mask1[3] = PermMask[3];
6341 if (Mask1[2] >= 0)
6342 Mask1[2] += 4;
6343 if (Mask1[3] >= 0)
6344 Mask1[3] += 4;
6345 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006346 }
6347
6348 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006349 int LoMask[] = { -1, -1, -1, -1 };
6350 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006351
Benjamin Kramer9c683542012-01-30 15:16:21 +00006352 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006353 unsigned MaskIdx = 0;
6354 unsigned LoIdx = 0;
6355 unsigned HiIdx = 2;
6356 for (unsigned i = 0; i != 4; ++i) {
6357 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006358 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006359 MaskIdx = 1;
6360 LoIdx = 0;
6361 HiIdx = 2;
6362 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006363 int Idx = PermMask[i];
6364 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006365 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006366 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006367 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006368 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006369 LoIdx++;
6370 } else {
6371 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006372 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006373 HiIdx++;
6374 }
6375 }
6376
Nate Begeman9008ca62009-04-27 18:41:29 +00006377 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6378 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006379 int MaskOps[] = { -1, -1, -1, -1 };
6380 for (unsigned i = 0; i != 4; ++i)
6381 if (Locs[i].first != -1)
6382 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006383 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006384}
6385
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006386static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006387 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006388 V = V.getOperand(0);
6389 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6390 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006391 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6392 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6393 // BUILD_VECTOR (load), undef
6394 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006395 if (MayFoldLoad(V))
6396 return true;
6397 return false;
6398}
6399
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006400// FIXME: the version above should always be used. Since there's
6401// a bug where several vector shuffles can't be folded because the
6402// DAG is not updated during lowering and a node claims to have two
6403// uses while it only has one, use this version, and let isel match
6404// another instruction if the load really happens to have more than
6405// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006406// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006407static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006408 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006409 V = V.getOperand(0);
6410 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6411 V = V.getOperand(0);
6412 if (ISD::isNormalLoad(V.getNode()))
6413 return true;
6414 return false;
6415}
6416
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006417static
Evan Cheng835580f2010-10-07 20:50:20 +00006418SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6419 EVT VT = Op.getValueType();
6420
6421 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006422 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6423 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006424 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6425 V1, DAG));
6426}
6427
6428static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006429SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006430 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006431 SDValue V1 = Op.getOperand(0);
6432 SDValue V2 = Op.getOperand(1);
6433 EVT VT = Op.getValueType();
6434
6435 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6436
Craig Topper1accb7e2012-01-10 06:54:16 +00006437 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006438 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6439
Evan Cheng0899f5c2011-08-31 02:05:24 +00006440 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6441 return DAG.getNode(ISD::BITCAST, dl, VT,
6442 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6443 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6444 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006445}
6446
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006447static
6448SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6449 SDValue V1 = Op.getOperand(0);
6450 SDValue V2 = Op.getOperand(1);
6451 EVT VT = Op.getValueType();
6452
6453 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6454 "unsupported shuffle type");
6455
6456 if (V2.getOpcode() == ISD::UNDEF)
6457 V2 = V1;
6458
6459 // v4i32 or v4f32
6460 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6461}
6462
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006463static
Craig Topper1accb7e2012-01-10 06:54:16 +00006464SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006465 SDValue V1 = Op.getOperand(0);
6466 SDValue V2 = Op.getOperand(1);
6467 EVT VT = Op.getValueType();
6468 unsigned NumElems = VT.getVectorNumElements();
6469
6470 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6471 // operand of these instructions is only memory, so check if there's a
6472 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6473 // same masks.
6474 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006475
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006476 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006477 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006478 CanFoldLoad = true;
6479
6480 // When V1 is a load, it can be folded later into a store in isel, example:
6481 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6482 // turns into:
6483 // (MOVLPSmr addr:$src1, VR128:$src2)
6484 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006485 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006486 CanFoldLoad = true;
6487
Dan Gohman65fd6562011-11-03 21:49:52 +00006488 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006489 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006490 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006491 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6492
6493 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006494 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006495 if (SVOp->getMaskElt(1) != -1)
6496 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006497 }
6498
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006499 // movl and movlp will both match v2i64, but v2i64 is never matched by
6500 // movl earlier because we make it strict to avoid messing with the movlp load
6501 // folding logic (see the code above getMOVLP call). Match it here then,
6502 // this is horrible, but will stay like this until we move all shuffle
6503 // matching to x86 specific nodes. Note that for the 1st condition all
6504 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006505 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006506 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6507 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006508 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006509 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006510 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006511 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006512
6513 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6514
6515 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006516 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006517 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006518}
6519
Nadav Rotem154819d2012-04-09 07:45:58 +00006520SDValue
6521X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006522 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6523 EVT VT = Op.getValueType();
6524 DebugLoc dl = Op.getDebugLoc();
6525 SDValue V1 = Op.getOperand(0);
6526 SDValue V2 = Op.getOperand(1);
6527
6528 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006529 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006530
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006531 // Handle splat operations
6532 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006533 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006534 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006535
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006536 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006537 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006538 if (Broadcast.getNode())
6539 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006540
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006541 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006542 if ((Size == 128 && NumElem <= 4) ||
6543 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006544 return SDValue();
6545
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006546 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006547 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006548 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006549
6550 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6551 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006552 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6553 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006554 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6555 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006556 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006557 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006558 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006559 // FIXME: Figure out a cleaner way to do this.
6560 // Try to make use of movq to zero out the top part.
6561 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6562 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6563 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006564 EVT NewVT = NewOp.getValueType();
6565 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6566 NewVT, true, false))
6567 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006568 DAG, Subtarget, dl);
6569 }
6570 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6571 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006572 if (NewOp.getNode()) {
6573 EVT NewVT = NewOp.getValueType();
6574 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6575 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6576 DAG, Subtarget, dl);
6577 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006578 }
6579 }
6580 return SDValue();
6581}
6582
Dan Gohman475871a2008-07-27 21:46:04 +00006583SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006584X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006585 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006586 SDValue V1 = Op.getOperand(0);
6587 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006588 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006589 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006590 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006591 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006592 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006593 bool V1IsSplat = false;
6594 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006595 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006596 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006597 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006598 MachineFunction &MF = DAG.getMachineFunction();
6599 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600
Craig Topper3426a3e2011-11-14 06:46:21 +00006601 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006602
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006603 if (V1IsUndef && V2IsUndef)
6604 return DAG.getUNDEF(VT);
6605
6606 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006607
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006608 // Vector shuffle lowering takes 3 steps:
6609 //
6610 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6611 // narrowing and commutation of operands should be handled.
6612 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6613 // shuffle nodes.
6614 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6615 // so the shuffle can be broken into other shuffles and the legalizer can
6616 // try the lowering again.
6617 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006618 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006619 // be matched during isel, all of them must be converted to a target specific
6620 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006621
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006622 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6623 // narrowing and commutation of operands should be handled. The actual code
6624 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006625 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006626 if (NewOp.getNode())
6627 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006628
Craig Topper5aaffa82012-02-19 02:53:47 +00006629 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6630
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006631 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6632 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006633 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006634 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006635 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006636 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006637
Craig Topperdd637ae2012-02-19 05:41:45 +00006638 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006639 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006640 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006641
Craig Topperdd637ae2012-02-19 05:41:45 +00006642 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006643 return getMOVHighToLow(Op, dl, DAG);
6644
6645 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006646 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006647 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006648 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006649
Craig Topper5aaffa82012-02-19 02:53:47 +00006650 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006651 // The actual implementation will match the mask in the if above and then
6652 // during isel it can match several different instructions, not only pshufd
6653 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006654 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6655 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006656
Craig Topper5aaffa82012-02-19 02:53:47 +00006657 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006658
Craig Topperdbd98a42012-02-07 06:28:42 +00006659 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6660 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6661
Craig Topper1accb7e2012-01-10 06:54:16 +00006662 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006663 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6664
Craig Topperb3982da2011-12-31 23:50:21 +00006665 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006666 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006667 }
Eric Christopherfd179292009-08-27 18:07:15 +00006668
Evan Chengf26ffe92008-05-29 08:22:04 +00006669 // Check if this can be converted into a logical shift.
6670 bool isLeft = false;
6671 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006672 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006673 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006674 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006675 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006676 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006677 EVT EltVT = VT.getVectorElementType();
6678 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006679 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006680 }
Eric Christopherfd179292009-08-27 18:07:15 +00006681
Craig Topper5aaffa82012-02-19 02:53:47 +00006682 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006683 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006684 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006685 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006686 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006687 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6688
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006689 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006690 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6691 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006692 }
Eric Christopherfd179292009-08-27 18:07:15 +00006693
Nate Begeman9008ca62009-04-27 18:41:29 +00006694 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006695 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006696 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006697
Craig Topperdd637ae2012-02-19 05:41:45 +00006698 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006699 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006700
Craig Topperdd637ae2012-02-19 05:41:45 +00006701 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006702 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006703
Craig Topperdd637ae2012-02-19 05:41:45 +00006704 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006705 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006706
Craig Topperdd637ae2012-02-19 05:41:45 +00006707 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006708 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006709
Craig Topperdd637ae2012-02-19 05:41:45 +00006710 if (ShouldXformToMOVHLPS(M, VT) ||
6711 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006712 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006713
Evan Chengf26ffe92008-05-29 08:22:04 +00006714 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006715 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006716 EVT EltVT = VT.getVectorElementType();
6717 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006718 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006719 }
Eric Christopherfd179292009-08-27 18:07:15 +00006720
Evan Cheng9eca5e82006-10-25 21:49:50 +00006721 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006722 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6723 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006724 V1IsSplat = isSplatVector(V1.getNode());
6725 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006726
Chris Lattner8a594482007-11-25 00:24:49 +00006727 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006728 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6729 CommuteVectorShuffleMask(M, NumElems);
6730 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006731 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006732 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006733 }
6734
Craig Topperbeabc6c2011-12-05 06:56:46 +00006735 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006736 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006737 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006738 return V1;
6739 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6740 // the instruction selector will not match, so get a canonical MOVL with
6741 // swapped operands to undo the commute.
6742 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006743 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006744
Craig Topperbeabc6c2011-12-05 06:56:46 +00006745 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006746 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006747
Craig Topperbeabc6c2011-12-05 06:56:46 +00006748 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006749 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006750
Evan Cheng9bbbb982006-10-25 20:48:19 +00006751 if (V2IsSplat) {
6752 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006753 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006754 // new vector_shuffle with the corrected mask.p
6755 SmallVector<int, 8> NewMask(M.begin(), M.end());
6756 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006757 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006758 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006759 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006760 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006761 }
6762
Evan Cheng9eca5e82006-10-25 21:49:50 +00006763 if (Commuted) {
6764 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006765 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006766 CommuteVectorShuffleMask(M, NumElems);
6767 std::swap(V1, V2);
6768 std::swap(V1IsSplat, V2IsSplat);
6769 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006770
Craig Topper39a9e482012-02-11 06:24:48 +00006771 if (isUNPCKLMask(M, VT, HasAVX2))
6772 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006773
Craig Topper39a9e482012-02-11 06:24:48 +00006774 if (isUNPCKHMask(M, VT, HasAVX2))
6775 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006776 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006777
Nate Begeman9008ca62009-04-27 18:41:29 +00006778 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006779 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006780 return CommuteVectorShuffle(SVOp, DAG);
6781
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006782 // The checks below are all present in isShuffleMaskLegal, but they are
6783 // inlined here right now to enable us to directly emit target specific
6784 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006785
Craig Topper0e2037b2012-01-20 05:53:00 +00006786 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006787 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006788 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006789 DAG);
6790
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006791 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6792 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006793 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006794 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006795 }
6796
Craig Toppera9a568a2012-05-02 08:03:44 +00006797 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006798 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006799 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006800 DAG);
6801
Craig Toppera9a568a2012-05-02 08:03:44 +00006802 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006803 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006804 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006805 DAG);
6806
Craig Topper1a7700a2012-01-19 08:19:12 +00006807 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006808 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006809 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006810
Craig Topper94438ba2011-12-16 08:06:31 +00006811 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006812 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006813 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006814 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006815
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006816 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006817 // Generate target specific nodes for 128 or 256-bit shuffles only
6818 // supported in the AVX instruction set.
6819 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006820
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006821 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006822 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006823 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6824
Craig Topper70b883b2011-11-28 10:14:51 +00006825 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006826 if (isVPERMILPMask(M, VT, HasAVX)) {
6827 if (HasAVX2 && VT == MVT::v8i32)
6828 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006829 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006830 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006831 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006832 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006833
Craig Topper70b883b2011-11-28 10:14:51 +00006834 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006835 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006836 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006837 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006838
Craig Topper1842ba02012-04-23 06:38:28 +00006839 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006840 if (BlendOp.getNode())
6841 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006842
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006843 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006844 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006845 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006846 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006847 }
Craig Topper92040742012-04-16 06:43:40 +00006848 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6849 &permclMask[0], 8);
6850 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006851 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006852 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006853 }
Craig Topper095c5282012-04-15 23:48:57 +00006854
Craig Topper8325c112012-04-16 00:41:45 +00006855 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6856 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006857 getShuffleCLImmediate(SVOp), DAG);
6858
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006859
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006860 //===--------------------------------------------------------------------===//
6861 // Since no target specific shuffle was selected for this generic one,
6862 // lower it into other known shuffles. FIXME: this isn't true yet, but
6863 // this is the plan.
6864 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006865
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006866 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6867 if (VT == MVT::v8i16) {
6868 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6869 if (NewOp.getNode())
6870 return NewOp;
6871 }
6872
6873 if (VT == MVT::v16i8) {
6874 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6875 if (NewOp.getNode())
6876 return NewOp;
6877 }
6878
Elena Demikhovsky41789462012-09-06 12:42:01 +00006879 if (VT == MVT::v32i8) {
6880 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, DAG, *this);
6881 if (NewOp.getNode())
6882 return NewOp;
6883 }
6884
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006885 // Handle all 128-bit wide vectors with 4 elements, and match them with
6886 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006887 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006888 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6889
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006890 // Handle general 256-bit shuffles
6891 if (VT.is256BitVector())
6892 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6893
Dan Gohman475871a2008-07-27 21:46:04 +00006894 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006895}
6896
Dan Gohman475871a2008-07-27 21:46:04 +00006897SDValue
6898X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006899 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006900 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006901 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006902
Craig Topper7a9a28b2012-08-12 02:23:29 +00006903 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006904 return SDValue();
6905
Duncan Sands83ec4b62008-06-06 12:08:01 +00006906 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006907 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006908 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006909 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006910 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006911 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006912 }
6913
6914 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006915 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6916 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6917 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006918 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6919 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006920 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006921 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006922 Op.getOperand(0)),
6923 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006924 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006925 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006926 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006927 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006928 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006929 }
6930
6931 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006932 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6933 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006934 // result has a single use which is a store or a bitcast to i32. And in
6935 // the case of a store, it's not worth it if the index is a constant 0,
6936 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006937 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006938 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006939 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006940 if ((User->getOpcode() != ISD::STORE ||
6941 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6942 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006943 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006944 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006945 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006946 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006947 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006948 Op.getOperand(0)),
6949 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006950 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006951 }
6952
6953 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006954 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006955 if (isa<ConstantSDNode>(Op.getOperand(1)))
6956 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006957 }
Dan Gohman475871a2008-07-27 21:46:04 +00006958 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006959}
6960
6961
Dan Gohman475871a2008-07-27 21:46:04 +00006962SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006963X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6964 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006965 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006966 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006967
David Greene74a579d2011-02-10 16:57:36 +00006968 SDValue Vec = Op.getOperand(0);
6969 EVT VecVT = Vec.getValueType();
6970
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006971 // If this is a 256-bit vector result, first extract the 128-bit vector and
6972 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006973 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00006974 DebugLoc dl = Op.getNode()->getDebugLoc();
6975 unsigned NumElems = VecVT.getVectorNumElements();
6976 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006977 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6978
6979 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006980 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006981
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006982 if (IdxVal >= NumElems/2)
6983 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00006984 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006985 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00006986 }
6987
Craig Topper7a9a28b2012-08-12 02:23:29 +00006988 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00006989
Craig Topperd0a31172012-01-10 06:37:29 +00006990 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006991 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006992 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006993 return Res;
6994 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006995
Owen Andersone50ed302009-08-10 22:56:29 +00006996 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006997 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006998 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006999 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00007000 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007001 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00007002 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00007003 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7004 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007005 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007006 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007007 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007008 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007009 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007010 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007011 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007012 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007013 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007014 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007015 }
7016
7017 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007018 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007019 if (Idx == 0)
7020 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007021
Evan Cheng0db9fe62006-04-25 20:13:52 +00007022 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007023 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007024 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007025 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007026 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007027 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007028 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007029 }
7030
7031 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007032 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7033 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7034 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007035 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007036 if (Idx == 0)
7037 return Op;
7038
7039 // UNPCKHPD the element to the lowest double word, then movsd.
7040 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7041 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007042 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007043 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007044 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007045 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007046 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007047 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007048 }
7049
Dan Gohman475871a2008-07-27 21:46:04 +00007050 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007051}
7052
Dan Gohman475871a2008-07-27 21:46:04 +00007053SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007054X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7055 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007056 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007057 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007058 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007059
Dan Gohman475871a2008-07-27 21:46:04 +00007060 SDValue N0 = Op.getOperand(0);
7061 SDValue N1 = Op.getOperand(1);
7062 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007063
Craig Topper7a9a28b2012-08-12 02:23:29 +00007064 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007065 return SDValue();
7066
Dan Gohman8a55ce42009-09-23 21:02:20 +00007067 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007068 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007069 unsigned Opc;
7070 if (VT == MVT::v8i16)
7071 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007072 else if (VT == MVT::v16i8)
7073 Opc = X86ISD::PINSRB;
7074 else
7075 Opc = X86ISD::PINSRB;
7076
Nate Begeman14d12ca2008-02-11 04:19:36 +00007077 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7078 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 if (N1.getValueType() != MVT::i32)
7080 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7081 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007082 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007083 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007084 }
7085
7086 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007087 // Bits [7:6] of the constant are the source select. This will always be
7088 // zero here. The DAG Combiner may combine an extract_elt index into these
7089 // bits. For example (insert (extract, 3), 2) could be matched by putting
7090 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007091 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007092 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007093 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007094 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007095 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007096 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007097 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007098 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007099 }
7100
7101 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007102 // PINSR* works with constant index.
7103 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007104 }
Dan Gohman475871a2008-07-27 21:46:04 +00007105 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007106}
7107
Dan Gohman475871a2008-07-27 21:46:04 +00007108SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007109X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007110 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007111 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007112
David Greene6b381262011-02-09 15:32:06 +00007113 DebugLoc dl = Op.getDebugLoc();
7114 SDValue N0 = Op.getOperand(0);
7115 SDValue N1 = Op.getOperand(1);
7116 SDValue N2 = Op.getOperand(2);
7117
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007118 // If this is a 256-bit vector result, first extract the 128-bit vector,
7119 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007120 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007121 if (!isa<ConstantSDNode>(N2))
7122 return SDValue();
7123
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007124 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007125 unsigned NumElems = VT.getVectorNumElements();
7126 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007127 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007128
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007129 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007130 bool Upper = IdxVal >= NumElems/2;
7131 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7132 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007133
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007134 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007135 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007136 }
7137
Craig Topperd0a31172012-01-10 06:37:29 +00007138 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007139 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7140
Dan Gohman8a55ce42009-09-23 21:02:20 +00007141 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007142 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007143
Dan Gohman8a55ce42009-09-23 21:02:20 +00007144 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007145 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7146 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007147 if (N1.getValueType() != MVT::i32)
7148 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7149 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007150 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007151 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007152 }
Dan Gohman475871a2008-07-27 21:46:04 +00007153 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007154}
7155
Dan Gohman475871a2008-07-27 21:46:04 +00007156SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007157X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007158 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007159 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007160 EVT OpVT = Op.getValueType();
7161
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007162 // If this is a 256-bit vector result, first insert into a 128-bit
7163 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007164 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007165 // Insert into a 128-bit vector.
7166 EVT VT128 = EVT::getVectorVT(*Context,
7167 OpVT.getVectorElementType(),
7168 OpVT.getVectorNumElements() / 2);
7169
7170 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7171
7172 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007173 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007174 }
7175
Craig Topperd77d2fe2012-04-29 20:22:05 +00007176 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007177 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007178 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007179
Owen Anderson825b72b2009-08-11 20:47:22 +00007180 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007181 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007182 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007183 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007184}
7185
David Greene91585092011-01-26 15:38:49 +00007186// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7187// a simple subregister reference or explicit instructions to grab
7188// upper bits of a vector.
7189SDValue
7190X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7191 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007192 DebugLoc dl = Op.getNode()->getDebugLoc();
7193 SDValue Vec = Op.getNode()->getOperand(0);
7194 SDValue Idx = Op.getNode()->getOperand(1);
7195
Craig Topper7a9a28b2012-08-12 02:23:29 +00007196 if (Op.getNode()->getValueType(0).is128BitVector() &&
7197 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007198 isa<ConstantSDNode>(Idx)) {
7199 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7200 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007201 }
David Greene91585092011-01-26 15:38:49 +00007202 }
7203 return SDValue();
7204}
7205
David Greenecfe33c42011-01-26 19:13:22 +00007206// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7207// simple superregister reference or explicit instructions to insert
7208// the upper bits of a vector.
7209SDValue
7210X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7211 if (Subtarget->hasAVX()) {
7212 DebugLoc dl = Op.getNode()->getDebugLoc();
7213 SDValue Vec = Op.getNode()->getOperand(0);
7214 SDValue SubVec = Op.getNode()->getOperand(1);
7215 SDValue Idx = Op.getNode()->getOperand(2);
7216
Craig Topper7a9a28b2012-08-12 02:23:29 +00007217 if (Op.getNode()->getValueType(0).is256BitVector() &&
7218 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007219 isa<ConstantSDNode>(Idx)) {
7220 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7221 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007222 }
7223 }
7224 return SDValue();
7225}
7226
Bill Wendling056292f2008-09-16 21:48:12 +00007227// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7228// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7229// one of the above mentioned nodes. It has to be wrapped because otherwise
7230// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7231// be used to form addressing mode. These wrapped nodes will be selected
7232// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007233SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007234X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007235 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007236
Chris Lattner41621a22009-06-26 19:22:52 +00007237 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7238 // global base reg.
7239 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007240 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007241 CodeModel::Model M = getTargetMachine().getCodeModel();
7242
Chris Lattner4f066492009-07-11 20:29:19 +00007243 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007244 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007245 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007246 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007247 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007248 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007249 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007250
Evan Cheng1606e8e2009-03-13 07:51:59 +00007251 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007252 CP->getAlignment(),
7253 CP->getOffset(), OpFlag);
7254 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007255 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007256 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007257 if (OpFlag) {
7258 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007259 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007260 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007261 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007262 }
7263
7264 return Result;
7265}
7266
Dan Gohmand858e902010-04-17 15:26:15 +00007267SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007268 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007269
Chris Lattner18c59872009-06-27 04:16:01 +00007270 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7271 // global base reg.
7272 unsigned char OpFlag = 0;
7273 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007274 CodeModel::Model M = getTargetMachine().getCodeModel();
7275
Chris Lattner4f066492009-07-11 20:29:19 +00007276 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007277 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007278 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007279 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007280 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007281 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007282 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007283
Chris Lattner18c59872009-06-27 04:16:01 +00007284 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7285 OpFlag);
7286 DebugLoc DL = JT->getDebugLoc();
7287 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007288
Chris Lattner18c59872009-06-27 04:16:01 +00007289 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007290 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007291 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7292 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007293 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007294 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007295
Chris Lattner18c59872009-06-27 04:16:01 +00007296 return Result;
7297}
7298
7299SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007300X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007301 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007302
Chris Lattner18c59872009-06-27 04:16:01 +00007303 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7304 // global base reg.
7305 unsigned char OpFlag = 0;
7306 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007307 CodeModel::Model M = getTargetMachine().getCodeModel();
7308
Chris Lattner4f066492009-07-11 20:29:19 +00007309 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007310 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7311 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7312 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007313 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007314 } else if (Subtarget->isPICStyleGOT()) {
7315 OpFlag = X86II::MO_GOT;
7316 } else if (Subtarget->isPICStyleStubPIC()) {
7317 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7318 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7319 OpFlag = X86II::MO_DARWIN_NONLAZY;
7320 }
Eric Christopherfd179292009-08-27 18:07:15 +00007321
Chris Lattner18c59872009-06-27 04:16:01 +00007322 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007323
Chris Lattner18c59872009-06-27 04:16:01 +00007324 DebugLoc DL = Op.getDebugLoc();
7325 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007326
7327
Chris Lattner18c59872009-06-27 04:16:01 +00007328 // With PIC, the address is actually $g + Offset.
7329 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007330 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007331 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7332 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007333 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007334 Result);
7335 }
Eric Christopherfd179292009-08-27 18:07:15 +00007336
Eli Friedman586272d2011-08-11 01:48:05 +00007337 // For symbols that require a load from a stub to get the address, emit the
7338 // load.
7339 if (isGlobalStubReference(OpFlag))
7340 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007341 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007342
Chris Lattner18c59872009-06-27 04:16:01 +00007343 return Result;
7344}
7345
Dan Gohman475871a2008-07-27 21:46:04 +00007346SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007347X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007348 // Create the TargetBlockAddressAddress node.
7349 unsigned char OpFlags =
7350 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007351 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007352 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007353 DebugLoc dl = Op.getDebugLoc();
7354 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7355 /*isTarget=*/true, OpFlags);
7356
Dan Gohmanf705adb2009-10-30 01:28:02 +00007357 if (Subtarget->isPICStyleRIPRel() &&
7358 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007359 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7360 else
7361 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007362
Dan Gohman29cbade2009-11-20 23:18:13 +00007363 // With PIC, the address is actually $g + Offset.
7364 if (isGlobalRelativeToPICBase(OpFlags)) {
7365 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7366 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7367 Result);
7368 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007369
7370 return Result;
7371}
7372
7373SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007374X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007375 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007376 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007377 // Create the TargetGlobalAddress node, folding in the constant
7378 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007379 unsigned char OpFlags =
7380 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007381 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007382 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007383 if (OpFlags == X86II::MO_NO_FLAG &&
7384 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007385 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007386 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007387 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007388 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007389 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007390 }
Eric Christopherfd179292009-08-27 18:07:15 +00007391
Chris Lattner4f066492009-07-11 20:29:19 +00007392 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007393 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007394 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7395 else
7396 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007397
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007398 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007399 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007400 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7401 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007402 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007403 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007404
Chris Lattner36c25012009-07-10 07:34:39 +00007405 // For globals that require a load from a stub to get the address, emit the
7406 // load.
7407 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007408 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007409 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007410
Dan Gohman6520e202008-10-18 02:06:02 +00007411 // If there was a non-zero offset that we didn't fold, create an explicit
7412 // addition for it.
7413 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007414 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007415 DAG.getConstant(Offset, getPointerTy()));
7416
Evan Cheng0db9fe62006-04-25 20:13:52 +00007417 return Result;
7418}
7419
Evan Chengda43bcf2008-09-24 00:05:32 +00007420SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007421X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007422 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007423 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007424 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007425}
7426
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007427static SDValue
7428GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007429 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007430 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007431 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007432 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007433 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007434 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007435 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007436 GA->getOffset(),
7437 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007438
7439 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7440 : X86ISD::TLSADDR;
7441
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007442 if (InFlag) {
7443 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007444 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007445 } else {
7446 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007447 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007448 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007449
7450 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007451 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007452
Rafael Espindola15f1b662009-04-24 12:59:40 +00007453 SDValue Flag = Chain.getValue(1);
7454 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007455}
7456
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007457// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007458static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007459LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007460 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007461 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007462 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7463 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007464 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007465 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007466 InFlag = Chain.getValue(1);
7467
Chris Lattnerb903bed2009-06-26 21:20:29 +00007468 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007469}
7470
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007471// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007472static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007473LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007474 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007475 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7476 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007477}
7478
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007479static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7480 SelectionDAG &DAG,
7481 const EVT PtrVT,
7482 bool is64Bit) {
7483 DebugLoc dl = GA->getDebugLoc();
7484
7485 // Get the start address of the TLS block for this module.
7486 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7487 .getInfo<X86MachineFunctionInfo>();
7488 MFI->incNumLocalDynamicTLSAccesses();
7489
7490 SDValue Base;
7491 if (is64Bit) {
7492 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7493 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7494 } else {
7495 SDValue InFlag;
7496 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7497 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7498 InFlag = Chain.getValue(1);
7499 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7500 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7501 }
7502
7503 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7504 // of Base.
7505
7506 // Build x@dtpoff.
7507 unsigned char OperandFlags = X86II::MO_DTPOFF;
7508 unsigned WrapperKind = X86ISD::Wrapper;
7509 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7510 GA->getValueType(0),
7511 GA->getOffset(), OperandFlags);
7512 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7513
7514 // Add x@dtpoff with the base.
7515 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7516}
7517
Hans Wennborg228756c2012-05-11 10:11:01 +00007518// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007519static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007520 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007521 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007522 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007523
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007524 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7525 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7526 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007527
Michael J. Spencerec38de22010-10-10 22:04:20 +00007528 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007529 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007530 MachinePointerInfo(Ptr),
7531 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007532
Chris Lattnerb903bed2009-06-26 21:20:29 +00007533 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007534 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7535 // initialexec.
7536 unsigned WrapperKind = X86ISD::Wrapper;
7537 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007538 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007539 } else if (model == TLSModel::InitialExec) {
7540 if (is64Bit) {
7541 OperandFlags = X86II::MO_GOTTPOFF;
7542 WrapperKind = X86ISD::WrapperRIP;
7543 } else {
7544 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7545 }
Chris Lattner18c59872009-06-27 04:16:01 +00007546 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007547 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007548 }
Eric Christopherfd179292009-08-27 18:07:15 +00007549
Hans Wennborg228756c2012-05-11 10:11:01 +00007550 // emit "addl x@ntpoff,%eax" (local exec)
7551 // or "addl x@indntpoff,%eax" (initial exec)
7552 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007553 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007554 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007555 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007556 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007557
Hans Wennborg228756c2012-05-11 10:11:01 +00007558 if (model == TLSModel::InitialExec) {
7559 if (isPIC && !is64Bit) {
7560 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7561 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7562 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007563 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007564
7565 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7566 MachinePointerInfo::getGOT(), false, false, false,
7567 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007568 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007569
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007570 // The address of the thread local variable is the add of the thread
7571 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007572 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007573}
7574
Dan Gohman475871a2008-07-27 21:46:04 +00007575SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007576X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007577
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007578 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007579 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007580
Eric Christopher30ef0e52010-06-03 04:07:48 +00007581 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007582 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007583
Eric Christopher30ef0e52010-06-03 04:07:48 +00007584 switch (model) {
7585 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007586 if (Subtarget->is64Bit())
7587 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7588 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007589 case TLSModel::LocalDynamic:
7590 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7591 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007592 case TLSModel::InitialExec:
7593 case TLSModel::LocalExec:
7594 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007595 Subtarget->is64Bit(),
7596 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007597 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007598 llvm_unreachable("Unknown TLS model.");
7599 }
7600
7601 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007602 // Darwin only has one model of TLS. Lower to that.
7603 unsigned char OpFlag = 0;
7604 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7605 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007606
Eric Christopher30ef0e52010-06-03 04:07:48 +00007607 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7608 // global base reg.
7609 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7610 !Subtarget->is64Bit();
7611 if (PIC32)
7612 OpFlag = X86II::MO_TLVP_PIC_BASE;
7613 else
7614 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007615 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007616 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007617 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007618 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007619 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007620
Eric Christopher30ef0e52010-06-03 04:07:48 +00007621 // With PIC32, the address is actually $g + Offset.
7622 if (PIC32)
7623 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7624 DAG.getNode(X86ISD::GlobalBaseReg,
7625 DebugLoc(), getPointerTy()),
7626 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007627
Eric Christopher30ef0e52010-06-03 04:07:48 +00007628 // Lowering the machine isd will make sure everything is in the right
7629 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007630 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007631 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007632 SDValue Args[] = { Chain, Offset };
7633 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007634
Eric Christopher30ef0e52010-06-03 04:07:48 +00007635 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7636 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7637 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007638
Eric Christopher30ef0e52010-06-03 04:07:48 +00007639 // And our return value (tls address) is in the standard call return value
7640 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007641 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007642 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7643 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007644 }
7645
7646 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007647 // Just use the implicit TLS architecture
7648 // Need to generate someting similar to:
7649 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7650 // ; from TEB
7651 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7652 // mov rcx, qword [rdx+rcx*8]
7653 // mov eax, .tls$:tlsvar
7654 // [rax+rcx] contains the address
7655 // Windows 64bit: gs:0x58
7656 // Windows 32bit: fs:__tls_array
7657
7658 // If GV is an alias then use the aliasee for determining
7659 // thread-localness.
7660 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7661 GV = GA->resolveAliasedGlobal(false);
7662 DebugLoc dl = GA->getDebugLoc();
7663 SDValue Chain = DAG.getEntryNode();
7664
7665 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7666 // %gs:0x58 (64-bit).
7667 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7668 ? Type::getInt8PtrTy(*DAG.getContext(),
7669 256)
7670 : Type::getInt32PtrTy(*DAG.getContext(),
7671 257));
7672
7673 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7674 Subtarget->is64Bit()
7675 ? DAG.getIntPtrConstant(0x58)
7676 : DAG.getExternalSymbol("_tls_array",
7677 getPointerTy()),
7678 MachinePointerInfo(Ptr),
7679 false, false, false, 0);
7680
7681 // Load the _tls_index variable
7682 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7683 if (Subtarget->is64Bit())
7684 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7685 IDX, MachinePointerInfo(), MVT::i32,
7686 false, false, 0);
7687 else
7688 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7689 false, false, false, 0);
7690
7691 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007692 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007693 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7694
7695 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7696 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7697 false, false, false, 0);
7698
7699 // Get the offset of start of .tls section
7700 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7701 GA->getValueType(0),
7702 GA->getOffset(), X86II::MO_SECREL);
7703 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7704
7705 // The address of the thread local variable is the add of the thread
7706 // pointer with the offset of the variable.
7707 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007708 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007709
David Blaikie4d6ccb52012-01-20 21:51:11 +00007710 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007711}
7712
Evan Cheng0db9fe62006-04-25 20:13:52 +00007713
Chad Rosierb90d2a92012-01-03 23:19:12 +00007714/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7715/// and take a 2 x i32 value to shift plus a shift amount.
7716SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007717 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007718 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007719 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007720 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007721 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007722 SDValue ShOpLo = Op.getOperand(0);
7723 SDValue ShOpHi = Op.getOperand(1);
7724 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007725 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007726 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007727 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007728
Dan Gohman475871a2008-07-27 21:46:04 +00007729 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007730 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007731 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7732 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007733 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007734 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7735 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007736 }
Evan Chenge3413162006-01-09 18:33:28 +00007737
Owen Anderson825b72b2009-08-11 20:47:22 +00007738 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7739 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007740 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007741 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007742
Dan Gohman475871a2008-07-27 21:46:04 +00007743 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007744 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007745 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7746 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007747
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007748 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007749 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7750 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007751 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007752 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7753 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007754 }
7755
Dan Gohman475871a2008-07-27 21:46:04 +00007756 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007757 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007758}
Evan Chenga3195e82006-01-12 22:54:21 +00007759
Dan Gohmand858e902010-04-17 15:26:15 +00007760SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7761 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007762 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007763
Dale Johannesen0488fb62010-09-30 23:57:10 +00007764 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007765 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007766
Owen Anderson825b72b2009-08-11 20:47:22 +00007767 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007768 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007769
Eli Friedman36df4992009-05-27 00:47:34 +00007770 // These are really Legal; return the operand so the caller accepts it as
7771 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007772 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007773 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007774 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007775 Subtarget->is64Bit()) {
7776 return Op;
7777 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007778
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007779 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007780 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007781 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007782 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007783 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007784 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007785 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007786 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007787 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007788 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7789}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007790
Owen Andersone50ed302009-08-10 22:56:29 +00007791SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007792 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007793 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007794 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007795 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007796 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007797 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007798 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007799 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007800 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007801 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007802
Chris Lattner492a43e2010-09-22 01:28:21 +00007803 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007804
Stuart Hastings84be9582011-06-02 15:57:11 +00007805 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7806 MachineMemOperand *MMO;
7807 if (FI) {
7808 int SSFI = FI->getIndex();
7809 MMO =
7810 DAG.getMachineFunction()
7811 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7812 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7813 } else {
7814 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7815 StackSlot = StackSlot.getOperand(1);
7816 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007817 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007818 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7819 X86ISD::FILD, DL,
7820 Tys, Ops, array_lengthof(Ops),
7821 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007822
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007823 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007824 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007825 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007826
7827 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7828 // shouldn't be necessary except that RFP cannot be live across
7829 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007830 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007831 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7832 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007833 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007834 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007835 SDValue Ops[] = {
7836 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7837 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007838 MachineMemOperand *MMO =
7839 DAG.getMachineFunction()
7840 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007841 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007842
Chris Lattner492a43e2010-09-22 01:28:21 +00007843 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7844 Ops, array_lengthof(Ops),
7845 Op.getValueType(), MMO);
7846 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007847 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007848 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007849 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007850
Evan Cheng0db9fe62006-04-25 20:13:52 +00007851 return Result;
7852}
7853
Bill Wendling8b8a6362009-01-17 03:56:04 +00007854// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007855SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7856 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007857 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007858 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007859 movq %rax, %xmm0
7860 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7861 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7862 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007863 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007864 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007865 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007866 addpd %xmm1, %xmm0
7867 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007868 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007869
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007870 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007871 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007872
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007873 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007874 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7875 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007876 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007877
Chris Lattner97484792012-01-25 09:56:22 +00007878 SmallVector<Constant*,2> CV1;
7879 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007880 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007881 CV1.push_back(
7882 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7883 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007884 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007885
Bill Wendling397ae212012-01-05 02:13:20 +00007886 // Load the 64-bit value into an XMM register.
7887 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7888 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007889 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007890 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007891 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007892 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7893 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7894 CLod0);
7895
Owen Anderson825b72b2009-08-11 20:47:22 +00007896 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007897 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007898 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007899 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007900 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007901 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007902
Craig Topperd0a31172012-01-10 06:37:29 +00007903 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007904 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7905 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7906 } else {
7907 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7908 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7909 S2F, 0x4E, DAG);
7910 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7911 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7912 Sub);
7913 }
7914
7915 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007916 DAG.getIntPtrConstant(0));
7917}
7918
Bill Wendling8b8a6362009-01-17 03:56:04 +00007919// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007920SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7921 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007922 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007923 // FP constant to bias correct the final result.
7924 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007925 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007926
7927 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007928 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007929 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007930
Eli Friedmanf3704762011-08-29 21:15:46 +00007931 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007932 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007933
Owen Anderson825b72b2009-08-11 20:47:22 +00007934 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007935 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007936 DAG.getIntPtrConstant(0));
7937
7938 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007939 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007940 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007941 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007942 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007943 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007944 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007945 MVT::v2f64, Bias)));
7946 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007947 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007948 DAG.getIntPtrConstant(0));
7949
7950 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007951 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007952
7953 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007954 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007955
Craig Topper69947b92012-04-23 06:57:04 +00007956 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007957 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007958 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007959 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007960 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007961
7962 // Handle final rounding.
7963 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007964}
7965
Dan Gohmand858e902010-04-17 15:26:15 +00007966SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7967 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007968 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007969 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007970
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007971 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007972 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7973 // the optimization here.
7974 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007975 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007976
Owen Andersone50ed302009-08-10 22:56:29 +00007977 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007978 EVT DstVT = Op.getValueType();
7979 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007980 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007981 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007982 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007983 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00007984 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00007985
7986 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007987 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007988 if (SrcVT == MVT::i32) {
7989 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7990 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7991 getPointerTy(), StackSlot, WordOff);
7992 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007993 StackSlot, MachinePointerInfo(),
7994 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007995 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007996 OffsetSlot, MachinePointerInfo(),
7997 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007998 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7999 return Fild;
8000 }
8001
8002 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
8003 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008004 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008005 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008006 // For i64 source, we need to add the appropriate power of 2 if the input
8007 // was negative. This is the same as the optimization in
8008 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8009 // we must be careful to do the computation in x87 extended precision, not
8010 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008011 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8012 MachineMemOperand *MMO =
8013 DAG.getMachineFunction()
8014 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8015 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008016
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008017 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8018 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008019 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8020 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008021
8022 APInt FF(32, 0x5F800000ULL);
8023
8024 // Check whether the sign bit is set.
8025 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8026 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8027 ISD::SETLT);
8028
8029 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8030 SDValue FudgePtr = DAG.getConstantPool(
8031 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8032 getPointerTy());
8033
8034 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8035 SDValue Zero = DAG.getIntPtrConstant(0);
8036 SDValue Four = DAG.getIntPtrConstant(4);
8037 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8038 Zero, Four);
8039 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8040
8041 // Load the value out, extending it from f32 to f80.
8042 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008043 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008044 FudgePtr, MachinePointerInfo::getConstantPool(),
8045 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008046 // Extend everything to 80 bits to force it to be done on x87.
8047 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8048 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008049}
8050
Dan Gohman475871a2008-07-27 21:46:04 +00008051std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008052FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008053 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008054
Owen Andersone50ed302009-08-10 22:56:29 +00008055 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008056
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008057 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008058 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8059 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008060 }
8061
Owen Anderson825b72b2009-08-11 20:47:22 +00008062 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8063 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008064 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008065
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008066 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008067 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008068 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008069 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008070 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008071 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008072 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008073 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008074
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008075 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8076 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008077 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008078 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008079 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008080 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008081
Evan Cheng0db9fe62006-04-25 20:13:52 +00008082 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008083 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8084 Opc = X86ISD::WIN_FTOL;
8085 else
8086 switch (DstTy.getSimpleVT().SimpleTy) {
8087 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8088 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8089 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8090 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8091 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008092
Dan Gohman475871a2008-07-27 21:46:04 +00008093 SDValue Chain = DAG.getEntryNode();
8094 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008095 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008096 // FIXME This causes a redundant load/store if the SSE-class value is already
8097 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008098 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008099 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008100 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008101 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008102 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008103 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008104 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008105 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008106 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008107
Chris Lattner492a43e2010-09-22 01:28:21 +00008108 MachineMemOperand *MMO =
8109 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8110 MachineMemOperand::MOLoad, MemSize, MemSize);
8111 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8112 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008113 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008114 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008115 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8116 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008117
Chris Lattner07290932010-09-22 01:05:16 +00008118 MachineMemOperand *MMO =
8119 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8120 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008121
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008122 if (Opc != X86ISD::WIN_FTOL) {
8123 // Build the FP_TO_INT*_IN_MEM
8124 SDValue Ops[] = { Chain, Value, StackSlot };
8125 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8126 Ops, 3, DstTy, MMO);
8127 return std::make_pair(FIST, StackSlot);
8128 } else {
8129 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8130 DAG.getVTList(MVT::Other, MVT::Glue),
8131 Chain, Value);
8132 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8133 MVT::i32, ftol.getValue(1));
8134 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8135 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008136 SDValue Ops[] = { eax, edx };
8137 SDValue pair = IsReplace
8138 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8139 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008140 return std::make_pair(pair, SDValue());
8141 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008142}
8143
Dan Gohmand858e902010-04-17 15:26:15 +00008144SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8145 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008146 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008147 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008148
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008149 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8150 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008151 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008152 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8153 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008154
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008155 if (StackSlot.getNode())
8156 // Load the result.
8157 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8158 FIST, StackSlot, MachinePointerInfo(),
8159 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008160
8161 // The node is the result.
8162 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008163}
8164
Dan Gohmand858e902010-04-17 15:26:15 +00008165SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8166 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008167 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8168 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008169 SDValue FIST = Vals.first, StackSlot = Vals.second;
8170 assert(FIST.getNode() && "Unexpected failure");
8171
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008172 if (StackSlot.getNode())
8173 // Load the result.
8174 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8175 FIST, StackSlot, MachinePointerInfo(),
8176 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008177
8178 // The node is the result.
8179 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008180}
8181
Craig Topper43620672012-09-08 07:31:51 +00008182SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008183 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008184 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008185 EVT VT = Op.getValueType();
8186 EVT EltVT = VT;
Craig Topper43620672012-09-08 07:31:51 +00008187 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8188 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008189 EltVT = VT.getVectorElementType();
Craig Topper43620672012-09-08 07:31:51 +00008190 NumElts = VT.getVectorNumElements();
Evan Cheng0db9fe62006-04-25 20:13:52 +00008191 }
Craig Topper43620672012-09-08 07:31:51 +00008192 Constant *C;
8193 if (EltVT == MVT::f64)
8194 C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
8195 else
8196 C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
8197 C = ConstantVector::getSplat(NumElts, C);
8198 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8199 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008200 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008201 MachinePointerInfo::getConstantPool(),
Craig Topper43620672012-09-08 07:31:51 +00008202 false, false, false, Alignment);
8203 if (VT.isVector()) {
8204 MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
8205 return DAG.getNode(ISD::BITCAST, dl, VT,
8206 DAG.getNode(ISD::AND, dl, ANDVT,
8207 DAG.getNode(ISD::BITCAST, dl, ANDVT,
8208 Op.getOperand(0)),
8209 DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask)));
8210 }
Dale Johannesenace16102009-02-03 19:33:06 +00008211 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008212}
8213
Dan Gohmand858e902010-04-17 15:26:15 +00008214SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008215 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008216 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008217 EVT VT = Op.getValueType();
8218 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008219 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8220 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008221 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008222 NumElts = VT.getVectorNumElements();
8223 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008224 Constant *C;
8225 if (EltVT == MVT::f64)
8226 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8227 else
8228 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8229 C = ConstantVector::getSplat(NumElts, C);
Craig Toppercacd9d62012-09-08 07:46:05 +00008230 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy());
8231 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dale Johannesenace16102009-02-03 19:33:06 +00008232 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008233 MachinePointerInfo::getConstantPool(),
Craig Toppercacd9d62012-09-08 07:46:05 +00008234 false, false, false, Alignment);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008235 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008236 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008237 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008238 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008239 DAG.getNode(ISD::BITCAST, dl, XORVT,
8240 Op.getOperand(0)),
8241 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008242 }
Craig Topper69947b92012-04-23 06:57:04 +00008243
8244 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008245}
8246
Dan Gohmand858e902010-04-17 15:26:15 +00008247SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008248 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008249 SDValue Op0 = Op.getOperand(0);
8250 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008251 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008252 EVT VT = Op.getValueType();
8253 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008254
8255 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008256 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008257 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008258 SrcVT = VT;
8259 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008260 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008261 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008262 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008263 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008264 }
8265
8266 // At this point the operands and the result should have the same
8267 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008268
Evan Cheng68c47cb2007-01-05 07:55:56 +00008269 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008270 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008271 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008272 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8273 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008274 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008275 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8276 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8277 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8278 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008279 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008280 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008281 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008282 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008283 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008284 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008285 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008286
8287 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008288 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008289 // Op0 is MVT::f32, Op1 is MVT::f64.
8290 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8291 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8292 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008293 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008294 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008295 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008296 }
8297
Evan Cheng73d6cf12007-01-05 21:37:56 +00008298 // Clear first operand sign bit.
8299 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008300 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008301 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8302 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008303 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008304 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8305 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8306 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8307 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008308 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008309 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008310 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008311 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008312 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008313 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008314 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008315
8316 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008317 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008318}
8319
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008320SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8321 SDValue N0 = Op.getOperand(0);
8322 DebugLoc dl = Op.getDebugLoc();
8323 EVT VT = Op.getValueType();
8324
8325 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8326 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8327 DAG.getConstant(1, VT));
8328 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8329}
8330
Dan Gohman076aee32009-03-04 19:44:21 +00008331/// Emit nodes that will be selected as "test Op0,Op0", or something
8332/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008333SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008334 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008335 DebugLoc dl = Op.getDebugLoc();
8336
Dan Gohman31125812009-03-07 01:58:32 +00008337 // CF and OF aren't always set the way we want. Determine which
8338 // of these we need.
8339 bool NeedCF = false;
8340 bool NeedOF = false;
8341 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008342 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008343 case X86::COND_A: case X86::COND_AE:
8344 case X86::COND_B: case X86::COND_BE:
8345 NeedCF = true;
8346 break;
8347 case X86::COND_G: case X86::COND_GE:
8348 case X86::COND_L: case X86::COND_LE:
8349 case X86::COND_O: case X86::COND_NO:
8350 NeedOF = true;
8351 break;
Dan Gohman31125812009-03-07 01:58:32 +00008352 }
8353
Dan Gohman076aee32009-03-04 19:44:21 +00008354 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008355 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8356 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008357 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8358 // Emit a CMP with 0, which is the TEST pattern.
8359 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8360 DAG.getConstant(0, Op.getValueType()));
8361
8362 unsigned Opcode = 0;
8363 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008364
8365 // Truncate operations may prevent the merge of the SETCC instruction
8366 // and the arithmetic intruction before it. Attempt to truncate the operands
8367 // of the arithmetic instruction and use a reduced bit-width instruction.
8368 bool NeedTruncation = false;
8369 SDValue ArithOp = Op;
8370 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8371 SDValue Arith = Op->getOperand(0);
8372 // Both the trunc and the arithmetic op need to have one user each.
8373 if (Arith->hasOneUse())
8374 switch (Arith.getOpcode()) {
8375 default: break;
8376 case ISD::ADD:
8377 case ISD::SUB:
8378 case ISD::AND:
8379 case ISD::OR:
8380 case ISD::XOR: {
8381 NeedTruncation = true;
8382 ArithOp = Arith;
8383 }
8384 }
8385 }
8386
8387 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8388 // which may be the result of a CAST. We use the variable 'Op', which is the
8389 // non-casted variable when we check for possible users.
8390 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008391 case ISD::ADD:
8392 // Due to an isel shortcoming, be conservative if this add is likely to be
8393 // selected as part of a load-modify-store instruction. When the root node
8394 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8395 // uses of other nodes in the match, such as the ADD in this case. This
8396 // leads to the ADD being left around and reselected, with the result being
8397 // two adds in the output. Alas, even if none our users are stores, that
8398 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8399 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8400 // climbing the DAG back to the root, and it doesn't seem to be worth the
8401 // effort.
8402 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008403 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8404 if (UI->getOpcode() != ISD::CopyToReg &&
8405 UI->getOpcode() != ISD::SETCC &&
8406 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008407 goto default_case;
8408
8409 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008410 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008411 // An add of one will be selected as an INC.
8412 if (C->getAPIntValue() == 1) {
8413 Opcode = X86ISD::INC;
8414 NumOperands = 1;
8415 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008416 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008417
8418 // An add of negative one (subtract of one) will be selected as a DEC.
8419 if (C->getAPIntValue().isAllOnesValue()) {
8420 Opcode = X86ISD::DEC;
8421 NumOperands = 1;
8422 break;
8423 }
Dan Gohman076aee32009-03-04 19:44:21 +00008424 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008425
8426 // Otherwise use a regular EFLAGS-setting add.
8427 Opcode = X86ISD::ADD;
8428 NumOperands = 2;
8429 break;
8430 case ISD::AND: {
8431 // If the primary and result isn't used, don't bother using X86ISD::AND,
8432 // because a TEST instruction will be better.
8433 bool NonFlagUse = false;
8434 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8435 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8436 SDNode *User = *UI;
8437 unsigned UOpNo = UI.getOperandNo();
8438 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8439 // Look pass truncate.
8440 UOpNo = User->use_begin().getOperandNo();
8441 User = *User->use_begin();
8442 }
8443
8444 if (User->getOpcode() != ISD::BRCOND &&
8445 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008446 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008447 NonFlagUse = true;
8448 break;
8449 }
Dan Gohman076aee32009-03-04 19:44:21 +00008450 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008451
8452 if (!NonFlagUse)
8453 break;
8454 }
8455 // FALL THROUGH
8456 case ISD::SUB:
8457 case ISD::OR:
8458 case ISD::XOR:
8459 // Due to the ISEL shortcoming noted above, be conservative if this op is
8460 // likely to be selected as part of a load-modify-store instruction.
8461 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8462 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8463 if (UI->getOpcode() == ISD::STORE)
8464 goto default_case;
8465
8466 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008467 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008468 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008469 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008470 case ISD::OR: Opcode = X86ISD::OR; break;
8471 case ISD::XOR: Opcode = X86ISD::XOR; break;
8472 case ISD::AND: Opcode = X86ISD::AND; break;
8473 }
8474
8475 NumOperands = 2;
8476 break;
8477 case X86ISD::ADD:
8478 case X86ISD::SUB:
8479 case X86ISD::INC:
8480 case X86ISD::DEC:
8481 case X86ISD::OR:
8482 case X86ISD::XOR:
8483 case X86ISD::AND:
8484 return SDValue(Op.getNode(), 1);
8485 default:
8486 default_case:
8487 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008488 }
8489
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008490 // If we found that truncation is beneficial, perform the truncation and
8491 // update 'Op'.
8492 if (NeedTruncation) {
8493 EVT VT = Op.getValueType();
8494 SDValue WideVal = Op->getOperand(0);
8495 EVT WideVT = WideVal.getValueType();
8496 unsigned ConvertedOp = 0;
8497 // Use a target machine opcode to prevent further DAGCombine
8498 // optimizations that may separate the arithmetic operations
8499 // from the setcc node.
8500 switch (WideVal.getOpcode()) {
8501 default: break;
8502 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8503 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8504 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8505 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8506 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8507 }
8508
8509 if (ConvertedOp) {
8510 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8511 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8512 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8513 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8514 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8515 }
8516 }
8517 }
8518
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008519 if (Opcode == 0)
8520 // Emit a CMP with 0, which is the TEST pattern.
8521 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8522 DAG.getConstant(0, Op.getValueType()));
8523
8524 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8525 SmallVector<SDValue, 4> Ops;
8526 for (unsigned i = 0; i != NumOperands; ++i)
8527 Ops.push_back(Op.getOperand(i));
8528
8529 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8530 DAG.ReplaceAllUsesWith(Op, New);
8531 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008532}
8533
8534/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8535/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008536SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008537 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008538 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8539 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008540 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008541
8542 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008543 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8544 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8545 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8546 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8547 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8548 Op0, Op1);
8549 return SDValue(Sub.getNode(), 1);
8550 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008551 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008552}
8553
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008554/// Convert a comparison if required by the subtarget.
8555SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8556 SelectionDAG &DAG) const {
8557 // If the subtarget does not support the FUCOMI instruction, floating-point
8558 // comparisons have to be converted.
8559 if (Subtarget->hasCMov() ||
8560 Cmp.getOpcode() != X86ISD::CMP ||
8561 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8562 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8563 return Cmp;
8564
8565 // The instruction selector will select an FUCOM instruction instead of
8566 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8567 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8568 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8569 DebugLoc dl = Cmp.getDebugLoc();
8570 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8571 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8572 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8573 DAG.getConstant(8, MVT::i8));
8574 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8575 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8576}
8577
Evan Chengd40d03e2010-01-06 19:38:29 +00008578/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8579/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008580SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8581 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008582 SDValue Op0 = And.getOperand(0);
8583 SDValue Op1 = And.getOperand(1);
8584 if (Op0.getOpcode() == ISD::TRUNCATE)
8585 Op0 = Op0.getOperand(0);
8586 if (Op1.getOpcode() == ISD::TRUNCATE)
8587 Op1 = Op1.getOperand(0);
8588
Evan Chengd40d03e2010-01-06 19:38:29 +00008589 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008590 if (Op1.getOpcode() == ISD::SHL)
8591 std::swap(Op0, Op1);
8592 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008593 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8594 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008595 // If we looked past a truncate, check that it's only truncating away
8596 // known zeros.
8597 unsigned BitWidth = Op0.getValueSizeInBits();
8598 unsigned AndBitWidth = And.getValueSizeInBits();
8599 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008600 APInt Zeros, Ones;
8601 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008602 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8603 return SDValue();
8604 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008605 LHS = Op1;
8606 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008607 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008608 } else if (Op1.getOpcode() == ISD::Constant) {
8609 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008610 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008611 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008612
8613 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008614 LHS = AndLHS.getOperand(0);
8615 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008616 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008617
8618 // Use BT if the immediate can't be encoded in a TEST instruction.
8619 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8620 LHS = AndLHS;
8621 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8622 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008623 }
Evan Cheng0488db92007-09-25 01:57:46 +00008624
Evan Chengd40d03e2010-01-06 19:38:29 +00008625 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008626 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008627 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008628 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008629 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008630 // Also promote i16 to i32 for performance / code size reason.
8631 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008632 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008633 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008634
Evan Chengd40d03e2010-01-06 19:38:29 +00008635 // If the operand types disagree, extend the shift amount to match. Since
8636 // BT ignores high bits (like shifts) we can use anyextend.
8637 if (LHS.getValueType() != RHS.getValueType())
8638 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008639
Evan Chengd40d03e2010-01-06 19:38:29 +00008640 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8641 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8642 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8643 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008644 }
8645
Evan Cheng54de3ea2010-01-05 06:52:31 +00008646 return SDValue();
8647}
8648
Dan Gohmand858e902010-04-17 15:26:15 +00008649SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008650
8651 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8652
Evan Cheng54de3ea2010-01-05 06:52:31 +00008653 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8654 SDValue Op0 = Op.getOperand(0);
8655 SDValue Op1 = Op.getOperand(1);
8656 DebugLoc dl = Op.getDebugLoc();
8657 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8658
8659 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008660 // Lower (X & (1 << N)) == 0 to BT(X, N).
8661 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8662 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008663 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008664 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008665 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008666 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8667 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8668 if (NewSetCC.getNode())
8669 return NewSetCC;
8670 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008671
Chris Lattner481eebc2010-12-19 21:23:48 +00008672 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8673 // these.
8674 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008675 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008676 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8677 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008678
Chris Lattner481eebc2010-12-19 21:23:48 +00008679 // If the input is a setcc, then reuse the input setcc or use a new one with
8680 // the inverted condition.
8681 if (Op0.getOpcode() == X86ISD::SETCC) {
8682 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8683 bool Invert = (CC == ISD::SETNE) ^
8684 cast<ConstantSDNode>(Op1)->isNullValue();
8685 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008686
Evan Cheng2c755ba2010-02-27 07:36:59 +00008687 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008688 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8689 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8690 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008691 }
8692
Evan Chenge5b51ac2010-04-17 06:13:15 +00008693 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008694 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008695 if (X86CC == X86::COND_INVALID)
8696 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008697
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008698 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008699 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008700 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008701 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008702}
8703
Craig Topper89af15e2011-09-18 08:03:58 +00008704// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008705// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008706static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008707 EVT VT = Op.getValueType();
8708
Craig Topper7a9a28b2012-08-12 02:23:29 +00008709 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008710 "Unsupported value type for operation");
8711
Craig Topper66ddd152012-04-27 22:54:43 +00008712 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008713 DebugLoc dl = Op.getDebugLoc();
8714 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008715
8716 // Extract the LHS vectors
8717 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008718 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8719 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008720
8721 // Extract the RHS vectors
8722 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008723 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8724 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008725
8726 // Issue the operation on the smaller types and concatenate the result back
8727 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8728 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8729 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8730 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8731 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8732}
8733
8734
Dan Gohmand858e902010-04-17 15:26:15 +00008735SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008736 SDValue Cond;
8737 SDValue Op0 = Op.getOperand(0);
8738 SDValue Op1 = Op.getOperand(1);
8739 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008740 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008741 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8742 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008743 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008744
8745 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008746#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008747 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008748 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8749#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008750
Craig Topper523908d2012-08-13 02:34:03 +00008751 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008752 bool Swap = false;
8753
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008754 // SSE Condition code mapping:
8755 // 0 - EQ
8756 // 1 - LT
8757 // 2 - LE
8758 // 3 - UNORD
8759 // 4 - NEQ
8760 // 5 - NLT
8761 // 6 - NLE
8762 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008763 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008764 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008765 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008766 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008767 case ISD::SETOGT:
8768 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008769 case ISD::SETLT:
8770 case ISD::SETOLT: SSECC = 1; break;
8771 case ISD::SETOGE:
8772 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008773 case ISD::SETLE:
8774 case ISD::SETOLE: SSECC = 2; break;
8775 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008776 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008777 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008778 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008779 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008780 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008781 case ISD::SETUGT: SSECC = 6; break;
8782 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008783 case ISD::SETUEQ:
8784 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008785 }
8786 if (Swap)
8787 std::swap(Op0, Op1);
8788
Nate Begemanfb8ead02008-07-25 19:05:58 +00008789 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008790 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008791 unsigned CC0, CC1;
8792 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008793 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008794 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8795 } else {
8796 assert(SetCCOpcode == ISD::SETONE);
8797 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008798 }
Craig Topper523908d2012-08-13 02:34:03 +00008799
8800 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8801 DAG.getConstant(CC0, MVT::i8));
8802 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8803 DAG.getConstant(CC1, MVT::i8));
8804 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008805 }
8806 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008807 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8808 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008809 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008810
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008811 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008812 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008813 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008814
Nate Begeman30a0de92008-07-17 16:51:19 +00008815 // We are handling one of the integer comparisons here. Since SSE only has
8816 // GT and EQ comparisons for integer, swapping operands and multiple
8817 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008818 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008819 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008820
Nate Begeman30a0de92008-07-17 16:51:19 +00008821 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008822 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008823 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008824 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008825 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008826 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008827 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008828 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008829 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008830 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008831 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008832 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008833 }
8834 if (Swap)
8835 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008836
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008837 // Check that the operation in question is available (most are plain SSE2,
8838 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008839 if (VT == MVT::v2i64) {
8840 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8841 return SDValue();
8842 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8843 return SDValue();
8844 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008845
Nate Begeman30a0de92008-07-17 16:51:19 +00008846 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8847 // bits of the inputs before performing those operations.
8848 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008849 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008850 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8851 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008852 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008853 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8854 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008855 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8856 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008857 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008858
Dale Johannesenace16102009-02-03 19:33:06 +00008859 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008860
8861 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008862 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008863 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008864
Nate Begeman30a0de92008-07-17 16:51:19 +00008865 return Result;
8866}
Evan Cheng0488db92007-09-25 01:57:46 +00008867
Evan Cheng370e5342008-12-03 08:38:43 +00008868// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008869static bool isX86LogicalCmp(SDValue Op) {
8870 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008871 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8872 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008873 return true;
8874 if (Op.getResNo() == 1 &&
8875 (Opc == X86ISD::ADD ||
8876 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008877 Opc == X86ISD::ADC ||
8878 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008879 Opc == X86ISD::SMUL ||
8880 Opc == X86ISD::UMUL ||
8881 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008882 Opc == X86ISD::DEC ||
8883 Opc == X86ISD::OR ||
8884 Opc == X86ISD::XOR ||
8885 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008886 return true;
8887
Chris Lattner9637d5b2010-12-05 07:49:54 +00008888 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8889 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008890
Dan Gohman076aee32009-03-04 19:44:21 +00008891 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008892}
8893
Chris Lattnera2b56002010-12-05 01:23:24 +00008894static bool isZero(SDValue V) {
8895 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8896 return C && C->isNullValue();
8897}
8898
Chris Lattner96908b12010-12-05 02:00:51 +00008899static bool isAllOnes(SDValue V) {
8900 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8901 return C && C->isAllOnesValue();
8902}
8903
Evan Chengb64dd5f2012-08-07 22:21:00 +00008904static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
8905 if (V.getOpcode() != ISD::TRUNCATE)
8906 return false;
8907
8908 SDValue VOp0 = V.getOperand(0);
8909 unsigned InBits = VOp0.getValueSizeInBits();
8910 unsigned Bits = V.getValueSizeInBits();
8911 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
8912}
8913
Dan Gohmand858e902010-04-17 15:26:15 +00008914SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008915 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008916 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008917 SDValue Op1 = Op.getOperand(1);
8918 SDValue Op2 = Op.getOperand(2);
8919 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008920 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008921
Dan Gohman1a492952009-10-20 16:22:37 +00008922 if (Cond.getOpcode() == ISD::SETCC) {
8923 SDValue NewCond = LowerSETCC(Cond, DAG);
8924 if (NewCond.getNode())
8925 Cond = NewCond;
8926 }
Evan Cheng734503b2006-09-11 02:19:56 +00008927
Chris Lattnera2b56002010-12-05 01:23:24 +00008928 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008929 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008930 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008931 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008932 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008933 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8934 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008935 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008936
Chris Lattnera2b56002010-12-05 01:23:24 +00008937 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008938
8939 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008940 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8941 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008942
8943 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00008944 // Apply further optimizations for special cases
8945 // (select (x != 0), -1, 0) -> neg & sbb
8946 // (select (x == 0), 0, -1) -> neg & sbb
8947 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00008948 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00008949 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
8950 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00008951 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
8952 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00008953 CmpOp0);
8954 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8955 DAG.getConstant(X86::COND_B, MVT::i8),
8956 SDValue(Neg.getNode(), 1));
8957 return Res;
8958 }
8959
Chris Lattnera2b56002010-12-05 01:23:24 +00008960 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8961 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008962 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008963
Chris Lattner96908b12010-12-05 02:00:51 +00008964 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008965 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8966 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008967
Chris Lattner96908b12010-12-05 02:00:51 +00008968 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8969 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008970
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008971 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008972 if (N2C == 0 || !N2C->isNullValue())
8973 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8974 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008975 }
8976 }
8977
Chris Lattnera2b56002010-12-05 01:23:24 +00008978 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008979 if (Cond.getOpcode() == ISD::AND &&
8980 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8981 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008982 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008983 Cond = Cond.getOperand(0);
8984 }
8985
Evan Cheng3f41d662007-10-08 22:16:29 +00008986 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8987 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008988 unsigned CondOpcode = Cond.getOpcode();
8989 if (CondOpcode == X86ISD::SETCC ||
8990 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008991 CC = Cond.getOperand(0);
8992
Dan Gohman475871a2008-07-27 21:46:04 +00008993 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008994 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008995 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008996
Evan Cheng3f41d662007-10-08 22:16:29 +00008997 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008998 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008999 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00009000 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00009001
Chris Lattnerd1980a52009-03-12 06:52:53 +00009002 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
9003 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00009004 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009005 addTest = false;
9006 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009007 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9008 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9009 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9010 Cond.getOperand(0).getValueType() != MVT::i8)) {
9011 SDValue LHS = Cond.getOperand(0);
9012 SDValue RHS = Cond.getOperand(1);
9013 unsigned X86Opcode;
9014 unsigned X86Cond;
9015 SDVTList VTs;
9016 switch (CondOpcode) {
9017 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9018 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9019 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9020 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9021 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9022 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9023 default: llvm_unreachable("unexpected overflowing operator");
9024 }
9025 if (CondOpcode == ISD::UMULO)
9026 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9027 MVT::i32);
9028 else
9029 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9030
9031 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9032
9033 if (CondOpcode == ISD::UMULO)
9034 Cond = X86Op.getValue(2);
9035 else
9036 Cond = X86Op.getValue(1);
9037
9038 CC = DAG.getConstant(X86Cond, MVT::i8);
9039 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009040 }
9041
9042 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009043 // Look pass the truncate if the high bits are known zero.
9044 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9045 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009046
9047 // We know the result of AND is compared against zero. Try to match
9048 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009049 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009050 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009051 if (NewSetCC.getNode()) {
9052 CC = NewSetCC.getOperand(0);
9053 Cond = NewSetCC.getOperand(1);
9054 addTest = false;
9055 }
9056 }
9057 }
9058
9059 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009060 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009061 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009062 }
9063
Benjamin Kramere915ff32010-12-22 23:09:28 +00009064 // a < b ? -1 : 0 -> RES = ~setcc_carry
9065 // a < b ? 0 : -1 -> RES = setcc_carry
9066 // a >= b ? -1 : 0 -> RES = setcc_carry
9067 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009068 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009069 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009070 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9071
9072 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9073 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9074 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9075 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9076 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9077 return DAG.getNOT(DL, Res, Res.getValueType());
9078 return Res;
9079 }
9080 }
9081
Evan Cheng0488db92007-09-25 01:57:46 +00009082 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9083 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009084 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009085 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009086 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009087}
9088
Evan Cheng370e5342008-12-03 08:38:43 +00009089// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9090// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9091// from the AND / OR.
9092static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9093 Opc = Op.getOpcode();
9094 if (Opc != ISD::OR && Opc != ISD::AND)
9095 return false;
9096 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9097 Op.getOperand(0).hasOneUse() &&
9098 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9099 Op.getOperand(1).hasOneUse());
9100}
9101
Evan Cheng961d6d42009-02-02 08:19:07 +00009102// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9103// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009104static bool isXor1OfSetCC(SDValue Op) {
9105 if (Op.getOpcode() != ISD::XOR)
9106 return false;
9107 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9108 if (N1C && N1C->getAPIntValue() == 1) {
9109 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9110 Op.getOperand(0).hasOneUse();
9111 }
9112 return false;
9113}
9114
Dan Gohmand858e902010-04-17 15:26:15 +00009115SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009116 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009117 SDValue Chain = Op.getOperand(0);
9118 SDValue Cond = Op.getOperand(1);
9119 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009120 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009121 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009122 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009123
Dan Gohman1a492952009-10-20 16:22:37 +00009124 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009125 // Check for setcc([su]{add,sub,mul}o == 0).
9126 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9127 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9128 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9129 Cond.getOperand(0).getResNo() == 1 &&
9130 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9131 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9132 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9133 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9134 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9135 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9136 Inverted = true;
9137 Cond = Cond.getOperand(0);
9138 } else {
9139 SDValue NewCond = LowerSETCC(Cond, DAG);
9140 if (NewCond.getNode())
9141 Cond = NewCond;
9142 }
Dan Gohman1a492952009-10-20 16:22:37 +00009143 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009144#if 0
9145 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009146 else if (Cond.getOpcode() == X86ISD::ADD ||
9147 Cond.getOpcode() == X86ISD::SUB ||
9148 Cond.getOpcode() == X86ISD::SMUL ||
9149 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009150 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009151#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009152
Evan Chengad9c0a32009-12-15 00:53:42 +00009153 // Look pass (and (setcc_carry (cmp ...)), 1).
9154 if (Cond.getOpcode() == ISD::AND &&
9155 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9156 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009157 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009158 Cond = Cond.getOperand(0);
9159 }
9160
Evan Cheng3f41d662007-10-08 22:16:29 +00009161 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9162 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009163 unsigned CondOpcode = Cond.getOpcode();
9164 if (CondOpcode == X86ISD::SETCC ||
9165 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009166 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009167
Dan Gohman475871a2008-07-27 21:46:04 +00009168 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009169 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009170 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009171 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009172 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009173 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009174 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009175 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009176 default: break;
9177 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009178 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009179 // These can only come from an arithmetic instruction with overflow,
9180 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009181 Cond = Cond.getNode()->getOperand(1);
9182 addTest = false;
9183 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009184 }
Evan Cheng0488db92007-09-25 01:57:46 +00009185 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009186 }
9187 CondOpcode = Cond.getOpcode();
9188 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9189 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9190 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9191 Cond.getOperand(0).getValueType() != MVT::i8)) {
9192 SDValue LHS = Cond.getOperand(0);
9193 SDValue RHS = Cond.getOperand(1);
9194 unsigned X86Opcode;
9195 unsigned X86Cond;
9196 SDVTList VTs;
9197 switch (CondOpcode) {
9198 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9199 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9200 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9201 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9202 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9203 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9204 default: llvm_unreachable("unexpected overflowing operator");
9205 }
9206 if (Inverted)
9207 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9208 if (CondOpcode == ISD::UMULO)
9209 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9210 MVT::i32);
9211 else
9212 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9213
9214 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9215
9216 if (CondOpcode == ISD::UMULO)
9217 Cond = X86Op.getValue(2);
9218 else
9219 Cond = X86Op.getValue(1);
9220
9221 CC = DAG.getConstant(X86Cond, MVT::i8);
9222 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009223 } else {
9224 unsigned CondOpc;
9225 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9226 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009227 if (CondOpc == ISD::OR) {
9228 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9229 // two branches instead of an explicit OR instruction with a
9230 // separate test.
9231 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009232 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009233 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009234 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009235 Chain, Dest, CC, Cmp);
9236 CC = Cond.getOperand(1).getOperand(0);
9237 Cond = Cmp;
9238 addTest = false;
9239 }
9240 } else { // ISD::AND
9241 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9242 // two branches instead of an explicit AND instruction with a
9243 // separate test. However, we only do this if this block doesn't
9244 // have a fall-through edge, because this requires an explicit
9245 // jmp when the condition is false.
9246 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009247 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009248 Op.getNode()->hasOneUse()) {
9249 X86::CondCode CCode =
9250 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9251 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009252 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009253 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009254 // Look for an unconditional branch following this conditional branch.
9255 // We need this because we need to reverse the successors in order
9256 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009257 if (User->getOpcode() == ISD::BR) {
9258 SDValue FalseBB = User->getOperand(1);
9259 SDNode *NewBR =
9260 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009261 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009262 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009263 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009264
Dale Johannesene4d209d2009-02-03 20:21:25 +00009265 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009266 Chain, Dest, CC, Cmp);
9267 X86::CondCode CCode =
9268 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9269 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009270 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009271 Cond = Cmp;
9272 addTest = false;
9273 }
9274 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009275 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009276 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9277 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9278 // It should be transformed during dag combiner except when the condition
9279 // is set by a arithmetics with overflow node.
9280 X86::CondCode CCode =
9281 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9282 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009283 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009284 Cond = Cond.getOperand(0).getOperand(1);
9285 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009286 } else if (Cond.getOpcode() == ISD::SETCC &&
9287 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9288 // For FCMP_OEQ, we can emit
9289 // two branches instead of an explicit AND instruction with a
9290 // separate test. However, we only do this if this block doesn't
9291 // have a fall-through edge, because this requires an explicit
9292 // jmp when the condition is false.
9293 if (Op.getNode()->hasOneUse()) {
9294 SDNode *User = *Op.getNode()->use_begin();
9295 // Look for an unconditional branch following this conditional branch.
9296 // We need this because we need to reverse the successors in order
9297 // to implement FCMP_OEQ.
9298 if (User->getOpcode() == ISD::BR) {
9299 SDValue FalseBB = User->getOperand(1);
9300 SDNode *NewBR =
9301 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9302 assert(NewBR == User);
9303 (void)NewBR;
9304 Dest = FalseBB;
9305
9306 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9307 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009308 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009309 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9310 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9311 Chain, Dest, CC, Cmp);
9312 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9313 Cond = Cmp;
9314 addTest = false;
9315 }
9316 }
9317 } else if (Cond.getOpcode() == ISD::SETCC &&
9318 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9319 // For FCMP_UNE, we can emit
9320 // two branches instead of an explicit AND instruction with a
9321 // separate test. However, we only do this if this block doesn't
9322 // have a fall-through edge, because this requires an explicit
9323 // jmp when the condition is false.
9324 if (Op.getNode()->hasOneUse()) {
9325 SDNode *User = *Op.getNode()->use_begin();
9326 // Look for an unconditional branch following this conditional branch.
9327 // We need this because we need to reverse the successors in order
9328 // to implement FCMP_UNE.
9329 if (User->getOpcode() == ISD::BR) {
9330 SDValue FalseBB = User->getOperand(1);
9331 SDNode *NewBR =
9332 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9333 assert(NewBR == User);
9334 (void)NewBR;
9335
9336 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9337 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009338 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009339 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9340 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9341 Chain, Dest, CC, Cmp);
9342 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9343 Cond = Cmp;
9344 addTest = false;
9345 Dest = FalseBB;
9346 }
9347 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009348 }
Evan Cheng0488db92007-09-25 01:57:46 +00009349 }
9350
9351 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009352 // Look pass the truncate if the high bits are known zero.
9353 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9354 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009355
9356 // We know the result of AND is compared against zero. Try to match
9357 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009358 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009359 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9360 if (NewSetCC.getNode()) {
9361 CC = NewSetCC.getOperand(0);
9362 Cond = NewSetCC.getOperand(1);
9363 addTest = false;
9364 }
9365 }
9366 }
9367
9368 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009369 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009370 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009371 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009372 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009373 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009374 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009375}
9376
Anton Korobeynikove060b532007-04-17 19:34:00 +00009377
9378// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9379// Calls to _alloca is needed to probe the stack when allocating more than 4k
9380// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9381// that the guard pages used by the OS virtual memory manager are allocated in
9382// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009383SDValue
9384X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009385 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009386 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009387 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009388 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009389 "are being used");
9390 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009391 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009392
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009393 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009394 SDValue Chain = Op.getOperand(0);
9395 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009396 // FIXME: Ensure alignment here
9397
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009398 bool Is64Bit = Subtarget->is64Bit();
9399 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009400
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009401 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009402 MachineFunction &MF = DAG.getMachineFunction();
9403 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009404
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009405 if (Is64Bit) {
9406 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009407 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009408 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009409
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009410 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009411 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009412 if (I->hasNestAttr())
9413 report_fatal_error("Cannot use segmented stacks with functions that "
9414 "have nested arguments.");
9415 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009416
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009417 const TargetRegisterClass *AddrRegClass =
9418 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9419 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9420 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9421 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9422 DAG.getRegister(Vreg, SPTy));
9423 SDValue Ops1[2] = { Value, Chain };
9424 return DAG.getMergeValues(Ops1, 2, dl);
9425 } else {
9426 SDValue Flag;
9427 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009428
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009429 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9430 Flag = Chain.getValue(1);
9431 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009432
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009433 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9434 Flag = Chain.getValue(1);
9435
9436 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9437
9438 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9439 return DAG.getMergeValues(Ops1, 2, dl);
9440 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009441}
9442
Dan Gohmand858e902010-04-17 15:26:15 +00009443SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009444 MachineFunction &MF = DAG.getMachineFunction();
9445 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9446
Dan Gohman69de1932008-02-06 22:27:42 +00009447 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009448 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009449
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009450 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009451 // vastart just stores the address of the VarArgsFrameIndex slot into the
9452 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009453 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9454 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009455 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9456 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009457 }
9458
9459 // __va_list_tag:
9460 // gp_offset (0 - 6 * 8)
9461 // fp_offset (48 - 48 + 8 * 16)
9462 // overflow_arg_area (point to parameters coming in memory).
9463 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009464 SmallVector<SDValue, 8> MemOps;
9465 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009466 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009467 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009468 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9469 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009470 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009471 MemOps.push_back(Store);
9472
9473 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009474 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009475 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009476 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009477 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9478 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009479 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009480 MemOps.push_back(Store);
9481
9482 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009483 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009484 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009485 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9486 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009487 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9488 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009489 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009490 MemOps.push_back(Store);
9491
9492 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009493 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009494 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009495 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9496 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009497 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9498 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009499 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009500 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009501 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009502}
9503
Dan Gohmand858e902010-04-17 15:26:15 +00009504SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009505 assert(Subtarget->is64Bit() &&
9506 "LowerVAARG only handles 64-bit va_arg!");
9507 assert((Subtarget->isTargetLinux() ||
9508 Subtarget->isTargetDarwin()) &&
9509 "Unhandled target in LowerVAARG");
9510 assert(Op.getNode()->getNumOperands() == 4);
9511 SDValue Chain = Op.getOperand(0);
9512 SDValue SrcPtr = Op.getOperand(1);
9513 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9514 unsigned Align = Op.getConstantOperandVal(3);
9515 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009516
Dan Gohman320afb82010-10-12 18:00:49 +00009517 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009518 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009519 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9520 uint8_t ArgMode;
9521
9522 // Decide which area this value should be read from.
9523 // TODO: Implement the AMD64 ABI in its entirety. This simple
9524 // selection mechanism works only for the basic types.
9525 if (ArgVT == MVT::f80) {
9526 llvm_unreachable("va_arg for f80 not yet implemented");
9527 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9528 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9529 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9530 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9531 } else {
9532 llvm_unreachable("Unhandled argument type in LowerVAARG");
9533 }
9534
9535 if (ArgMode == 2) {
9536 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009537 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009538 !(DAG.getMachineFunction()
9539 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009540 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009541 }
9542
9543 // Insert VAARG_64 node into the DAG
9544 // VAARG_64 returns two values: Variable Argument Address, Chain
9545 SmallVector<SDValue, 11> InstOps;
9546 InstOps.push_back(Chain);
9547 InstOps.push_back(SrcPtr);
9548 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9549 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9550 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9551 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9552 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9553 VTs, &InstOps[0], InstOps.size(),
9554 MVT::i64,
9555 MachinePointerInfo(SV),
9556 /*Align=*/0,
9557 /*Volatile=*/false,
9558 /*ReadMem=*/true,
9559 /*WriteMem=*/true);
9560 Chain = VAARG.getValue(1);
9561
9562 // Load the next argument and return it
9563 return DAG.getLoad(ArgVT, dl,
9564 Chain,
9565 VAARG,
9566 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009567 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009568}
9569
Dan Gohmand858e902010-04-17 15:26:15 +00009570SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009571 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009572 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009573 SDValue Chain = Op.getOperand(0);
9574 SDValue DstPtr = Op.getOperand(1);
9575 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009576 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9577 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009578 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009579
Chris Lattnere72f2022010-09-21 05:40:29 +00009580 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009581 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009582 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009583 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009584}
9585
Craig Topper80e46362012-01-23 06:16:53 +00009586// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9587// may or may not be a constant. Takes immediate version of shift as input.
9588static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9589 SDValue SrcOp, SDValue ShAmt,
9590 SelectionDAG &DAG) {
9591 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9592
9593 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009594 // Constant may be a TargetConstant. Use a regular constant.
9595 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009596 switch (Opc) {
9597 default: llvm_unreachable("Unknown target vector shift node");
9598 case X86ISD::VSHLI:
9599 case X86ISD::VSRLI:
9600 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009601 return DAG.getNode(Opc, dl, VT, SrcOp,
9602 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009603 }
9604 }
9605
9606 // Change opcode to non-immediate version
9607 switch (Opc) {
9608 default: llvm_unreachable("Unknown target vector shift node");
9609 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9610 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9611 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9612 }
9613
9614 // Need to build a vector containing shift amount
9615 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9616 SDValue ShOps[4];
9617 ShOps[0] = ShAmt;
9618 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009619 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009620 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009621
9622 // The return type has to be a 128-bit type with the same element
9623 // type as the input type.
9624 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9625 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9626
9627 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009628 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9629}
9630
Dan Gohman475871a2008-07-27 21:46:04 +00009631SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009632X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009633 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009634 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009635 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009636 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009637 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009638 case Intrinsic::x86_sse_comieq_ss:
9639 case Intrinsic::x86_sse_comilt_ss:
9640 case Intrinsic::x86_sse_comile_ss:
9641 case Intrinsic::x86_sse_comigt_ss:
9642 case Intrinsic::x86_sse_comige_ss:
9643 case Intrinsic::x86_sse_comineq_ss:
9644 case Intrinsic::x86_sse_ucomieq_ss:
9645 case Intrinsic::x86_sse_ucomilt_ss:
9646 case Intrinsic::x86_sse_ucomile_ss:
9647 case Intrinsic::x86_sse_ucomigt_ss:
9648 case Intrinsic::x86_sse_ucomige_ss:
9649 case Intrinsic::x86_sse_ucomineq_ss:
9650 case Intrinsic::x86_sse2_comieq_sd:
9651 case Intrinsic::x86_sse2_comilt_sd:
9652 case Intrinsic::x86_sse2_comile_sd:
9653 case Intrinsic::x86_sse2_comigt_sd:
9654 case Intrinsic::x86_sse2_comige_sd:
9655 case Intrinsic::x86_sse2_comineq_sd:
9656 case Intrinsic::x86_sse2_ucomieq_sd:
9657 case Intrinsic::x86_sse2_ucomilt_sd:
9658 case Intrinsic::x86_sse2_ucomile_sd:
9659 case Intrinsic::x86_sse2_ucomigt_sd:
9660 case Intrinsic::x86_sse2_ucomige_sd:
9661 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009662 unsigned Opc;
9663 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009664 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009665 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009666 case Intrinsic::x86_sse_comieq_ss:
9667 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009668 Opc = X86ISD::COMI;
9669 CC = ISD::SETEQ;
9670 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009671 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009672 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009673 Opc = X86ISD::COMI;
9674 CC = ISD::SETLT;
9675 break;
9676 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009677 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009678 Opc = X86ISD::COMI;
9679 CC = ISD::SETLE;
9680 break;
9681 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009682 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009683 Opc = X86ISD::COMI;
9684 CC = ISD::SETGT;
9685 break;
9686 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009687 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009688 Opc = X86ISD::COMI;
9689 CC = ISD::SETGE;
9690 break;
9691 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009692 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009693 Opc = X86ISD::COMI;
9694 CC = ISD::SETNE;
9695 break;
9696 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009697 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009698 Opc = X86ISD::UCOMI;
9699 CC = ISD::SETEQ;
9700 break;
9701 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009702 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009703 Opc = X86ISD::UCOMI;
9704 CC = ISD::SETLT;
9705 break;
9706 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009707 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009708 Opc = X86ISD::UCOMI;
9709 CC = ISD::SETLE;
9710 break;
9711 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009712 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009713 Opc = X86ISD::UCOMI;
9714 CC = ISD::SETGT;
9715 break;
9716 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009717 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009718 Opc = X86ISD::UCOMI;
9719 CC = ISD::SETGE;
9720 break;
9721 case Intrinsic::x86_sse_ucomineq_ss:
9722 case Intrinsic::x86_sse2_ucomineq_sd:
9723 Opc = X86ISD::UCOMI;
9724 CC = ISD::SETNE;
9725 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009726 }
Evan Cheng734503b2006-09-11 02:19:56 +00009727
Dan Gohman475871a2008-07-27 21:46:04 +00009728 SDValue LHS = Op.getOperand(1);
9729 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009730 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009731 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009732 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9733 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9734 DAG.getConstant(X86CC, MVT::i8), Cond);
9735 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009736 }
Craig Topper6d688152012-08-14 07:43:25 +00009737
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009738 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009739 case Intrinsic::x86_sse2_pmulu_dq:
9740 case Intrinsic::x86_avx2_pmulu_dq:
9741 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9742 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009743
9744 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009745 case Intrinsic::x86_sse3_hadd_ps:
9746 case Intrinsic::x86_sse3_hadd_pd:
9747 case Intrinsic::x86_avx_hadd_ps_256:
9748 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009749 case Intrinsic::x86_sse3_hsub_ps:
9750 case Intrinsic::x86_sse3_hsub_pd:
9751 case Intrinsic::x86_avx_hsub_ps_256:
9752 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009753 case Intrinsic::x86_ssse3_phadd_w_128:
9754 case Intrinsic::x86_ssse3_phadd_d_128:
9755 case Intrinsic::x86_avx2_phadd_w:
9756 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009757 case Intrinsic::x86_ssse3_phsub_w_128:
9758 case Intrinsic::x86_ssse3_phsub_d_128:
9759 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009760 case Intrinsic::x86_avx2_phsub_d: {
9761 unsigned Opcode;
9762 switch (IntNo) {
9763 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9764 case Intrinsic::x86_sse3_hadd_ps:
9765 case Intrinsic::x86_sse3_hadd_pd:
9766 case Intrinsic::x86_avx_hadd_ps_256:
9767 case Intrinsic::x86_avx_hadd_pd_256:
9768 Opcode = X86ISD::FHADD;
9769 break;
9770 case Intrinsic::x86_sse3_hsub_ps:
9771 case Intrinsic::x86_sse3_hsub_pd:
9772 case Intrinsic::x86_avx_hsub_ps_256:
9773 case Intrinsic::x86_avx_hsub_pd_256:
9774 Opcode = X86ISD::FHSUB;
9775 break;
9776 case Intrinsic::x86_ssse3_phadd_w_128:
9777 case Intrinsic::x86_ssse3_phadd_d_128:
9778 case Intrinsic::x86_avx2_phadd_w:
9779 case Intrinsic::x86_avx2_phadd_d:
9780 Opcode = X86ISD::HADD;
9781 break;
9782 case Intrinsic::x86_ssse3_phsub_w_128:
9783 case Intrinsic::x86_ssse3_phsub_d_128:
9784 case Intrinsic::x86_avx2_phsub_w:
9785 case Intrinsic::x86_avx2_phsub_d:
9786 Opcode = X86ISD::HSUB;
9787 break;
9788 }
9789 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009790 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009791 }
9792
9793 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009794 case Intrinsic::x86_avx2_psllv_d:
9795 case Intrinsic::x86_avx2_psllv_q:
9796 case Intrinsic::x86_avx2_psllv_d_256:
9797 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009798 case Intrinsic::x86_avx2_psrlv_d:
9799 case Intrinsic::x86_avx2_psrlv_q:
9800 case Intrinsic::x86_avx2_psrlv_d_256:
9801 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009802 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009803 case Intrinsic::x86_avx2_psrav_d_256: {
9804 unsigned Opcode;
9805 switch (IntNo) {
9806 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9807 case Intrinsic::x86_avx2_psllv_d:
9808 case Intrinsic::x86_avx2_psllv_q:
9809 case Intrinsic::x86_avx2_psllv_d_256:
9810 case Intrinsic::x86_avx2_psllv_q_256:
9811 Opcode = ISD::SHL;
9812 break;
9813 case Intrinsic::x86_avx2_psrlv_d:
9814 case Intrinsic::x86_avx2_psrlv_q:
9815 case Intrinsic::x86_avx2_psrlv_d_256:
9816 case Intrinsic::x86_avx2_psrlv_q_256:
9817 Opcode = ISD::SRL;
9818 break;
9819 case Intrinsic::x86_avx2_psrav_d:
9820 case Intrinsic::x86_avx2_psrav_d_256:
9821 Opcode = ISD::SRA;
9822 break;
9823 }
9824 return DAG.getNode(Opcode, dl, Op.getValueType(),
9825 Op.getOperand(1), Op.getOperand(2));
9826 }
9827
Craig Topper969ba282012-01-25 06:43:11 +00009828 case Intrinsic::x86_ssse3_pshuf_b_128:
9829 case Intrinsic::x86_avx2_pshuf_b:
9830 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9831 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009832
Craig Topper969ba282012-01-25 06:43:11 +00009833 case Intrinsic::x86_ssse3_psign_b_128:
9834 case Intrinsic::x86_ssse3_psign_w_128:
9835 case Intrinsic::x86_ssse3_psign_d_128:
9836 case Intrinsic::x86_avx2_psign_b:
9837 case Intrinsic::x86_avx2_psign_w:
9838 case Intrinsic::x86_avx2_psign_d:
9839 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9840 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009841
Craig Toppere566cd02012-01-26 07:18:03 +00009842 case Intrinsic::x86_sse41_insertps:
9843 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9844 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009845
Craig Toppere566cd02012-01-26 07:18:03 +00009846 case Intrinsic::x86_avx_vperm2f128_ps_256:
9847 case Intrinsic::x86_avx_vperm2f128_pd_256:
9848 case Intrinsic::x86_avx_vperm2f128_si_256:
9849 case Intrinsic::x86_avx2_vperm2i128:
9850 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9851 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009852
Craig Topperffa6c402012-04-16 07:13:00 +00009853 case Intrinsic::x86_avx2_permd:
9854 case Intrinsic::x86_avx2_permps:
9855 // Operands intentionally swapped. Mask is last operand to intrinsic,
9856 // but second operand for node/intruction.
9857 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9858 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009859
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009860 // ptest and testp intrinsics. The intrinsic these come from are designed to
9861 // return an integer value, not just an instruction so lower it to the ptest
9862 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009863 case Intrinsic::x86_sse41_ptestz:
9864 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009865 case Intrinsic::x86_sse41_ptestnzc:
9866 case Intrinsic::x86_avx_ptestz_256:
9867 case Intrinsic::x86_avx_ptestc_256:
9868 case Intrinsic::x86_avx_ptestnzc_256:
9869 case Intrinsic::x86_avx_vtestz_ps:
9870 case Intrinsic::x86_avx_vtestc_ps:
9871 case Intrinsic::x86_avx_vtestnzc_ps:
9872 case Intrinsic::x86_avx_vtestz_pd:
9873 case Intrinsic::x86_avx_vtestc_pd:
9874 case Intrinsic::x86_avx_vtestnzc_pd:
9875 case Intrinsic::x86_avx_vtestz_ps_256:
9876 case Intrinsic::x86_avx_vtestc_ps_256:
9877 case Intrinsic::x86_avx_vtestnzc_ps_256:
9878 case Intrinsic::x86_avx_vtestz_pd_256:
9879 case Intrinsic::x86_avx_vtestc_pd_256:
9880 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9881 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +00009882 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +00009883 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009884 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009885 case Intrinsic::x86_avx_vtestz_ps:
9886 case Intrinsic::x86_avx_vtestz_pd:
9887 case Intrinsic::x86_avx_vtestz_ps_256:
9888 case Intrinsic::x86_avx_vtestz_pd_256:
9889 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009890 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009891 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009892 // ZF = 1
9893 X86CC = X86::COND_E;
9894 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009895 case Intrinsic::x86_avx_vtestc_ps:
9896 case Intrinsic::x86_avx_vtestc_pd:
9897 case Intrinsic::x86_avx_vtestc_ps_256:
9898 case Intrinsic::x86_avx_vtestc_pd_256:
9899 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009900 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009901 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009902 // CF = 1
9903 X86CC = X86::COND_B;
9904 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009905 case Intrinsic::x86_avx_vtestnzc_ps:
9906 case Intrinsic::x86_avx_vtestnzc_pd:
9907 case Intrinsic::x86_avx_vtestnzc_ps_256:
9908 case Intrinsic::x86_avx_vtestnzc_pd_256:
9909 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009910 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009911 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009912 // ZF and CF = 0
9913 X86CC = X86::COND_A;
9914 break;
9915 }
Eric Christopherfd179292009-08-27 18:07:15 +00009916
Eric Christopher71c67532009-07-29 00:28:05 +00009917 SDValue LHS = Op.getOperand(1);
9918 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009919 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9920 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009921 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9922 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9923 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009924 }
Evan Cheng5759f972008-05-04 09:15:50 +00009925
Craig Topper80e46362012-01-23 06:16:53 +00009926 // SSE/AVX shift intrinsics
9927 case Intrinsic::x86_sse2_psll_w:
9928 case Intrinsic::x86_sse2_psll_d:
9929 case Intrinsic::x86_sse2_psll_q:
9930 case Intrinsic::x86_avx2_psll_w:
9931 case Intrinsic::x86_avx2_psll_d:
9932 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +00009933 case Intrinsic::x86_sse2_psrl_w:
9934 case Intrinsic::x86_sse2_psrl_d:
9935 case Intrinsic::x86_sse2_psrl_q:
9936 case Intrinsic::x86_avx2_psrl_w:
9937 case Intrinsic::x86_avx2_psrl_d:
9938 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +00009939 case Intrinsic::x86_sse2_psra_w:
9940 case Intrinsic::x86_sse2_psra_d:
9941 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +00009942 case Intrinsic::x86_avx2_psra_d: {
9943 unsigned Opcode;
9944 switch (IntNo) {
9945 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9946 case Intrinsic::x86_sse2_psll_w:
9947 case Intrinsic::x86_sse2_psll_d:
9948 case Intrinsic::x86_sse2_psll_q:
9949 case Intrinsic::x86_avx2_psll_w:
9950 case Intrinsic::x86_avx2_psll_d:
9951 case Intrinsic::x86_avx2_psll_q:
9952 Opcode = X86ISD::VSHL;
9953 break;
9954 case Intrinsic::x86_sse2_psrl_w:
9955 case Intrinsic::x86_sse2_psrl_d:
9956 case Intrinsic::x86_sse2_psrl_q:
9957 case Intrinsic::x86_avx2_psrl_w:
9958 case Intrinsic::x86_avx2_psrl_d:
9959 case Intrinsic::x86_avx2_psrl_q:
9960 Opcode = X86ISD::VSRL;
9961 break;
9962 case Intrinsic::x86_sse2_psra_w:
9963 case Intrinsic::x86_sse2_psra_d:
9964 case Intrinsic::x86_avx2_psra_w:
9965 case Intrinsic::x86_avx2_psra_d:
9966 Opcode = X86ISD::VSRA;
9967 break;
9968 }
9969 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +00009970 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009971 }
9972
9973 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +00009974 case Intrinsic::x86_sse2_pslli_w:
9975 case Intrinsic::x86_sse2_pslli_d:
9976 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009977 case Intrinsic::x86_avx2_pslli_w:
9978 case Intrinsic::x86_avx2_pslli_d:
9979 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009980 case Intrinsic::x86_sse2_psrli_w:
9981 case Intrinsic::x86_sse2_psrli_d:
9982 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009983 case Intrinsic::x86_avx2_psrli_w:
9984 case Intrinsic::x86_avx2_psrli_d:
9985 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009986 case Intrinsic::x86_sse2_psrai_w:
9987 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +00009988 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +00009989 case Intrinsic::x86_avx2_psrai_d: {
9990 unsigned Opcode;
9991 switch (IntNo) {
9992 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9993 case Intrinsic::x86_sse2_pslli_w:
9994 case Intrinsic::x86_sse2_pslli_d:
9995 case Intrinsic::x86_sse2_pslli_q:
9996 case Intrinsic::x86_avx2_pslli_w:
9997 case Intrinsic::x86_avx2_pslli_d:
9998 case Intrinsic::x86_avx2_pslli_q:
9999 Opcode = X86ISD::VSHLI;
10000 break;
10001 case Intrinsic::x86_sse2_psrli_w:
10002 case Intrinsic::x86_sse2_psrli_d:
10003 case Intrinsic::x86_sse2_psrli_q:
10004 case Intrinsic::x86_avx2_psrli_w:
10005 case Intrinsic::x86_avx2_psrli_d:
10006 case Intrinsic::x86_avx2_psrli_q:
10007 Opcode = X86ISD::VSRLI;
10008 break;
10009 case Intrinsic::x86_sse2_psrai_w:
10010 case Intrinsic::x86_sse2_psrai_d:
10011 case Intrinsic::x86_avx2_psrai_w:
10012 case Intrinsic::x86_avx2_psrai_d:
10013 Opcode = X86ISD::VSRAI;
10014 break;
10015 }
10016 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010017 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010018 }
10019
Craig Topper4feb6472012-08-06 06:22:36 +000010020 case Intrinsic::x86_sse42_pcmpistria128:
10021 case Intrinsic::x86_sse42_pcmpestria128:
10022 case Intrinsic::x86_sse42_pcmpistric128:
10023 case Intrinsic::x86_sse42_pcmpestric128:
10024 case Intrinsic::x86_sse42_pcmpistrio128:
10025 case Intrinsic::x86_sse42_pcmpestrio128:
10026 case Intrinsic::x86_sse42_pcmpistris128:
10027 case Intrinsic::x86_sse42_pcmpestris128:
10028 case Intrinsic::x86_sse42_pcmpistriz128:
10029 case Intrinsic::x86_sse42_pcmpestriz128: {
10030 unsigned Opcode;
10031 unsigned X86CC;
10032 switch (IntNo) {
10033 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10034 case Intrinsic::x86_sse42_pcmpistria128:
10035 Opcode = X86ISD::PCMPISTRI;
10036 X86CC = X86::COND_A;
10037 break;
10038 case Intrinsic::x86_sse42_pcmpestria128:
10039 Opcode = X86ISD::PCMPESTRI;
10040 X86CC = X86::COND_A;
10041 break;
10042 case Intrinsic::x86_sse42_pcmpistric128:
10043 Opcode = X86ISD::PCMPISTRI;
10044 X86CC = X86::COND_B;
10045 break;
10046 case Intrinsic::x86_sse42_pcmpestric128:
10047 Opcode = X86ISD::PCMPESTRI;
10048 X86CC = X86::COND_B;
10049 break;
10050 case Intrinsic::x86_sse42_pcmpistrio128:
10051 Opcode = X86ISD::PCMPISTRI;
10052 X86CC = X86::COND_O;
10053 break;
10054 case Intrinsic::x86_sse42_pcmpestrio128:
10055 Opcode = X86ISD::PCMPESTRI;
10056 X86CC = X86::COND_O;
10057 break;
10058 case Intrinsic::x86_sse42_pcmpistris128:
10059 Opcode = X86ISD::PCMPISTRI;
10060 X86CC = X86::COND_S;
10061 break;
10062 case Intrinsic::x86_sse42_pcmpestris128:
10063 Opcode = X86ISD::PCMPESTRI;
10064 X86CC = X86::COND_S;
10065 break;
10066 case Intrinsic::x86_sse42_pcmpistriz128:
10067 Opcode = X86ISD::PCMPISTRI;
10068 X86CC = X86::COND_E;
10069 break;
10070 case Intrinsic::x86_sse42_pcmpestriz128:
10071 Opcode = X86ISD::PCMPESTRI;
10072 X86CC = X86::COND_E;
10073 break;
10074 }
10075 SmallVector<SDValue, 5> NewOps;
10076 NewOps.append(Op->op_begin()+1, Op->op_end());
10077 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10078 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10079 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10080 DAG.getConstant(X86CC, MVT::i8),
10081 SDValue(PCMP.getNode(), 1));
10082 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10083 }
Craig Topper6d688152012-08-14 07:43:25 +000010084
Craig Topper4feb6472012-08-06 06:22:36 +000010085 case Intrinsic::x86_sse42_pcmpistri128:
10086 case Intrinsic::x86_sse42_pcmpestri128: {
10087 unsigned Opcode;
10088 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10089 Opcode = X86ISD::PCMPISTRI;
10090 else
10091 Opcode = X86ISD::PCMPESTRI;
10092
10093 SmallVector<SDValue, 5> NewOps;
10094 NewOps.append(Op->op_begin()+1, Op->op_end());
10095 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10096 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10097 }
Craig Topper0e292372012-08-24 04:03:22 +000010098 case Intrinsic::x86_fma_vfmadd_ps:
10099 case Intrinsic::x86_fma_vfmadd_pd:
10100 case Intrinsic::x86_fma_vfmsub_ps:
10101 case Intrinsic::x86_fma_vfmsub_pd:
10102 case Intrinsic::x86_fma_vfnmadd_ps:
10103 case Intrinsic::x86_fma_vfnmadd_pd:
10104 case Intrinsic::x86_fma_vfnmsub_ps:
10105 case Intrinsic::x86_fma_vfnmsub_pd:
10106 case Intrinsic::x86_fma_vfmaddsub_ps:
10107 case Intrinsic::x86_fma_vfmaddsub_pd:
10108 case Intrinsic::x86_fma_vfmsubadd_ps:
10109 case Intrinsic::x86_fma_vfmsubadd_pd:
10110 case Intrinsic::x86_fma_vfmadd_ps_256:
10111 case Intrinsic::x86_fma_vfmadd_pd_256:
10112 case Intrinsic::x86_fma_vfmsub_ps_256:
10113 case Intrinsic::x86_fma_vfmsub_pd_256:
10114 case Intrinsic::x86_fma_vfnmadd_ps_256:
10115 case Intrinsic::x86_fma_vfnmadd_pd_256:
10116 case Intrinsic::x86_fma_vfnmsub_ps_256:
10117 case Intrinsic::x86_fma_vfnmsub_pd_256:
10118 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10119 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10120 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10121 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010122 unsigned Opc;
10123 switch (IntNo) {
10124 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10125 case Intrinsic::x86_fma_vfmadd_ps:
10126 case Intrinsic::x86_fma_vfmadd_pd:
10127 case Intrinsic::x86_fma_vfmadd_ps_256:
10128 case Intrinsic::x86_fma_vfmadd_pd_256:
10129 Opc = X86ISD::FMADD;
10130 break;
10131 case Intrinsic::x86_fma_vfmsub_ps:
10132 case Intrinsic::x86_fma_vfmsub_pd:
10133 case Intrinsic::x86_fma_vfmsub_ps_256:
10134 case Intrinsic::x86_fma_vfmsub_pd_256:
10135 Opc = X86ISD::FMSUB;
10136 break;
10137 case Intrinsic::x86_fma_vfnmadd_ps:
10138 case Intrinsic::x86_fma_vfnmadd_pd:
10139 case Intrinsic::x86_fma_vfnmadd_ps_256:
10140 case Intrinsic::x86_fma_vfnmadd_pd_256:
10141 Opc = X86ISD::FNMADD;
10142 break;
10143 case Intrinsic::x86_fma_vfnmsub_ps:
10144 case Intrinsic::x86_fma_vfnmsub_pd:
10145 case Intrinsic::x86_fma_vfnmsub_ps_256:
10146 case Intrinsic::x86_fma_vfnmsub_pd_256:
10147 Opc = X86ISD::FNMSUB;
10148 break;
10149 case Intrinsic::x86_fma_vfmaddsub_ps:
10150 case Intrinsic::x86_fma_vfmaddsub_pd:
10151 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10152 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10153 Opc = X86ISD::FMADDSUB;
10154 break;
10155 case Intrinsic::x86_fma_vfmsubadd_ps:
10156 case Intrinsic::x86_fma_vfmsubadd_pd:
10157 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10158 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10159 Opc = X86ISD::FMSUBADD;
10160 break;
10161 }
10162
10163 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10164 Op.getOperand(2), Op.getOperand(3));
10165 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010166 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010167}
Evan Cheng72261582005-12-20 06:22:03 +000010168
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010169SDValue
10170X86TargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const {
10171 DebugLoc dl = Op.getDebugLoc();
10172 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10173 switch (IntNo) {
10174 default: return SDValue(); // Don't custom lower most intrinsics.
10175
10176 // RDRAND intrinsics.
10177 case Intrinsic::x86_rdrand_16:
10178 case Intrinsic::x86_rdrand_32:
10179 case Intrinsic::x86_rdrand_64: {
10180 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010181 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10182 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010183
10184 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10185 // return the value from Rand, which is always 0, casted to i32.
10186 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10187 DAG.getConstant(1, Op->getValueType(1)),
10188 DAG.getConstant(X86::COND_B, MVT::i32),
10189 SDValue(Result.getNode(), 1) };
10190 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10191 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10192 Ops, 4);
10193
10194 // Return { result, isValid, chain }.
10195 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010196 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010197 }
10198 }
10199}
10200
Dan Gohmand858e902010-04-17 15:26:15 +000010201SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10202 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010203 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10204 MFI->setReturnAddressIsTaken(true);
10205
Bill Wendling64e87322009-01-16 19:25:27 +000010206 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010207 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010208
10209 if (Depth > 0) {
10210 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10211 SDValue Offset =
10212 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010213 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010214 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010215 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010216 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010217 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010218 }
10219
10220 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010221 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010222 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010223 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010224}
10225
Dan Gohmand858e902010-04-17 15:26:15 +000010226SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010227 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10228 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010229
Owen Andersone50ed302009-08-10 22:56:29 +000010230 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010231 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010232 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10233 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010234 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010235 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010236 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10237 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010238 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010239 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010240}
10241
Dan Gohman475871a2008-07-27 21:46:04 +000010242SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010243 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010244 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010245}
10246
Dan Gohmand858e902010-04-17 15:26:15 +000010247SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010248 SDValue Chain = Op.getOperand(0);
10249 SDValue Offset = Op.getOperand(1);
10250 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010251 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010252
Dan Gohmand8816272010-08-11 18:14:00 +000010253 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10254 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10255 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010256 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010257
Dan Gohmand8816272010-08-11 18:14:00 +000010258 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
10259 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010260 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010261 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10262 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010263 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010264
Dale Johannesene4d209d2009-02-03 20:21:25 +000010265 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010266 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010267 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010268}
10269
Duncan Sands4a544a72011-09-06 13:37:06 +000010270SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
10271 SelectionDAG &DAG) const {
10272 return Op.getOperand(0);
10273}
10274
10275SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10276 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010277 SDValue Root = Op.getOperand(0);
10278 SDValue Trmp = Op.getOperand(1); // trampoline
10279 SDValue FPtr = Op.getOperand(2); // nested function
10280 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010281 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010282
Dan Gohman69de1932008-02-06 22:27:42 +000010283 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010284
10285 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010286 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010287
10288 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010289 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10290 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010291
Evan Cheng0e6a0522011-07-18 20:57:22 +000010292 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10293 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010294
10295 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10296
10297 // Load the pointer to the nested function into R11.
10298 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010299 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010300 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010301 Addr, MachinePointerInfo(TrmpAddr),
10302 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010303
Owen Anderson825b72b2009-08-11 20:47:22 +000010304 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10305 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010306 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10307 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010308 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010309
10310 // Load the 'nest' parameter value into R10.
10311 // R10 is specified in X86CallingConv.td
10312 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010313 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10314 DAG.getConstant(10, MVT::i64));
10315 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010316 Addr, MachinePointerInfo(TrmpAddr, 10),
10317 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010318
Owen Anderson825b72b2009-08-11 20:47:22 +000010319 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10320 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010321 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10322 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010323 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010324
10325 // Jump to the nested function.
10326 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010327 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10328 DAG.getConstant(20, MVT::i64));
10329 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010330 Addr, MachinePointerInfo(TrmpAddr, 20),
10331 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010332
10333 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010334 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10335 DAG.getConstant(22, MVT::i64));
10336 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010337 MachinePointerInfo(TrmpAddr, 22),
10338 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010339
Duncan Sands4a544a72011-09-06 13:37:06 +000010340 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010341 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010342 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010343 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010344 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010345 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010346
10347 switch (CC) {
10348 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010349 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010350 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010351 case CallingConv::X86_StdCall: {
10352 // Pass 'nest' parameter in ECX.
10353 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010354 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010355
10356 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010357 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010358 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010359
Chris Lattner58d74912008-03-12 17:45:29 +000010360 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010361 unsigned InRegCount = 0;
10362 unsigned Idx = 1;
10363
10364 for (FunctionType::param_iterator I = FTy->param_begin(),
10365 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010366 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010367 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010368 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010369
10370 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010371 report_fatal_error("Nest register in use - reduce number of inreg"
10372 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010373 }
10374 }
10375 break;
10376 }
10377 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010378 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010379 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010380 // Pass 'nest' parameter in EAX.
10381 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010382 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010383 break;
10384 }
10385
Dan Gohman475871a2008-07-27 21:46:04 +000010386 SDValue OutChains[4];
10387 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010388
Owen Anderson825b72b2009-08-11 20:47:22 +000010389 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10390 DAG.getConstant(10, MVT::i32));
10391 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010392
Chris Lattnera62fe662010-02-05 19:20:30 +000010393 // This is storing the opcode for MOV32ri.
10394 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010395 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010396 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010397 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010398 Trmp, MachinePointerInfo(TrmpAddr),
10399 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010400
Owen Anderson825b72b2009-08-11 20:47:22 +000010401 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10402 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010403 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10404 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010405 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010406
Chris Lattnera62fe662010-02-05 19:20:30 +000010407 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010408 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10409 DAG.getConstant(5, MVT::i32));
10410 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010411 MachinePointerInfo(TrmpAddr, 5),
10412 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010413
Owen Anderson825b72b2009-08-11 20:47:22 +000010414 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10415 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010416 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10417 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010418 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010419
Duncan Sands4a544a72011-09-06 13:37:06 +000010420 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010421 }
10422}
10423
Dan Gohmand858e902010-04-17 15:26:15 +000010424SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10425 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010426 /*
10427 The rounding mode is in bits 11:10 of FPSR, and has the following
10428 settings:
10429 00 Round to nearest
10430 01 Round to -inf
10431 10 Round to +inf
10432 11 Round to 0
10433
10434 FLT_ROUNDS, on the other hand, expects the following:
10435 -1 Undefined
10436 0 Round to 0
10437 1 Round to nearest
10438 2 Round to +inf
10439 3 Round to -inf
10440
10441 To perform the conversion, we do:
10442 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10443 */
10444
10445 MachineFunction &MF = DAG.getMachineFunction();
10446 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010447 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010448 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010449 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010450 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010451
10452 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010453 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010454 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010455
Michael J. Spencerec38de22010-10-10 22:04:20 +000010456
Chris Lattner2156b792010-09-22 01:11:26 +000010457 MachineMemOperand *MMO =
10458 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10459 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010460
Chris Lattner2156b792010-09-22 01:11:26 +000010461 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10462 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10463 DAG.getVTList(MVT::Other),
10464 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010465
10466 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010467 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010468 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010469
10470 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010471 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010472 DAG.getNode(ISD::SRL, DL, MVT::i16,
10473 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010474 CWD, DAG.getConstant(0x800, MVT::i16)),
10475 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010476 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010477 DAG.getNode(ISD::SRL, DL, MVT::i16,
10478 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010479 CWD, DAG.getConstant(0x400, MVT::i16)),
10480 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010481
Dan Gohman475871a2008-07-27 21:46:04 +000010482 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010483 DAG.getNode(ISD::AND, DL, MVT::i16,
10484 DAG.getNode(ISD::ADD, DL, MVT::i16,
10485 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010486 DAG.getConstant(1, MVT::i16)),
10487 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010488
10489
Duncan Sands83ec4b62008-06-06 12:08:01 +000010490 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010491 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010492}
10493
Dan Gohmand858e902010-04-17 15:26:15 +000010494SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010495 EVT VT = Op.getValueType();
10496 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010497 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010498 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010499
10500 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010501 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010502 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010503 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010504 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010505 }
Evan Cheng18efe262007-12-14 02:13:44 +000010506
Evan Cheng152804e2007-12-14 08:30:15 +000010507 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010508 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010509 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010510
10511 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010512 SDValue Ops[] = {
10513 Op,
10514 DAG.getConstant(NumBits+NumBits-1, OpVT),
10515 DAG.getConstant(X86::COND_E, MVT::i8),
10516 Op.getValue(1)
10517 };
10518 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010519
10520 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010521 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010522
Owen Anderson825b72b2009-08-11 20:47:22 +000010523 if (VT == MVT::i8)
10524 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010525 return Op;
10526}
10527
Chandler Carruthacc068e2011-12-24 10:55:54 +000010528SDValue X86TargetLowering::LowerCTLZ_ZERO_UNDEF(SDValue Op,
10529 SelectionDAG &DAG) const {
10530 EVT VT = Op.getValueType();
10531 EVT OpVT = VT;
10532 unsigned NumBits = VT.getSizeInBits();
10533 DebugLoc dl = Op.getDebugLoc();
10534
10535 Op = Op.getOperand(0);
10536 if (VT == MVT::i8) {
10537 // Zero extend to i32 since there is not an i8 bsr.
10538 OpVT = MVT::i32;
10539 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10540 }
10541
10542 // Issue a bsr (scan bits in reverse).
10543 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10544 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10545
10546 // And xor with NumBits-1.
10547 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10548
10549 if (VT == MVT::i8)
10550 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10551 return Op;
10552}
10553
Dan Gohmand858e902010-04-17 15:26:15 +000010554SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010555 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010556 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010557 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010558 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010559
10560 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010561 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010562 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010563
10564 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010565 SDValue Ops[] = {
10566 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010567 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010568 DAG.getConstant(X86::COND_E, MVT::i8),
10569 Op.getValue(1)
10570 };
Chandler Carruth77821022011-12-24 12:12:34 +000010571 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010572}
10573
Craig Topper13894fa2011-08-24 06:14:18 +000010574// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10575// ones, and then concatenate the result back.
10576static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010577 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010578
Craig Topper7a9a28b2012-08-12 02:23:29 +000010579 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010580 "Unsupported value type for operation");
10581
Craig Topper66ddd152012-04-27 22:54:43 +000010582 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010583 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010584
10585 // Extract the LHS vectors
10586 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010587 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10588 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010589
10590 // Extract the RHS vectors
10591 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010592 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10593 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010594
10595 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10596 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10597
10598 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10599 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10600 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10601}
10602
10603SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010604 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010605 Op.getValueType().isInteger() &&
10606 "Only handle AVX 256-bit vector integer operation");
10607 return Lower256IntArith(Op, DAG);
10608}
10609
10610SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010611 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010612 Op.getValueType().isInteger() &&
10613 "Only handle AVX 256-bit vector integer operation");
10614 return Lower256IntArith(Op, DAG);
10615}
10616
10617SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10618 EVT VT = Op.getValueType();
10619
10620 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010621 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010622 return Lower256IntArith(Op, DAG);
10623
Craig Topper5b209e82012-02-05 03:14:49 +000010624 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10625 "Only know how to lower V2I64/V4I64 multiply");
10626
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010627 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010628
Craig Topper5b209e82012-02-05 03:14:49 +000010629 // Ahi = psrlqi(a, 32);
10630 // Bhi = psrlqi(b, 32);
10631 //
10632 // AloBlo = pmuludq(a, b);
10633 // AloBhi = pmuludq(a, Bhi);
10634 // AhiBlo = pmuludq(Ahi, b);
10635
10636 // AloBhi = psllqi(AloBhi, 32);
10637 // AhiBlo = psllqi(AhiBlo, 32);
10638 // return AloBlo + AloBhi + AhiBlo;
10639
Craig Topperaaa643c2011-11-09 07:28:55 +000010640 SDValue A = Op.getOperand(0);
10641 SDValue B = Op.getOperand(1);
10642
Craig Topper5b209e82012-02-05 03:14:49 +000010643 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010644
Craig Topper5b209e82012-02-05 03:14:49 +000010645 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10646 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010647
Craig Topper5b209e82012-02-05 03:14:49 +000010648 // Bit cast to 32-bit vectors for MULUDQ
10649 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10650 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10651 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10652 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10653 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010654
Craig Topper5b209e82012-02-05 03:14:49 +000010655 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10656 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10657 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010658
Craig Topper5b209e82012-02-05 03:14:49 +000010659 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10660 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010661
Dale Johannesene4d209d2009-02-03 20:21:25 +000010662 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010663 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010664}
10665
Nadav Rotem43012222011-05-11 08:12:09 +000010666SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10667
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010668 EVT VT = Op.getValueType();
10669 DebugLoc dl = Op.getDebugLoc();
10670 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010671 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010672 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010673
Craig Topper1accb7e2012-01-10 06:54:16 +000010674 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010675 return SDValue();
10676
Nadav Rotem43012222011-05-11 08:12:09 +000010677 // Optimize shl/srl/sra with constant shift amount.
10678 if (isSplatVector(Amt.getNode())) {
10679 SDValue SclrAmt = Amt->getOperand(0);
10680 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10681 uint64_t ShiftAmt = C->getZExtValue();
10682
Craig Toppered2e13d2012-01-22 19:15:14 +000010683 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10684 (Subtarget->hasAVX2() &&
10685 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10686 if (Op.getOpcode() == ISD::SHL)
10687 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10688 DAG.getConstant(ShiftAmt, MVT::i32));
10689 if (Op.getOpcode() == ISD::SRL)
10690 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10691 DAG.getConstant(ShiftAmt, MVT::i32));
10692 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10693 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10694 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010695 }
10696
Craig Toppered2e13d2012-01-22 19:15:14 +000010697 if (VT == MVT::v16i8) {
10698 if (Op.getOpcode() == ISD::SHL) {
10699 // Make a large shift.
10700 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10701 DAG.getConstant(ShiftAmt, MVT::i32));
10702 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10703 // Zero out the rightmost bits.
10704 SmallVector<SDValue, 16> V(16,
10705 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10706 MVT::i8));
10707 return DAG.getNode(ISD::AND, dl, VT, SHL,
10708 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010709 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010710 if (Op.getOpcode() == ISD::SRL) {
10711 // Make a large shift.
10712 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10713 DAG.getConstant(ShiftAmt, MVT::i32));
10714 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10715 // Zero out the leftmost bits.
10716 SmallVector<SDValue, 16> V(16,
10717 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10718 MVT::i8));
10719 return DAG.getNode(ISD::AND, dl, VT, SRL,
10720 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10721 }
10722 if (Op.getOpcode() == ISD::SRA) {
10723 if (ShiftAmt == 7) {
10724 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010725 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010726 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010727 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010728
Craig Toppered2e13d2012-01-22 19:15:14 +000010729 // R s>> a === ((R u>> a) ^ m) - m
10730 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10731 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10732 MVT::i8));
10733 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10734 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10735 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10736 return Res;
10737 }
Craig Topper731dfd02012-04-23 03:42:40 +000010738 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010739 }
Craig Topper46154eb2011-11-11 07:39:23 +000010740
Craig Topper0d86d462011-11-20 00:12:05 +000010741 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10742 if (Op.getOpcode() == ISD::SHL) {
10743 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010744 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10745 DAG.getConstant(ShiftAmt, MVT::i32));
10746 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010747 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010748 SmallVector<SDValue, 32> V(32,
10749 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10750 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010751 return DAG.getNode(ISD::AND, dl, VT, SHL,
10752 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010753 }
Craig Topper0d86d462011-11-20 00:12:05 +000010754 if (Op.getOpcode() == ISD::SRL) {
10755 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010756 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10757 DAG.getConstant(ShiftAmt, MVT::i32));
10758 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010759 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010760 SmallVector<SDValue, 32> V(32,
10761 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10762 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010763 return DAG.getNode(ISD::AND, dl, VT, SRL,
10764 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10765 }
10766 if (Op.getOpcode() == ISD::SRA) {
10767 if (ShiftAmt == 7) {
10768 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010769 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010770 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010771 }
10772
10773 // R s>> a === ((R u>> a) ^ m) - m
10774 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10775 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10776 MVT::i8));
10777 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10778 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10779 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10780 return Res;
10781 }
Craig Topper731dfd02012-04-23 03:42:40 +000010782 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010783 }
Nadav Rotem43012222011-05-11 08:12:09 +000010784 }
10785 }
10786
10787 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010788 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010789 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10790 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010791
Chris Lattner7302d802012-02-06 21:56:39 +000010792 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10793 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010794 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10795 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010796 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010797 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010798
10799 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010800 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010801 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10802 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10803 }
Nadav Rotem43012222011-05-11 08:12:09 +000010804 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010805 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010806
Nate Begeman51409212010-07-28 00:21:48 +000010807 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010808 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10809 DAG.getConstant(5, MVT::i32));
10810 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010811
Lang Hames8b99c1e2011-12-17 01:08:46 +000010812 // Turn 'a' into a mask suitable for VSELECT
10813 SDValue VSelM = DAG.getConstant(0x80, VT);
10814 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010815 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010816
Lang Hames8b99c1e2011-12-17 01:08:46 +000010817 SDValue CM1 = DAG.getConstant(0x0f, VT);
10818 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010819
Lang Hames8b99c1e2011-12-17 01:08:46 +000010820 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10821 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010822 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10823 DAG.getConstant(4, MVT::i32), DAG);
10824 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010825 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10826
Nate Begeman51409212010-07-28 00:21:48 +000010827 // a += a
10828 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010829 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010830 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010831
Lang Hames8b99c1e2011-12-17 01:08:46 +000010832 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10833 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010834 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10835 DAG.getConstant(2, MVT::i32), DAG);
10836 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010837 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10838
Nate Begeman51409212010-07-28 00:21:48 +000010839 // a += a
10840 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010841 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010842 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010843
Lang Hames8b99c1e2011-12-17 01:08:46 +000010844 // return VSELECT(r, r+r, a);
10845 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010846 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010847 return R;
10848 }
Craig Topper46154eb2011-11-11 07:39:23 +000010849
10850 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010851 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010852 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010853 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10854 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10855
10856 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010857 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10858 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010859
10860 // Recreate the shift amount vectors
10861 SDValue Amt1, Amt2;
10862 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10863 // Constant shift amount
10864 SmallVector<SDValue, 4> Amt1Csts;
10865 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010866 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010867 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010868 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010869 Amt2Csts.push_back(Amt->getOperand(i));
10870
10871 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10872 &Amt1Csts[0], NumElems/2);
10873 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10874 &Amt2Csts[0], NumElems/2);
10875 } else {
10876 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000010877 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
10878 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010879 }
10880
10881 // Issue new vector shifts for the smaller types
10882 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10883 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10884
10885 // Concatenate the result back
10886 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10887 }
10888
Nate Begeman51409212010-07-28 00:21:48 +000010889 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010890}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010891
Dan Gohmand858e902010-04-17 15:26:15 +000010892SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010893 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10894 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010895 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10896 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010897 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010898 SDValue LHS = N->getOperand(0);
10899 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010900 unsigned BaseOp = 0;
10901 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010902 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010903 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010904 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010905 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010906 // A subtract of one will be selected as a INC. Note that INC doesn't
10907 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010908 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10909 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010910 BaseOp = X86ISD::INC;
10911 Cond = X86::COND_O;
10912 break;
10913 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010914 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010915 Cond = X86::COND_O;
10916 break;
10917 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010918 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010919 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010920 break;
10921 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010922 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10923 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010924 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10925 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010926 BaseOp = X86ISD::DEC;
10927 Cond = X86::COND_O;
10928 break;
10929 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010930 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010931 Cond = X86::COND_O;
10932 break;
10933 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010934 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010935 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010936 break;
10937 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010938 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010939 Cond = X86::COND_O;
10940 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010941 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10942 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10943 MVT::i32);
10944 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010945
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010946 SDValue SetCC =
10947 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10948 DAG.getConstant(X86::COND_O, MVT::i32),
10949 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010950
Dan Gohman6e5fda22011-07-22 18:45:15 +000010951 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010952 }
Bill Wendling74c37652008-12-09 22:08:41 +000010953 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010954
Bill Wendling61edeb52008-12-02 01:06:39 +000010955 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010956 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010957 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010958
Bill Wendling61edeb52008-12-02 01:06:39 +000010959 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010960 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10961 DAG.getConstant(Cond, MVT::i32),
10962 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010963
Dan Gohman6e5fda22011-07-22 18:45:15 +000010964 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010965}
10966
Chad Rosier30450e82011-12-22 22:35:21 +000010967SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10968 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010969 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010970 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10971 EVT VT = Op.getValueType();
10972
Craig Toppered2e13d2012-01-22 19:15:14 +000010973 if (!Subtarget->hasSSE2() || !VT.isVector())
10974 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010975
Craig Toppered2e13d2012-01-22 19:15:14 +000010976 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10977 ExtraVT.getScalarType().getSizeInBits();
10978 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10979
10980 switch (VT.getSimpleVT().SimpleTy) {
10981 default: return SDValue();
10982 case MVT::v8i32:
10983 case MVT::v16i16:
10984 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010985 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000010986 if (!Subtarget->hasAVX2()) {
10987 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000010988 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000010989
Craig Toppered2e13d2012-01-22 19:15:14 +000010990 // Extract the LHS vectors
10991 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010992 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10993 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000010994
Craig Toppered2e13d2012-01-22 19:15:14 +000010995 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10996 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000010997
Craig Toppered2e13d2012-01-22 19:15:14 +000010998 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000010999 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000011000 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
11001 ExtraNumElems/2);
11002 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000011003
Craig Toppered2e13d2012-01-22 19:15:14 +000011004 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
11005 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000011006
Craig Toppered2e13d2012-01-22 19:15:14 +000011007 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
11008 }
11009 // fall through
11010 case MVT::v4i32:
11011 case MVT::v8i16: {
11012 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
11013 Op.getOperand(0), ShAmt, DAG);
11014 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011015 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011016 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011017}
11018
11019
Eric Christopher9a9d2752010-07-22 02:48:34 +000011020SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
11021 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011022
Eric Christopher77ed1352011-07-08 00:04:56 +000011023 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11024 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011025 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011026 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011027 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011028 SDValue Ops[] = {
11029 DAG.getRegister(X86::ESP, MVT::i32), // Base
11030 DAG.getTargetConstant(1, MVT::i8), // Scale
11031 DAG.getRegister(0, MVT::i32), // Index
11032 DAG.getTargetConstant(0, MVT::i32), // Disp
11033 DAG.getRegister(0, MVT::i32), // Segment.
11034 Zero,
11035 Chain
11036 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011037 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011038 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11039 array_lengthof(Ops));
11040 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011041 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011042
Eric Christopher9a9d2752010-07-22 02:48:34 +000011043 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011044 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011045 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011046
Chris Lattner132929a2010-08-14 17:26:09 +000011047 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11048 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11049 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11050 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011051
Chris Lattner132929a2010-08-14 17:26:09 +000011052 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11053 if (!Op1 && !Op2 && !Op3 && Op4)
11054 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011055
Chris Lattner132929a2010-08-14 17:26:09 +000011056 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11057 if (Op1 && !Op2 && !Op3 && !Op4)
11058 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011059
11060 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011061 // (MFENCE)>;
11062 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011063}
11064
Eli Friedman14648462011-07-27 22:21:52 +000011065SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
11066 SelectionDAG &DAG) const {
11067 DebugLoc dl = Op.getDebugLoc();
11068 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11069 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11070 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11071 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11072
11073 // The only fence that needs an instruction is a sequentially-consistent
11074 // cross-thread fence.
11075 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11076 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11077 // no-sse2). There isn't any reason to disable it if the target processor
11078 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011079 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011080 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11081
11082 SDValue Chain = Op.getOperand(0);
11083 SDValue Zero = DAG.getConstant(0, MVT::i32);
11084 SDValue Ops[] = {
11085 DAG.getRegister(X86::ESP, MVT::i32), // Base
11086 DAG.getTargetConstant(1, MVT::i8), // Scale
11087 DAG.getRegister(0, MVT::i32), // Index
11088 DAG.getTargetConstant(0, MVT::i32), // Disp
11089 DAG.getRegister(0, MVT::i32), // Segment.
11090 Zero,
11091 Chain
11092 };
11093 SDNode *Res =
11094 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11095 array_lengthof(Ops));
11096 return SDValue(Res, 0);
11097 }
11098
11099 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11100 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11101}
11102
11103
Dan Gohmand858e902010-04-17 15:26:15 +000011104SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000011105 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011106 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011107 unsigned Reg = 0;
11108 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011109 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011110 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011111 case MVT::i8: Reg = X86::AL; size = 1; break;
11112 case MVT::i16: Reg = X86::AX; size = 2; break;
11113 case MVT::i32: Reg = X86::EAX; size = 4; break;
11114 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011115 assert(Subtarget->is64Bit() && "Node not type legal!");
11116 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011117 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011118 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011119 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011120 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011121 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011122 Op.getOperand(1),
11123 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011124 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011125 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011126 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011127 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11128 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11129 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011130 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011131 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011132 return cpOut;
11133}
11134
Duncan Sands1607f052008-12-01 11:39:25 +000011135SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000011136 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000011137 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011138 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011139 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011140 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011141 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011142 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11143 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011144 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011145 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11146 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011147 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011148 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011149 rdx.getValue(1)
11150 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011151 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011152}
11153
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011154SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000011155 SelectionDAG &DAG) const {
11156 EVT SrcVT = Op.getOperand(0).getValueType();
11157 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011158 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011159 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011160 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011161 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011162 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011163 // i64 <=> MMX conversions are Legal.
11164 if (SrcVT==MVT::i64 && DstVT.isVector())
11165 return Op;
11166 if (DstVT==MVT::i64 && SrcVT.isVector())
11167 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011168 // MMX <=> MMX conversions are Legal.
11169 if (SrcVT.isVector() && DstVT.isVector())
11170 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011171 // All other conversions need to be expanded.
11172 return SDValue();
11173}
Chris Lattner5b856542010-12-20 00:59:46 +000011174
Dan Gohmand858e902010-04-17 15:26:15 +000011175SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011176 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011177 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011178 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011179 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011180 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011181 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011182 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011183 Node->getOperand(0),
11184 Node->getOperand(1), negOp,
11185 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011186 cast<AtomicSDNode>(Node)->getAlignment(),
11187 cast<AtomicSDNode>(Node)->getOrdering(),
11188 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011189}
11190
Eli Friedman327236c2011-08-24 20:50:09 +000011191static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11192 SDNode *Node = Op.getNode();
11193 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011194 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011195
11196 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011197 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11198 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11199 // (The only way to get a 16-byte store is cmpxchg16b)
11200 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11201 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11202 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011203 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11204 cast<AtomicSDNode>(Node)->getMemoryVT(),
11205 Node->getOperand(0),
11206 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011207 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011208 cast<AtomicSDNode>(Node)->getOrdering(),
11209 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011210 return Swap.getValue(1);
11211 }
11212 // Other atomic stores have a simple pattern.
11213 return Op;
11214}
11215
Chris Lattner5b856542010-12-20 00:59:46 +000011216static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11217 EVT VT = Op.getNode()->getValueType(0);
11218
11219 // Let legalize expand this if it isn't a legal type yet.
11220 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11221 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011222
Chris Lattner5b856542010-12-20 00:59:46 +000011223 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011224
Chris Lattner5b856542010-12-20 00:59:46 +000011225 unsigned Opc;
11226 bool ExtraOp = false;
11227 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011228 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011229 case ISD::ADDC: Opc = X86ISD::ADD; break;
11230 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11231 case ISD::SUBC: Opc = X86ISD::SUB; break;
11232 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11233 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011234
Chris Lattner5b856542010-12-20 00:59:46 +000011235 if (!ExtraOp)
11236 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11237 Op.getOperand(1));
11238 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11239 Op.getOperand(1), Op.getOperand(2));
11240}
11241
Evan Cheng0db9fe62006-04-25 20:13:52 +000011242/// LowerOperation - Provide custom lowering hooks for some operations.
11243///
Dan Gohmand858e902010-04-17 15:26:15 +000011244SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011245 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011246 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011247 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000011248 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000011249 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011250 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
11251 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011252 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011253 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011254 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011255 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11256 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11257 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000011258 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000011259 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011260 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11261 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11262 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011263 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011264 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011265 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011266 case ISD::SHL_PARTS:
11267 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011268 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011269 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011270 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011271 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011272 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011273 case ISD::FABS: return LowerFABS(Op, DAG);
11274 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011275 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011276 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011277 case ISD::SETCC: return LowerSETCC(Op, DAG);
11278 case ISD::SELECT: return LowerSELECT(Op, DAG);
11279 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011280 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011281 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011282 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000011283 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011284 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011285 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011286 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11287 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011288 case ISD::FRAME_TO_ARGS_OFFSET:
11289 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011290 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011291 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011292 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11293 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011294 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011295 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011296 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011297 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011298 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011299 case ISD::SRA:
11300 case ISD::SRL:
11301 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011302 case ISD::SADDO:
11303 case ISD::UADDO:
11304 case ISD::SSUBO:
11305 case ISD::USUBO:
11306 case ISD::SMULO:
11307 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000011308 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011309 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011310 case ISD::ADDC:
11311 case ISD::ADDE:
11312 case ISD::SUBC:
11313 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011314 case ISD::ADD: return LowerADD(Op, DAG);
11315 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011316 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011317}
11318
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011319static void ReplaceATOMIC_LOAD(SDNode *Node,
11320 SmallVectorImpl<SDValue> &Results,
11321 SelectionDAG &DAG) {
11322 DebugLoc dl = Node->getDebugLoc();
11323 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11324
11325 // Convert wide load -> cmpxchg8b/cmpxchg16b
11326 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11327 // (The only way to get a 16-byte load is cmpxchg16b)
11328 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011329 SDValue Zero = DAG.getConstant(0, VT);
11330 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011331 Node->getOperand(0),
11332 Node->getOperand(1), Zero, Zero,
11333 cast<AtomicSDNode>(Node)->getMemOperand(),
11334 cast<AtomicSDNode>(Node)->getOrdering(),
11335 cast<AtomicSDNode>(Node)->getSynchScope());
11336 Results.push_back(Swap.getValue(0));
11337 Results.push_back(Swap.getValue(1));
11338}
11339
Craig Topperc0878702012-08-17 06:55:11 +000011340static void
Duncan Sands1607f052008-12-01 11:39:25 +000011341ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011342 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011343 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011344 assert (Node->getValueType(0) == MVT::i64 &&
11345 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011346
11347 SDValue Chain = Node->getOperand(0);
11348 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011349 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011350 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011351 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011352 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011353 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011354 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011355 SDValue Result =
11356 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11357 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011358 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011359 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011360 Results.push_back(Result.getValue(2));
11361}
11362
Duncan Sands126d9072008-07-04 11:47:58 +000011363/// ReplaceNodeResults - Replace a node with an illegal result type
11364/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011365void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11366 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011367 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011368 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011369 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011370 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011371 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011372 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011373 case ISD::ADDC:
11374 case ISD::ADDE:
11375 case ISD::SUBC:
11376 case ISD::SUBE:
11377 // We don't want to expand or promote these.
11378 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011379 case ISD::FP_TO_SINT:
11380 case ISD::FP_TO_UINT: {
11381 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11382
11383 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11384 return;
11385
Eli Friedman948e95a2009-05-23 09:59:16 +000011386 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011387 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011388 SDValue FIST = Vals.first, StackSlot = Vals.second;
11389 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011390 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011391 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011392 if (StackSlot.getNode() != 0)
11393 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11394 MachinePointerInfo(),
11395 false, false, false, 0));
11396 else
11397 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011398 }
11399 return;
11400 }
11401 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011402 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011403 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011404 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011405 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011406 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011407 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011408 eax.getValue(2));
11409 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11410 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011411 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011412 Results.push_back(edx.getValue(1));
11413 return;
11414 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011415 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011416 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011417 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011418 bool Regs64bit = T == MVT::i128;
11419 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011420 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011421 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11422 DAG.getConstant(0, HalfT));
11423 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11424 DAG.getConstant(1, HalfT));
11425 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11426 Regs64bit ? X86::RAX : X86::EAX,
11427 cpInL, SDValue());
11428 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11429 Regs64bit ? X86::RDX : X86::EDX,
11430 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011431 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011432 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11433 DAG.getConstant(0, HalfT));
11434 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11435 DAG.getConstant(1, HalfT));
11436 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11437 Regs64bit ? X86::RBX : X86::EBX,
11438 swapInL, cpInH.getValue(1));
11439 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011440 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011441 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011442 SDValue Ops[] = { swapInH.getValue(0),
11443 N->getOperand(1),
11444 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011445 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011446 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011447 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11448 X86ISD::LCMPXCHG8_DAG;
11449 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011450 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011451 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11452 Regs64bit ? X86::RAX : X86::EAX,
11453 HalfT, Result.getValue(1));
11454 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11455 Regs64bit ? X86::RDX : X86::EDX,
11456 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011457 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011458 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011459 Results.push_back(cpOutH.getValue(1));
11460 return;
11461 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011462 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011463 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011464 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011465 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011466 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011467 case ISD::ATOMIC_LOAD_XOR:
Craig Topperc0878702012-08-17 06:55:11 +000011468 case ISD::ATOMIC_SWAP: {
11469 unsigned Opc;
11470 switch (N->getOpcode()) {
11471 default: llvm_unreachable("Unexpected opcode");
11472 case ISD::ATOMIC_LOAD_ADD:
11473 Opc = X86ISD::ATOMADD64_DAG;
11474 break;
11475 case ISD::ATOMIC_LOAD_AND:
11476 Opc = X86ISD::ATOMAND64_DAG;
11477 break;
11478 case ISD::ATOMIC_LOAD_NAND:
11479 Opc = X86ISD::ATOMNAND64_DAG;
11480 break;
11481 case ISD::ATOMIC_LOAD_OR:
11482 Opc = X86ISD::ATOMOR64_DAG;
11483 break;
11484 case ISD::ATOMIC_LOAD_SUB:
11485 Opc = X86ISD::ATOMSUB64_DAG;
11486 break;
11487 case ISD::ATOMIC_LOAD_XOR:
11488 Opc = X86ISD::ATOMXOR64_DAG;
11489 break;
11490 case ISD::ATOMIC_SWAP:
11491 Opc = X86ISD::ATOMSWAP64_DAG;
11492 break;
11493 }
11494 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011495 return;
Craig Topperc0878702012-08-17 06:55:11 +000011496 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011497 case ISD::ATOMIC_LOAD:
11498 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011499 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011500}
11501
Evan Cheng72261582005-12-20 06:22:03 +000011502const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11503 switch (Opcode) {
11504 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011505 case X86ISD::BSF: return "X86ISD::BSF";
11506 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011507 case X86ISD::SHLD: return "X86ISD::SHLD";
11508 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011509 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011510 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011511 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011512 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011513 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011514 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011515 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11516 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11517 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011518 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011519 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011520 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011521 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011522 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011523 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011524 case X86ISD::COMI: return "X86ISD::COMI";
11525 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011526 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011527 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011528 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11529 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011530 case X86ISD::CMOV: return "X86ISD::CMOV";
11531 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011532 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011533 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11534 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011535 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011536 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011537 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011538 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011539 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011540 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11541 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011542 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011543 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011544 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011545 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011546 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011547 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11548 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11549 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011550 case X86ISD::HADD: return "X86ISD::HADD";
11551 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011552 case X86ISD::FHADD: return "X86ISD::FHADD";
11553 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011554 case X86ISD::FMAX: return "X86ISD::FMAX";
11555 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011556 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11557 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011558 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11559 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011560 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011561 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011562 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011563 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011564 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011565 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011566 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011567 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11568 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011569 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11570 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11571 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11572 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11573 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11574 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011575 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011576 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011577 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011578 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Craig Toppered2e13d2012-01-22 19:15:14 +000011579 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11580 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011581 case X86ISD::VSHL: return "X86ISD::VSHL";
11582 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011583 case X86ISD::VSRA: return "X86ISD::VSRA";
11584 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11585 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11586 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011587 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011588 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11589 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011590 case X86ISD::ADD: return "X86ISD::ADD";
11591 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011592 case X86ISD::ADC: return "X86ISD::ADC";
11593 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011594 case X86ISD::SMUL: return "X86ISD::SMUL";
11595 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011596 case X86ISD::INC: return "X86ISD::INC";
11597 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011598 case X86ISD::OR: return "X86ISD::OR";
11599 case X86ISD::XOR: return "X86ISD::XOR";
11600 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011601 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011602 case X86ISD::BLSI: return "X86ISD::BLSI";
11603 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11604 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011605 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011606 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011607 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011608 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11609 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11610 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011611 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011612 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011613 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011614 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011615 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011616 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11617 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011618 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11619 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11620 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011621 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11622 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011623 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11624 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011625 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011626 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011627 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011628 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11629 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011630 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011631 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011632 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011633 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011634 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011635 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011636 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011637 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011638 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011639 case X86ISD::FMADD: return "X86ISD::FMADD";
11640 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11641 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11642 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11643 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11644 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011645 }
11646}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011647
Chris Lattnerc9addb72007-03-30 23:15:24 +000011648// isLegalAddressingMode - Return true if the addressing mode represented
11649// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011650bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011651 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011652 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011653 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011654 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011655
Chris Lattnerc9addb72007-03-30 23:15:24 +000011656 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011657 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011658 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011659
Chris Lattnerc9addb72007-03-30 23:15:24 +000011660 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011661 unsigned GVFlags =
11662 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011663
Chris Lattnerdfed4132009-07-10 07:38:24 +000011664 // If a reference to this global requires an extra load, we can't fold it.
11665 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011666 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011667
Chris Lattnerdfed4132009-07-10 07:38:24 +000011668 // If BaseGV requires a register for the PIC base, we cannot also have a
11669 // BaseReg specified.
11670 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011671 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011672
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011673 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011674 if ((M != CodeModel::Small || R != Reloc::Static) &&
11675 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011676 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011677 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011678
Chris Lattnerc9addb72007-03-30 23:15:24 +000011679 switch (AM.Scale) {
11680 case 0:
11681 case 1:
11682 case 2:
11683 case 4:
11684 case 8:
11685 // These scales always work.
11686 break;
11687 case 3:
11688 case 5:
11689 case 9:
11690 // These scales are formed with basereg+scalereg. Only accept if there is
11691 // no basereg yet.
11692 if (AM.HasBaseReg)
11693 return false;
11694 break;
11695 default: // Other stuff never works.
11696 return false;
11697 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011698
Chris Lattnerc9addb72007-03-30 23:15:24 +000011699 return true;
11700}
11701
11702
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011703bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011704 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011705 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011706 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11707 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011708 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011709 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011710 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011711}
11712
Evan Cheng70e10d32012-07-17 06:53:39 +000011713bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11714 return Imm == (int32_t)Imm;
11715}
11716
11717bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011718 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011719 return Imm == (int32_t)Imm;
11720}
11721
Owen Andersone50ed302009-08-10 22:56:29 +000011722bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011723 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011724 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011725 unsigned NumBits1 = VT1.getSizeInBits();
11726 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011727 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011728 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011729 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011730}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011731
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011732bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011733 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011734 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011735}
11736
Owen Andersone50ed302009-08-10 22:56:29 +000011737bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011738 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011739 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011740}
11741
Owen Andersone50ed302009-08-10 22:56:29 +000011742bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011743 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011744 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011745}
11746
Evan Cheng60c07e12006-07-05 22:17:51 +000011747/// isShuffleMaskLegal - Targets can use this to indicate that they only
11748/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11749/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11750/// are assumed to be legal.
11751bool
Eric Christopherfd179292009-08-27 18:07:15 +000011752X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011753 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011754 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011755 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011756 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011757
Nate Begemana09008b2009-10-19 02:17:23 +000011758 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011759 return (VT.getVectorNumElements() == 2 ||
11760 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11761 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011762 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011763 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011764 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11765 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011766 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011767 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11768 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011769 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11770 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011771}
11772
Dan Gohman7d8143f2008-04-09 20:09:42 +000011773bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011774X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011775 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011776 unsigned NumElts = VT.getVectorNumElements();
11777 // FIXME: This collection of masks seems suspect.
11778 if (NumElts == 2)
11779 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011780 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011781 return (isMOVLMask(Mask, VT) ||
11782 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011783 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11784 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011785 }
11786 return false;
11787}
11788
11789//===----------------------------------------------------------------------===//
11790// X86 Scheduler Hooks
11791//===----------------------------------------------------------------------===//
11792
Mon P Wang63307c32008-05-05 19:05:59 +000011793// private utility function
11794MachineBasicBlock *
11795X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11796 MachineBasicBlock *MBB,
11797 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011798 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011799 unsigned LoadOpc,
11800 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011801 unsigned notOpc,
11802 unsigned EAXreg,
Craig Topper44d23822012-02-22 05:59:10 +000011803 const TargetRegisterClass *RC,
Richard Smith42fc29e2012-04-13 22:47:00 +000011804 bool Invert) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011805 // For the atomic bitwise operator, we generate
11806 // thisMBB:
11807 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011808 // ld t1 = [bitinstr.addr]
11809 // op t2 = t1, [bitinstr.val]
Richard Smith42fc29e2012-04-13 22:47:00 +000011810 // not t3 = t2 (if Invert)
Mon P Wangab3e7472008-05-05 22:56:23 +000011811 // mov EAX = t1
Richard Smith42fc29e2012-04-13 22:47:00 +000011812 // lcs dest = [bitinstr.addr], t3 [EAX is implicit]
Mon P Wang63307c32008-05-05 19:05:59 +000011813 // bz newMBB
11814 // fallthrough -->nextMBB
11815 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11816 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011817 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011818 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011819
Mon P Wang63307c32008-05-05 19:05:59 +000011820 /// First build the CFG
11821 MachineFunction *F = MBB->getParent();
11822 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011823 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11824 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11825 F->insert(MBBIter, newMBB);
11826 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011827
Dan Gohman14152b42010-07-06 20:24:04 +000011828 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11829 nextMBB->splice(nextMBB->begin(), thisMBB,
11830 llvm::next(MachineBasicBlock::iterator(bInstr)),
11831 thisMBB->end());
11832 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011833
Mon P Wang63307c32008-05-05 19:05:59 +000011834 // Update thisMBB to fall through to newMBB
11835 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011836
Mon P Wang63307c32008-05-05 19:05:59 +000011837 // newMBB jumps to itself and fall through to nextMBB
11838 newMBB->addSuccessor(nextMBB);
11839 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011840
Mon P Wang63307c32008-05-05 19:05:59 +000011841 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011842 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011843 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011844 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011845 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011846 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011847 int numArgs = bInstr->getNumOperands() - 1;
11848 for (int i=0; i < numArgs; ++i)
11849 argOpers[i] = &bInstr->getOperand(i+1);
11850
11851 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011852 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011853 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011854
Dale Johannesen140be2d2008-08-19 18:47:28 +000011855 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011856 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011857 for (int i=0; i <= lastAddrIndx; ++i)
11858 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011859
Dale Johannesen140be2d2008-08-19 18:47:28 +000011860 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011861 assert((argOpers[valArgIndx]->isReg() ||
11862 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011863 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011864 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011865 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011866 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011867 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Richard Smith42fc29e2012-04-13 22:47:00 +000011868 MIB.addReg(t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011869 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011870
Richard Smith42fc29e2012-04-13 22:47:00 +000011871 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11872 if (Invert) {
11873 MIB = BuildMI(newMBB, dl, TII->get(notOpc), t3).addReg(t2);
11874 }
11875 else
11876 t3 = t2;
11877
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011878 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Richard Smith2c651fe2012-04-16 18:43:53 +000011879 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011880
Dale Johannesene4d209d2009-02-03 20:21:25 +000011881 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011882 for (int i=0; i <= lastAddrIndx; ++i)
11883 (*MIB).addOperand(*argOpers[i]);
Richard Smith42fc29e2012-04-13 22:47:00 +000011884 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011885 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011886 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11887 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011888
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011889 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011890 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011891
Mon P Wang63307c32008-05-05 19:05:59 +000011892 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011893 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011894
Dan Gohman14152b42010-07-06 20:24:04 +000011895 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011896 return nextMBB;
11897}
11898
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011899// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011900MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011901X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11902 MachineBasicBlock *MBB,
11903 unsigned regOpcL,
11904 unsigned regOpcH,
11905 unsigned immOpcL,
11906 unsigned immOpcH,
Richard Smith42fc29e2012-04-13 22:47:00 +000011907 bool Invert) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011908 // For the atomic bitwise operator, we generate
11909 // thisMBB (instructions are in pairs, except cmpxchg8b)
11910 // ld t1,t2 = [bitinstr.addr]
11911 // newMBB:
11912 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11913 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011914 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Richard Smith42fc29e2012-04-13 22:47:00 +000011915 // neg t7, t8 < t5, t6 (if Invert)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011916 // mov ECX, EBX <- t5, t6
11917 // mov EAX, EDX <- t1, t2
11918 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11919 // mov t3, t4 <- EAX, EDX
11920 // bz newMBB
11921 // result in out1, out2
11922 // fallthrough -->nextMBB
11923
Craig Topperc9099502012-04-20 06:31:50 +000011924 const TargetRegisterClass *RC = &X86::GR32RegClass;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011925 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011926 const unsigned NotOpc = X86::NOT32r;
11927 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11928 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11929 MachineFunction::iterator MBBIter = MBB;
11930 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011931
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011932 /// First build the CFG
11933 MachineFunction *F = MBB->getParent();
11934 MachineBasicBlock *thisMBB = MBB;
11935 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11936 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11937 F->insert(MBBIter, newMBB);
11938 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011939
Dan Gohman14152b42010-07-06 20:24:04 +000011940 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11941 nextMBB->splice(nextMBB->begin(), thisMBB,
11942 llvm::next(MachineBasicBlock::iterator(bInstr)),
11943 thisMBB->end());
11944 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011945
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011946 // Update thisMBB to fall through to newMBB
11947 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011948
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011949 // newMBB jumps to itself and fall through to nextMBB
11950 newMBB->addSuccessor(nextMBB);
11951 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011952
Dale Johannesene4d209d2009-02-03 20:21:25 +000011953 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011954 // Insert instructions into newMBB based on incoming instruction
11955 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011956 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011957 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011958 MachineOperand& dest1Oper = bInstr->getOperand(0);
11959 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011960 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11961 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011962 argOpers[i] = &bInstr->getOperand(i+2);
11963
Dan Gohman71ea4e52010-05-14 21:01:44 +000011964 // We use some of the operands multiple times, so conservatively just
11965 // clear any kill flags that might be present.
11966 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11967 argOpers[i]->setIsKill(false);
11968 }
11969
Evan Chengad5b52f2010-01-08 19:14:57 +000011970 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011971 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011972
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011973 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011974 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011975 for (int i=0; i <= lastAddrIndx; ++i)
11976 (*MIB).addOperand(*argOpers[i]);
11977 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011978 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011979 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011980 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011981 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011982 MachineOperand newOp3 = *(argOpers[3]);
11983 if (newOp3.isImm())
11984 newOp3.setImm(newOp3.getImm()+4);
11985 else
11986 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011987 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011988 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011989
11990 // t3/4 are defined later, at the bottom of the loop
11991 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11992 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011993 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011994 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011995 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011996 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11997
Evan Cheng306b4ca2010-01-08 23:41:50 +000011998 // The subsequent operations should be using the destination registers of
Richard Smith42fc29e2012-04-13 22:47:00 +000011999 // the PHI instructions.
12000 t1 = dest1Oper.getReg();
12001 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012002
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012003 int valArgIndx = lastAddrIndx + 1;
12004 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000012005 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012006 "invalid operand");
12007 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
12008 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012009 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000012010 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012011 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012012 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000012013 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012014 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012015 (*MIB).addOperand(*argOpers[valArgIndx]);
12016 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012017 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012018 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012019 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012020 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000012021 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012022 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012023 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000012024 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012025 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012026 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012027
Richard Smith42fc29e2012-04-13 22:47:00 +000012028 unsigned t7, t8;
12029 if (Invert) {
12030 t7 = F->getRegInfo().createVirtualRegister(RC);
12031 t8 = F->getRegInfo().createVirtualRegister(RC);
12032 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t7).addReg(t5);
12033 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t8).addReg(t6);
12034 } else {
12035 t7 = t5;
12036 t8 = t6;
12037 }
12038
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012039 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012040 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012041 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012042 MIB.addReg(t2);
12043
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012044 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012045 MIB.addReg(t7);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012046 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012047 MIB.addReg(t8);
Scott Michelfdc40a02009-02-17 22:15:04 +000012048
Dale Johannesene4d209d2009-02-03 20:21:25 +000012049 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012050 for (int i=0; i <= lastAddrIndx; ++i)
12051 (*MIB).addOperand(*argOpers[i]);
12052
12053 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012054 (*MIB).setMemRefs(bInstr->memoperands_begin(),
12055 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012056
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012057 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012058 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012059 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012060 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012061
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012062 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012063 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012064
Dan Gohman14152b42010-07-06 20:24:04 +000012065 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012066 return nextMBB;
12067}
12068
12069// private utility function
12070MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000012071X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
12072 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000012073 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000012074 // For the atomic min/max operator, we generate
12075 // thisMBB:
12076 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000012077 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000012078 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000012079 // cmp t1, t2
12080 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000012081 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000012082 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
12083 // bz newMBB
12084 // fallthrough -->nextMBB
12085 //
12086 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12087 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012088 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012089 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000012090
Mon P Wang63307c32008-05-05 19:05:59 +000012091 /// First build the CFG
12092 MachineFunction *F = MBB->getParent();
12093 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012094 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
12095 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
12096 F->insert(MBBIter, newMBB);
12097 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012098
Dan Gohman14152b42010-07-06 20:24:04 +000012099 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
12100 nextMBB->splice(nextMBB->begin(), thisMBB,
12101 llvm::next(MachineBasicBlock::iterator(mInstr)),
12102 thisMBB->end());
12103 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012104
Mon P Wang63307c32008-05-05 19:05:59 +000012105 // Update thisMBB to fall through to newMBB
12106 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012107
Mon P Wang63307c32008-05-05 19:05:59 +000012108 // newMBB jumps to newMBB and fall through to nextMBB
12109 newMBB->addSuccessor(nextMBB);
12110 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012111
Dale Johannesene4d209d2009-02-03 20:21:25 +000012112 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000012113 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012114 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000012115 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000012116 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012117 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000012118 int numArgs = mInstr->getNumOperands() - 1;
12119 for (int i=0; i < numArgs; ++i)
12120 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000012121
Mon P Wang63307c32008-05-05 19:05:59 +000012122 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012123 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012124 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000012125
Craig Topperc9099502012-04-20 06:31:50 +000012126 unsigned t1 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012127 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000012128 for (int i=0; i <= lastAddrIndx; ++i)
12129 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000012130
Mon P Wang63307c32008-05-05 19:05:59 +000012131 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000012132 assert((argOpers[valArgIndx]->isReg() ||
12133 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000012134 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000012135
Craig Topperc9099502012-04-20 06:31:50 +000012136 unsigned t2 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dan Gohmand735b802008-10-03 15:45:36 +000012137 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012138 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000012139 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012140 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000012141 (*MIB).addOperand(*argOpers[valArgIndx]);
12142
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012143 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012144 MIB.addReg(t1);
12145
Dale Johannesene4d209d2009-02-03 20:21:25 +000012146 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000012147 MIB.addReg(t1);
12148 MIB.addReg(t2);
12149
12150 // Generate movc
Craig Topperc9099502012-04-20 06:31:50 +000012151 unsigned t3 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012152 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000012153 MIB.addReg(t2);
12154 MIB.addReg(t1);
12155
12156 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000012157 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000012158 for (int i=0; i <= lastAddrIndx; ++i)
12159 (*MIB).addOperand(*argOpers[i]);
12160 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000012161 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012162 (*MIB).setMemRefs(mInstr->memoperands_begin(),
12163 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000012164
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012165 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000012166 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012167
Mon P Wang63307c32008-05-05 19:05:59 +000012168 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012169 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012170
Dan Gohman14152b42010-07-06 20:24:04 +000012171 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000012172 return nextMBB;
12173}
12174
Eric Christopherf83a5de2009-08-27 18:08:16 +000012175// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012176// or XMM0_V32I8 in AVX all of this code can be replaced with that
12177// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012178MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012179X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012180 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012181 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012182 "Target must have SSE4.2 or AVX features enabled");
12183
Eric Christopherb120ab42009-08-18 22:50:32 +000012184 DebugLoc dl = MI->getDebugLoc();
12185 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012186 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012187 if (!Subtarget->hasAVX()) {
12188 if (memArg)
12189 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12190 else
12191 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12192 } else {
12193 if (memArg)
12194 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12195 else
12196 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12197 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012198
Eric Christopher41c902f2010-11-30 08:20:21 +000012199 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012200 for (unsigned i = 0; i < numArgs; ++i) {
12201 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012202 if (!(Op.isReg() && Op.isImplicit()))
12203 MIB.addOperand(Op);
12204 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012205 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012206 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012207 .addReg(X86::XMM0);
12208
Dan Gohman14152b42010-07-06 20:24:04 +000012209 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012210 return BB;
12211}
12212
12213MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012214X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012215 DebugLoc dl = MI->getDebugLoc();
12216 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012217
Eric Christopher228232b2010-11-30 07:20:12 +000012218 // Address into RAX/EAX, other two args into ECX, EDX.
12219 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12220 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12221 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12222 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012223 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012224
Eric Christopher228232b2010-11-30 07:20:12 +000012225 unsigned ValOps = X86::AddrNumOperands;
12226 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12227 .addReg(MI->getOperand(ValOps).getReg());
12228 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12229 .addReg(MI->getOperand(ValOps+1).getReg());
12230
12231 // The instruction doesn't actually take any operands though.
12232 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012233
Eric Christopher228232b2010-11-30 07:20:12 +000012234 MI->eraseFromParent(); // The pseudo is gone now.
12235 return BB;
12236}
12237
12238MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012239X86TargetLowering::EmitVAARG64WithCustomInserter(
12240 MachineInstr *MI,
12241 MachineBasicBlock *MBB) const {
12242 // Emit va_arg instruction on X86-64.
12243
12244 // Operands to this pseudo-instruction:
12245 // 0 ) Output : destination address (reg)
12246 // 1-5) Input : va_list address (addr, i64mem)
12247 // 6 ) ArgSize : Size (in bytes) of vararg type
12248 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12249 // 8 ) Align : Alignment of type
12250 // 9 ) EFLAGS (implicit-def)
12251
12252 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12253 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12254
12255 unsigned DestReg = MI->getOperand(0).getReg();
12256 MachineOperand &Base = MI->getOperand(1);
12257 MachineOperand &Scale = MI->getOperand(2);
12258 MachineOperand &Index = MI->getOperand(3);
12259 MachineOperand &Disp = MI->getOperand(4);
12260 MachineOperand &Segment = MI->getOperand(5);
12261 unsigned ArgSize = MI->getOperand(6).getImm();
12262 unsigned ArgMode = MI->getOperand(7).getImm();
12263 unsigned Align = MI->getOperand(8).getImm();
12264
12265 // Memory Reference
12266 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12267 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12268 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12269
12270 // Machine Information
12271 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12272 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12273 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12274 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12275 DebugLoc DL = MI->getDebugLoc();
12276
12277 // struct va_list {
12278 // i32 gp_offset
12279 // i32 fp_offset
12280 // i64 overflow_area (address)
12281 // i64 reg_save_area (address)
12282 // }
12283 // sizeof(va_list) = 24
12284 // alignment(va_list) = 8
12285
12286 unsigned TotalNumIntRegs = 6;
12287 unsigned TotalNumXMMRegs = 8;
12288 bool UseGPOffset = (ArgMode == 1);
12289 bool UseFPOffset = (ArgMode == 2);
12290 unsigned MaxOffset = TotalNumIntRegs * 8 +
12291 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12292
12293 /* Align ArgSize to a multiple of 8 */
12294 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12295 bool NeedsAlign = (Align > 8);
12296
12297 MachineBasicBlock *thisMBB = MBB;
12298 MachineBasicBlock *overflowMBB;
12299 MachineBasicBlock *offsetMBB;
12300 MachineBasicBlock *endMBB;
12301
12302 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12303 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12304 unsigned OffsetReg = 0;
12305
12306 if (!UseGPOffset && !UseFPOffset) {
12307 // If we only pull from the overflow region, we don't create a branch.
12308 // We don't need to alter control flow.
12309 OffsetDestReg = 0; // unused
12310 OverflowDestReg = DestReg;
12311
12312 offsetMBB = NULL;
12313 overflowMBB = thisMBB;
12314 endMBB = thisMBB;
12315 } else {
12316 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12317 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12318 // If not, pull from overflow_area. (branch to overflowMBB)
12319 //
12320 // thisMBB
12321 // | .
12322 // | .
12323 // offsetMBB overflowMBB
12324 // | .
12325 // | .
12326 // endMBB
12327
12328 // Registers for the PHI in endMBB
12329 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12330 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12331
12332 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12333 MachineFunction *MF = MBB->getParent();
12334 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12335 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12336 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12337
12338 MachineFunction::iterator MBBIter = MBB;
12339 ++MBBIter;
12340
12341 // Insert the new basic blocks
12342 MF->insert(MBBIter, offsetMBB);
12343 MF->insert(MBBIter, overflowMBB);
12344 MF->insert(MBBIter, endMBB);
12345
12346 // Transfer the remainder of MBB and its successor edges to endMBB.
12347 endMBB->splice(endMBB->begin(), thisMBB,
12348 llvm::next(MachineBasicBlock::iterator(MI)),
12349 thisMBB->end());
12350 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12351
12352 // Make offsetMBB and overflowMBB successors of thisMBB
12353 thisMBB->addSuccessor(offsetMBB);
12354 thisMBB->addSuccessor(overflowMBB);
12355
12356 // endMBB is a successor of both offsetMBB and overflowMBB
12357 offsetMBB->addSuccessor(endMBB);
12358 overflowMBB->addSuccessor(endMBB);
12359
12360 // Load the offset value into a register
12361 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12362 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12363 .addOperand(Base)
12364 .addOperand(Scale)
12365 .addOperand(Index)
12366 .addDisp(Disp, UseFPOffset ? 4 : 0)
12367 .addOperand(Segment)
12368 .setMemRefs(MMOBegin, MMOEnd);
12369
12370 // Check if there is enough room left to pull this argument.
12371 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12372 .addReg(OffsetReg)
12373 .addImm(MaxOffset + 8 - ArgSizeA8);
12374
12375 // Branch to "overflowMBB" if offset >= max
12376 // Fall through to "offsetMBB" otherwise
12377 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12378 .addMBB(overflowMBB);
12379 }
12380
12381 // In offsetMBB, emit code to use the reg_save_area.
12382 if (offsetMBB) {
12383 assert(OffsetReg != 0);
12384
12385 // Read the reg_save_area address.
12386 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12387 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12388 .addOperand(Base)
12389 .addOperand(Scale)
12390 .addOperand(Index)
12391 .addDisp(Disp, 16)
12392 .addOperand(Segment)
12393 .setMemRefs(MMOBegin, MMOEnd);
12394
12395 // Zero-extend the offset
12396 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12397 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12398 .addImm(0)
12399 .addReg(OffsetReg)
12400 .addImm(X86::sub_32bit);
12401
12402 // Add the offset to the reg_save_area to get the final address.
12403 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12404 .addReg(OffsetReg64)
12405 .addReg(RegSaveReg);
12406
12407 // Compute the offset for the next argument
12408 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12409 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12410 .addReg(OffsetReg)
12411 .addImm(UseFPOffset ? 16 : 8);
12412
12413 // Store it back into the va_list.
12414 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12415 .addOperand(Base)
12416 .addOperand(Scale)
12417 .addOperand(Index)
12418 .addDisp(Disp, UseFPOffset ? 4 : 0)
12419 .addOperand(Segment)
12420 .addReg(NextOffsetReg)
12421 .setMemRefs(MMOBegin, MMOEnd);
12422
12423 // Jump to endMBB
12424 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12425 .addMBB(endMBB);
12426 }
12427
12428 //
12429 // Emit code to use overflow area
12430 //
12431
12432 // Load the overflow_area address into a register.
12433 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12434 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12435 .addOperand(Base)
12436 .addOperand(Scale)
12437 .addOperand(Index)
12438 .addDisp(Disp, 8)
12439 .addOperand(Segment)
12440 .setMemRefs(MMOBegin, MMOEnd);
12441
12442 // If we need to align it, do so. Otherwise, just copy the address
12443 // to OverflowDestReg.
12444 if (NeedsAlign) {
12445 // Align the overflow address
12446 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12447 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12448
12449 // aligned_addr = (addr + (align-1)) & ~(align-1)
12450 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12451 .addReg(OverflowAddrReg)
12452 .addImm(Align-1);
12453
12454 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12455 .addReg(TmpReg)
12456 .addImm(~(uint64_t)(Align-1));
12457 } else {
12458 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12459 .addReg(OverflowAddrReg);
12460 }
12461
12462 // Compute the next overflow address after this argument.
12463 // (the overflow address should be kept 8-byte aligned)
12464 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12465 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12466 .addReg(OverflowDestReg)
12467 .addImm(ArgSizeA8);
12468
12469 // Store the new overflow address.
12470 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12471 .addOperand(Base)
12472 .addOperand(Scale)
12473 .addOperand(Index)
12474 .addDisp(Disp, 8)
12475 .addOperand(Segment)
12476 .addReg(NextAddrReg)
12477 .setMemRefs(MMOBegin, MMOEnd);
12478
12479 // If we branched, emit the PHI to the front of endMBB.
12480 if (offsetMBB) {
12481 BuildMI(*endMBB, endMBB->begin(), DL,
12482 TII->get(X86::PHI), DestReg)
12483 .addReg(OffsetDestReg).addMBB(offsetMBB)
12484 .addReg(OverflowDestReg).addMBB(overflowMBB);
12485 }
12486
12487 // Erase the pseudo instruction
12488 MI->eraseFromParent();
12489
12490 return endMBB;
12491}
12492
12493MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012494X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12495 MachineInstr *MI,
12496 MachineBasicBlock *MBB) const {
12497 // Emit code to save XMM registers to the stack. The ABI says that the
12498 // number of registers to save is given in %al, so it's theoretically
12499 // possible to do an indirect jump trick to avoid saving all of them,
12500 // however this code takes a simpler approach and just executes all
12501 // of the stores if %al is non-zero. It's less code, and it's probably
12502 // easier on the hardware branch predictor, and stores aren't all that
12503 // expensive anyway.
12504
12505 // Create the new basic blocks. One block contains all the XMM stores,
12506 // and one block is the final destination regardless of whether any
12507 // stores were performed.
12508 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12509 MachineFunction *F = MBB->getParent();
12510 MachineFunction::iterator MBBIter = MBB;
12511 ++MBBIter;
12512 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12513 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12514 F->insert(MBBIter, XMMSaveMBB);
12515 F->insert(MBBIter, EndMBB);
12516
Dan Gohman14152b42010-07-06 20:24:04 +000012517 // Transfer the remainder of MBB and its successor edges to EndMBB.
12518 EndMBB->splice(EndMBB->begin(), MBB,
12519 llvm::next(MachineBasicBlock::iterator(MI)),
12520 MBB->end());
12521 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12522
Dan Gohmand6708ea2009-08-15 01:38:56 +000012523 // The original block will now fall through to the XMM save block.
12524 MBB->addSuccessor(XMMSaveMBB);
12525 // The XMMSaveMBB will fall through to the end block.
12526 XMMSaveMBB->addSuccessor(EndMBB);
12527
12528 // Now add the instructions.
12529 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12530 DebugLoc DL = MI->getDebugLoc();
12531
12532 unsigned CountReg = MI->getOperand(0).getReg();
12533 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12534 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12535
12536 if (!Subtarget->isTargetWin64()) {
12537 // If %al is 0, branch around the XMM save block.
12538 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012539 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012540 MBB->addSuccessor(EndMBB);
12541 }
12542
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012543 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012544 // In the XMM save block, save all the XMM argument registers.
12545 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12546 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012547 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012548 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012549 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012550 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012551 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012552 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012553 .addFrameIndex(RegSaveFrameIndex)
12554 .addImm(/*Scale=*/1)
12555 .addReg(/*IndexReg=*/0)
12556 .addImm(/*Disp=*/Offset)
12557 .addReg(/*Segment=*/0)
12558 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012559 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012560 }
12561
Dan Gohman14152b42010-07-06 20:24:04 +000012562 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012563
12564 return EndMBB;
12565}
Mon P Wang63307c32008-05-05 19:05:59 +000012566
Lang Hames6e3f7e42012-02-03 01:13:49 +000012567// The EFLAGS operand of SelectItr might be missing a kill marker
12568// because there were multiple uses of EFLAGS, and ISel didn't know
12569// which to mark. Figure out whether SelectItr should have had a
12570// kill marker, and set it if it should. Returns the correct kill
12571// marker value.
12572static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12573 MachineBasicBlock* BB,
12574 const TargetRegisterInfo* TRI) {
12575 // Scan forward through BB for a use/def of EFLAGS.
12576 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12577 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012578 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012579 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012580 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012581 if (mi.definesRegister(X86::EFLAGS))
12582 break; // Should have kill-flag - update below.
12583 }
12584
12585 // If we hit the end of the block, check whether EFLAGS is live into a
12586 // successor.
12587 if (miI == BB->end()) {
12588 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12589 sEnd = BB->succ_end();
12590 sItr != sEnd; ++sItr) {
12591 MachineBasicBlock* succ = *sItr;
12592 if (succ->isLiveIn(X86::EFLAGS))
12593 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012594 }
12595 }
12596
Lang Hames6e3f7e42012-02-03 01:13:49 +000012597 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12598 // out. SelectMI should have a kill flag on EFLAGS.
12599 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012600 return true;
12601}
12602
Evan Cheng60c07e12006-07-05 22:17:51 +000012603MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012604X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012605 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012606 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12607 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012608
Chris Lattner52600972009-09-02 05:57:00 +000012609 // To "insert" a SELECT_CC instruction, we actually have to insert the
12610 // diamond control-flow pattern. The incoming instruction knows the
12611 // destination vreg to set, the condition code register to branch on, the
12612 // true/false values to select between, and a branch opcode to use.
12613 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12614 MachineFunction::iterator It = BB;
12615 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012616
Chris Lattner52600972009-09-02 05:57:00 +000012617 // thisMBB:
12618 // ...
12619 // TrueVal = ...
12620 // cmpTY ccX, r1, r2
12621 // bCC copy1MBB
12622 // fallthrough --> copy0MBB
12623 MachineBasicBlock *thisMBB = BB;
12624 MachineFunction *F = BB->getParent();
12625 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12626 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012627 F->insert(It, copy0MBB);
12628 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012629
Bill Wendling730c07e2010-06-25 20:48:10 +000012630 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12631 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012632 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12633 if (!MI->killsRegister(X86::EFLAGS) &&
12634 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12635 copy0MBB->addLiveIn(X86::EFLAGS);
12636 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012637 }
12638
Dan Gohman14152b42010-07-06 20:24:04 +000012639 // Transfer the remainder of BB and its successor edges to sinkMBB.
12640 sinkMBB->splice(sinkMBB->begin(), BB,
12641 llvm::next(MachineBasicBlock::iterator(MI)),
12642 BB->end());
12643 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12644
12645 // Add the true and fallthrough blocks as its successors.
12646 BB->addSuccessor(copy0MBB);
12647 BB->addSuccessor(sinkMBB);
12648
12649 // Create the conditional branch instruction.
12650 unsigned Opc =
12651 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12652 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12653
Chris Lattner52600972009-09-02 05:57:00 +000012654 // copy0MBB:
12655 // %FalseValue = ...
12656 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012657 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012658
Chris Lattner52600972009-09-02 05:57:00 +000012659 // sinkMBB:
12660 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12661 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012662 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12663 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012664 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12665 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12666
Dan Gohman14152b42010-07-06 20:24:04 +000012667 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012668 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012669}
12670
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012671MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012672X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12673 bool Is64Bit) const {
12674 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12675 DebugLoc DL = MI->getDebugLoc();
12676 MachineFunction *MF = BB->getParent();
12677 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12678
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012679 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012680
12681 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12682 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12683
12684 // BB:
12685 // ... [Till the alloca]
12686 // If stacklet is not large enough, jump to mallocMBB
12687 //
12688 // bumpMBB:
12689 // Allocate by subtracting from RSP
12690 // Jump to continueMBB
12691 //
12692 // mallocMBB:
12693 // Allocate by call to runtime
12694 //
12695 // continueMBB:
12696 // ...
12697 // [rest of original BB]
12698 //
12699
12700 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12701 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12702 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12703
12704 MachineRegisterInfo &MRI = MF->getRegInfo();
12705 const TargetRegisterClass *AddrRegClass =
12706 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12707
12708 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12709 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12710 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012711 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012712 sizeVReg = MI->getOperand(1).getReg(),
12713 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12714
12715 MachineFunction::iterator MBBIter = BB;
12716 ++MBBIter;
12717
12718 MF->insert(MBBIter, bumpMBB);
12719 MF->insert(MBBIter, mallocMBB);
12720 MF->insert(MBBIter, continueMBB);
12721
12722 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12723 (MachineBasicBlock::iterator(MI)), BB->end());
12724 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12725
12726 // Add code to the main basic block to check if the stack limit has been hit,
12727 // and if so, jump to mallocMBB otherwise to bumpMBB.
12728 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012729 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012730 .addReg(tmpSPVReg).addReg(sizeVReg);
12731 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012732 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012733 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012734 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12735
12736 // bumpMBB simply decreases the stack pointer, since we know the current
12737 // stacklet has enough space.
12738 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012739 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012740 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012741 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012742 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12743
12744 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012745 const uint32_t *RegMask =
12746 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012747 if (Is64Bit) {
12748 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12749 .addReg(sizeVReg);
12750 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012751 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012752 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012753 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012754 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012755 } else {
12756 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12757 .addImm(12);
12758 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12759 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012760 .addExternalSymbol("__morestack_allocate_stack_space")
12761 .addRegMask(RegMask)
12762 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012763 }
12764
12765 if (!Is64Bit)
12766 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12767 .addImm(16);
12768
12769 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12770 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12771 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12772
12773 // Set up the CFG correctly.
12774 BB->addSuccessor(bumpMBB);
12775 BB->addSuccessor(mallocMBB);
12776 mallocMBB->addSuccessor(continueMBB);
12777 bumpMBB->addSuccessor(continueMBB);
12778
12779 // Take care of the PHI nodes.
12780 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12781 MI->getOperand(0).getReg())
12782 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12783 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12784
12785 // Delete the original pseudo instruction.
12786 MI->eraseFromParent();
12787
12788 // And we're done.
12789 return continueMBB;
12790}
12791
12792MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012793X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012794 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012795 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12796 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012797
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012798 assert(!Subtarget->isTargetEnvMacho());
12799
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012800 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12801 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012802
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012803 if (Subtarget->isTargetWin64()) {
12804 if (Subtarget->isTargetCygMing()) {
12805 // ___chkstk(Mingw64):
12806 // Clobbers R10, R11, RAX and EFLAGS.
12807 // Updates RSP.
12808 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12809 .addExternalSymbol("___chkstk")
12810 .addReg(X86::RAX, RegState::Implicit)
12811 .addReg(X86::RSP, RegState::Implicit)
12812 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12813 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12814 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12815 } else {
12816 // __chkstk(MSVCRT): does not update stack pointer.
12817 // Clobbers R10, R11 and EFLAGS.
12818 // FIXME: RAX(allocated size) might be reused and not killed.
12819 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12820 .addExternalSymbol("__chkstk")
12821 .addReg(X86::RAX, RegState::Implicit)
12822 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12823 // RAX has the offset to subtracted from RSP.
12824 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12825 .addReg(X86::RSP)
12826 .addReg(X86::RAX);
12827 }
12828 } else {
12829 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012830 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12831
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012832 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12833 .addExternalSymbol(StackProbeSymbol)
12834 .addReg(X86::EAX, RegState::Implicit)
12835 .addReg(X86::ESP, RegState::Implicit)
12836 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12837 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12838 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12839 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012840
Dan Gohman14152b42010-07-06 20:24:04 +000012841 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012842 return BB;
12843}
Chris Lattner52600972009-09-02 05:57:00 +000012844
12845MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012846X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12847 MachineBasicBlock *BB) const {
12848 // This is pretty easy. We're taking the value that we received from
12849 // our load from the relocation, sticking it in either RDI (x86-64)
12850 // or EAX and doing an indirect call. The return value will then
12851 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012852 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012853 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012854 DebugLoc DL = MI->getDebugLoc();
12855 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012856
12857 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012858 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012859
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012860 // Get a register mask for the lowered call.
12861 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
12862 // proper register mask.
12863 const uint32_t *RegMask =
12864 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012865 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012866 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12867 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012868 .addReg(X86::RIP)
12869 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012870 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012871 MI->getOperand(3).getTargetFlags())
12872 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012873 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012874 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012875 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000012876 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012877 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12878 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012879 .addReg(0)
12880 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012881 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012882 MI->getOperand(3).getTargetFlags())
12883 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012884 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012885 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012886 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012887 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012888 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12889 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012890 .addReg(TII->getGlobalBaseReg(F))
12891 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012892 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012893 MI->getOperand(3).getTargetFlags())
12894 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012895 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012896 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012897 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012898 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012899
Dan Gohman14152b42010-07-06 20:24:04 +000012900 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012901 return BB;
12902}
12903
12904MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012905X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012906 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012907 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012908 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012909 case X86::TAILJMPd64:
12910 case X86::TAILJMPr64:
12911 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000012912 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012913 case X86::TCRETURNdi64:
12914 case X86::TCRETURNri64:
12915 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012916 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012917 case X86::WIN_ALLOCA:
12918 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012919 case X86::SEG_ALLOCA_32:
12920 return EmitLoweredSegAlloca(MI, BB, false);
12921 case X86::SEG_ALLOCA_64:
12922 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012923 case X86::TLSCall_32:
12924 case X86::TLSCall_64:
12925 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012926 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012927 case X86::CMOV_FR32:
12928 case X86::CMOV_FR64:
12929 case X86::CMOV_V4F32:
12930 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012931 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012932 case X86::CMOV_V8F32:
12933 case X86::CMOV_V4F64:
12934 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012935 case X86::CMOV_GR16:
12936 case X86::CMOV_GR32:
12937 case X86::CMOV_RFP32:
12938 case X86::CMOV_RFP64:
12939 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012940 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012941
Dale Johannesen849f2142007-07-03 00:53:03 +000012942 case X86::FP32_TO_INT16_IN_MEM:
12943 case X86::FP32_TO_INT32_IN_MEM:
12944 case X86::FP32_TO_INT64_IN_MEM:
12945 case X86::FP64_TO_INT16_IN_MEM:
12946 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012947 case X86::FP64_TO_INT64_IN_MEM:
12948 case X86::FP80_TO_INT16_IN_MEM:
12949 case X86::FP80_TO_INT32_IN_MEM:
12950 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012951 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12952 DebugLoc DL = MI->getDebugLoc();
12953
Evan Cheng60c07e12006-07-05 22:17:51 +000012954 // Change the floating point control register to use "round towards zero"
12955 // mode when truncating to an integer value.
12956 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012957 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012958 addFrameReference(BuildMI(*BB, MI, DL,
12959 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012960
12961 // Load the old value of the high byte of the control word...
12962 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000012963 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012964 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012965 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012966
12967 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012968 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012969 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012970
12971 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012972 addFrameReference(BuildMI(*BB, MI, DL,
12973 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012974
12975 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012976 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012977 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012978
12979 // Get the X86 opcode to use.
12980 unsigned Opc;
12981 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012982 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012983 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12984 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12985 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12986 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12987 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12988 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012989 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12990 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12991 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012992 }
12993
12994 X86AddressMode AM;
12995 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012996 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012997 AM.BaseType = X86AddressMode::RegBase;
12998 AM.Base.Reg = Op.getReg();
12999 } else {
13000 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000013001 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000013002 }
13003 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000013004 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013005 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013006 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000013007 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000013008 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013009 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000013010 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000013011 AM.GV = Op.getGlobal();
13012 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000013013 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013014 }
Dan Gohman14152b42010-07-06 20:24:04 +000013015 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013016 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013017
13018 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013019 addFrameReference(BuildMI(*BB, MI, DL,
13020 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013021
Dan Gohman14152b42010-07-06 20:24:04 +000013022 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013023 return BB;
13024 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013025 // String/text processing lowering.
13026 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013027 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013028 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013029 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013030 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013031 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013032 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013033 case X86::VPCMPESTRM128MEM: {
13034 unsigned NumArgs;
13035 bool MemArg;
13036 switch (MI->getOpcode()) {
13037 default: llvm_unreachable("illegal opcode!");
13038 case X86::PCMPISTRM128REG:
13039 case X86::VPCMPISTRM128REG:
13040 NumArgs = 3; MemArg = false; break;
13041 case X86::PCMPISTRM128MEM:
13042 case X86::VPCMPISTRM128MEM:
13043 NumArgs = 3; MemArg = true; break;
13044 case X86::PCMPESTRM128REG:
13045 case X86::VPCMPESTRM128REG:
13046 NumArgs = 5; MemArg = false; break;
13047 case X86::PCMPESTRM128MEM:
13048 case X86::VPCMPESTRM128MEM:
13049 NumArgs = 5; MemArg = true; break;
13050 }
13051 return EmitPCMP(MI, BB, NumArgs, MemArg);
13052 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013053
Eric Christopher228232b2010-11-30 07:20:12 +000013054 // Thread synchronization.
13055 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013056 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013057
Eric Christopherb120ab42009-08-18 22:50:32 +000013058 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000013059 case X86::ATOMMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013060 case X86::ATOMMAX32:
Mon P Wang63307c32008-05-05 19:05:59 +000013061 case X86::ATOMUMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013062 case X86::ATOMUMAX32:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013063 case X86::ATOMMIN16:
13064 case X86::ATOMMAX16:
13065 case X86::ATOMUMIN16:
13066 case X86::ATOMUMAX16:
13067 case X86::ATOMMIN64:
13068 case X86::ATOMMAX64:
13069 case X86::ATOMUMIN64:
13070 case X86::ATOMUMAX64: {
13071 unsigned Opc;
13072 switch (MI->getOpcode()) {
13073 default: llvm_unreachable("illegal opcode!");
13074 case X86::ATOMMIN32: Opc = X86::CMOVL32rr; break;
13075 case X86::ATOMMAX32: Opc = X86::CMOVG32rr; break;
13076 case X86::ATOMUMIN32: Opc = X86::CMOVB32rr; break;
13077 case X86::ATOMUMAX32: Opc = X86::CMOVA32rr; break;
13078 case X86::ATOMMIN16: Opc = X86::CMOVL16rr; break;
13079 case X86::ATOMMAX16: Opc = X86::CMOVG16rr; break;
13080 case X86::ATOMUMIN16: Opc = X86::CMOVB16rr; break;
13081 case X86::ATOMUMAX16: Opc = X86::CMOVA16rr; break;
13082 case X86::ATOMMIN64: Opc = X86::CMOVL64rr; break;
13083 case X86::ATOMMAX64: Opc = X86::CMOVG64rr; break;
13084 case X86::ATOMUMIN64: Opc = X86::CMOVB64rr; break;
13085 case X86::ATOMUMAX64: Opc = X86::CMOVA64rr; break;
13086 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
13087 }
13088 return EmitAtomicMinMaxWithCustomInserter(MI, BB, Opc);
13089 }
13090
13091 case X86::ATOMAND32:
13092 case X86::ATOMOR32:
13093 case X86::ATOMXOR32:
13094 case X86::ATOMNAND32: {
13095 bool Invert = false;
13096 unsigned RegOpc, ImmOpc;
13097 switch (MI->getOpcode()) {
13098 default: llvm_unreachable("illegal opcode!");
13099 case X86::ATOMAND32:
13100 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; break;
13101 case X86::ATOMOR32:
13102 RegOpc = X86::OR32rr; ImmOpc = X86::OR32ri; break;
13103 case X86::ATOMXOR32:
13104 RegOpc = X86::XOR32rr; ImmOpc = X86::XOR32ri; break;
13105 case X86::ATOMNAND32:
13106 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; Invert = true; break;
13107 }
13108 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13109 X86::MOV32rm, X86::LCMPXCHG32,
13110 X86::NOT32r, X86::EAX,
13111 &X86::GR32RegClass, Invert);
13112 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013113
13114 case X86::ATOMAND16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013115 case X86::ATOMOR16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013116 case X86::ATOMXOR16:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013117 case X86::ATOMNAND16: {
13118 bool Invert = false;
13119 unsigned RegOpc, ImmOpc;
13120 switch (MI->getOpcode()) {
13121 default: llvm_unreachable("illegal opcode!");
13122 case X86::ATOMAND16:
13123 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; break;
13124 case X86::ATOMOR16:
13125 RegOpc = X86::OR16rr; ImmOpc = X86::OR16ri; break;
13126 case X86::ATOMXOR16:
13127 RegOpc = X86::XOR16rr; ImmOpc = X86::XOR16ri; break;
13128 case X86::ATOMNAND16:
13129 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; Invert = true; break;
13130 }
13131 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13132 X86::MOV16rm, X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013133 X86::NOT16r, X86::AX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013134 &X86::GR16RegClass, Invert);
13135 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013136
13137 case X86::ATOMAND8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013138 case X86::ATOMOR8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013139 case X86::ATOMXOR8:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013140 case X86::ATOMNAND8: {
13141 bool Invert = false;
13142 unsigned RegOpc, ImmOpc;
13143 switch (MI->getOpcode()) {
13144 default: llvm_unreachable("illegal opcode!");
13145 case X86::ATOMAND8:
13146 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; break;
13147 case X86::ATOMOR8:
13148 RegOpc = X86::OR8rr; ImmOpc = X86::OR8ri; break;
13149 case X86::ATOMXOR8:
13150 RegOpc = X86::XOR8rr; ImmOpc = X86::XOR8ri; break;
13151 case X86::ATOMNAND8:
13152 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; Invert = true; break;
13153 }
13154 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13155 X86::MOV8rm, X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013156 X86::NOT8r, X86::AL,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013157 &X86::GR8RegClass, Invert);
13158 }
13159
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013160 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000013161 case X86::ATOMAND64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013162 case X86::ATOMOR64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013163 case X86::ATOMXOR64:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013164 case X86::ATOMNAND64: {
13165 bool Invert = false;
13166 unsigned RegOpc, ImmOpc;
13167 switch (MI->getOpcode()) {
13168 default: llvm_unreachable("illegal opcode!");
13169 case X86::ATOMAND64:
13170 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; break;
13171 case X86::ATOMOR64:
13172 RegOpc = X86::OR64rr; ImmOpc = X86::OR64ri32; break;
13173 case X86::ATOMXOR64:
13174 RegOpc = X86::XOR64rr; ImmOpc = X86::XOR64ri32; break;
13175 case X86::ATOMNAND64:
13176 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; Invert = true; break;
13177 }
13178 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13179 X86::MOV64rm, X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013180 X86::NOT64r, X86::RAX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013181 &X86::GR64RegClass, Invert);
13182 }
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013183
13184 // This group does 64-bit operations on a 32-bit host.
13185 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013186 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013187 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013188 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013189 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013190 case X86::ATOMSUB6432:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013191 case X86::ATOMSWAP6432: {
13192 bool Invert = false;
13193 unsigned RegOpcL, RegOpcH, ImmOpcL, ImmOpcH;
13194 switch (MI->getOpcode()) {
13195 default: llvm_unreachable("illegal opcode!");
13196 case X86::ATOMAND6432:
13197 RegOpcL = RegOpcH = X86::AND32rr;
13198 ImmOpcL = ImmOpcH = X86::AND32ri;
13199 break;
13200 case X86::ATOMOR6432:
13201 RegOpcL = RegOpcH = X86::OR32rr;
13202 ImmOpcL = ImmOpcH = X86::OR32ri;
13203 break;
13204 case X86::ATOMXOR6432:
13205 RegOpcL = RegOpcH = X86::XOR32rr;
13206 ImmOpcL = ImmOpcH = X86::XOR32ri;
13207 break;
13208 case X86::ATOMNAND6432:
13209 RegOpcL = RegOpcH = X86::AND32rr;
13210 ImmOpcL = ImmOpcH = X86::AND32ri;
13211 Invert = true;
13212 break;
13213 case X86::ATOMADD6432:
13214 RegOpcL = X86::ADD32rr; RegOpcH = X86::ADC32rr;
13215 ImmOpcL = X86::ADD32ri; ImmOpcH = X86::ADC32ri;
13216 break;
13217 case X86::ATOMSUB6432:
13218 RegOpcL = X86::SUB32rr; RegOpcH = X86::SBB32rr;
13219 ImmOpcL = X86::SUB32ri; ImmOpcH = X86::SBB32ri;
13220 break;
13221 case X86::ATOMSWAP6432:
13222 RegOpcL = RegOpcH = X86::MOV32rr;
13223 ImmOpcL = ImmOpcH = X86::MOV32ri;
13224 break;
13225 }
13226 return EmitAtomicBit6432WithCustomInserter(MI, BB, RegOpcL, RegOpcH,
13227 ImmOpcL, ImmOpcH, Invert);
13228 }
13229
Dan Gohmand6708ea2009-08-15 01:38:56 +000013230 case X86::VASTART_SAVE_XMM_REGS:
13231 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013232
13233 case X86::VAARG_64:
13234 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013235 }
13236}
13237
13238//===----------------------------------------------------------------------===//
13239// X86 Optimization Hooks
13240//===----------------------------------------------------------------------===//
13241
Dan Gohman475871a2008-07-27 21:46:04 +000013242void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013243 APInt &KnownZero,
13244 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013245 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013246 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013247 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013248 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013249 assert((Opc >= ISD::BUILTIN_OP_END ||
13250 Opc == ISD::INTRINSIC_WO_CHAIN ||
13251 Opc == ISD::INTRINSIC_W_CHAIN ||
13252 Opc == ISD::INTRINSIC_VOID) &&
13253 "Should use MaskedValueIsZero if you don't know whether Op"
13254 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013255
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013256 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013257 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013258 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013259 case X86ISD::ADD:
13260 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013261 case X86ISD::ADC:
13262 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013263 case X86ISD::SMUL:
13264 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013265 case X86ISD::INC:
13266 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013267 case X86ISD::OR:
13268 case X86ISD::XOR:
13269 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013270 // These nodes' second result is a boolean.
13271 if (Op.getResNo() == 0)
13272 break;
13273 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013274 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013275 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013276 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013277 case ISD::INTRINSIC_WO_CHAIN: {
13278 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13279 unsigned NumLoBits = 0;
13280 switch (IntId) {
13281 default: break;
13282 case Intrinsic::x86_sse_movmsk_ps:
13283 case Intrinsic::x86_avx_movmsk_ps_256:
13284 case Intrinsic::x86_sse2_movmsk_pd:
13285 case Intrinsic::x86_avx_movmsk_pd_256:
13286 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013287 case Intrinsic::x86_sse2_pmovmskb_128:
13288 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013289 // High bits of movmskp{s|d}, pmovmskb are known zero.
13290 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013291 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013292 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13293 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13294 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13295 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13296 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13297 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013298 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013299 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013300 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013301 break;
13302 }
13303 }
13304 break;
13305 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013306 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013307}
Chris Lattner259e97c2006-01-31 19:43:35 +000013308
Owen Andersonbc146b02010-09-21 20:42:50 +000013309unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13310 unsigned Depth) const {
13311 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13312 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13313 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013314
Owen Andersonbc146b02010-09-21 20:42:50 +000013315 // Fallback case.
13316 return 1;
13317}
13318
Evan Cheng206ee9d2006-07-07 08:33:52 +000013319/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013320/// node is a GlobalAddress + offset.
13321bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013322 const GlobalValue* &GA,
13323 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013324 if (N->getOpcode() == X86ISD::Wrapper) {
13325 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013326 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013327 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013328 return true;
13329 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013330 }
Evan Chengad4196b2008-05-12 19:56:52 +000013331 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013332}
13333
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013334/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13335/// same as extracting the high 128-bit part of 256-bit vector and then
13336/// inserting the result into the low part of a new 256-bit vector
13337static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13338 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013339 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013340
13341 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013342 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013343 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13344 SVOp->getMaskElt(j) >= 0)
13345 return false;
13346
13347 return true;
13348}
13349
13350/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13351/// same as extracting the low 128-bit part of 256-bit vector and then
13352/// inserting the result into the high part of a new 256-bit vector
13353static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13354 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013355 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013356
13357 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013358 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013359 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13360 SVOp->getMaskElt(j) >= 0)
13361 return false;
13362
13363 return true;
13364}
13365
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013366/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13367static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013368 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013369 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013370 DebugLoc dl = N->getDebugLoc();
13371 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13372 SDValue V1 = SVOp->getOperand(0);
13373 SDValue V2 = SVOp->getOperand(1);
13374 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013375 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013376
13377 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13378 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13379 //
13380 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013381 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013382 // V UNDEF BUILD_VECTOR UNDEF
13383 // \ / \ /
13384 // CONCAT_VECTOR CONCAT_VECTOR
13385 // \ /
13386 // \ /
13387 // RESULT: V + zero extended
13388 //
13389 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13390 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13391 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13392 return SDValue();
13393
13394 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13395 return SDValue();
13396
13397 // To match the shuffle mask, the first half of the mask should
13398 // be exactly the first vector, and all the rest a splat with the
13399 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013400 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013401 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13402 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13403 return SDValue();
13404
Chad Rosier3d1161e2012-01-03 21:05:52 +000013405 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13406 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013407 if (Ld->hasNUsesOfValue(1, 0)) {
13408 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13409 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13410 SDValue ResNode =
13411 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13412 Ld->getMemoryVT(),
13413 Ld->getPointerInfo(),
13414 Ld->getAlignment(),
13415 false/*isVolatile*/, true/*ReadMem*/,
13416 false/*WriteMem*/);
13417 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13418 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013419 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013420
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013421 // Emit a zeroed vector and insert the desired subvector on its
13422 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013423 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013424 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013425 return DCI.CombineTo(N, InsV);
13426 }
13427
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013428 //===--------------------------------------------------------------------===//
13429 // Combine some shuffles into subvector extracts and inserts:
13430 //
13431
13432 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13433 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013434 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13435 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013436 return DCI.CombineTo(N, InsV);
13437 }
13438
13439 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13440 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013441 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13442 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013443 return DCI.CombineTo(N, InsV);
13444 }
13445
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013446 return SDValue();
13447}
13448
13449/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013450static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013451 TargetLowering::DAGCombinerInfo &DCI,
13452 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013453 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013454 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013455
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013456 // Don't create instructions with illegal types after legalize types has run.
13457 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13458 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13459 return SDValue();
13460
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013461 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013462 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013463 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013464 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013465
13466 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013467 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013468 return SDValue();
13469
13470 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13471 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13472 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013473 SmallVector<SDValue, 16> Elts;
13474 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013475 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013476
Nate Begemanfdea31a2010-03-24 20:49:50 +000013477 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013478}
Evan Chengd880b972008-05-09 21:53:03 +000013479
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013480
Craig Topperc16f8512012-04-25 06:39:39 +000013481/// DCI, PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013482/// a sequence of vector shuffle operations.
13483/// It is possible when we truncate 256-bit vector to 128-bit vector
13484
Chad Rosiera20e1e72012-08-01 18:39:17 +000013485SDValue X86TargetLowering::PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013486 DAGCombinerInfo &DCI) const {
13487 if (!DCI.isBeforeLegalizeOps())
13488 return SDValue();
13489
Craig Topper3ef43cf2012-04-24 06:36:35 +000013490 if (!Subtarget->hasAVX())
13491 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013492
13493 EVT VT = N->getValueType(0);
13494 SDValue Op = N->getOperand(0);
13495 EVT OpVT = Op.getValueType();
13496 DebugLoc dl = N->getDebugLoc();
13497
13498 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13499
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013500 if (Subtarget->hasAVX2()) {
13501 // AVX2: v4i64 -> v4i32
13502
13503 // VPERMD
13504 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13505
13506 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13507 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13508 ShufMask);
13509
Craig Topperd63fa652012-04-22 18:51:37 +000013510 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13511 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013512 }
13513
13514 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013515 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013516 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013517
13518 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013519 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013520
13521 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13522 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13523
13524 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013525 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013526
Craig Toppercacafd42012-08-14 08:18:43 +000013527 SDValue Undef = DAG.getUNDEF(VT);
13528 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13529 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013530
13531 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013532 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013533
Elena Demikhovsky73252572012-02-01 10:33:05 +000013534 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013535 }
Craig Topperd63fa652012-04-22 18:51:37 +000013536
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013537 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13538
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013539 if (Subtarget->hasAVX2()) {
13540 // AVX2: v8i32 -> v8i16
13541
13542 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013543
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013544 // PSHUFB
13545 SmallVector<SDValue,32> pshufbMask;
13546 for (unsigned i = 0; i < 2; ++i) {
13547 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13548 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13549 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13550 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13551 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13552 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13553 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13554 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13555 for (unsigned j = 0; j < 8; ++j)
13556 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13557 }
Craig Topperd63fa652012-04-22 18:51:37 +000013558 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13559 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013560 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13561
13562 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13563
13564 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013565 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013566 &ShufMask[0]);
13567
Craig Topperd63fa652012-04-22 18:51:37 +000013568 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13569 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013570
13571 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13572 }
13573
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013574 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013575 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013576
13577 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013578 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013579
13580 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13581 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13582
13583 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013584 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13585 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013586
Craig Toppercacafd42012-08-14 08:18:43 +000013587 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13588 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13589 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013590
13591 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13592 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13593
13594 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013595 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013596
Elena Demikhovsky73252572012-02-01 10:33:05 +000013597 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013598 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013599 }
13600
13601 return SDValue();
13602}
13603
Craig Topper89f4e662012-03-20 07:17:59 +000013604/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13605/// specific shuffle of a load can be folded into a single element load.
13606/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13607/// shuffles have been customed lowered so we need to handle those here.
13608static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13609 TargetLowering::DAGCombinerInfo &DCI) {
13610 if (DCI.isBeforeLegalizeOps())
13611 return SDValue();
13612
13613 SDValue InVec = N->getOperand(0);
13614 SDValue EltNo = N->getOperand(1);
13615
13616 if (!isa<ConstantSDNode>(EltNo))
13617 return SDValue();
13618
13619 EVT VT = InVec.getValueType();
13620
13621 bool HasShuffleIntoBitcast = false;
13622 if (InVec.getOpcode() == ISD::BITCAST) {
13623 // Don't duplicate a load with other uses.
13624 if (!InVec.hasOneUse())
13625 return SDValue();
13626 EVT BCVT = InVec.getOperand(0).getValueType();
13627 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13628 return SDValue();
13629 InVec = InVec.getOperand(0);
13630 HasShuffleIntoBitcast = true;
13631 }
13632
13633 if (!isTargetShuffle(InVec.getOpcode()))
13634 return SDValue();
13635
13636 // Don't duplicate a load with other uses.
13637 if (!InVec.hasOneUse())
13638 return SDValue();
13639
13640 SmallVector<int, 16> ShuffleMask;
13641 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013642 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13643 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013644 return SDValue();
13645
13646 // Select the input vector, guarding against out of range extract vector.
13647 unsigned NumElems = VT.getVectorNumElements();
13648 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13649 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13650 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13651 : InVec.getOperand(1);
13652
13653 // If inputs to shuffle are the same for both ops, then allow 2 uses
13654 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13655
13656 if (LdNode.getOpcode() == ISD::BITCAST) {
13657 // Don't duplicate a load with other uses.
13658 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13659 return SDValue();
13660
13661 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13662 LdNode = LdNode.getOperand(0);
13663 }
13664
13665 if (!ISD::isNormalLoad(LdNode.getNode()))
13666 return SDValue();
13667
13668 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13669
13670 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13671 return SDValue();
13672
13673 if (HasShuffleIntoBitcast) {
13674 // If there's a bitcast before the shuffle, check if the load type and
13675 // alignment is valid.
13676 unsigned Align = LN0->getAlignment();
13677 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13678 unsigned NewAlign = TLI.getTargetData()->
13679 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13680
13681 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13682 return SDValue();
13683 }
13684
13685 // All checks match so transform back to vector_shuffle so that DAG combiner
13686 // can finish the job
13687 DebugLoc dl = N->getDebugLoc();
13688
13689 // Create shuffle node taking into account the case that its a unary shuffle
13690 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13691 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13692 InVec.getOperand(0), Shuffle,
13693 &ShuffleMask[0]);
13694 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13695 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13696 EltNo);
13697}
13698
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013699/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13700/// generation and convert it from being a bunch of shuffles and extracts
13701/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013702static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013703 TargetLowering::DAGCombinerInfo &DCI) {
13704 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13705 if (NewOp.getNode())
13706 return NewOp;
13707
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013708 SDValue InputVector = N->getOperand(0);
13709
13710 // Only operate on vectors of 4 elements, where the alternative shuffling
13711 // gets to be more expensive.
13712 if (InputVector.getValueType() != MVT::v4i32)
13713 return SDValue();
13714
13715 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13716 // single use which is a sign-extend or zero-extend, and all elements are
13717 // used.
13718 SmallVector<SDNode *, 4> Uses;
13719 unsigned ExtractedElements = 0;
13720 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13721 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13722 if (UI.getUse().getResNo() != InputVector.getResNo())
13723 return SDValue();
13724
13725 SDNode *Extract = *UI;
13726 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13727 return SDValue();
13728
13729 if (Extract->getValueType(0) != MVT::i32)
13730 return SDValue();
13731 if (!Extract->hasOneUse())
13732 return SDValue();
13733 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13734 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13735 return SDValue();
13736 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13737 return SDValue();
13738
13739 // Record which element was extracted.
13740 ExtractedElements |=
13741 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13742
13743 Uses.push_back(Extract);
13744 }
13745
13746 // If not all the elements were used, this may not be worthwhile.
13747 if (ExtractedElements != 15)
13748 return SDValue();
13749
13750 // Ok, we've now decided to do the transformation.
13751 DebugLoc dl = InputVector.getDebugLoc();
13752
13753 // Store the value to a temporary stack slot.
13754 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013755 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13756 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013757
13758 // Replace each use (extract) with a load of the appropriate element.
13759 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13760 UE = Uses.end(); UI != UE; ++UI) {
13761 SDNode *Extract = *UI;
13762
Nadav Rotem86694292011-05-17 08:31:57 +000013763 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013764 SDValue Idx = Extract->getOperand(1);
13765 unsigned EltSize =
13766 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13767 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013768 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013769 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13770
Nadav Rotem86694292011-05-17 08:31:57 +000013771 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013772 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013773
13774 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013775 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013776 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013777 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013778
13779 // Replace the exact with the load.
13780 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13781 }
13782
13783 // The replacement was made in place; don't return anything.
13784 return SDValue();
13785}
13786
Duncan Sands6bcd2192011-09-17 16:49:39 +000013787/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13788/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013789static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013790 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013791 const X86Subtarget *Subtarget) {
13792 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013793 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013794 // Get the LHS/RHS of the select.
13795 SDValue LHS = N->getOperand(1);
13796 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013797 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013798
Dan Gohman670e5392009-09-21 18:03:22 +000013799 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013800 // instructions match the semantics of the common C idiom x<y?x:y but not
13801 // x<=y?x:y, because of how they handle negative zero (which can be
13802 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013803 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13804 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013805 (Subtarget->hasSSE2() ||
13806 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013807 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013808
Chris Lattner47b4ce82009-03-11 05:48:52 +000013809 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013810 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013811 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13812 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013813 switch (CC) {
13814 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013815 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013816 // Converting this to a min would handle NaNs incorrectly, and swapping
13817 // the operands would cause it to handle comparisons between positive
13818 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013819 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013820 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013821 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13822 break;
13823 std::swap(LHS, RHS);
13824 }
Dan Gohman670e5392009-09-21 18:03:22 +000013825 Opcode = X86ISD::FMIN;
13826 break;
13827 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013828 // Converting this to a min would handle comparisons between positive
13829 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013830 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013831 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13832 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013833 Opcode = X86ISD::FMIN;
13834 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013835 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013836 // Converting this to a min would handle both negative zeros and NaNs
13837 // incorrectly, but we can swap the operands to fix both.
13838 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013839 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013840 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013841 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013842 Opcode = X86ISD::FMIN;
13843 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013844
Dan Gohman670e5392009-09-21 18:03:22 +000013845 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013846 // Converting this to a max would handle comparisons between positive
13847 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013848 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013849 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013850 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013851 Opcode = X86ISD::FMAX;
13852 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013853 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013854 // Converting this to a max would handle NaNs incorrectly, and swapping
13855 // the operands would cause it to handle comparisons between positive
13856 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013857 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013858 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013859 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13860 break;
13861 std::swap(LHS, RHS);
13862 }
Dan Gohman670e5392009-09-21 18:03:22 +000013863 Opcode = X86ISD::FMAX;
13864 break;
13865 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013866 // Converting this to a max would handle both negative zeros and NaNs
13867 // incorrectly, but we can swap the operands to fix both.
13868 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013869 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013870 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013871 case ISD::SETGE:
13872 Opcode = X86ISD::FMAX;
13873 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013874 }
Dan Gohman670e5392009-09-21 18:03:22 +000013875 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013876 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13877 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013878 switch (CC) {
13879 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013880 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013881 // Converting this to a min would handle comparisons between positive
13882 // and negative zero incorrectly, and swapping the operands would
13883 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013884 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013885 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013886 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013887 break;
13888 std::swap(LHS, RHS);
13889 }
Dan Gohman670e5392009-09-21 18:03:22 +000013890 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013891 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013892 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013893 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013894 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013895 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13896 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013897 Opcode = X86ISD::FMIN;
13898 break;
13899 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013900 // Converting this to a min would handle both negative zeros and NaNs
13901 // incorrectly, but we can swap the operands to fix both.
13902 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013903 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013904 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013905 case ISD::SETGE:
13906 Opcode = X86ISD::FMIN;
13907 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013908
Dan Gohman670e5392009-09-21 18:03:22 +000013909 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013910 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013911 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013912 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013913 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013914 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013915 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013916 // Converting this to a max would handle comparisons between positive
13917 // and negative zero incorrectly, and swapping the operands would
13918 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013919 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013920 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013921 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013922 break;
13923 std::swap(LHS, RHS);
13924 }
Dan Gohman670e5392009-09-21 18:03:22 +000013925 Opcode = X86ISD::FMAX;
13926 break;
13927 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013928 // Converting this to a max would handle both negative zeros and NaNs
13929 // incorrectly, but we can swap the operands to fix both.
13930 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013931 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013932 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013933 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013934 Opcode = X86ISD::FMAX;
13935 break;
13936 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013937 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013938
Chris Lattner47b4ce82009-03-11 05:48:52 +000013939 if (Opcode)
13940 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013941 }
Eric Christopherfd179292009-08-27 18:07:15 +000013942
Chris Lattnerd1980a52009-03-12 06:52:53 +000013943 // If this is a select between two integer constants, try to do some
13944 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013945 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13946 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013947 // Don't do this for crazy integer types.
13948 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13949 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013950 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013951 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013952
Chris Lattnercee56e72009-03-13 05:53:31 +000013953 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013954 // Efficiently invertible.
13955 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13956 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13957 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13958 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013959 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013960 }
Eric Christopherfd179292009-08-27 18:07:15 +000013961
Chris Lattnerd1980a52009-03-12 06:52:53 +000013962 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013963 if (FalseC->getAPIntValue() == 0 &&
13964 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013965 if (NeedsCondInvert) // Invert the condition if needed.
13966 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13967 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013968
Chris Lattnerd1980a52009-03-12 06:52:53 +000013969 // Zero extend the condition if needed.
13970 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013971
Chris Lattnercee56e72009-03-13 05:53:31 +000013972 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013973 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013974 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013975 }
Eric Christopherfd179292009-08-27 18:07:15 +000013976
Chris Lattner97a29a52009-03-13 05:22:11 +000013977 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013978 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000013979 if (NeedsCondInvert) // Invert the condition if needed.
13980 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13981 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013982
Chris Lattner97a29a52009-03-13 05:22:11 +000013983 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013984 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13985 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013986 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013987 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013988 }
Eric Christopherfd179292009-08-27 18:07:15 +000013989
Chris Lattnercee56e72009-03-13 05:53:31 +000013990 // Optimize cases that will turn into an LEA instruction. This requires
13991 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013992 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013993 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013994 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013995
Chris Lattnercee56e72009-03-13 05:53:31 +000013996 bool isFastMultiplier = false;
13997 if (Diff < 10) {
13998 switch ((unsigned char)Diff) {
13999 default: break;
14000 case 1: // result = add base, cond
14001 case 2: // result = lea base( , cond*2)
14002 case 3: // result = lea base(cond, cond*2)
14003 case 4: // result = lea base( , cond*4)
14004 case 5: // result = lea base(cond, cond*4)
14005 case 8: // result = lea base( , cond*8)
14006 case 9: // result = lea base(cond, cond*8)
14007 isFastMultiplier = true;
14008 break;
14009 }
14010 }
Eric Christopherfd179292009-08-27 18:07:15 +000014011
Chris Lattnercee56e72009-03-13 05:53:31 +000014012 if (isFastMultiplier) {
14013 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14014 if (NeedsCondInvert) // Invert the condition if needed.
14015 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14016 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014017
Chris Lattnercee56e72009-03-13 05:53:31 +000014018 // Zero extend the condition if needed.
14019 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14020 Cond);
14021 // Scale the condition by the difference.
14022 if (Diff != 1)
14023 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14024 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014025
Chris Lattnercee56e72009-03-13 05:53:31 +000014026 // Add the base if non-zero.
14027 if (FalseC->getAPIntValue() != 0)
14028 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14029 SDValue(FalseC, 0));
14030 return Cond;
14031 }
Eric Christopherfd179292009-08-27 18:07:15 +000014032 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014033 }
14034 }
Eric Christopherfd179292009-08-27 18:07:15 +000014035
Evan Cheng56f582d2012-01-04 01:41:39 +000014036 // Canonicalize max and min:
14037 // (x > y) ? x : y -> (x >= y) ? x : y
14038 // (x < y) ? x : y -> (x <= y) ? x : y
14039 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14040 // the need for an extra compare
14041 // against zero. e.g.
14042 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14043 // subl %esi, %edi
14044 // testl %edi, %edi
14045 // movl $0, %eax
14046 // cmovgl %edi, %eax
14047 // =>
14048 // xorl %eax, %eax
14049 // subl %esi, $edi
14050 // cmovsl %eax, %edi
14051 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14052 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14053 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14054 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14055 switch (CC) {
14056 default: break;
14057 case ISD::SETLT:
14058 case ISD::SETGT: {
14059 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14060 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14061 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14062 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14063 }
14064 }
14065 }
14066
Nadav Rotemcc616562012-01-15 19:27:55 +000014067 // If we know that this node is legal then we know that it is going to be
14068 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14069 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14070 // to simplify previous instructions.
14071 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14072 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014073 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014074 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014075
14076 // Don't optimize vector selects that map to mask-registers.
14077 if (BitWidth == 1)
14078 return SDValue();
14079
Nadav Rotemcc616562012-01-15 19:27:55 +000014080 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14081 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14082
14083 APInt KnownZero, KnownOne;
14084 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14085 DCI.isBeforeLegalizeOps());
14086 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14087 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14088 DCI.CommitTargetLoweringOpt(TLO);
14089 }
14090
Dan Gohman475871a2008-07-27 21:46:04 +000014091 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014092}
14093
Michael Liao2a33cec2012-08-10 19:58:13 +000014094// Check whether a boolean test is testing a boolean value generated by
14095// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14096// code.
14097//
14098// Simplify the following patterns:
14099// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14100// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14101// to (Op EFLAGS Cond)
14102//
14103// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14104// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14105// to (Op EFLAGS !Cond)
14106//
14107// where Op could be BRCOND or CMOV.
14108//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014109static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014110 // Quit if not CMP and SUB with its value result used.
14111 if (Cmp.getOpcode() != X86ISD::CMP &&
14112 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14113 return SDValue();
14114
14115 // Quit if not used as a boolean value.
14116 if (CC != X86::COND_E && CC != X86::COND_NE)
14117 return SDValue();
14118
14119 // Check CMP operands. One of them should be 0 or 1 and the other should be
14120 // an SetCC or extended from it.
14121 SDValue Op1 = Cmp.getOperand(0);
14122 SDValue Op2 = Cmp.getOperand(1);
14123
14124 SDValue SetCC;
14125 const ConstantSDNode* C = 0;
14126 bool needOppositeCond = (CC == X86::COND_E);
14127
14128 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14129 SetCC = Op2;
14130 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14131 SetCC = Op1;
14132 else // Quit if all operands are not constants.
14133 return SDValue();
14134
14135 if (C->getZExtValue() == 1)
14136 needOppositeCond = !needOppositeCond;
14137 else if (C->getZExtValue() != 0)
14138 // Quit if the constant is neither 0 or 1.
14139 return SDValue();
14140
14141 // Skip 'zext' node.
14142 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14143 SetCC = SetCC.getOperand(0);
14144
14145 // Quit if not SETCC.
14146 // FIXME: So far we only handle the boolean value generated from SETCC. If
14147 // there is other ways to generate boolean values, we need handle them here
14148 // as well.
14149 if (SetCC.getOpcode() != X86ISD::SETCC)
14150 return SDValue();
14151
14152 // Set the condition code or opposite one if necessary.
14153 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14154 if (needOppositeCond)
14155 CC = X86::GetOppositeBranchCondition(CC);
14156
14157 return SetCC.getOperand(1);
14158}
14159
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014160/// checkFlaggedOrCombine - DAG combination on X86ISD::OR, i.e. with EFLAGS
14161/// updated. If only flag result is used and the result is evaluated from a
14162/// series of element extraction, try to combine it into a PTEST.
14163static SDValue checkFlaggedOrCombine(SDValue Or, X86::CondCode &CC,
14164 SelectionDAG &DAG,
14165 const X86Subtarget *Subtarget) {
14166 SDNode *N = Or.getNode();
14167 DebugLoc DL = N->getDebugLoc();
14168
14169 // Only SSE4.1 and beyond supports PTEST or like.
14170 if (!Subtarget->hasSSE41())
14171 return SDValue();
14172
14173 if (N->getOpcode() != X86ISD::OR)
14174 return SDValue();
14175
14176 // Quit if the value result of OR is used.
14177 if (N->hasAnyUseOfValue(0))
14178 return SDValue();
14179
14180 // Quit if not used as a boolean value.
14181 if (CC != X86::COND_E && CC != X86::COND_NE)
14182 return SDValue();
14183
14184 SmallVector<SDValue, 8> Opnds;
14185 SDValue VecIn;
14186 EVT VT = MVT::Other;
14187 unsigned Mask = 0;
14188
14189 // Recognize a special case where a vector is casted into wide integer to
14190 // test all 0s.
14191 Opnds.push_back(N->getOperand(0));
14192 Opnds.push_back(N->getOperand(1));
14193
14194 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
14195 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
14196 // BFS traverse all OR'd operands.
14197 if (I->getOpcode() == ISD::OR) {
14198 Opnds.push_back(I->getOperand(0));
14199 Opnds.push_back(I->getOperand(1));
14200 // Re-evaluate the number of nodes to be traversed.
Michael Liao95c22a32012-08-28 23:42:17 +000014201 e += 2; // 2 more nodes (LHS and RHS) are pushed.
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014202 continue;
14203 }
14204
14205 // Quit if a non-EXTRACT_VECTOR_ELT
14206 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14207 return SDValue();
14208
14209 // Quit if without a constant index.
14210 SDValue Idx = I->getOperand(1);
14211 if (!isa<ConstantSDNode>(Idx))
14212 return SDValue();
14213
14214 // Check if all elements are extracted from the same vector.
14215 SDValue ExtractedFromVec = I->getOperand(0);
14216 if (VecIn.getNode() == 0) {
14217 VT = ExtractedFromVec.getValueType();
14218 // FIXME: only 128-bit vector is supported so far.
14219 if (!VT.is128BitVector())
14220 return SDValue();
14221 VecIn = ExtractedFromVec;
14222 } else if (VecIn != ExtractedFromVec)
14223 return SDValue();
14224
14225 // Record the constant index.
14226 Mask |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
14227 }
14228
14229 assert(VT.is128BitVector() && "Only 128-bit vector PTEST is supported so far.");
14230
14231 // Quit if not all elements are used.
14232 if (Mask != (1U << VT.getVectorNumElements()) - 1U)
14233 return SDValue();
14234
14235 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32, VecIn, VecIn);
14236}
14237
Chris Lattnerd1980a52009-03-12 06:52:53 +000014238/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14239static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014240 TargetLowering::DAGCombinerInfo &DCI,
14241 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014242 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014243
Chris Lattnerd1980a52009-03-12 06:52:53 +000014244 // If the flag operand isn't dead, don't touch this CMOV.
14245 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14246 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014247
Evan Chengb5a55d92011-05-24 01:48:22 +000014248 SDValue FalseOp = N->getOperand(0);
14249 SDValue TrueOp = N->getOperand(1);
14250 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14251 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014252
Evan Chengb5a55d92011-05-24 01:48:22 +000014253 if (CC == X86::COND_E || CC == X86::COND_NE) {
14254 switch (Cond.getOpcode()) {
14255 default: break;
14256 case X86ISD::BSR:
14257 case X86ISD::BSF:
14258 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14259 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14260 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14261 }
14262 }
14263
Michael Liao2a33cec2012-08-10 19:58:13 +000014264 SDValue Flags;
14265
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014266 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014267 if (Flags.getNode() &&
14268 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014269 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014270 SDValue Ops[] = { FalseOp, TrueOp,
14271 DAG.getConstant(CC, MVT::i8), Flags };
14272 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14273 Ops, array_lengthof(Ops));
14274 }
14275
14276 Flags = checkFlaggedOrCombine(Cond, CC, DAG, Subtarget);
14277 if (Flags.getNode()) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014278 SDValue Ops[] = { FalseOp, TrueOp,
14279 DAG.getConstant(CC, MVT::i8), Flags };
14280 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14281 Ops, array_lengthof(Ops));
14282 }
14283
Chris Lattnerd1980a52009-03-12 06:52:53 +000014284 // If this is a select between two integer constants, try to do some
14285 // optimizations. Note that the operands are ordered the opposite of SELECT
14286 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014287 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14288 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014289 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14290 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014291 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14292 CC = X86::GetOppositeBranchCondition(CC);
14293 std::swap(TrueC, FalseC);
14294 }
Eric Christopherfd179292009-08-27 18:07:15 +000014295
Chris Lattnerd1980a52009-03-12 06:52:53 +000014296 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014297 // This is efficient for any integer data type (including i8/i16) and
14298 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014299 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014300 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14301 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014302
Chris Lattnerd1980a52009-03-12 06:52:53 +000014303 // Zero extend the condition if needed.
14304 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014305
Chris Lattnerd1980a52009-03-12 06:52:53 +000014306 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14307 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014308 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014309 if (N->getNumValues() == 2) // Dead flag value?
14310 return DCI.CombineTo(N, Cond, SDValue());
14311 return Cond;
14312 }
Eric Christopherfd179292009-08-27 18:07:15 +000014313
Chris Lattnercee56e72009-03-13 05:53:31 +000014314 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14315 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014316 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014317 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14318 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014319
Chris Lattner97a29a52009-03-13 05:22:11 +000014320 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014321 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14322 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014323 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14324 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014325
Chris Lattner97a29a52009-03-13 05:22:11 +000014326 if (N->getNumValues() == 2) // Dead flag value?
14327 return DCI.CombineTo(N, Cond, SDValue());
14328 return Cond;
14329 }
Eric Christopherfd179292009-08-27 18:07:15 +000014330
Chris Lattnercee56e72009-03-13 05:53:31 +000014331 // Optimize cases that will turn into an LEA instruction. This requires
14332 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014333 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014334 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014335 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014336
Chris Lattnercee56e72009-03-13 05:53:31 +000014337 bool isFastMultiplier = false;
14338 if (Diff < 10) {
14339 switch ((unsigned char)Diff) {
14340 default: break;
14341 case 1: // result = add base, cond
14342 case 2: // result = lea base( , cond*2)
14343 case 3: // result = lea base(cond, cond*2)
14344 case 4: // result = lea base( , cond*4)
14345 case 5: // result = lea base(cond, cond*4)
14346 case 8: // result = lea base( , cond*8)
14347 case 9: // result = lea base(cond, cond*8)
14348 isFastMultiplier = true;
14349 break;
14350 }
14351 }
Eric Christopherfd179292009-08-27 18:07:15 +000014352
Chris Lattnercee56e72009-03-13 05:53:31 +000014353 if (isFastMultiplier) {
14354 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014355 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14356 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014357 // Zero extend the condition if needed.
14358 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14359 Cond);
14360 // Scale the condition by the difference.
14361 if (Diff != 1)
14362 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14363 DAG.getConstant(Diff, Cond.getValueType()));
14364
14365 // Add the base if non-zero.
14366 if (FalseC->getAPIntValue() != 0)
14367 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14368 SDValue(FalseC, 0));
14369 if (N->getNumValues() == 2) // Dead flag value?
14370 return DCI.CombineTo(N, Cond, SDValue());
14371 return Cond;
14372 }
Eric Christopherfd179292009-08-27 18:07:15 +000014373 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014374 }
14375 }
14376 return SDValue();
14377}
14378
14379
Evan Cheng0b0cd912009-03-28 05:57:29 +000014380/// PerformMulCombine - Optimize a single multiply with constant into two
14381/// in order to implement it with two cheaper instructions, e.g.
14382/// LEA + SHL, LEA + LEA.
14383static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14384 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014385 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14386 return SDValue();
14387
Owen Andersone50ed302009-08-10 22:56:29 +000014388 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014389 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014390 return SDValue();
14391
14392 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14393 if (!C)
14394 return SDValue();
14395 uint64_t MulAmt = C->getZExtValue();
14396 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14397 return SDValue();
14398
14399 uint64_t MulAmt1 = 0;
14400 uint64_t MulAmt2 = 0;
14401 if ((MulAmt % 9) == 0) {
14402 MulAmt1 = 9;
14403 MulAmt2 = MulAmt / 9;
14404 } else if ((MulAmt % 5) == 0) {
14405 MulAmt1 = 5;
14406 MulAmt2 = MulAmt / 5;
14407 } else if ((MulAmt % 3) == 0) {
14408 MulAmt1 = 3;
14409 MulAmt2 = MulAmt / 3;
14410 }
14411 if (MulAmt2 &&
14412 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14413 DebugLoc DL = N->getDebugLoc();
14414
14415 if (isPowerOf2_64(MulAmt2) &&
14416 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14417 // If second multiplifer is pow2, issue it first. We want the multiply by
14418 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14419 // is an add.
14420 std::swap(MulAmt1, MulAmt2);
14421
14422 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014423 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014424 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014425 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014426 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014427 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014428 DAG.getConstant(MulAmt1, VT));
14429
Eric Christopherfd179292009-08-27 18:07:15 +000014430 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014431 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014432 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014433 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014434 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014435 DAG.getConstant(MulAmt2, VT));
14436
14437 // Do not add new nodes to DAG combiner worklist.
14438 DCI.CombineTo(N, NewMul, false);
14439 }
14440 return SDValue();
14441}
14442
Evan Chengad9c0a32009-12-15 00:53:42 +000014443static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14444 SDValue N0 = N->getOperand(0);
14445 SDValue N1 = N->getOperand(1);
14446 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14447 EVT VT = N0.getValueType();
14448
14449 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14450 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014451 if (VT.isInteger() && !VT.isVector() &&
14452 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014453 N0.getOperand(1).getOpcode() == ISD::Constant) {
14454 SDValue N00 = N0.getOperand(0);
14455 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14456 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14457 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14458 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14459 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14460 APInt ShAmt = N1C->getAPIntValue();
14461 Mask = Mask.shl(ShAmt);
14462 if (Mask != 0)
14463 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14464 N00, DAG.getConstant(Mask, VT));
14465 }
14466 }
14467
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014468
14469 // Hardware support for vector shifts is sparse which makes us scalarize the
14470 // vector operations in many cases. Also, on sandybridge ADD is faster than
14471 // shl.
14472 // (shl V, 1) -> add V,V
14473 if (isSplatVector(N1.getNode())) {
14474 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14475 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14476 // We shift all of the values by one. In many cases we do not have
14477 // hardware support for this operation. This is better expressed as an ADD
14478 // of two values.
14479 if (N1C && (1 == N1C->getZExtValue())) {
14480 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14481 }
14482 }
14483
Evan Chengad9c0a32009-12-15 00:53:42 +000014484 return SDValue();
14485}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014486
Nate Begeman740ab032009-01-26 00:52:55 +000014487/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14488/// when possible.
14489static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014490 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014491 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014492 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014493 if (N->getOpcode() == ISD::SHL) {
14494 SDValue V = PerformSHLCombine(N, DAG);
14495 if (V.getNode()) return V;
14496 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014497
Nate Begeman740ab032009-01-26 00:52:55 +000014498 // On X86 with SSE2 support, we can transform this to a vector shift if
14499 // all elements are shifted by the same amount. We can't do this in legalize
14500 // because the a constant vector is typically transformed to a constant pool
14501 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014502 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014503 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014504
Craig Topper7be5dfd2011-11-12 09:58:49 +000014505 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14506 (!Subtarget->hasAVX2() ||
14507 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014508 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014509
Mon P Wang3becd092009-01-28 08:12:05 +000014510 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014511 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014512 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014513 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014514 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14515 unsigned NumElts = VT.getVectorNumElements();
14516 unsigned i = 0;
14517 for (; i != NumElts; ++i) {
14518 SDValue Arg = ShAmtOp.getOperand(i);
14519 if (Arg.getOpcode() == ISD::UNDEF) continue;
14520 BaseShAmt = Arg;
14521 break;
14522 }
Craig Topper37c26772012-01-17 04:44:50 +000014523 // Handle the case where the build_vector is all undef
14524 // FIXME: Should DAG allow this?
14525 if (i == NumElts)
14526 return SDValue();
14527
Mon P Wang3becd092009-01-28 08:12:05 +000014528 for (; i != NumElts; ++i) {
14529 SDValue Arg = ShAmtOp.getOperand(i);
14530 if (Arg.getOpcode() == ISD::UNDEF) continue;
14531 if (Arg != BaseShAmt) {
14532 return SDValue();
14533 }
14534 }
14535 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014536 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014537 SDValue InVec = ShAmtOp.getOperand(0);
14538 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14539 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14540 unsigned i = 0;
14541 for (; i != NumElts; ++i) {
14542 SDValue Arg = InVec.getOperand(i);
14543 if (Arg.getOpcode() == ISD::UNDEF) continue;
14544 BaseShAmt = Arg;
14545 break;
14546 }
14547 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14548 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014549 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014550 if (C->getZExtValue() == SplatIdx)
14551 BaseShAmt = InVec.getOperand(1);
14552 }
14553 }
Mon P Wang845b1892012-02-01 22:15:20 +000014554 if (BaseShAmt.getNode() == 0) {
14555 // Don't create instructions with illegal types after legalize
14556 // types has run.
14557 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14558 !DCI.isBeforeLegalize())
14559 return SDValue();
14560
Mon P Wangefa42202009-09-03 19:56:25 +000014561 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14562 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014563 }
Mon P Wang3becd092009-01-28 08:12:05 +000014564 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014565 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014566
Mon P Wangefa42202009-09-03 19:56:25 +000014567 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014568 if (EltVT.bitsGT(MVT::i32))
14569 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14570 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014571 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014572
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014573 // The shift amount is identical so we can do a vector shift.
14574 SDValue ValOp = N->getOperand(0);
14575 switch (N->getOpcode()) {
14576 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014577 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014578 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014579 switch (VT.getSimpleVT().SimpleTy) {
14580 default: return SDValue();
14581 case MVT::v2i64:
14582 case MVT::v4i32:
14583 case MVT::v8i16:
14584 case MVT::v4i64:
14585 case MVT::v8i32:
14586 case MVT::v16i16:
14587 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14588 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014589 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014590 switch (VT.getSimpleVT().SimpleTy) {
14591 default: return SDValue();
14592 case MVT::v4i32:
14593 case MVT::v8i16:
14594 case MVT::v8i32:
14595 case MVT::v16i16:
14596 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14597 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014598 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014599 switch (VT.getSimpleVT().SimpleTy) {
14600 default: return SDValue();
14601 case MVT::v2i64:
14602 case MVT::v4i32:
14603 case MVT::v8i16:
14604 case MVT::v4i64:
14605 case MVT::v8i32:
14606 case MVT::v16i16:
14607 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14608 }
Nate Begeman740ab032009-01-26 00:52:55 +000014609 }
Nate Begeman740ab032009-01-26 00:52:55 +000014610}
14611
Nate Begemanb65c1752010-12-17 22:55:37 +000014612
Stuart Hastings865f0932011-06-03 23:53:54 +000014613// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14614// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14615// and friends. Likewise for OR -> CMPNEQSS.
14616static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14617 TargetLowering::DAGCombinerInfo &DCI,
14618 const X86Subtarget *Subtarget) {
14619 unsigned opcode;
14620
14621 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14622 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014623 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014624 SDValue N0 = N->getOperand(0);
14625 SDValue N1 = N->getOperand(1);
14626 SDValue CMP0 = N0->getOperand(1);
14627 SDValue CMP1 = N1->getOperand(1);
14628 DebugLoc DL = N->getDebugLoc();
14629
14630 // The SETCCs should both refer to the same CMP.
14631 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14632 return SDValue();
14633
14634 SDValue CMP00 = CMP0->getOperand(0);
14635 SDValue CMP01 = CMP0->getOperand(1);
14636 EVT VT = CMP00.getValueType();
14637
14638 if (VT == MVT::f32 || VT == MVT::f64) {
14639 bool ExpectingFlags = false;
14640 // Check for any users that want flags:
14641 for (SDNode::use_iterator UI = N->use_begin(),
14642 UE = N->use_end();
14643 !ExpectingFlags && UI != UE; ++UI)
14644 switch (UI->getOpcode()) {
14645 default:
14646 case ISD::BR_CC:
14647 case ISD::BRCOND:
14648 case ISD::SELECT:
14649 ExpectingFlags = true;
14650 break;
14651 case ISD::CopyToReg:
14652 case ISD::SIGN_EXTEND:
14653 case ISD::ZERO_EXTEND:
14654 case ISD::ANY_EXTEND:
14655 break;
14656 }
14657
14658 if (!ExpectingFlags) {
14659 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14660 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14661
14662 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14663 X86::CondCode tmp = cc0;
14664 cc0 = cc1;
14665 cc1 = tmp;
14666 }
14667
14668 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14669 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14670 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14671 X86ISD::NodeType NTOperator = is64BitFP ?
14672 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14673 // FIXME: need symbolic constants for these magic numbers.
14674 // See X86ATTInstPrinter.cpp:printSSECC().
14675 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14676 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14677 DAG.getConstant(x86cc, MVT::i8));
14678 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14679 OnesOrZeroesF);
14680 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14681 DAG.getConstant(1, MVT::i32));
14682 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14683 return OneBitOfTruth;
14684 }
14685 }
14686 }
14687 }
14688 return SDValue();
14689}
14690
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014691/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14692/// so it can be folded inside ANDNP.
14693static bool CanFoldXORWithAllOnes(const SDNode *N) {
14694 EVT VT = N->getValueType(0);
14695
14696 // Match direct AllOnes for 128 and 256-bit vectors
14697 if (ISD::isBuildVectorAllOnes(N))
14698 return true;
14699
14700 // Look through a bit convert.
14701 if (N->getOpcode() == ISD::BITCAST)
14702 N = N->getOperand(0).getNode();
14703
14704 // Sometimes the operand may come from a insert_subvector building a 256-bit
14705 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014706 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014707 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14708 SDValue V1 = N->getOperand(0);
14709 SDValue V2 = N->getOperand(1);
14710
14711 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14712 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14713 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14714 ISD::isBuildVectorAllOnes(V2.getNode()))
14715 return true;
14716 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014717
14718 return false;
14719}
14720
Nate Begemanb65c1752010-12-17 22:55:37 +000014721static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14722 TargetLowering::DAGCombinerInfo &DCI,
14723 const X86Subtarget *Subtarget) {
14724 if (DCI.isBeforeLegalizeOps())
14725 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014726
Stuart Hastings865f0932011-06-03 23:53:54 +000014727 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14728 if (R.getNode())
14729 return R;
14730
Craig Topper54a11172011-10-14 07:06:56 +000014731 EVT VT = N->getValueType(0);
14732
Craig Topperb4c94572011-10-21 06:55:01 +000014733 // Create ANDN, BLSI, and BLSR instructions
14734 // BLSI is X & (-X)
14735 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014736 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14737 SDValue N0 = N->getOperand(0);
14738 SDValue N1 = N->getOperand(1);
14739 DebugLoc DL = N->getDebugLoc();
14740
14741 // Check LHS for not
14742 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14743 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14744 // Check RHS for not
14745 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14746 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14747
Craig Topperb4c94572011-10-21 06:55:01 +000014748 // Check LHS for neg
14749 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14750 isZero(N0.getOperand(0)))
14751 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14752
14753 // Check RHS for neg
14754 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14755 isZero(N1.getOperand(0)))
14756 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14757
14758 // Check LHS for X-1
14759 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14760 isAllOnes(N0.getOperand(1)))
14761 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14762
14763 // Check RHS for X-1
14764 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14765 isAllOnes(N1.getOperand(1)))
14766 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14767
Craig Topper54a11172011-10-14 07:06:56 +000014768 return SDValue();
14769 }
14770
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014771 // Want to form ANDNP nodes:
14772 // 1) In the hopes of then easily combining them with OR and AND nodes
14773 // to form PBLEND/PSIGN.
14774 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014775 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014776 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014777
Nate Begemanb65c1752010-12-17 22:55:37 +000014778 SDValue N0 = N->getOperand(0);
14779 SDValue N1 = N->getOperand(1);
14780 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014781
Nate Begemanb65c1752010-12-17 22:55:37 +000014782 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014783 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014784 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14785 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014786 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014787
14788 // Check RHS for vnot
14789 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014790 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14791 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014792 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014793
Nate Begemanb65c1752010-12-17 22:55:37 +000014794 return SDValue();
14795}
14796
Evan Cheng760d1942010-01-04 21:22:48 +000014797static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014798 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014799 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014800 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014801 return SDValue();
14802
Stuart Hastings865f0932011-06-03 23:53:54 +000014803 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14804 if (R.getNode())
14805 return R;
14806
Evan Cheng760d1942010-01-04 21:22:48 +000014807 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014808
Evan Cheng760d1942010-01-04 21:22:48 +000014809 SDValue N0 = N->getOperand(0);
14810 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014811
Nate Begemanb65c1752010-12-17 22:55:37 +000014812 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014813 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014814 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014815 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14816 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014817
Craig Topper1666cb62011-11-19 07:07:26 +000014818 // Canonicalize pandn to RHS
14819 if (N0.getOpcode() == X86ISD::ANDNP)
14820 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014821 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014822 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14823 SDValue Mask = N1.getOperand(0);
14824 SDValue X = N1.getOperand(1);
14825 SDValue Y;
14826 if (N0.getOperand(0) == Mask)
14827 Y = N0.getOperand(1);
14828 if (N0.getOperand(1) == Mask)
14829 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014830
Craig Topper1666cb62011-11-19 07:07:26 +000014831 // Check to see if the mask appeared in both the AND and ANDNP and
14832 if (!Y.getNode())
14833 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014834
Craig Topper1666cb62011-11-19 07:07:26 +000014835 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014836 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014837 if (Mask.getOpcode() == ISD::BITCAST)
14838 Mask = Mask.getOperand(0);
14839 if (X.getOpcode() == ISD::BITCAST)
14840 X = X.getOperand(0);
14841 if (Y.getOpcode() == ISD::BITCAST)
14842 Y = Y.getOperand(0);
14843
Craig Topper1666cb62011-11-19 07:07:26 +000014844 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014845
Craig Toppered2e13d2012-01-22 19:15:14 +000014846 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014847 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14848 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014849 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014850 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014851
14852 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014853 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014854 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14855 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14856 if ((SraAmt + 1) != EltBits)
14857 return SDValue();
14858
14859 DebugLoc DL = N->getDebugLoc();
14860
14861 // Now we know we at least have a plendvb with the mask val. See if
14862 // we can form a psignb/w/d.
14863 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000014864 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14865 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000014866 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
14867 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
14868 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014869 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000014870 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000014871 }
14872 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000014873 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000014874 return SDValue();
14875
14876 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14877
14878 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14879 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14880 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000014881 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000014882 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014883 }
14884 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014885
Craig Topper1666cb62011-11-19 07:07:26 +000014886 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14887 return SDValue();
14888
Nate Begemanb65c1752010-12-17 22:55:37 +000014889 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014890 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14891 std::swap(N0, N1);
14892 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14893 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014894 if (!N0.hasOneUse() || !N1.hasOneUse())
14895 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014896
14897 SDValue ShAmt0 = N0.getOperand(1);
14898 if (ShAmt0.getValueType() != MVT::i8)
14899 return SDValue();
14900 SDValue ShAmt1 = N1.getOperand(1);
14901 if (ShAmt1.getValueType() != MVT::i8)
14902 return SDValue();
14903 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14904 ShAmt0 = ShAmt0.getOperand(0);
14905 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14906 ShAmt1 = ShAmt1.getOperand(0);
14907
14908 DebugLoc DL = N->getDebugLoc();
14909 unsigned Opc = X86ISD::SHLD;
14910 SDValue Op0 = N0.getOperand(0);
14911 SDValue Op1 = N1.getOperand(0);
14912 if (ShAmt0.getOpcode() == ISD::SUB) {
14913 Opc = X86ISD::SHRD;
14914 std::swap(Op0, Op1);
14915 std::swap(ShAmt0, ShAmt1);
14916 }
14917
Evan Cheng8b1190a2010-04-28 01:18:01 +000014918 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014919 if (ShAmt1.getOpcode() == ISD::SUB) {
14920 SDValue Sum = ShAmt1.getOperand(0);
14921 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014922 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14923 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14924 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14925 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014926 return DAG.getNode(Opc, DL, VT,
14927 Op0, Op1,
14928 DAG.getNode(ISD::TRUNCATE, DL,
14929 MVT::i8, ShAmt0));
14930 }
14931 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14932 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14933 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014934 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014935 return DAG.getNode(Opc, DL, VT,
14936 N0.getOperand(0), N1.getOperand(0),
14937 DAG.getNode(ISD::TRUNCATE, DL,
14938 MVT::i8, ShAmt0));
14939 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014940
Evan Cheng760d1942010-01-04 21:22:48 +000014941 return SDValue();
14942}
14943
Manman Ren92363622012-06-07 22:39:10 +000014944// Generate NEG and CMOV for integer abs.
14945static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
14946 EVT VT = N->getValueType(0);
14947
14948 // Since X86 does not have CMOV for 8-bit integer, we don't convert
14949 // 8-bit integer abs to NEG and CMOV.
14950 if (VT.isInteger() && VT.getSizeInBits() == 8)
14951 return SDValue();
14952
14953 SDValue N0 = N->getOperand(0);
14954 SDValue N1 = N->getOperand(1);
14955 DebugLoc DL = N->getDebugLoc();
14956
14957 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
14958 // and change it to SUB and CMOV.
14959 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
14960 N0.getOpcode() == ISD::ADD &&
14961 N0.getOperand(1) == N1 &&
14962 N1.getOpcode() == ISD::SRA &&
14963 N1.getOperand(0) == N0.getOperand(0))
14964 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
14965 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
14966 // Generate SUB & CMOV.
14967 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
14968 DAG.getConstant(0, VT), N0.getOperand(0));
14969
14970 SDValue Ops[] = { N0.getOperand(0), Neg,
14971 DAG.getConstant(X86::COND_GE, MVT::i8),
14972 SDValue(Neg.getNode(), 1) };
14973 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
14974 Ops, array_lengthof(Ops));
14975 }
14976 return SDValue();
14977}
14978
Craig Topper3738ccd2011-12-27 06:27:23 +000014979// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000014980static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14981 TargetLowering::DAGCombinerInfo &DCI,
14982 const X86Subtarget *Subtarget) {
14983 if (DCI.isBeforeLegalizeOps())
14984 return SDValue();
14985
Manman Ren45d53b82012-06-08 18:58:26 +000014986 if (Subtarget->hasCMov()) {
14987 SDValue RV = performIntegerAbsCombine(N, DAG);
14988 if (RV.getNode())
14989 return RV;
14990 }
Manman Ren92363622012-06-07 22:39:10 +000014991
14992 // Try forming BMI if it is available.
14993 if (!Subtarget->hasBMI())
14994 return SDValue();
14995
Craig Topperb4c94572011-10-21 06:55:01 +000014996 EVT VT = N->getValueType(0);
14997
14998 if (VT != MVT::i32 && VT != MVT::i64)
14999 return SDValue();
15000
Craig Topper3738ccd2011-12-27 06:27:23 +000015001 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
15002
Craig Topperb4c94572011-10-21 06:55:01 +000015003 // Create BLSMSK instructions by finding X ^ (X-1)
15004 SDValue N0 = N->getOperand(0);
15005 SDValue N1 = N->getOperand(1);
15006 DebugLoc DL = N->getDebugLoc();
15007
15008 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
15009 isAllOnes(N0.getOperand(1)))
15010 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
15011
15012 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
15013 isAllOnes(N1.getOperand(1)))
15014 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15015
15016 return SDValue();
15017}
15018
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015019/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15020static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015021 TargetLowering::DAGCombinerInfo &DCI,
15022 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015023 LoadSDNode *Ld = cast<LoadSDNode>(N);
15024 EVT RegVT = Ld->getValueType(0);
15025 EVT MemVT = Ld->getMemoryVT();
15026 DebugLoc dl = Ld->getDebugLoc();
15027 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15028
15029 ISD::LoadExtType Ext = Ld->getExtensionType();
15030
Nadav Rotemca6f2962011-09-18 19:00:23 +000015031 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015032 // shuffle. We need SSE4 for the shuffles.
15033 // TODO: It is possible to support ZExt by zeroing the undef values
15034 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015035 if (RegVT.isVector() && RegVT.isInteger() &&
15036 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015037 assert(MemVT != RegVT && "Cannot extend to the same type");
15038 assert(MemVT.isVector() && "Must load a vector from memory");
15039
15040 unsigned NumElems = RegVT.getVectorNumElements();
15041 unsigned RegSz = RegVT.getSizeInBits();
15042 unsigned MemSz = MemVT.getSizeInBits();
15043 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015044
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015045 // All sizes must be a power of two.
15046 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15047 return SDValue();
15048
15049 // Attempt to load the original value using scalar loads.
15050 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015051 MVT SclrLoadTy = MVT::i8;
15052 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15053 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15054 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015055 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015056 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015057 }
15058 }
15059
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015060 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15061 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15062 (64 <= MemSz))
15063 SclrLoadTy = MVT::f64;
15064
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015065 // Calculate the number of scalar loads that we need to perform
15066 // in order to load our vector from memory.
15067 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015068
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015069 // Represent our vector as a sequence of elements which are the
15070 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015071 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15072 RegSz/SclrLoadTy.getSizeInBits());
15073
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015074 // Represent the data using the same element type that is stored in
15075 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015076 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15077 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015078
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015079 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15080 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015081
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015082 // We can't shuffle using an illegal type.
15083 if (!TLI.isTypeLegal(WideVecVT))
15084 return SDValue();
15085
15086 SmallVector<SDValue, 8> Chains;
15087 SDValue Ptr = Ld->getBasePtr();
15088 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15089 TLI.getPointerTy());
15090 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15091
15092 for (unsigned i = 0; i < NumLoads; ++i) {
15093 // Perform a single load.
15094 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15095 Ptr, Ld->getPointerInfo(),
15096 Ld->isVolatile(), Ld->isNonTemporal(),
15097 Ld->isInvariant(), Ld->getAlignment());
15098 Chains.push_back(ScalarLoad.getValue(1));
15099 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15100 // another round of DAGCombining.
15101 if (i == 0)
15102 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15103 else
15104 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15105 ScalarLoad, DAG.getIntPtrConstant(i));
15106
15107 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15108 }
15109
15110 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15111 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015112
15113 // Bitcast the loaded value to a vector of the original element type, in
15114 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015115 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015116 unsigned SizeRatio = RegSz/MemSz;
15117
15118 // Redistribute the loaded elements into the different locations.
15119 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015120 for (unsigned i = 0; i != NumElems; ++i)
15121 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015122
15123 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015124 DAG.getUNDEF(WideVecVT),
15125 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015126
15127 // Bitcast to the requested type.
15128 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15129 // Replace the original load with the new sequence
15130 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015131 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015132 }
15133
15134 return SDValue();
15135}
15136
Chris Lattner149a4e52008-02-22 02:09:43 +000015137/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015138static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015139 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015140 StoreSDNode *St = cast<StoreSDNode>(N);
15141 EVT VT = St->getValue().getValueType();
15142 EVT StVT = St->getMemoryVT();
15143 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015144 SDValue StoredVal = St->getOperand(1);
15145 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15146
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015147 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015148 // On Sandy Bridge, 256-bit memory operations are executed by two
15149 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15150 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015151 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015152 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15153 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015154 SDValue Value0 = StoredVal.getOperand(0);
15155 SDValue Value1 = StoredVal.getOperand(1);
15156
15157 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15158 SDValue Ptr0 = St->getBasePtr();
15159 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15160
15161 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15162 St->getPointerInfo(), St->isVolatile(),
15163 St->isNonTemporal(), St->getAlignment());
15164 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15165 St->getPointerInfo(), St->isVolatile(),
15166 St->isNonTemporal(), St->getAlignment());
15167 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15168 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015169
15170 // Optimize trunc store (of multiple scalars) to shuffle and store.
15171 // First, pack all of the elements in one place. Next, store to memory
15172 // in fewer chunks.
15173 if (St->isTruncatingStore() && VT.isVector()) {
15174 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15175 unsigned NumElems = VT.getVectorNumElements();
15176 assert(StVT != VT && "Cannot truncate to the same type");
15177 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15178 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15179
15180 // From, To sizes and ElemCount must be pow of two
15181 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015182 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015183 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015184 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015185
Nadav Rotem614061b2011-08-10 19:30:14 +000015186 unsigned SizeRatio = FromSz / ToSz;
15187
15188 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15189
15190 // Create a type on which we perform the shuffle
15191 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15192 StVT.getScalarType(), NumElems*SizeRatio);
15193
15194 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15195
15196 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15197 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015198 for (unsigned i = 0; i != NumElems; ++i)
15199 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015200
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015201 // Can't shuffle using an illegal type.
15202 if (!TLI.isTypeLegal(WideVecVT))
15203 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015204
15205 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015206 DAG.getUNDEF(WideVecVT),
15207 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015208 // At this point all of the data is stored at the bottom of the
15209 // register. We now need to save it to mem.
15210
15211 // Find the largest store unit
15212 MVT StoreType = MVT::i8;
15213 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15214 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15215 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015216 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015217 StoreType = Tp;
15218 }
15219
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015220 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15221 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15222 (64 <= NumElems * ToSz))
15223 StoreType = MVT::f64;
15224
Nadav Rotem614061b2011-08-10 19:30:14 +000015225 // Bitcast the original vector into a vector of store-size units
15226 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015227 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015228 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15229 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15230 SmallVector<SDValue, 8> Chains;
15231 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15232 TLI.getPointerTy());
15233 SDValue Ptr = St->getBasePtr();
15234
15235 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015236 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015237 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15238 StoreType, ShuffWide,
15239 DAG.getIntPtrConstant(i));
15240 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15241 St->getPointerInfo(), St->isVolatile(),
15242 St->isNonTemporal(), St->getAlignment());
15243 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15244 Chains.push_back(Ch);
15245 }
15246
15247 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15248 Chains.size());
15249 }
15250
15251
Chris Lattner149a4e52008-02-22 02:09:43 +000015252 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15253 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015254 // A preferable solution to the general problem is to figure out the right
15255 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015256
15257 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015258 if (VT.getSizeInBits() != 64)
15259 return SDValue();
15260
Devang Patel578efa92009-06-05 21:57:13 +000015261 const Function *F = DAG.getMachineFunction().getFunction();
15262 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015263 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015264 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015265 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015266 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015267 isa<LoadSDNode>(St->getValue()) &&
15268 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15269 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015270 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015271 LoadSDNode *Ld = 0;
15272 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015273 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015274 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015275 // Must be a store of a load. We currently handle two cases: the load
15276 // is a direct child, and it's under an intervening TokenFactor. It is
15277 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015278 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015279 Ld = cast<LoadSDNode>(St->getChain());
15280 else if (St->getValue().hasOneUse() &&
15281 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015282 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015283 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015284 TokenFactorIndex = i;
15285 Ld = cast<LoadSDNode>(St->getValue());
15286 } else
15287 Ops.push_back(ChainVal->getOperand(i));
15288 }
15289 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015290
Evan Cheng536e6672009-03-12 05:59:15 +000015291 if (!Ld || !ISD::isNormalLoad(Ld))
15292 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015293
Evan Cheng536e6672009-03-12 05:59:15 +000015294 // If this is not the MMX case, i.e. we are just turning i64 load/store
15295 // into f64 load/store, avoid the transformation if there are multiple
15296 // uses of the loaded value.
15297 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15298 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015299
Evan Cheng536e6672009-03-12 05:59:15 +000015300 DebugLoc LdDL = Ld->getDebugLoc();
15301 DebugLoc StDL = N->getDebugLoc();
15302 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15303 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15304 // pair instead.
15305 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015306 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015307 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15308 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015309 Ld->isNonTemporal(), Ld->isInvariant(),
15310 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015311 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015312 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015313 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015314 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015315 Ops.size());
15316 }
Evan Cheng536e6672009-03-12 05:59:15 +000015317 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015318 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015319 St->isVolatile(), St->isNonTemporal(),
15320 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015321 }
Evan Cheng536e6672009-03-12 05:59:15 +000015322
15323 // Otherwise, lower to two pairs of 32-bit loads / stores.
15324 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015325 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15326 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015327
Owen Anderson825b72b2009-08-11 20:47:22 +000015328 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015329 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015330 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015331 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015332 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015333 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015334 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015335 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015336 MinAlign(Ld->getAlignment(), 4));
15337
15338 SDValue NewChain = LoLd.getValue(1);
15339 if (TokenFactorIndex != -1) {
15340 Ops.push_back(LoLd);
15341 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015342 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015343 Ops.size());
15344 }
15345
15346 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015347 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15348 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015349
15350 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015351 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015352 St->isVolatile(), St->isNonTemporal(),
15353 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015354 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015355 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015356 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015357 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015358 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015359 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015360 }
Dan Gohman475871a2008-07-27 21:46:04 +000015361 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015362}
15363
Duncan Sands17470be2011-09-22 20:15:48 +000015364/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15365/// and return the operands for the horizontal operation in LHS and RHS. A
15366/// horizontal operation performs the binary operation on successive elements
15367/// of its first operand, then on successive elements of its second operand,
15368/// returning the resulting values in a vector. For example, if
15369/// A = < float a0, float a1, float a2, float a3 >
15370/// and
15371/// B = < float b0, float b1, float b2, float b3 >
15372/// then the result of doing a horizontal operation on A and B is
15373/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15374/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15375/// A horizontal-op B, for some already available A and B, and if so then LHS is
15376/// set to A, RHS to B, and the routine returns 'true'.
15377/// Note that the binary operation should have the property that if one of the
15378/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015379static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015380 // Look for the following pattern: if
15381 // A = < float a0, float a1, float a2, float a3 >
15382 // B = < float b0, float b1, float b2, float b3 >
15383 // and
15384 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15385 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15386 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15387 // which is A horizontal-op B.
15388
15389 // At least one of the operands should be a vector shuffle.
15390 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15391 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15392 return false;
15393
15394 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015395
15396 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15397 "Unsupported vector type for horizontal add/sub");
15398
15399 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15400 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015401 unsigned NumElts = VT.getVectorNumElements();
15402 unsigned NumLanes = VT.getSizeInBits()/128;
15403 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015404 assert((NumLaneElts % 2 == 0) &&
15405 "Vector type should have an even number of elements in each lane");
15406 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015407
15408 // View LHS in the form
15409 // LHS = VECTOR_SHUFFLE A, B, LMask
15410 // If LHS is not a shuffle then pretend it is the shuffle
15411 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15412 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15413 // type VT.
15414 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015415 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015416 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15417 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15418 A = LHS.getOperand(0);
15419 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15420 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015421 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15422 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015423 } else {
15424 if (LHS.getOpcode() != ISD::UNDEF)
15425 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015426 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015427 LMask[i] = i;
15428 }
15429
15430 // Likewise, view RHS in the form
15431 // RHS = VECTOR_SHUFFLE C, D, RMask
15432 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015433 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015434 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15435 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15436 C = RHS.getOperand(0);
15437 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15438 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015439 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15440 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015441 } else {
15442 if (RHS.getOpcode() != ISD::UNDEF)
15443 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015444 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015445 RMask[i] = i;
15446 }
15447
15448 // Check that the shuffles are both shuffling the same vectors.
15449 if (!(A == C && B == D) && !(A == D && B == C))
15450 return false;
15451
15452 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15453 if (!A.getNode() && !B.getNode())
15454 return false;
15455
15456 // If A and B occur in reverse order in RHS, then "swap" them (which means
15457 // rewriting the mask).
15458 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015459 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015460
15461 // At this point LHS and RHS are equivalent to
15462 // LHS = VECTOR_SHUFFLE A, B, LMask
15463 // RHS = VECTOR_SHUFFLE A, B, RMask
15464 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015465 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015466 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015467
Craig Topperf8363302011-12-02 08:18:41 +000015468 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015469 if (LIdx < 0 || RIdx < 0 ||
15470 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15471 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015472 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015473
Craig Topperf8363302011-12-02 08:18:41 +000015474 // Check that successive elements are being operated on. If not, this is
15475 // not a horizontal operation.
15476 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15477 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015478 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015479 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015480 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015481 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015482 }
15483
15484 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15485 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15486 return true;
15487}
15488
15489/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15490static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15491 const X86Subtarget *Subtarget) {
15492 EVT VT = N->getValueType(0);
15493 SDValue LHS = N->getOperand(0);
15494 SDValue RHS = N->getOperand(1);
15495
15496 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015497 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015498 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015499 isHorizontalBinOp(LHS, RHS, true))
15500 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15501 return SDValue();
15502}
15503
15504/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15505static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15506 const X86Subtarget *Subtarget) {
15507 EVT VT = N->getValueType(0);
15508 SDValue LHS = N->getOperand(0);
15509 SDValue RHS = N->getOperand(1);
15510
15511 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015512 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015513 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015514 isHorizontalBinOp(LHS, RHS, false))
15515 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15516 return SDValue();
15517}
15518
Chris Lattner6cf73262008-01-25 06:14:17 +000015519/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15520/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015521static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015522 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15523 // F[X]OR(0.0, x) -> x
15524 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015525 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15526 if (C->getValueAPF().isPosZero())
15527 return N->getOperand(1);
15528 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15529 if (C->getValueAPF().isPosZero())
15530 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015531 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015532}
15533
Nadav Rotemd60cb112012-08-19 13:06:16 +000015534/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15535/// X86ISD::FMAX nodes.
15536static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15537 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15538
15539 // Only perform optimizations if UnsafeMath is used.
15540 if (!DAG.getTarget().Options.UnsafeFPMath)
15541 return SDValue();
15542
15543 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015544 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015545 unsigned NewOp = 0;
15546 switch (N->getOpcode()) {
15547 default: llvm_unreachable("unknown opcode");
15548 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15549 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15550 }
15551
15552 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
15553 N->getOperand(0), N->getOperand(1));
15554}
15555
15556
Chris Lattneraf723b92008-01-25 05:46:26 +000015557/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015558static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015559 // FAND(0.0, x) -> 0.0
15560 // FAND(x, 0.0) -> 0.0
15561 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15562 if (C->getValueAPF().isPosZero())
15563 return N->getOperand(0);
15564 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15565 if (C->getValueAPF().isPosZero())
15566 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015567 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015568}
15569
Dan Gohmane5af2d32009-01-29 01:59:02 +000015570static SDValue PerformBTCombine(SDNode *N,
15571 SelectionDAG &DAG,
15572 TargetLowering::DAGCombinerInfo &DCI) {
15573 // BT ignores high bits in the bit index operand.
15574 SDValue Op1 = N->getOperand(1);
15575 if (Op1.hasOneUse()) {
15576 unsigned BitWidth = Op1.getValueSizeInBits();
15577 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15578 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015579 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15580 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015581 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015582 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15583 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15584 DCI.CommitTargetLoweringOpt(TLO);
15585 }
15586 return SDValue();
15587}
Chris Lattner83e6c992006-10-04 06:57:07 +000015588
Eli Friedman7a5e5552009-06-07 06:52:44 +000015589static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15590 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015591 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015592 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015593 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015594 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015595 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015596 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015597 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015598 }
15599 return SDValue();
15600}
15601
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015602static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15603 TargetLowering::DAGCombinerInfo &DCI,
15604 const X86Subtarget *Subtarget) {
15605 if (!DCI.isBeforeLegalizeOps())
15606 return SDValue();
15607
Craig Topper3ef43cf2012-04-24 06:36:35 +000015608 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015609 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015610
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015611 EVT VT = N->getValueType(0);
15612 SDValue Op = N->getOperand(0);
15613 EVT OpVT = Op.getValueType();
15614 DebugLoc dl = N->getDebugLoc();
15615
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015616 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15617 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015618
Craig Topper3ef43cf2012-04-24 06:36:35 +000015619 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015620 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015621
15622 // Optimize vectors in AVX mode
15623 // Sign extend v8i16 to v8i32 and
15624 // v4i32 to v4i64
15625 //
15626 // Divide input vector into two parts
15627 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15628 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15629 // concat the vectors to original VT
15630
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015631 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015632 SDValue Undef = DAG.getUNDEF(OpVT);
15633
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015634 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015635 for (unsigned i = 0; i != NumElems/2; ++i)
15636 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015637
Craig Toppercacafd42012-08-14 08:18:43 +000015638 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015639
15640 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015641 for (unsigned i = 0; i != NumElems/2; ++i)
15642 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015643
Craig Toppercacafd42012-08-14 08:18:43 +000015644 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015645
Craig Topper3ef43cf2012-04-24 06:36:35 +000015646 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015647 VT.getVectorNumElements()/2);
15648
Craig Topper3ef43cf2012-04-24 06:36:35 +000015649 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015650 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15651
15652 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15653 }
15654 return SDValue();
15655}
15656
Michael Liaof6c24ee2012-08-10 14:39:24 +000015657static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015658 const X86Subtarget* Subtarget) {
15659 DebugLoc dl = N->getDebugLoc();
15660 EVT VT = N->getValueType(0);
15661
Craig Topperb1bdd7d2012-08-30 06:56:15 +000015662 // Let legalize expand this if it isn't a legal type yet.
15663 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
15664 return SDValue();
15665
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015666 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000015667 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
15668 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015669 return SDValue();
15670
15671 SDValue A = N->getOperand(0);
15672 SDValue B = N->getOperand(1);
15673 SDValue C = N->getOperand(2);
15674
15675 bool NegA = (A.getOpcode() == ISD::FNEG);
15676 bool NegB = (B.getOpcode() == ISD::FNEG);
15677 bool NegC = (C.getOpcode() == ISD::FNEG);
15678
Michael Liaof6c24ee2012-08-10 14:39:24 +000015679 // Negative multiplication when NegA xor NegB
15680 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015681 if (NegA)
15682 A = A.getOperand(0);
15683 if (NegB)
15684 B = B.getOperand(0);
15685 if (NegC)
15686 C = C.getOperand(0);
15687
15688 unsigned Opcode;
15689 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000015690 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015691 else
Craig Topperbf404372012-08-31 15:40:30 +000015692 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
15693
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015694 return DAG.getNode(Opcode, dl, VT, A, B, C);
15695}
15696
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015697static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015698 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015699 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015700 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15701 // (and (i32 x86isd::setcc_carry), 1)
15702 // This eliminates the zext. This transformation is necessary because
15703 // ISD::SETCC is always legalized to i8.
15704 DebugLoc dl = N->getDebugLoc();
15705 SDValue N0 = N->getOperand(0);
15706 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015707 EVT OpVT = N0.getValueType();
15708
Evan Cheng2e489c42009-12-16 00:53:11 +000015709 if (N0.getOpcode() == ISD::AND &&
15710 N0.hasOneUse() &&
15711 N0.getOperand(0).hasOneUse()) {
15712 SDValue N00 = N0.getOperand(0);
15713 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15714 return SDValue();
15715 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15716 if (!C || C->getZExtValue() != 1)
15717 return SDValue();
15718 return DAG.getNode(ISD::AND, dl, VT,
15719 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15720 N00.getOperand(0), N00.getOperand(1)),
15721 DAG.getConstant(1, VT));
15722 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015723
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015724 // Optimize vectors in AVX mode:
15725 //
15726 // v8i16 -> v8i32
15727 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15728 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15729 // Concat upper and lower parts.
15730 //
15731 // v4i32 -> v4i64
15732 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15733 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15734 // Concat upper and lower parts.
15735 //
Craig Topperc16f8512012-04-25 06:39:39 +000015736 if (!DCI.isBeforeLegalizeOps())
15737 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015738
Craig Topperc16f8512012-04-25 06:39:39 +000015739 if (!Subtarget->hasAVX())
15740 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015741
Craig Topperc16f8512012-04-25 06:39:39 +000015742 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15743 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015744
Craig Topperc16f8512012-04-25 06:39:39 +000015745 if (Subtarget->hasAVX2())
15746 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015747
Craig Topperc16f8512012-04-25 06:39:39 +000015748 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15749 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15750 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015751
Craig Topperc16f8512012-04-25 06:39:39 +000015752 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15753 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015754
Craig Topperc16f8512012-04-25 06:39:39 +000015755 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15756 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15757
15758 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015759 }
15760
Evan Cheng2e489c42009-12-16 00:53:11 +000015761 return SDValue();
15762}
15763
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015764// Optimize x == -y --> x+y == 0
15765// x != -y --> x+y != 0
15766static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15767 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15768 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015769 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015770
15771 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15772 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15773 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15774 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15775 LHS.getValueType(), RHS, LHS.getOperand(1));
15776 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15777 addV, DAG.getConstant(0, addV.getValueType()), CC);
15778 }
15779 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15780 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15781 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15782 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15783 RHS.getValueType(), LHS, RHS.getOperand(1));
15784 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15785 addV, DAG.getConstant(0, addV.getValueType()), CC);
15786 }
15787 return SDValue();
15788}
15789
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015790// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015791static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
15792 TargetLowering::DAGCombinerInfo &DCI,
15793 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015794 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015795 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15796 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015797
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015798 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15799 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15800 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015801 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015802 return DAG.getNode(ISD::AND, DL, MVT::i8,
15803 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015804 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015805 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015806
Michael Liao2a33cec2012-08-10 19:58:13 +000015807 SDValue Flags;
15808
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015809 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15810 if (Flags.getNode()) {
15811 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15812 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15813 }
15814
15815 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015816 if (Flags.getNode()) {
15817 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15818 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15819 }
15820
15821 return SDValue();
15822}
15823
15824// Optimize branch condition evaluation.
15825//
15826static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15827 TargetLowering::DAGCombinerInfo &DCI,
15828 const X86Subtarget *Subtarget) {
15829 DebugLoc DL = N->getDebugLoc();
15830 SDValue Chain = N->getOperand(0);
15831 SDValue Dest = N->getOperand(1);
15832 SDValue EFLAGS = N->getOperand(3);
15833 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15834
15835 SDValue Flags;
15836
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015837 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15838 if (Flags.getNode()) {
15839 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15840 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15841 Flags);
15842 }
15843
15844 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015845 if (Flags.getNode()) {
15846 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15847 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15848 Flags);
15849 }
15850
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015851 return SDValue();
15852}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015853
Craig Topper7fd5e162012-04-24 06:02:29 +000015854static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015855 SDValue Op0 = N->getOperand(0);
15856 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015857
15858 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015859 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015860 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015861 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015862 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15863 // Notice that we use SINT_TO_FP because we know that the high bits
15864 // are zero and SINT_TO_FP is better supported by the hardware.
15865 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15866 }
15867
15868 return SDValue();
15869}
15870
Benjamin Kramer1396c402011-06-18 11:09:41 +000015871static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15872 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015873 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015874 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015875
15876 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015877 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015878 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015879 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015880 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
15881 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15882 }
15883
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015884 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
15885 // a 32-bit target where SSE doesn't support i64->FP operations.
15886 if (Op0.getOpcode() == ISD::LOAD) {
15887 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
15888 EVT VT = Ld->getValueType(0);
15889 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
15890 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
15891 !XTLI->getSubtarget()->is64Bit() &&
15892 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000015893 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
15894 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015895 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
15896 return FILDChain;
15897 }
15898 }
15899 return SDValue();
15900}
15901
Craig Topper7fd5e162012-04-24 06:02:29 +000015902static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
15903 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015904
15905 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000015906 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
15907 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015908 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015909 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
15910 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
15911 }
15912
15913 return SDValue();
15914}
15915
Chris Lattner23a01992010-12-20 01:37:09 +000015916// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
15917static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
15918 X86TargetLowering::DAGCombinerInfo &DCI) {
15919 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
15920 // the result is either zero or one (depending on the input carry bit).
15921 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
15922 if (X86::isZeroNode(N->getOperand(0)) &&
15923 X86::isZeroNode(N->getOperand(1)) &&
15924 // We don't have a good way to replace an EFLAGS use, so only do this when
15925 // dead right now.
15926 SDValue(N, 1).use_empty()) {
15927 DebugLoc DL = N->getDebugLoc();
15928 EVT VT = N->getValueType(0);
15929 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
15930 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
15931 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
15932 DAG.getConstant(X86::COND_B,MVT::i8),
15933 N->getOperand(2)),
15934 DAG.getConstant(1, VT));
15935 return DCI.CombineTo(N, Res1, CarryOut);
15936 }
15937
15938 return SDValue();
15939}
15940
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015941// fold (add Y, (sete X, 0)) -> adc 0, Y
15942// (add Y, (setne X, 0)) -> sbb -1, Y
15943// (sub (sete X, 0), Y) -> sbb 0, Y
15944// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015945static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015946 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015947
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015948 // Look through ZExts.
15949 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
15950 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
15951 return SDValue();
15952
15953 SDValue SetCC = Ext.getOperand(0);
15954 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
15955 return SDValue();
15956
15957 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
15958 if (CC != X86::COND_E && CC != X86::COND_NE)
15959 return SDValue();
15960
15961 SDValue Cmp = SetCC.getOperand(1);
15962 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000015963 !X86::isZeroNode(Cmp.getOperand(1)) ||
15964 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015965 return SDValue();
15966
15967 SDValue CmpOp0 = Cmp.getOperand(0);
15968 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
15969 DAG.getConstant(1, CmpOp0.getValueType()));
15970
15971 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
15972 if (CC == X86::COND_NE)
15973 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
15974 DL, OtherVal.getValueType(), OtherVal,
15975 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
15976 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
15977 DL, OtherVal.getValueType(), OtherVal,
15978 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
15979}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015980
Craig Topper54f952a2011-11-19 09:02:40 +000015981/// PerformADDCombine - Do target-specific dag combines on integer adds.
15982static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
15983 const X86Subtarget *Subtarget) {
15984 EVT VT = N->getValueType(0);
15985 SDValue Op0 = N->getOperand(0);
15986 SDValue Op1 = N->getOperand(1);
15987
15988 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015989 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000015990 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000015991 isHorizontalBinOp(Op0, Op1, true))
15992 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
15993
15994 return OptimizeConditionalInDecrement(N, DAG);
15995}
15996
15997static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
15998 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015999 SDValue Op0 = N->getOperand(0);
16000 SDValue Op1 = N->getOperand(1);
16001
16002 // X86 can't encode an immediate LHS of a sub. See if we can push the
16003 // negation into a preceding instruction.
16004 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016005 // If the RHS of the sub is a XOR with one use and a constant, invert the
16006 // immediate. Then add one to the LHS of the sub so we can turn
16007 // X-Y -> X+~Y+1, saving one register.
16008 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
16009 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000016010 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016011 EVT VT = Op0.getValueType();
16012 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
16013 Op1.getOperand(0),
16014 DAG.getConstant(~XorC, VT));
16015 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016016 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016017 }
16018 }
16019
Craig Topper54f952a2011-11-19 09:02:40 +000016020 // Try to synthesize horizontal adds from adds of shuffles.
16021 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016022 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016023 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16024 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016025 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16026
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016027 return OptimizeConditionalInDecrement(N, DAG);
16028}
16029
Dan Gohman475871a2008-07-27 21:46:04 +000016030SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016031 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016032 SelectionDAG &DAG = DCI.DAG;
16033 switch (N->getOpcode()) {
16034 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016035 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016036 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016037 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016038 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016039 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016040 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16041 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016042 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016043 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016044 case ISD::SHL:
16045 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016046 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016047 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016048 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016049 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016050 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016051 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016052 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016053 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000016054 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000016055 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16056 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016057 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016058 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016059 case X86ISD::FMIN:
16060 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016061 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016062 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016063 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016064 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016065 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016066 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000016067 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG, DCI);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016068 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016069 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016070 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016071 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016072 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016073 case X86ISD::UNPCKH:
16074 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016075 case X86ISD::MOVHLPS:
16076 case X86ISD::MOVLHPS:
16077 case X86ISD::PSHUFD:
16078 case X86ISD::PSHUFHW:
16079 case X86ISD::PSHUFLW:
16080 case X86ISD::MOVSS:
16081 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016082 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016083 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016084 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016085 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016086 }
16087
Dan Gohman475871a2008-07-27 21:46:04 +000016088 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016089}
16090
Evan Chenge5b51ac2010-04-17 06:13:15 +000016091/// isTypeDesirableForOp - Return true if the target has native support for
16092/// the specified value type and it is 'desirable' to use the type for the
16093/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16094/// instruction encodings are longer and some i16 instructions are slow.
16095bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16096 if (!isTypeLegal(VT))
16097 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016098 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016099 return true;
16100
16101 switch (Opc) {
16102 default:
16103 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016104 case ISD::LOAD:
16105 case ISD::SIGN_EXTEND:
16106 case ISD::ZERO_EXTEND:
16107 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016108 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016109 case ISD::SRL:
16110 case ISD::SUB:
16111 case ISD::ADD:
16112 case ISD::MUL:
16113 case ISD::AND:
16114 case ISD::OR:
16115 case ISD::XOR:
16116 return false;
16117 }
16118}
16119
16120/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016121/// beneficial for dag combiner to promote the specified node. If true, it
16122/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016123bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016124 EVT VT = Op.getValueType();
16125 if (VT != MVT::i16)
16126 return false;
16127
Evan Cheng4c26e932010-04-19 19:29:22 +000016128 bool Promote = false;
16129 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016130 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016131 default: break;
16132 case ISD::LOAD: {
16133 LoadSDNode *LD = cast<LoadSDNode>(Op);
16134 // If the non-extending load has a single use and it's not live out, then it
16135 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016136 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16137 Op.hasOneUse()*/) {
16138 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16139 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16140 // The only case where we'd want to promote LOAD (rather then it being
16141 // promoted as an operand is when it's only use is liveout.
16142 if (UI->getOpcode() != ISD::CopyToReg)
16143 return false;
16144 }
16145 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016146 Promote = true;
16147 break;
16148 }
16149 case ISD::SIGN_EXTEND:
16150 case ISD::ZERO_EXTEND:
16151 case ISD::ANY_EXTEND:
16152 Promote = true;
16153 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016154 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016155 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016156 SDValue N0 = Op.getOperand(0);
16157 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016158 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016159 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016160 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016161 break;
16162 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016163 case ISD::ADD:
16164 case ISD::MUL:
16165 case ISD::AND:
16166 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016167 case ISD::XOR:
16168 Commute = true;
16169 // fallthrough
16170 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016171 SDValue N0 = Op.getOperand(0);
16172 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016173 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016174 return false;
16175 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016176 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016177 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016178 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016179 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016180 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016181 }
16182 }
16183
16184 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016185 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016186}
16187
Evan Cheng60c07e12006-07-05 22:17:51 +000016188//===----------------------------------------------------------------------===//
16189// X86 Inline Assembly Support
16190//===----------------------------------------------------------------------===//
16191
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016192namespace {
16193 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016194 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016195 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016196
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016197 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016198 StringRef piece(*args[i]);
16199 if (!s.startswith(piece)) // Check if the piece matches.
16200 return false;
16201
16202 s = s.substr(piece.size());
16203 StringRef::size_type pos = s.find_first_not_of(" \t");
16204 if (pos == 0) // We matched a prefix.
16205 return false;
16206
16207 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016208 }
16209
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016210 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016211 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016212 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016213}
16214
Chris Lattnerb8105652009-07-20 17:51:36 +000016215bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16216 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016217
16218 std::string AsmStr = IA->getAsmString();
16219
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016220 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16221 if (!Ty || Ty->getBitWidth() % 16 != 0)
16222 return false;
16223
Chris Lattnerb8105652009-07-20 17:51:36 +000016224 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016225 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016226 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016227
16228 switch (AsmPieces.size()) {
16229 default: return false;
16230 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016231 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016232 // we will turn this bswap into something that will be lowered to logical
16233 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16234 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016235 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016236 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16237 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16238 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16239 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16240 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16241 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016242 // No need to check constraints, nothing other than the equivalent of
16243 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016244 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016245 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016246
Chris Lattnerb8105652009-07-20 17:51:36 +000016247 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016248 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016249 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016250 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16251 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016252 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016253 const std::string &ConstraintsStr = IA->getConstraintString();
16254 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016255 std::sort(AsmPieces.begin(), AsmPieces.end());
16256 if (AsmPieces.size() == 4 &&
16257 AsmPieces[0] == "~{cc}" &&
16258 AsmPieces[1] == "~{dirflag}" &&
16259 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016260 AsmPieces[3] == "~{fpsr}")
16261 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016262 }
16263 break;
16264 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016265 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016266 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016267 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16268 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16269 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016270 AsmPieces.clear();
16271 const std::string &ConstraintsStr = IA->getConstraintString();
16272 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16273 std::sort(AsmPieces.begin(), AsmPieces.end());
16274 if (AsmPieces.size() == 4 &&
16275 AsmPieces[0] == "~{cc}" &&
16276 AsmPieces[1] == "~{dirflag}" &&
16277 AsmPieces[2] == "~{flags}" &&
16278 AsmPieces[3] == "~{fpsr}")
16279 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016280 }
Evan Cheng55d42002011-01-08 01:24:27 +000016281
16282 if (CI->getType()->isIntegerTy(64)) {
16283 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16284 if (Constraints.size() >= 2 &&
16285 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16286 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16287 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016288 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16289 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16290 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016291 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016292 }
16293 }
16294 break;
16295 }
16296 return false;
16297}
16298
16299
16300
Chris Lattnerf4dff842006-07-11 02:54:03 +000016301/// getConstraintType - Given a constraint letter, return the type of
16302/// constraint it is for this target.
16303X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016304X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16305 if (Constraint.size() == 1) {
16306 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016307 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016308 case 'q':
16309 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016310 case 'f':
16311 case 't':
16312 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016313 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016314 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016315 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016316 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016317 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016318 case 'a':
16319 case 'b':
16320 case 'c':
16321 case 'd':
16322 case 'S':
16323 case 'D':
16324 case 'A':
16325 return C_Register;
16326 case 'I':
16327 case 'J':
16328 case 'K':
16329 case 'L':
16330 case 'M':
16331 case 'N':
16332 case 'G':
16333 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016334 case 'e':
16335 case 'Z':
16336 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016337 default:
16338 break;
16339 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016340 }
Chris Lattner4234f572007-03-25 02:14:49 +000016341 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016342}
16343
John Thompson44ab89e2010-10-29 17:29:13 +000016344/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016345/// This object must already have been set up with the operand type
16346/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016347TargetLowering::ConstraintWeight
16348 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016349 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016350 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016351 Value *CallOperandVal = info.CallOperandVal;
16352 // If we don't have a value, we can't do a match,
16353 // but allow it at the lowest weight.
16354 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016355 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016356 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016357 // Look at the constraint type.
16358 switch (*constraint) {
16359 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016360 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16361 case 'R':
16362 case 'q':
16363 case 'Q':
16364 case 'a':
16365 case 'b':
16366 case 'c':
16367 case 'd':
16368 case 'S':
16369 case 'D':
16370 case 'A':
16371 if (CallOperandVal->getType()->isIntegerTy())
16372 weight = CW_SpecificReg;
16373 break;
16374 case 'f':
16375 case 't':
16376 case 'u':
16377 if (type->isFloatingPointTy())
16378 weight = CW_SpecificReg;
16379 break;
16380 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016381 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016382 weight = CW_SpecificReg;
16383 break;
16384 case 'x':
16385 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016386 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016387 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016388 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016389 break;
16390 case 'I':
16391 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16392 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016393 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016394 }
16395 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016396 case 'J':
16397 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16398 if (C->getZExtValue() <= 63)
16399 weight = CW_Constant;
16400 }
16401 break;
16402 case 'K':
16403 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16404 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16405 weight = CW_Constant;
16406 }
16407 break;
16408 case 'L':
16409 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16410 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16411 weight = CW_Constant;
16412 }
16413 break;
16414 case 'M':
16415 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16416 if (C->getZExtValue() <= 3)
16417 weight = CW_Constant;
16418 }
16419 break;
16420 case 'N':
16421 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16422 if (C->getZExtValue() <= 0xff)
16423 weight = CW_Constant;
16424 }
16425 break;
16426 case 'G':
16427 case 'C':
16428 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16429 weight = CW_Constant;
16430 }
16431 break;
16432 case 'e':
16433 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16434 if ((C->getSExtValue() >= -0x80000000LL) &&
16435 (C->getSExtValue() <= 0x7fffffffLL))
16436 weight = CW_Constant;
16437 }
16438 break;
16439 case 'Z':
16440 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16441 if (C->getZExtValue() <= 0xffffffff)
16442 weight = CW_Constant;
16443 }
16444 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016445 }
16446 return weight;
16447}
16448
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016449/// LowerXConstraint - try to replace an X constraint, which matches anything,
16450/// with another that has more specific requirements based on the type of the
16451/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016452const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016453LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016454 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16455 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016456 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016457 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016458 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016459 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016460 return "x";
16461 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016462
Chris Lattner5e764232008-04-26 23:02:14 +000016463 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016464}
16465
Chris Lattner48884cd2007-08-25 00:47:38 +000016466/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16467/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016468void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016469 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016470 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016471 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016472 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016473
Eric Christopher100c8332011-06-02 23:16:42 +000016474 // Only support length 1 constraints for now.
16475 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016476
Eric Christopher100c8332011-06-02 23:16:42 +000016477 char ConstraintLetter = Constraint[0];
16478 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016479 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016480 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016481 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016482 if (C->getZExtValue() <= 31) {
16483 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016484 break;
16485 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016486 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016487 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016488 case 'J':
16489 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016490 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016491 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16492 break;
16493 }
16494 }
16495 return;
16496 case 'K':
16497 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016498 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016499 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16500 break;
16501 }
16502 }
16503 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016504 case 'N':
16505 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016506 if (C->getZExtValue() <= 255) {
16507 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016508 break;
16509 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016510 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016511 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016512 case 'e': {
16513 // 32-bit signed value
16514 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016515 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16516 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016517 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016518 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016519 break;
16520 }
16521 // FIXME gcc accepts some relocatable values here too, but only in certain
16522 // memory models; it's complicated.
16523 }
16524 return;
16525 }
16526 case 'Z': {
16527 // 32-bit unsigned value
16528 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016529 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16530 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016531 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16532 break;
16533 }
16534 }
16535 // FIXME gcc accepts some relocatable values here too, but only in certain
16536 // memory models; it's complicated.
16537 return;
16538 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016539 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016540 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016541 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016542 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016543 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016544 break;
16545 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016546
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016547 // In any sort of PIC mode addresses need to be computed at runtime by
16548 // adding in a register or some sort of table lookup. These can't
16549 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016550 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016551 return;
16552
Chris Lattnerdc43a882007-05-03 16:52:29 +000016553 // If we are in non-pic codegen mode, we allow the address of a global (with
16554 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016555 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016556 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016557
Chris Lattner49921962009-05-08 18:23:14 +000016558 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16559 while (1) {
16560 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16561 Offset += GA->getOffset();
16562 break;
16563 } else if (Op.getOpcode() == ISD::ADD) {
16564 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16565 Offset += C->getZExtValue();
16566 Op = Op.getOperand(0);
16567 continue;
16568 }
16569 } else if (Op.getOpcode() == ISD::SUB) {
16570 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16571 Offset += -C->getZExtValue();
16572 Op = Op.getOperand(0);
16573 continue;
16574 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016575 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016576
Chris Lattner49921962009-05-08 18:23:14 +000016577 // Otherwise, this isn't something we can handle, reject it.
16578 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016579 }
Eric Christopherfd179292009-08-27 18:07:15 +000016580
Dan Gohman46510a72010-04-15 01:51:59 +000016581 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016582 // If we require an extra load to get this address, as in PIC mode, we
16583 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016584 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16585 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016586 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016587
Devang Patel0d881da2010-07-06 22:08:15 +000016588 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16589 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016590 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016591 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016592 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016593
Gabor Greifba36cb52008-08-28 21:40:38 +000016594 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016595 Ops.push_back(Result);
16596 return;
16597 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016598 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016599}
16600
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016601std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016602X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016603 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016604 // First, see if this is a constraint that directly corresponds to an LLVM
16605 // register class.
16606 if (Constraint.size() == 1) {
16607 // GCC Constraint Letters
16608 switch (Constraint[0]) {
16609 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016610 // TODO: Slight differences here in allocation order and leaving
16611 // RIP in the class. Do they matter any more here than they do
16612 // in the normal allocation?
16613 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16614 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016615 if (VT == MVT::i32 || VT == MVT::f32)
16616 return std::make_pair(0U, &X86::GR32RegClass);
16617 if (VT == MVT::i16)
16618 return std::make_pair(0U, &X86::GR16RegClass);
16619 if (VT == MVT::i8 || VT == MVT::i1)
16620 return std::make_pair(0U, &X86::GR8RegClass);
16621 if (VT == MVT::i64 || VT == MVT::f64)
16622 return std::make_pair(0U, &X86::GR64RegClass);
16623 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016624 }
16625 // 32-bit fallthrough
16626 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016627 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016628 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16629 if (VT == MVT::i16)
16630 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16631 if (VT == MVT::i8 || VT == MVT::i1)
16632 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16633 if (VT == MVT::i64)
16634 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016635 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016636 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016637 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016638 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016639 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016640 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016641 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016642 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016643 return std::make_pair(0U, &X86::GR32RegClass);
16644 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016645 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016646 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016647 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016648 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016649 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016650 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016651 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16652 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016653 case 'f': // FP Stack registers.
16654 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16655 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016656 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016657 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016658 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016659 return std::make_pair(0U, &X86::RFP64RegClass);
16660 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016661 case 'y': // MMX_REGS if MMX allowed.
16662 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016663 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016664 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016665 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016666 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016667 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016668 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016669
Owen Anderson825b72b2009-08-11 20:47:22 +000016670 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016671 default: break;
16672 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016673 case MVT::f32:
16674 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016675 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016676 case MVT::f64:
16677 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016678 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016679 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016680 case MVT::v16i8:
16681 case MVT::v8i16:
16682 case MVT::v4i32:
16683 case MVT::v2i64:
16684 case MVT::v4f32:
16685 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016686 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016687 // AVX types.
16688 case MVT::v32i8:
16689 case MVT::v16i16:
16690 case MVT::v8i32:
16691 case MVT::v4i64:
16692 case MVT::v8f32:
16693 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016694 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016695 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016696 break;
16697 }
16698 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016699
Chris Lattnerf76d1802006-07-31 23:26:50 +000016700 // Use the default implementation in TargetLowering to convert the register
16701 // constraint into a member of a register class.
16702 std::pair<unsigned, const TargetRegisterClass*> Res;
16703 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016704
16705 // Not found as a standard register?
16706 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016707 // Map st(0) -> st(7) -> ST0
16708 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16709 tolower(Constraint[1]) == 's' &&
16710 tolower(Constraint[2]) == 't' &&
16711 Constraint[3] == '(' &&
16712 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16713 Constraint[5] == ')' &&
16714 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016715
Chris Lattner56d77c72009-09-13 22:41:48 +000016716 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016717 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016718 return Res;
16719 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016720
Chris Lattner56d77c72009-09-13 22:41:48 +000016721 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016722 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016723 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016724 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016725 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016726 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016727
16728 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016729 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016730 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016731 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016732 return Res;
16733 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016734
Dale Johannesen330169f2008-11-13 21:52:36 +000016735 // 'A' means EAX + EDX.
16736 if (Constraint == "A") {
16737 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016738 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016739 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016740 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016741 return Res;
16742 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016743
Chris Lattnerf76d1802006-07-31 23:26:50 +000016744 // Otherwise, check to see if this is a register class of the wrong value
16745 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16746 // turn into {ax},{dx}.
16747 if (Res.second->hasType(VT))
16748 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016749
Chris Lattnerf76d1802006-07-31 23:26:50 +000016750 // All of the single-register GCC register classes map their values onto
16751 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16752 // really want an 8-bit or 32-bit register, map to the appropriate register
16753 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016754 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016755 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016756 unsigned DestReg = 0;
16757 switch (Res.first) {
16758 default: break;
16759 case X86::AX: DestReg = X86::AL; break;
16760 case X86::DX: DestReg = X86::DL; break;
16761 case X86::CX: DestReg = X86::CL; break;
16762 case X86::BX: DestReg = X86::BL; break;
16763 }
16764 if (DestReg) {
16765 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016766 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016767 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016768 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016769 unsigned DestReg = 0;
16770 switch (Res.first) {
16771 default: break;
16772 case X86::AX: DestReg = X86::EAX; break;
16773 case X86::DX: DestReg = X86::EDX; break;
16774 case X86::CX: DestReg = X86::ECX; break;
16775 case X86::BX: DestReg = X86::EBX; break;
16776 case X86::SI: DestReg = X86::ESI; break;
16777 case X86::DI: DestReg = X86::EDI; break;
16778 case X86::BP: DestReg = X86::EBP; break;
16779 case X86::SP: DestReg = X86::ESP; break;
16780 }
16781 if (DestReg) {
16782 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016783 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016784 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016785 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016786 unsigned DestReg = 0;
16787 switch (Res.first) {
16788 default: break;
16789 case X86::AX: DestReg = X86::RAX; break;
16790 case X86::DX: DestReg = X86::RDX; break;
16791 case X86::CX: DestReg = X86::RCX; break;
16792 case X86::BX: DestReg = X86::RBX; break;
16793 case X86::SI: DestReg = X86::RSI; break;
16794 case X86::DI: DestReg = X86::RDI; break;
16795 case X86::BP: DestReg = X86::RBP; break;
16796 case X86::SP: DestReg = X86::RSP; break;
16797 }
16798 if (DestReg) {
16799 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016800 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016801 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016802 }
Craig Topperc9099502012-04-20 06:31:50 +000016803 } else if (Res.second == &X86::FR32RegClass ||
16804 Res.second == &X86::FR64RegClass ||
16805 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016806 // Handle references to XMM physical registers that got mapped into the
16807 // wrong class. This can happen with constraints like {xmm0} where the
16808 // target independent register mapper will just pick the first match it can
16809 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016810
16811 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016812 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016813 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016814 Res.second = &X86::FR64RegClass;
16815 else if (X86::VR128RegClass.hasType(VT))
16816 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016817 else if (X86::VR256RegClass.hasType(VT))
16818 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016819 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016820
Chris Lattnerf76d1802006-07-31 23:26:50 +000016821 return Res;
16822}