blob: 761fe62ca6a7d0128119866d69d14d4b532f7caa [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);
832 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
833 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
834 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
835 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
836 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000837 }
838
Craig Topper1accb7e2012-01-10 06:54:16 +0000839 if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) {
Craig Topperc9099502012-04-20 06:31:50 +0000840 addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000841
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000842 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
843 // registers cannot be used even for integer operations.
Craig Topperc9099502012-04-20 06:31:50 +0000844 addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
845 addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
846 addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
847 addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000848
Owen Anderson825b72b2009-08-11 20:47:22 +0000849 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
850 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
851 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
852 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
853 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
854 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
855 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
856 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
857 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
858 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
859 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
860 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
861 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
862 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
863 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
864 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000865
Nadav Rotem354efd82011-09-18 14:57:03 +0000866 setOperationAction(ISD::SETCC, MVT::v2i64, Custom);
Duncan Sands28b77e92011-09-06 19:07:46 +0000867 setOperationAction(ISD::SETCC, MVT::v16i8, Custom);
868 setOperationAction(ISD::SETCC, MVT::v8i16, Custom);
869 setOperationAction(ISD::SETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
872 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
873 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
874 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
875 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000876
Evan Cheng2c3ae372006-04-12 21:21:57 +0000877 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Jakub Staszak6610b1d2012-04-29 20:52:53 +0000878 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000879 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000880 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000881 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000882 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000883 // Do not attempt to custom lower non-128-bit vectors
884 if (!VT.is128BitVector())
885 continue;
Craig Topper0d1f1762012-08-12 00:34:56 +0000886 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
887 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
888 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000889 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000890
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
892 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
893 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
894 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
895 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
896 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000897
Nate Begemancdd1eec2008-02-12 22:51:28 +0000898 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
900 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000901 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000902
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000903 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Craig Topper31a207a2012-05-04 06:39:13 +0000904 for (int i = MVT::v16i8; i != MVT::v2i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +0000905 MVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000906
907 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000908 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000909 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000910
Craig Topper0d1f1762012-08-12 00:34:56 +0000911 setOperationAction(ISD::AND, VT, Promote);
912 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
913 setOperationAction(ISD::OR, VT, Promote);
914 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
915 setOperationAction(ISD::XOR, VT, Promote);
916 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
917 setOperationAction(ISD::LOAD, VT, Promote);
918 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
919 setOperationAction(ISD::SELECT, VT, Promote);
920 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000921 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000922
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000924
Evan Cheng2c3ae372006-04-12 21:21:57 +0000925 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
927 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
928 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
929 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000930
Owen Anderson825b72b2009-08-11 20:47:22 +0000931 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
932 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000933 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000934
Craig Topperd0a31172012-01-10 06:37:29 +0000935 if (Subtarget->hasSSE41()) {
Benjamin Kramerb6533972011-12-09 15:44:03 +0000936 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
937 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
938 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
939 setOperationAction(ISD::FRINT, MVT::f32, Legal);
940 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
941 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
942 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
943 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
944 setOperationAction(ISD::FRINT, MVT::f64, Legal);
945 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
946
Nate Begeman14d12ca2008-02-11 04:19:36 +0000947 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000949
Nadav Rotemfbad25e2011-09-11 15:02:23 +0000950 setOperationAction(ISD::VSELECT, MVT::v2f64, Legal);
951 setOperationAction(ISD::VSELECT, MVT::v2i64, Legal);
952 setOperationAction(ISD::VSELECT, MVT::v16i8, Legal);
953 setOperationAction(ISD::VSELECT, MVT::v4i32, Legal);
954 setOperationAction(ISD::VSELECT, MVT::v4f32, Legal);
Nadav Rotemffe3e7d2011-09-08 08:11:19 +0000955
Nate Begeman14d12ca2008-02-11 04:19:36 +0000956 // i8 and i16 vectors are custom , because the source register and source
957 // source memory operand types are not the same width. f32 vectors are
958 // custom since the immediate controlling the insert encodes additional
959 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
961 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
962 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
963 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000964
Owen Anderson825b72b2009-08-11 20:47:22 +0000965 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
966 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
967 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
968 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000969
Pete Coopera77214a2011-11-14 19:38:42 +0000970 // FIXME: these should be Legal but thats only for the case where
Chad Rosier30450e82011-12-22 22:35:21 +0000971 // the index is constant. For now custom expand to deal with that.
Nate Begeman14d12ca2008-02-11 04:19:36 +0000972 if (Subtarget->is64Bit()) {
Pete Coopera77214a2011-11-14 19:38:42 +0000973 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
974 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000975 }
976 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000977
Craig Topper1accb7e2012-01-10 06:54:16 +0000978 if (Subtarget->hasSSE2()) {
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +0000979 setOperationAction(ISD::SRL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000980 setOperationAction(ISD::SRL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000981
Nadav Rotem43012222011-05-11 08:12:09 +0000982 setOperationAction(ISD::SHL, MVT::v8i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000983 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nadav Rotem43012222011-05-11 08:12:09 +0000984
Nadav Rotem43012222011-05-11 08:12:09 +0000985 setOperationAction(ISD::SRA, MVT::v8i16, Custom);
Eli Friedmanf6aa6b12011-11-01 21:18:39 +0000986 setOperationAction(ISD::SRA, MVT::v16i8, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +0000987
988 if (Subtarget->hasAVX2()) {
989 setOperationAction(ISD::SRL, MVT::v2i64, Legal);
990 setOperationAction(ISD::SRL, MVT::v4i32, Legal);
991
992 setOperationAction(ISD::SHL, MVT::v2i64, Legal);
993 setOperationAction(ISD::SHL, MVT::v4i32, Legal);
994
995 setOperationAction(ISD::SRA, MVT::v4i32, Legal);
996 } else {
997 setOperationAction(ISD::SRL, MVT::v2i64, Custom);
998 setOperationAction(ISD::SRL, MVT::v4i32, Custom);
999
1000 setOperationAction(ISD::SHL, MVT::v2i64, Custom);
1001 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
1002
1003 setOperationAction(ISD::SRA, MVT::v4i32, Custom);
1004 }
Nadav Rotem43012222011-05-11 08:12:09 +00001005 }
1006
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001007 if (!TM.Options.UseSoftFloat && Subtarget->hasAVX()) {
Craig Topperc9099502012-04-20 06:31:50 +00001008 addRegisterClass(MVT::v32i8, &X86::VR256RegClass);
1009 addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1010 addRegisterClass(MVT::v8i32, &X86::VR256RegClass);
1011 addRegisterClass(MVT::v8f32, &X86::VR256RegClass);
1012 addRegisterClass(MVT::v4i64, &X86::VR256RegClass);
1013 addRegisterClass(MVT::v4f64, &X86::VR256RegClass);
David Greened94c1012009-06-29 22:50:51 +00001014
Owen Anderson825b72b2009-08-11 20:47:22 +00001015 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +00001016 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
1017 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +00001018
Owen Anderson825b72b2009-08-11 20:47:22 +00001019 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
1020 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
1021 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
1022 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
1023 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
1024 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001025
Owen Anderson825b72b2009-08-11 20:47:22 +00001026 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
1027 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
1028 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
1029 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
1030 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
1031 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001032
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001033 setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal);
1034 setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal);
Bruno Cardoso Lopes55244ce2011-08-01 21:54:09 +00001035 setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal);
Bruno Cardoso Lopes2e64ae42011-07-28 01:26:39 +00001036
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001037 setOperationAction(ISD::SRL, MVT::v16i16, Custom);
1038 setOperationAction(ISD::SRL, MVT::v32i8, Custom);
1039
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001040 setOperationAction(ISD::SHL, MVT::v16i16, Custom);
1041 setOperationAction(ISD::SHL, MVT::v32i8, Custom);
1042
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001043 setOperationAction(ISD::SRA, MVT::v16i16, Custom);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001044 setOperationAction(ISD::SRA, MVT::v32i8, Custom);
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +00001045
Duncan Sands28b77e92011-09-06 19:07:46 +00001046 setOperationAction(ISD::SETCC, MVT::v32i8, Custom);
1047 setOperationAction(ISD::SETCC, MVT::v16i16, Custom);
1048 setOperationAction(ISD::SETCC, MVT::v8i32, Custom);
1049 setOperationAction(ISD::SETCC, MVT::v4i64, Custom);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00001050
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +00001051 setOperationAction(ISD::SELECT, MVT::v4f64, Custom);
1052 setOperationAction(ISD::SELECT, MVT::v4i64, Custom);
1053 setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
1054
Craig Topperaaa643c2011-11-09 07:28:55 +00001055 setOperationAction(ISD::VSELECT, MVT::v4f64, Legal);
1056 setOperationAction(ISD::VSELECT, MVT::v4i64, Legal);
1057 setOperationAction(ISD::VSELECT, MVT::v8i32, Legal);
1058 setOperationAction(ISD::VSELECT, MVT::v8f32, Legal);
Nadav Rotem8ffad562011-09-09 20:29:17 +00001059
Craig Topperbf404372012-08-31 15:40:30 +00001060 if (Subtarget->hasFMA() || Subtarget->hasFMA4()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001061 setOperationAction(ISD::FMA, MVT::v8f32, Custom);
1062 setOperationAction(ISD::FMA, MVT::v4f64, Custom);
1063 setOperationAction(ISD::FMA, MVT::v4f32, Custom);
1064 setOperationAction(ISD::FMA, MVT::v2f64, Custom);
1065 setOperationAction(ISD::FMA, MVT::f32, Custom);
1066 setOperationAction(ISD::FMA, MVT::f64, Custom);
1067 }
Craig Topper880ef452012-08-11 22:34:26 +00001068
Craig Topperaaa643c2011-11-09 07:28:55 +00001069 if (Subtarget->hasAVX2()) {
1070 setOperationAction(ISD::ADD, MVT::v4i64, Legal);
1071 setOperationAction(ISD::ADD, MVT::v8i32, Legal);
1072 setOperationAction(ISD::ADD, MVT::v16i16, Legal);
1073 setOperationAction(ISD::ADD, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001074
Craig Topperaaa643c2011-11-09 07:28:55 +00001075 setOperationAction(ISD::SUB, MVT::v4i64, Legal);
1076 setOperationAction(ISD::SUB, MVT::v8i32, Legal);
1077 setOperationAction(ISD::SUB, MVT::v16i16, Legal);
1078 setOperationAction(ISD::SUB, MVT::v32i8, Legal);
Craig Topper13894fa2011-08-24 06:14:18 +00001079
Craig Topperaaa643c2011-11-09 07:28:55 +00001080 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1081 setOperationAction(ISD::MUL, MVT::v8i32, Legal);
1082 setOperationAction(ISD::MUL, MVT::v16i16, Legal);
Craig Topper46154eb2011-11-11 07:39:23 +00001083 // Don't lower v32i8 because there is no 128-bit byte mul
Nadav Rotembb539bf2011-11-09 13:21:28 +00001084
1085 setOperationAction(ISD::VSELECT, MVT::v32i8, Legal);
Craig Topper7be5dfd2011-11-12 09:58:49 +00001086
1087 setOperationAction(ISD::SRL, MVT::v4i64, Legal);
1088 setOperationAction(ISD::SRL, MVT::v8i32, Legal);
1089
1090 setOperationAction(ISD::SHL, MVT::v4i64, Legal);
1091 setOperationAction(ISD::SHL, MVT::v8i32, Legal);
1092
1093 setOperationAction(ISD::SRA, MVT::v8i32, Legal);
Craig Topperaaa643c2011-11-09 07:28:55 +00001094 } else {
1095 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
1096 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
1097 setOperationAction(ISD::ADD, MVT::v16i16, Custom);
1098 setOperationAction(ISD::ADD, MVT::v32i8, Custom);
1099
1100 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
1101 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
1102 setOperationAction(ISD::SUB, MVT::v16i16, Custom);
1103 setOperationAction(ISD::SUB, MVT::v32i8, Custom);
1104
1105 setOperationAction(ISD::MUL, MVT::v4i64, Custom);
1106 setOperationAction(ISD::MUL, MVT::v8i32, Custom);
1107 setOperationAction(ISD::MUL, MVT::v16i16, Custom);
1108 // Don't lower v32i8 because there is no 128-bit byte mul
Craig Topper7be5dfd2011-11-12 09:58:49 +00001109
1110 setOperationAction(ISD::SRL, MVT::v4i64, Custom);
1111 setOperationAction(ISD::SRL, MVT::v8i32, Custom);
1112
1113 setOperationAction(ISD::SHL, MVT::v4i64, Custom);
1114 setOperationAction(ISD::SHL, MVT::v8i32, Custom);
1115
1116 setOperationAction(ISD::SRA, MVT::v8i32, Custom);
Craig Topperaaa643c2011-11-09 07:28:55 +00001117 }
Craig Topper13894fa2011-08-24 06:14:18 +00001118
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001119 // Custom lower several nodes for 256-bit types.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001120 for (int i = MVT::FIRST_VECTOR_VALUETYPE;
1121 i <= MVT::LAST_VECTOR_VALUETYPE; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001122 MVT VT = (MVT::SimpleValueType)i;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001123
1124 // Extract subvector is special because the value type
1125 // (result) is 128-bit but the source is 256-bit wide.
1126 if (VT.is128BitVector())
Craig Topper0d1f1762012-08-12 00:34:56 +00001127 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001128
1129 // Do not attempt to custom lower other non-256-bit vectors
1130 if (!VT.is256BitVector())
David Greene9b9838d2009-06-29 16:47:10 +00001131 continue;
David Greene54d8eba2011-01-27 22:38:56 +00001132
Craig Topper0d1f1762012-08-12 00:34:56 +00001133 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
1134 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
1135 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
1136 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1137 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
1138 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
1139 setOperationAction(ISD::CONCAT_VECTORS, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +00001140 }
1141
David Greene54d8eba2011-01-27 22:38:56 +00001142 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001143 for (int i = MVT::v32i8; i != MVT::v4i64; ++i) {
Craig Topper0d1f1762012-08-12 00:34:56 +00001144 MVT VT = (MVT::SimpleValueType)i;
David Greene54d8eba2011-01-27 22:38:56 +00001145
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001146 // Do not attempt to promote non-256-bit vectors
1147 if (!VT.is256BitVector())
David Greene54d8eba2011-01-27 22:38:56 +00001148 continue;
Bruno Cardoso Lopes5bc37dd2011-07-15 22:24:33 +00001149
Craig Topper0d1f1762012-08-12 00:34:56 +00001150 setOperationAction(ISD::AND, VT, Promote);
1151 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
1152 setOperationAction(ISD::OR, VT, Promote);
1153 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
1154 setOperationAction(ISD::XOR, VT, Promote);
1155 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
1156 setOperationAction(ISD::LOAD, VT, Promote);
1157 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
1158 setOperationAction(ISD::SELECT, VT, Promote);
1159 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene54d8eba2011-01-27 22:38:56 +00001160 }
David Greene9b9838d2009-06-29 16:47:10 +00001161 }
1162
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001163 // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion
1164 // of this type with custom code.
Jakub Staszak6610b1d2012-04-29 20:52:53 +00001165 for (int VT = MVT::FIRST_VECTOR_VALUETYPE;
1166 VT != MVT::LAST_VECTOR_VALUETYPE; VT++) {
Chad Rosier30450e82011-12-22 22:35:21 +00001167 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,
1168 Custom);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +00001169 }
1170
Evan Cheng6be2c582006-04-05 23:38:46 +00001171 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001172 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Benjamin Kramerb9bee042012-07-12 09:31:43 +00001173 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001174
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001175
Eli Friedman962f5492010-06-02 19:35:46 +00001176 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1177 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +00001178 //
Eli Friedman962f5492010-06-02 19:35:46 +00001179 // FIXME: We really should do custom legalization for addition and
1180 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
1181 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001182 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
1183 // Add/Sub/Mul with overflow operations are custom lowered.
1184 MVT VT = IntVTs[i];
1185 setOperationAction(ISD::SADDO, VT, Custom);
1186 setOperationAction(ISD::UADDO, VT, Custom);
1187 setOperationAction(ISD::SSUBO, VT, Custom);
1188 setOperationAction(ISD::USUBO, VT, Custom);
1189 setOperationAction(ISD::SMULO, VT, Custom);
1190 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +00001191 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00001192
Chris Lattnera34b3cf2010-12-19 20:03:11 +00001193 // There are no 8-bit 3-address imul/mul instructions
1194 setOperationAction(ISD::SMULO, MVT::i8, Expand);
1195 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001196
Evan Chengd54f2d52009-03-31 19:38:51 +00001197 if (!Subtarget->is64Bit()) {
1198 // These libcalls are not available in 32-bit.
1199 setLibcallName(RTLIB::SHL_I128, 0);
1200 setLibcallName(RTLIB::SRL_I128, 0);
1201 setLibcallName(RTLIB::SRA_I128, 0);
1202 }
1203
Evan Cheng206ee9d2006-07-07 08:33:52 +00001204 // We have target-specific dag combine patterns for the following nodes:
1205 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00001206 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Duncan Sands6bcd2192011-09-17 16:49:39 +00001207 setTargetDAGCombine(ISD::VSELECT);
Chris Lattner83e6c992006-10-04 06:57:07 +00001208 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001209 setTargetDAGCombine(ISD::SHL);
1210 setTargetDAGCombine(ISD::SRA);
1211 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001212 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +00001213 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001214 setTargetDAGCombine(ISD::ADD);
Duncan Sands17470be2011-09-22 20:15:48 +00001215 setTargetDAGCombine(ISD::FADD);
1216 setTargetDAGCombine(ISD::FSUB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00001217 setTargetDAGCombine(ISD::FMA);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +00001218 setTargetDAGCombine(ISD::SUB);
Nadav Rotem91e43fd2011-09-18 10:39:32 +00001219 setTargetDAGCombine(ISD::LOAD);
Chris Lattner149a4e52008-02-22 02:09:43 +00001220 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +00001221 setTargetDAGCombine(ISD::ZERO_EXTEND);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00001222 setTargetDAGCombine(ISD::ANY_EXTEND);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +00001223 setTargetDAGCombine(ISD::SIGN_EXTEND);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +00001224 setTargetDAGCombine(ISD::TRUNCATE);
Nadav Rotema3540772012-04-23 21:53:37 +00001225 setTargetDAGCombine(ISD::UINT_TO_FP);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +00001226 setTargetDAGCombine(ISD::SINT_TO_FP);
Chad Rosiera73b6fc2012-04-27 22:33:25 +00001227 setTargetDAGCombine(ISD::SETCC);
Nadav Rotema3540772012-04-23 21:53:37 +00001228 setTargetDAGCombine(ISD::FP_TO_SINT);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001229 if (Subtarget->is64Bit())
1230 setTargetDAGCombine(ISD::MUL);
Manman Ren92363622012-06-07 22:39:10 +00001231 setTargetDAGCombine(ISD::XOR);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001232
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001233 computeRegisterProperties();
1234
Evan Cheng05219282011-01-06 06:52:41 +00001235 // On Darwin, -Os means optimize for size without hurting performance,
1236 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +00001237 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001238 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +00001239 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +00001240 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
1241 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1242 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001243 setPrefLoopAlignment(4); // 2^4 bytes.
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001244 benefitFromCodePlacementOpt = true;
Eli Friedmanfc5d3052011-05-06 20:34:06 +00001245
Benjamin Krameraaf723d2012-05-05 12:49:14 +00001246 // Predictable cmov don't hurt on atom because it's in-order.
1247 predictableSelectIsExpensive = !Subtarget->isAtom();
1248
Jakob Stoklund Olesen8c741b82011-12-06 01:26:19 +00001249 setPrefFunctionAlignment(4); // 2^4 bytes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001250}
1251
Scott Michel5b8f82e2008-03-10 15:42:14 +00001252
Duncan Sands28b77e92011-09-06 19:07:46 +00001253EVT X86TargetLowering::getSetCCResultType(EVT VT) const {
1254 if (!VT.isVector()) return MVT::i8;
1255 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +00001256}
1257
1258
Evan Cheng29286502008-01-23 23:17:41 +00001259/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1260/// the desired ByVal argument alignment.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001261static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
Evan Cheng29286502008-01-23 23:17:41 +00001262 if (MaxAlign == 16)
1263 return;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001264 if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001265 if (VTy->getBitWidth() == 128)
1266 MaxAlign = 16;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001267 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001268 unsigned EltAlign = 0;
1269 getMaxByValAlign(ATy->getElementType(), EltAlign);
1270 if (EltAlign > MaxAlign)
1271 MaxAlign = EltAlign;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001272 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
Evan Cheng29286502008-01-23 23:17:41 +00001273 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1274 unsigned EltAlign = 0;
1275 getMaxByValAlign(STy->getElementType(i), EltAlign);
1276 if (EltAlign > MaxAlign)
1277 MaxAlign = EltAlign;
1278 if (MaxAlign == 16)
1279 break;
1280 }
1281 }
Evan Cheng29286502008-01-23 23:17:41 +00001282}
1283
1284/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1285/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001286/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1287/// are at 4-byte boundaries.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001288unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001289 if (Subtarget->is64Bit()) {
1290 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001291 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001292 if (TyAlign > 8)
1293 return TyAlign;
1294 return 8;
1295 }
1296
Evan Cheng29286502008-01-23 23:17:41 +00001297 unsigned Align = 4;
Craig Topper1accb7e2012-01-10 06:54:16 +00001298 if (Subtarget->hasSSE1())
Dale Johannesen0c191872008-02-08 19:48:20 +00001299 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001300 return Align;
1301}
Chris Lattner2b02a442007-02-25 08:29:00 +00001302
Evan Chengf0df0312008-05-15 08:39:06 +00001303/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001304/// and store operations as a result of memset, memcpy, and memmove
1305/// lowering. If DstAlign is zero that means it's safe to destination
1306/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1307/// means there isn't a need to check it against alignment requirement,
1308/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00001309/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengc3b0c342010-04-08 07:37:57 +00001310/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1311/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1312/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001313/// It returns EVT::Other if the type should be determined using generic
1314/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001315EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001316X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1317 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00001318 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00001319 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001320 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001321 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1322 // linux. This is because the stack realignment code can't handle certain
1323 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001324 const Function *F = MF.getFunction();
Lang Hames15701f82011-10-26 23:50:43 +00001325 if (IsZeroVal &&
Evan Chenga5e13622011-01-07 19:35:30 +00001326 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001327 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001328 (Subtarget->isUnalignedMemAccessFast() ||
1329 ((DstAlign == 0 || DstAlign >= 16) &&
1330 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001331 Subtarget->getStackAlignment() >= 16) {
Craig Topper562659f2012-01-13 08:32:21 +00001332 if (Subtarget->getStackAlignment() >= 32) {
1333 if (Subtarget->hasAVX2())
1334 return MVT::v8i32;
1335 if (Subtarget->hasAVX())
1336 return MVT::v8f32;
1337 }
Craig Topper1accb7e2012-01-10 06:54:16 +00001338 if (Subtarget->hasSSE2())
Evan Cheng255f20f2010-04-01 06:04:33 +00001339 return MVT::v4i32;
Craig Topper1accb7e2012-01-10 06:54:16 +00001340 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001341 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001342 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001343 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001344 Subtarget->getStackAlignment() >= 8 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001345 Subtarget->hasSSE2()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001346 // Do not use f64 to lower memcpy if source is string constant. It's
1347 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001348 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001349 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001350 }
Evan Chengf0df0312008-05-15 08:39:06 +00001351 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001352 return MVT::i64;
1353 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001354}
1355
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001356/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1357/// current function. The returned value is a member of the
1358/// MachineJumpTableInfo::JTEntryKind enum.
1359unsigned X86TargetLowering::getJumpTableEncoding() const {
1360 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1361 // symbol.
1362 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1363 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001364 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001365
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001366 // Otherwise, use the normal jump table encoding heuristics.
1367 return TargetLowering::getJumpTableEncoding();
1368}
1369
Chris Lattnerc64daab2010-01-26 05:02:42 +00001370const MCExpr *
1371X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1372 const MachineBasicBlock *MBB,
1373 unsigned uid,MCContext &Ctx) const{
1374 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1375 Subtarget->isPICStyleGOT());
1376 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1377 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001378 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1379 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001380}
1381
Evan Chengcc415862007-11-09 01:32:10 +00001382/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1383/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001384SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001385 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001386 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001387 // This doesn't have DebugLoc associated with it, but is not really the
1388 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001389 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001390 return Table;
1391}
1392
Chris Lattner589c6f62010-01-26 06:28:43 +00001393/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1394/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1395/// MCExpr.
1396const MCExpr *X86TargetLowering::
1397getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1398 MCContext &Ctx) const {
1399 // X86-64 uses RIP relative addressing based on the jump table label.
1400 if (Subtarget->isPICStyleRIPRel())
1401 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1402
1403 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001404 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001405}
1406
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001407// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001408std::pair<const TargetRegisterClass*, uint8_t>
1409X86TargetLowering::findRepresentativeClass(EVT VT) const{
1410 const TargetRegisterClass *RRC = 0;
1411 uint8_t Cost = 1;
1412 switch (VT.getSimpleVT().SimpleTy) {
1413 default:
1414 return TargetLowering::findRepresentativeClass(VT);
1415 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +00001416 RRC = Subtarget->is64Bit() ?
1417 (const TargetRegisterClass*)&X86::GR64RegClass :
1418 (const TargetRegisterClass*)&X86::GR32RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001419 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001420 case MVT::x86mmx:
Craig Topperc9099502012-04-20 06:31:50 +00001421 RRC = &X86::VR64RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001422 break;
1423 case MVT::f32: case MVT::f64:
1424 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1425 case MVT::v4f32: case MVT::v2f64:
1426 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1427 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +00001428 RRC = &X86::VR128RegClass;
Evan Chengdee81012010-07-26 21:50:05 +00001429 break;
1430 }
1431 return std::make_pair(RRC, Cost);
1432}
1433
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001434bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1435 unsigned &Offset) const {
1436 if (!Subtarget->isTargetLinux())
1437 return false;
1438
1439 if (Subtarget->is64Bit()) {
1440 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1441 Offset = 0x28;
1442 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1443 AddressSpace = 256;
1444 else
1445 AddressSpace = 257;
1446 } else {
1447 // %gs:0x14 on i386
1448 Offset = 0x14;
1449 AddressSpace = 256;
1450 }
1451 return true;
1452}
1453
1454
Chris Lattner2b02a442007-02-25 08:29:00 +00001455//===----------------------------------------------------------------------===//
1456// Return Value Calling Convention Implementation
1457//===----------------------------------------------------------------------===//
1458
Chris Lattner59ed56b2007-02-28 04:55:35 +00001459#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001460
Michael J. Spencerec38de22010-10-10 22:04:20 +00001461bool
Eric Christopher471e4222011-06-08 23:55:35 +00001462X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Craig Topper0fbf3642012-04-23 03:28:34 +00001463 MachineFunction &MF, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001464 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001465 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001466 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001467 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001468 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001469 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001470}
1471
Dan Gohman98ca4f22009-08-05 01:29:28 +00001472SDValue
1473X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001474 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001475 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001476 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001477 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001478 MachineFunction &MF = DAG.getMachineFunction();
1479 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001480
Chris Lattner9774c912007-02-27 05:28:59 +00001481 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001482 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483 RVLocs, *DAG.getContext());
1484 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001485
Evan Chengdcea1632010-02-04 02:40:39 +00001486 // Add the regs to the liveout set for the function.
1487 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1488 for (unsigned i = 0; i != RVLocs.size(); ++i)
1489 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1490 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001491
Dan Gohman475871a2008-07-27 21:46:04 +00001492 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001493
Dan Gohman475871a2008-07-27 21:46:04 +00001494 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001495 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1496 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001497 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1498 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001499
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001500 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001501 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1502 CCValAssign &VA = RVLocs[i];
1503 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001504 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001505 EVT ValVT = ValToCopy.getValueType();
1506
Jakob Stoklund Olesenee66b412012-05-31 17:28:20 +00001507 // Promote values to the appropriate types
1508 if (VA.getLocInfo() == CCValAssign::SExt)
1509 ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
1510 else if (VA.getLocInfo() == CCValAssign::ZExt)
1511 ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
1512 else if (VA.getLocInfo() == CCValAssign::AExt)
1513 ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
1514 else if (VA.getLocInfo() == CCValAssign::BCvt)
1515 ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy);
1516
Dale Johannesenc4510512010-09-24 19:05:48 +00001517 // If this is x86-64, and we disabled SSE, we can't return FP values,
1518 // or SSE or MMX vectors.
1519 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1520 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001521 (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001522 report_fatal_error("SSE register return with SSE disabled");
1523 }
1524 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1525 // llvm-gcc has never done it right and no one has noticed, so this
1526 // should be OK for now.
1527 if (ValVT == MVT::f64 &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001528 (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001529 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001530
Chris Lattner447ff682008-03-11 03:23:40 +00001531 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1532 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001533 if (VA.getLocReg() == X86::ST0 ||
1534 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001535 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1536 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001537 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001538 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001539 RetOps.push_back(ValToCopy);
1540 // Don't emit a copytoreg.
1541 continue;
1542 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001543
Evan Cheng242b38b2009-02-23 09:03:22 +00001544 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1545 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001546 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001547 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001548 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001549 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001550 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1551 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001552 // If we don't have SSE2 available, convert to v4f32 so the generated
1553 // register is legal.
Craig Topper1accb7e2012-01-10 06:54:16 +00001554 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001555 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001556 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001557 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001558 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001559
Dale Johannesendd64c412009-02-04 00:33:20 +00001560 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001561 Flag = Chain.getValue(1);
1562 }
Dan Gohman61a92132008-04-21 23:59:07 +00001563
1564 // The x86-64 ABI for returning structs by value requires that we copy
1565 // the sret argument into %rax for the return. We saved the argument into
1566 // a virtual register in the entry block, so now we copy the value out
1567 // and into %rax.
1568 if (Subtarget->is64Bit() &&
1569 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1570 MachineFunction &MF = DAG.getMachineFunction();
1571 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1572 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001573 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001574 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001575 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001576
Dale Johannesendd64c412009-02-04 00:33:20 +00001577 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001578 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001579
1580 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001581 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001582 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001583
Chris Lattner447ff682008-03-11 03:23:40 +00001584 RetOps[0] = Chain; // Update chain.
1585
1586 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001587 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001588 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001589
1590 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001591 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001592}
1593
Evan Chengbf010eb2012-04-10 01:51:00 +00001594bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001595 if (N->getNumValues() != 1)
1596 return false;
1597 if (!N->hasNUsesOfValue(1, 0))
1598 return false;
1599
Evan Chengbf010eb2012-04-10 01:51:00 +00001600 SDValue TCChain = Chain;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001601 SDNode *Copy = *N->use_begin();
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001602 if (Copy->getOpcode() == ISD::CopyToReg) {
1603 // If the copy has a glue operand, we conservatively assume it isn't safe to
1604 // perform a tail call.
1605 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
1606 return false;
Evan Chengbf010eb2012-04-10 01:51:00 +00001607 TCChain = Copy->getOperand(0);
Chad Rosierc8d7eea2012-03-05 19:27:12 +00001608 } else if (Copy->getOpcode() != ISD::FP_EXTEND)
Chad Rosier74bab7f2012-03-02 02:50:46 +00001609 return false;
1610
Evan Cheng1bf891a2010-12-01 22:59:46 +00001611 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001612 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001613 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001614 if (UI->getOpcode() != X86ISD::RET_FLAG)
1615 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001616 HasRet = true;
1617 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001618
Evan Chengbf010eb2012-04-10 01:51:00 +00001619 if (!HasRet)
1620 return false;
1621
1622 Chain = TCChain;
1623 return true;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001624}
1625
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001626EVT
1627X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
Cameron Zwarich44579682011-03-17 14:21:56 +00001628 ISD::NodeType ExtendKind) const {
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001629 MVT ReturnMVT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001630 // TODO: Is this also valid on 32-bit?
1631 if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001632 ReturnMVT = MVT::i8;
1633 else
1634 ReturnMVT = MVT::i32;
1635
1636 EVT MinVT = getRegisterType(Context, ReturnMVT);
1637 return VT.bitsLT(MinVT) ? MinVT : VT;
Cameron Zwarichebe81732011-03-16 22:20:18 +00001638}
1639
Dan Gohman98ca4f22009-08-05 01:29:28 +00001640/// LowerCallResult - Lower the result values of a call into the
1641/// appropriate copies out of appropriate physical registers.
1642///
1643SDValue
1644X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001645 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001646 const SmallVectorImpl<ISD::InputArg> &Ins,
1647 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001648 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001649
Chris Lattnere32bbf62007-02-28 07:09:55 +00001650 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001651 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001652 bool Is64Bit = Subtarget->is64Bit();
Eric Christopher471e4222011-06-08 23:55:35 +00001653 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00001654 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001655 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001656
Chris Lattner3085e152007-02-25 08:59:22 +00001657 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001658 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001659 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001660 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001661
Torok Edwin3f142c32009-02-01 18:15:56 +00001662 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001663 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00001664 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001665 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001666 }
1667
Evan Cheng79fb3b42009-02-20 20:43:02 +00001668 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001669
1670 // If this is a call to a function that returns an fp value on the floating
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001671 // point stack, we must guarantee the value is popped from the stack, so
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001672 // a CopyFromReg is not good enough - the copy instruction may be eliminated
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001673 // if the return value is not used. We use the FpPOP_RETVAL instruction
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001674 // instead.
1675 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1676 // If we prefer to use the value in xmm registers, copy it out as f80 and
1677 // use a truncate to move it from fp stack reg to xmm reg.
1678 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001679 SDValue Ops[] = { Chain, InFlag };
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001680 Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT,
1681 MVT::Other, MVT::Glue, Ops, 2), 1);
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001682 Val = Chain.getValue(0);
1683
1684 // Round the f80 to the right size, which also moves it to the appropriate
1685 // xmm register.
1686 if (CopyVT != VA.getValVT())
1687 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1688 // This truncation won't change the value.
1689 DAG.getIntPtrConstant(1));
Evan Cheng79fb3b42009-02-20 20:43:02 +00001690 } else {
1691 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1692 CopyVT, InFlag).getValue(1);
1693 Val = Chain.getValue(0);
1694 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001695 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001696 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001697 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001698
Dan Gohman98ca4f22009-08-05 01:29:28 +00001699 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001700}
1701
1702
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001703//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001704// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001705//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001706// StdCall calling convention seems to be standard for many Windows' API
1707// routines and around. It differs from C calling convention just a little:
1708// callee should clean up the stack, not caller. Symbols should be also
1709// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001710// For info on fast calling convention see Fast Calling Convention (tail call)
1711// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001712
Dan Gohman98ca4f22009-08-05 01:29:28 +00001713/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001714/// semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001715enum StructReturnType {
1716 NotStructReturn,
1717 RegStructReturn,
1718 StackStructReturn
1719};
1720static StructReturnType
1721callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722 if (Outs.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001723 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001724
Rafael Espindola1cee7102012-07-25 13:41:10 +00001725 const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
1726 if (!Flags.isSRet())
1727 return NotStructReturn;
1728 if (Flags.isInReg())
1729 return RegStructReturn;
1730 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001731}
1732
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001733/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001734/// return semantics.
Rafael Espindola1cee7102012-07-25 13:41:10 +00001735static StructReturnType
1736argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001737 if (Ins.empty())
Rafael Espindola1cee7102012-07-25 13:41:10 +00001738 return NotStructReturn;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001739
Rafael Espindola1cee7102012-07-25 13:41:10 +00001740 const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
1741 if (!Flags.isSRet())
1742 return NotStructReturn;
1743 if (Flags.isInReg())
1744 return RegStructReturn;
1745 return StackStructReturn;
Gordon Henriksen86737662008-01-05 16:56:59 +00001746}
1747
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001748/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1749/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001750/// the specific parameter attribute. The copy will be passed as a byval
1751/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001752static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001753CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001754 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1755 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001756 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001757
Dale Johannesendd64c412009-02-04 00:33:20 +00001758 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Stuart Hastings03d58262011-03-10 00:25:53 +00001759 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001760 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001761}
1762
Chris Lattner29689432010-03-11 00:22:57 +00001763/// IsTailCallConvention - Return true if the calling convention is one that
1764/// supports tail call optimization.
1765static bool IsTailCallConvention(CallingConv::ID CC) {
1766 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1767}
1768
Evan Cheng485fafc2011-03-21 01:19:09 +00001769bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Nick Lewycky22de16d2012-01-19 00:34:10 +00001770 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng485fafc2011-03-21 01:19:09 +00001771 return false;
1772
1773 CallSite CS(CI);
1774 CallingConv::ID CalleeCC = CS.getCallingConv();
1775 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1776 return false;
1777
1778 return true;
1779}
1780
Evan Cheng0c439eb2010-01-27 00:07:07 +00001781/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1782/// a tailcall target by changing its ABI.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001783static bool FuncIsMadeTailCallSafe(CallingConv::ID CC,
1784 bool GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00001785 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001786}
1787
Dan Gohman98ca4f22009-08-05 01:29:28 +00001788SDValue
1789X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001790 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001791 const SmallVectorImpl<ISD::InputArg> &Ins,
1792 DebugLoc dl, SelectionDAG &DAG,
1793 const CCValAssign &VA,
1794 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001795 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001796 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001797 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001798 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv,
1799 getTargetMachine().Options.GuaranteedTailCallOpt);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001800 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001801 EVT ValVT;
1802
1803 // If value is passed by pointer we have address passed instead of the value
1804 // itself.
1805 if (VA.getLocInfo() == CCValAssign::Indirect)
1806 ValVT = VA.getLocVT();
1807 else
1808 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001809
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001810 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001811 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001812 // In case of tail call optimization mark all arguments mutable. Since they
1813 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001814 if (Flags.isByVal()) {
Evan Chengee2e0e32011-03-30 23:44:13 +00001815 unsigned Bytes = Flags.getByValSize();
1816 if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
1817 int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001818 return DAG.getFrameIndex(FI, getPointerTy());
1819 } else {
1820 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001821 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001822 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1823 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001824 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001825 false, false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001826 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001827}
1828
Dan Gohman475871a2008-07-27 21:46:04 +00001829SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001830X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001831 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001832 bool isVarArg,
1833 const SmallVectorImpl<ISD::InputArg> &Ins,
1834 DebugLoc dl,
1835 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001836 SmallVectorImpl<SDValue> &InVals)
1837 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001838 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001839 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001840
Gordon Henriksen86737662008-01-05 16:56:59 +00001841 const Function* Fn = MF.getFunction();
1842 if (Fn->hasExternalLinkage() &&
1843 Subtarget->isTargetCygMing() &&
1844 Fn->getName() == "main")
1845 FuncInfo->setForceFramePointer(true);
1846
Evan Cheng1bc78042006-04-26 01:20:17 +00001847 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001848 bool Is64Bit = Subtarget->is64Bit();
Eli Friedman9a2478a2012-01-20 00:05:46 +00001849 bool IsWindows = Subtarget->isTargetWindows();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001850 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001851
Chris Lattner29689432010-03-11 00:22:57 +00001852 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1853 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001854
Chris Lattner638402b2007-02-28 07:00:42 +00001855 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001856 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001857 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001858 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00001859
1860 // Allocate shadow area for Win64
1861 if (IsWin64) {
1862 CCInfo.AllocateStack(32, 8);
1863 }
1864
Duncan Sands45907662010-10-31 13:21:44 +00001865 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001866
Chris Lattnerf39f7712007-02-28 05:46:49 +00001867 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001868 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001869 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1870 CCValAssign &VA = ArgLocs[i];
1871 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1872 // places.
1873 assert(VA.getValNo() != LastVal &&
1874 "Don't support value assigned to multiple locs yet");
Duncan Sands17001ce2011-10-18 12:44:00 +00001875 (void)LastVal;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001876 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001877
Chris Lattnerf39f7712007-02-28 05:46:49 +00001878 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001879 EVT RegVT = VA.getLocVT();
Craig Topper44d23822012-02-22 05:59:10 +00001880 const TargetRegisterClass *RC;
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 if (RegVT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00001882 RC = &X86::GR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 else if (Is64Bit && RegVT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00001884 RC = &X86::GR64RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001885 else if (RegVT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +00001886 RC = &X86::FR32RegClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001887 else if (RegVT == MVT::f64)
Craig Topperc9099502012-04-20 06:31:50 +00001888 RC = &X86::FR64RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001889 else if (RegVT.is256BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001890 RC = &X86::VR256RegClass;
Craig Topper7a9a28b2012-08-12 02:23:29 +00001891 else if (RegVT.is128BitVector())
Craig Topperc9099502012-04-20 06:31:50 +00001892 RC = &X86::VR128RegClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001893 else if (RegVT == MVT::x86mmx)
Craig Topperc9099502012-04-20 06:31:50 +00001894 RC = &X86::VR64RegClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001895 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001896 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001897
Devang Patel68e6bee2011-02-21 23:21:26 +00001898 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001899 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001900
Chris Lattnerf39f7712007-02-28 05:46:49 +00001901 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1902 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1903 // right size.
1904 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001905 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001906 DAG.getValueType(VA.getValVT()));
1907 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001908 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001909 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001910 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001911 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001912
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001913 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001914 // Handle MMX values passed in XMM regs.
1915 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001916 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1917 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001918 } else
1919 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001920 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001921 } else {
1922 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001923 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001924 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001925
1926 // If value is passed via pointer - do a load.
1927 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001928 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001929 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001930
Dan Gohman98ca4f22009-08-05 01:29:28 +00001931 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001932 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001933
Dan Gohman61a92132008-04-21 23:59:07 +00001934 // The x86-64 ABI for returning structs by value requires that we copy
1935 // the sret argument into %rax for the return. Save the argument into
1936 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001937 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001938 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1939 unsigned Reg = FuncInfo->getSRetReturnReg();
1940 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001941 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001942 FuncInfo->setSRetReturnReg(Reg);
1943 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001944 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001946 }
1947
Chris Lattnerf39f7712007-02-28 05:46:49 +00001948 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001949 // Align stack specially for tail calls.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001950 if (FuncIsMadeTailCallSafe(CallConv,
1951 MF.getTarget().Options.GuaranteedTailCallOpt))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001952 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001953
Evan Cheng1bc78042006-04-26 01:20:17 +00001954 // If the function takes variable number of arguments, make a frame index for
1955 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001956 if (isVarArg) {
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00001957 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1958 CallConv != CallingConv::X86_ThisCall)) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001959 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001960 }
1961 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001962 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1963
1964 // FIXME: We should really autogenerate these arrays
Craig Topperc5eaae42012-03-11 07:57:25 +00001965 static const uint16_t GPR64ArgRegsWin64[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001966 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001967 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001968 static const uint16_t GPR64ArgRegs64Bit[] = {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001969 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1970 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001971 static const uint16_t XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001972 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1973 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1974 };
Craig Topperc5eaae42012-03-11 07:57:25 +00001975 const uint16_t *GPR64ArgRegs;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001976 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001977
1978 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001979 // The XMM registers which might contain var arg parameters are shadowed
1980 // in their paired GPR. So we only need to save the GPR to their home
1981 // slots.
1982 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001983 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001984 } else {
1985 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1986 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001987
Chad Rosier30450e82011-12-22 22:35:21 +00001988 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit,
1989 TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001990 }
1991 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1992 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001993
Devang Patel578efa92009-06-05 21:57:13 +00001994 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Craig Topper1accb7e2012-01-10 06:54:16 +00001995 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001996 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001997 assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat &&
1998 NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001999 "SSE register cannot be used when SSE is disabled!");
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002000 if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps ||
Craig Topper1accb7e2012-01-10 06:54:16 +00002001 !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00002002 // Kernel mode asks for SSE to be disabled, so don't push them
2003 // on the stack.
2004 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00002005
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002006 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002007 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002008 // Get to the caller-allocated home save location. Add 8 to account
2009 // for the return address.
2010 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002011 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00002012 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
NAKAMURA Takumi3ca99432011-03-09 11:33:15 +00002013 // Fixup to set vararg frame on shadow area (4 x i64).
2014 if (NumIntRegs < 4)
2015 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002016 } else {
2017 // For X86-64, if there are vararg parameters that are passed via
Chad Rosier30450e82011-12-22 22:35:21 +00002018 // registers, then we must store them to their spots on the stack so
2019 // they may be loaded by deferencing the result of va_next.
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002020 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2021 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
2022 FuncInfo->setRegSaveFrameIndex(
2023 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00002024 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002025 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002026
Gordon Henriksen86737662008-01-05 16:56:59 +00002027 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002028 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00002029 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2030 getPointerTy());
2031 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002032 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00002033 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
2034 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00002035 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002036 &X86::GR64RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002037 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00002038 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00002039 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002040 MachinePointerInfo::getFixedStack(
2041 FuncInfo->getRegSaveFrameIndex(), Offset),
2042 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00002043 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002044 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00002045 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002046
Dan Gohmanface41a2009-08-16 21:24:25 +00002047 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
2048 // Now store the XMM (fp + vector) parameter registers.
2049 SmallVector<SDValue, 11> SaveXMMOps;
2050 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002051
Craig Topperc9099502012-04-20 06:31:50 +00002052 unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002053 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
2054 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00002055
Dan Gohman1e93df62010-04-17 14:41:14 +00002056 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2057 FuncInfo->getRegSaveFrameIndex()));
2058 SaveXMMOps.push_back(DAG.getIntPtrConstant(
2059 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00002060
Dan Gohmanface41a2009-08-16 21:24:25 +00002061 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00002062 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Craig Topperc9099502012-04-20 06:31:50 +00002063 &X86::VR128RegClass);
Dan Gohmanface41a2009-08-16 21:24:25 +00002064 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
2065 SaveXMMOps.push_back(Val);
2066 }
2067 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2068 MVT::Other,
2069 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00002070 }
Dan Gohmanface41a2009-08-16 21:24:25 +00002071
2072 if (!MemOps.empty())
2073 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2074 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002075 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002076 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002077
Gordon Henriksen86737662008-01-05 16:56:59 +00002078 // Some CCs need callee pop.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002079 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2080 MF.getTarget().Options.GuaranteedTailCallOpt)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002081 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002082 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00002083 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00002084 // If this is an sret function, the return should pop the hidden pointer.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002085 if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002086 argsAreStructReturn(Ins) == StackStructReturn)
Dan Gohman1e93df62010-04-17 14:41:14 +00002087 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002088 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002089
Gordon Henriksen86737662008-01-05 16:56:59 +00002090 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00002091 // RegSaveFrameIndex is X86-64 only.
2092 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00002093 if (CallConv == CallingConv::X86_FastCall ||
2094 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00002095 // fastcc functions can't have varargs.
2096 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00002097 }
Evan Cheng25caf632006-05-23 21:06:34 +00002098
Rafael Espindola76927d752011-08-30 19:39:58 +00002099 FuncInfo->setArgumentStackSize(StackSize);
2100
Dan Gohman98ca4f22009-08-05 01:29:28 +00002101 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002102}
2103
Dan Gohman475871a2008-07-27 21:46:04 +00002104SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00002105X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2106 SDValue StackPtr, SDValue Arg,
2107 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00002108 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00002109 ISD::ArgFlagsTy Flags) const {
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002110 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00002111 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00002112 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002113 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00002114 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00002115
2116 return DAG.getStore(Chain, dl, Arg, PtrOff,
2117 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00002118 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00002119}
2120
Bill Wendling64e87322009-01-16 19:25:27 +00002121/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002122/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00002123SDValue
2124X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00002125 SDValue &OutRetAddr, SDValue Chain,
2126 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00002127 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002128 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00002129 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002130 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00002131
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002132 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00002133 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002134 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002135 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002136}
2137
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002138/// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002139/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00002140static SDValue
2141EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002142 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00002143 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002144 // Store the return address to the appropriate stack slot.
2145 if (!FPDiff) return Chain;
2146 // Calculate the new stack slot for the return address.
2147 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002148 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00002149 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00002150 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002151 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002152 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00002153 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00002154 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002155 return Chain;
2156}
2157
Dan Gohman98ca4f22009-08-05 01:29:28 +00002158SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002159X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002160 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002161 SelectionDAG &DAG = CLI.DAG;
2162 DebugLoc &dl = CLI.DL;
2163 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
2164 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
2165 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
2166 SDValue Chain = CLI.Chain;
2167 SDValue Callee = CLI.Callee;
2168 CallingConv::ID CallConv = CLI.CallConv;
2169 bool &isTailCall = CLI.IsTailCall;
2170 bool isVarArg = CLI.IsVarArg;
2171
Dan Gohman98ca4f22009-08-05 01:29:28 +00002172 MachineFunction &MF = DAG.getMachineFunction();
2173 bool Is64Bit = Subtarget->is64Bit();
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002174 bool IsWin64 = Subtarget->isTargetWin64();
Eli Friedman9a2478a2012-01-20 00:05:46 +00002175 bool IsWindows = Subtarget->isTargetWindows();
Rafael Espindola1cee7102012-07-25 13:41:10 +00002176 StructReturnType SR = callIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00002177 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002178
Nick Lewycky22de16d2012-01-19 00:34:10 +00002179 if (MF.getTarget().Options.DisableTailCalls)
2180 isTailCall = false;
2181
Evan Cheng5f941932010-02-05 02:21:12 +00002182 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002183 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00002184 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Rafael Espindola1cee7102012-07-25 13:41:10 +00002185 isVarArg, SR != NotStructReturn,
2186 MF.getFunction()->hasStructRetAttr(),
2187 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00002188
2189 // Sibcalls are automatically detected tailcalls which do not require
2190 // ABI changes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002191 if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00002192 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00002193
2194 if (isTailCall)
2195 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00002196 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00002197
Chris Lattner29689432010-03-11 00:22:57 +00002198 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
2199 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00002200
Chris Lattner638402b2007-02-28 07:00:42 +00002201 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00002202 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002203 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002204 ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002205
2206 // Allocate shadow area for Win64
2207 if (IsWin64) {
2208 CCInfo.AllocateStack(32, 8);
2209 }
2210
Duncan Sands45907662010-10-31 13:21:44 +00002211 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00002212
Chris Lattner423c5f42007-02-28 05:31:48 +00002213 // Get a count of how many bytes are to be pushed on the stack.
2214 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00002215 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00002216 // This is a sibcall. The memory operands are available in caller's
2217 // own caller's stack.
2218 NumBytes = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002219 else if (getTargetMachine().Options.GuaranteedTailCallOpt &&
2220 IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00002221 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002222
Gordon Henriksen86737662008-01-05 16:56:59 +00002223 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00002224 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002225 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00002226 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00002227 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
2228 FPDiff = NumBytesCallerPushed - NumBytes;
2229
2230 // Set the delta of movement of the returnaddr stackslot.
2231 // But only set if delta is greater than previous delta.
2232 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
2233 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
2234 }
2235
Evan Chengf22f9b32010-02-06 03:28:46 +00002236 if (!IsSibcall)
2237 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002238
Dan Gohman475871a2008-07-27 21:46:04 +00002239 SDValue RetAddrFrIdx;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002240 // Load return address for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00002241 if (isTailCall && FPDiff)
2242 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
2243 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002244
Dan Gohman475871a2008-07-27 21:46:04 +00002245 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2246 SmallVector<SDValue, 8> MemOpChains;
2247 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00002248
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002249 // Walk the register/memloc assignments, inserting copies/loads. In the case
2250 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00002251 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2252 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002253 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00002254 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002255 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00002256 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00002257
Chris Lattner423c5f42007-02-28 05:31:48 +00002258 // Promote the value if needed.
2259 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002260 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00002261 case CCValAssign::Full: break;
2262 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002263 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002264 break;
2265 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002266 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002267 break;
2268 case CCValAssign::AExt:
Craig Topper7a9a28b2012-08-12 02:23:29 +00002269 if (RegVT.is128BitVector()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002270 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002271 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002272 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
2273 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00002274 } else
2275 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
2276 break;
2277 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002278 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00002279 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002280 case CCValAssign::Indirect: {
2281 // Store the argument.
2282 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00002283 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002284 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00002285 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002286 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00002287 Arg = SpillSlot;
2288 break;
2289 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00002290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002291
Chris Lattner423c5f42007-02-28 05:31:48 +00002292 if (VA.isRegLoc()) {
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002293 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2294 if (isVarArg && IsWin64) {
2295 // Win64 ABI requires argument XMM reg to be copied to the corresponding
2296 // shadow reg if callee is a varargs function.
2297 unsigned ShadowReg = 0;
2298 switch (VA.getLocReg()) {
2299 case X86::XMM0: ShadowReg = X86::RCX; break;
2300 case X86::XMM1: ShadowReg = X86::RDX; break;
2301 case X86::XMM2: ShadowReg = X86::R8; break;
2302 case X86::XMM3: ShadowReg = X86::R9; break;
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002303 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002304 if (ShadowReg)
2305 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00002306 }
Evan Chengf22f9b32010-02-06 03:28:46 +00002307 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00002308 assert(VA.isMemLoc());
2309 if (StackPtr.getNode() == 0)
2310 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
2311 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
2312 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002313 }
Stuart Hastings2aa0f232011-05-26 04:09:49 +00002314 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002315
Evan Cheng32fe1032006-05-25 00:59:30 +00002316 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002317 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002318 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002319
Chris Lattner88e1fd52009-07-09 04:24:46 +00002320 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002321 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2322 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002323 if (!isTailCall) {
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002324 RegsToPass.push_back(std::make_pair(unsigned(X86::EBX),
2325 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy())));
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002326 } else {
2327 // If we are tail calling and generating PIC/GOT style code load the
2328 // address of the callee into ECX. The value in ecx is used as target of
2329 // the tail jump. This is done to circumvent the ebx/callee-saved problem
2330 // for tail calls on PIC/GOT architectures. Normally we would just put the
2331 // address of GOT into ebx and then call target@PLT. But for tail calls
2332 // ebx would be restored (since ebx is callee saved) before jumping to the
2333 // target@PLT.
2334
2335 // Note: The actual moving to ECX is done further down.
2336 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2337 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2338 !G->getGlobal()->hasProtectedVisibility())
2339 Callee = LowerGlobalAddress(Callee, DAG);
2340 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002341 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002342 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002343 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002344
NAKAMURA Takumifb840c92011-02-05 15:11:13 +00002345 if (Is64Bit && isVarArg && !IsWin64) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002346 // From AMD64 ABI document:
2347 // For calls that may call functions that use varargs or stdargs
2348 // (prototype-less calls or calls to functions containing ellipsis (...) in
2349 // the declaration) %al is used as hidden argument to specify the number
2350 // of SSE registers used. The contents of %al do not need to match exactly
2351 // the number of registers, but must be an ubound on the number of SSE
2352 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002353
Gordon Henriksen86737662008-01-05 16:56:59 +00002354 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002355 static const uint16_t XMMArgRegs[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00002356 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2357 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2358 };
2359 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Craig Topper1accb7e2012-01-10 06:54:16 +00002360 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002361 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002362
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002363 RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
2364 DAG.getConstant(NumXMMRegs, MVT::i8)));
Gordon Henriksen86737662008-01-05 16:56:59 +00002365 }
2366
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002367 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002368 if (isTailCall) {
2369 // Force all the incoming stack arguments to be loaded from the stack
2370 // before any new outgoing arguments are stored to the stack, because the
2371 // outgoing stack slots may alias the incoming argument stack slots, and
2372 // the alias isn't otherwise explicit. This is slightly more conservative
2373 // than necessary, because it means that each store effectively depends
2374 // on every argument instead of just those arguments it would clobber.
2375 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2376
Dan Gohman475871a2008-07-27 21:46:04 +00002377 SmallVector<SDValue, 8> MemOpChains2;
2378 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002379 int FI = 0;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002380 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002381 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2382 CCValAssign &VA = ArgLocs[i];
2383 if (VA.isRegLoc())
2384 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002385 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002386 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002387 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002388 // Create frame index.
2389 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002390 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002391 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002392 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002393
Duncan Sands276dcbd2008-03-21 09:14:45 +00002394 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002395 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002396 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002397 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002398 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002399 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002400 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002401
Dan Gohman98ca4f22009-08-05 01:29:28 +00002402 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2403 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002404 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002405 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002406 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002407 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002408 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002409 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002410 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002411 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002412 }
2413 }
2414
2415 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002416 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002417 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002418
2419 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002420 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002421 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002422 }
2423
Jakob Stoklund Olesenb8720782012-07-04 19:28:31 +00002424 // Build a sequence of copy-to-reg nodes chained together with token chain
2425 // and flag operands which copy the outgoing args into registers.
2426 SDValue InFlag;
2427 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2428 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2429 RegsToPass[i].second, InFlag);
2430 InFlag = Chain.getValue(1);
2431 }
2432
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002433 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2434 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2435 // In the 64-bit large code model, we have to make all calls
2436 // through a register, since the call instruction's 32-bit
2437 // pc-relative offset may not be large enough to hold the whole
2438 // address.
2439 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002440 // If the callee is a GlobalAddress node (quite common, every direct call
2441 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2442 // it.
2443
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002444 // We should use extra load for direct calls to dllimported functions in
2445 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002446 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002447 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002448 unsigned char OpFlags = 0;
John McCall3a3465b2011-06-15 20:36:13 +00002449 bool ExtraLoad = false;
2450 unsigned WrapperKind = ISD::DELETED_NODE;
Eric Christopherfd179292009-08-27 18:07:15 +00002451
Chris Lattner48a7d022009-07-09 05:02:21 +00002452 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2453 // external symbols most go through the PLT in PIC mode. If the symbol
2454 // has hidden or protected visibility, or if it is static or local, then
2455 // we don't need to use the PLT - we can directly call it.
2456 if (Subtarget->isTargetELF() &&
2457 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002458 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002459 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002460 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002461 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002462 (!Subtarget->getTargetTriple().isMacOSX() ||
2463 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002464 // PC-relative references to external symbols should go through $stub,
2465 // unless we're building with the leopard linker or later, which
2466 // automatically synthesizes these stubs.
2467 OpFlags = X86II::MO_DARWIN_STUB;
John McCall3a3465b2011-06-15 20:36:13 +00002468 } else if (Subtarget->isPICStyleRIPRel() &&
2469 isa<Function>(GV) &&
2470 cast<Function>(GV)->hasFnAttr(Attribute::NonLazyBind)) {
2471 // If the function is marked as non-lazy, generate an indirect call
2472 // which loads from the GOT directly. This avoids runtime overhead
2473 // at the cost of eager binding (and one extra byte of encoding).
2474 OpFlags = X86II::MO_GOTPCREL;
2475 WrapperKind = X86ISD::WrapperRIP;
2476 ExtraLoad = true;
Chris Lattner74e726e2009-07-09 05:27:35 +00002477 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002478
Devang Patel0d881da2010-07-06 22:08:15 +00002479 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002480 G->getOffset(), OpFlags);
John McCall3a3465b2011-06-15 20:36:13 +00002481
2482 // Add a wrapper if needed.
2483 if (WrapperKind != ISD::DELETED_NODE)
2484 Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee);
2485 // Add extra indirection if needed.
2486 if (ExtraLoad)
2487 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
2488 MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002489 false, false, false, 0);
Chris Lattner48a7d022009-07-09 05:02:21 +00002490 }
Bill Wendling056292f2008-09-16 21:48:12 +00002491 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002492 unsigned char OpFlags = 0;
2493
Evan Cheng1bf891a2010-12-01 22:59:46 +00002494 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2495 // external symbols should go through the PLT.
2496 if (Subtarget->isTargetELF() &&
2497 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2498 OpFlags = X86II::MO_PLT;
2499 } else if (Subtarget->isPICStyleStubAny() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002500 (!Subtarget->getTargetTriple().isMacOSX() ||
2501 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Evan Cheng1bf891a2010-12-01 22:59:46 +00002502 // PC-relative references to external symbols should go through $stub,
2503 // unless we're building with the leopard linker or later, which
2504 // automatically synthesizes these stubs.
2505 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002506 }
Eric Christopherfd179292009-08-27 18:07:15 +00002507
Chris Lattner48a7d022009-07-09 05:02:21 +00002508 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2509 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002510 }
2511
Chris Lattnerd96d0722007-02-25 06:40:16 +00002512 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002513 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002514 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002515
Evan Chengf22f9b32010-02-06 03:28:46 +00002516 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002517 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2518 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002519 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002520 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002521
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002522 Ops.push_back(Chain);
2523 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002524
Dan Gohman98ca4f22009-08-05 01:29:28 +00002525 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002526 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002527
Gordon Henriksen86737662008-01-05 16:56:59 +00002528 // Add argument registers to the end of the list so that they are known live
2529 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002530 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2531 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2532 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002533
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002534 // Add a register mask operand representing the call-preserved registers.
2535 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2536 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
2537 assert(Mask && "Missing call preserved mask for calling convention");
2538 Ops.push_back(DAG.getRegisterMask(Mask));
Jakob Stoklund Olesenc38c4562012-01-18 23:52:22 +00002539
Gabor Greifba36cb52008-08-28 21:40:38 +00002540 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002541 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002542
Dan Gohman98ca4f22009-08-05 01:29:28 +00002543 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002544 // We used to do:
2545 //// If this is the first return lowered for this function, add the regs
2546 //// to the liveout set for the function.
2547 // This isn't right, although it's probably harmless on x86; liveouts
2548 // should be computed from returns not tail calls. Consider a void
2549 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002550 return DAG.getNode(X86ISD::TC_RETURN, dl,
2551 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002552 }
2553
Dale Johannesenace16102009-02-03 19:33:06 +00002554 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002555 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002556
Chris Lattner2d297092006-05-23 18:50:38 +00002557 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002558 unsigned NumBytesForCalleeToPush;
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002559 if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2560 getTargetMachine().Options.GuaranteedTailCallOpt))
Gordon Henriksen86737662008-01-05 16:56:59 +00002561 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Eli Friedman9a2478a2012-01-20 00:05:46 +00002562 else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows &&
Rafael Espindola1cee7102012-07-25 13:41:10 +00002563 SR == StackStructReturn)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002564 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002565 // pops the hidden struct pointer, so we have to push it back.
2566 // This is common for Darwin/X86, Linux & Mingw32 targets.
Eli Friedman9a2478a2012-01-20 00:05:46 +00002567 // For MSVC Win32 targets, the caller pops the hidden struct pointer.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002568 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002569 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002570 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002571
Gordon Henriksenae636f82008-01-03 16:47:34 +00002572 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002573 if (!IsSibcall) {
2574 Chain = DAG.getCALLSEQ_END(Chain,
2575 DAG.getIntPtrConstant(NumBytes, true),
2576 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2577 true),
2578 InFlag);
2579 InFlag = Chain.getValue(1);
2580 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002581
Chris Lattner3085e152007-02-25 08:59:22 +00002582 // Handle result values, copying them out of physregs into vregs that we
2583 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002584 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2585 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002586}
2587
Evan Cheng25ab6902006-09-08 06:48:29 +00002588
2589//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002590// Fast Calling Convention (tail call) implementation
2591//===----------------------------------------------------------------------===//
2592
2593// Like std call, callee cleans arguments, convention except that ECX is
2594// reserved for storing the tail called function address. Only 2 registers are
2595// free for argument passing (inreg). Tail call optimization is performed
2596// provided:
2597// * tailcallopt is enabled
2598// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002599// On X86_64 architecture with GOT-style position independent code only local
2600// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002601// To keep the stack aligned according to platform abi the function
2602// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2603// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002604// If a tail called function callee has more arguments than the caller the
2605// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002606// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002607// original REtADDR, but before the saved framepointer or the spilled registers
2608// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2609// stack layout:
2610// arg1
2611// arg2
2612// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002613// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002614// move area ]
2615// (possible EBP)
2616// ESI
2617// EDI
2618// local1 ..
2619
2620/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2621/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002622unsigned
2623X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2624 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002625 MachineFunction &MF = DAG.getMachineFunction();
2626 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002627 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002628 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002629 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002630 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002631 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002632 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2633 // Number smaller than 12 so just add the difference.
2634 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2635 } else {
2636 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002637 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002638 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002639 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002640 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002641}
2642
Evan Cheng5f941932010-02-05 02:21:12 +00002643/// MatchingStackOffset - Return true if the given stack call argument is
2644/// already available in the same position (relatively) of the caller's
2645/// incoming argument stack.
2646static
2647bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2648 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2649 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002650 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2651 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002652 if (Arg.getOpcode() == ISD::CopyFromReg) {
2653 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002654 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002655 return false;
2656 MachineInstr *Def = MRI->getVRegDef(VR);
2657 if (!Def)
2658 return false;
2659 if (!Flags.isByVal()) {
2660 if (!TII->isLoadFromStackSlot(Def, FI))
2661 return false;
2662 } else {
2663 unsigned Opcode = Def->getOpcode();
2664 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2665 Def->getOperand(1).isFI()) {
2666 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002667 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002668 } else
2669 return false;
2670 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002671 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2672 if (Flags.isByVal())
2673 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002674 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002675 // define @foo(%struct.X* %A) {
2676 // tail call @bar(%struct.X* byval %A)
2677 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002678 return false;
2679 SDValue Ptr = Ld->getBasePtr();
2680 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2681 if (!FINode)
2682 return false;
2683 FI = FINode->getIndex();
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002684 } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
Chad Rosier14d71aa2011-06-25 18:51:28 +00002685 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
Chad Rosierdf78fcd2011-06-25 02:04:56 +00002686 FI = FINode->getIndex();
2687 Bytes = Flags.getByValSize();
Evan Cheng4cae1332010-03-05 08:38:04 +00002688 } else
2689 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002690
Evan Cheng4cae1332010-03-05 08:38:04 +00002691 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002692 if (!MFI->isFixedObjectIndex(FI))
2693 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002694 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002695}
2696
Dan Gohman98ca4f22009-08-05 01:29:28 +00002697/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2698/// for tail call optimization. Targets which want to do tail call
2699/// optimization should implement this function.
2700bool
2701X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002702 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002703 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002704 bool isCalleeStructRet,
2705 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002706 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002707 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002708 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002709 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002710 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002711 CalleeCC != CallingConv::C)
2712 return false;
2713
Evan Cheng7096ae42010-01-29 06:45:59 +00002714 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002715 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002716 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002717 CallingConv::ID CallerCC = CallerF->getCallingConv();
2718 bool CCMatch = CallerCC == CalleeCC;
2719
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002720 if (getTargetMachine().Options.GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002721 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002722 return true;
2723 return false;
2724 }
2725
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002726 // Look for obvious safe cases to perform tail call optimization that do not
2727 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002728
Evan Cheng2c12cb42010-03-26 16:26:03 +00002729 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2730 // emit a special epilogue.
2731 if (RegInfo->needsStackRealignment(MF))
2732 return false;
2733
Evan Chenga375d472010-03-15 18:54:48 +00002734 // Also avoid sibcall optimization if either caller or callee uses struct
2735 // return semantics.
2736 if (isCalleeStructRet || isCallerStructRet)
2737 return false;
2738
Chad Rosier2416da32011-06-24 21:15:36 +00002739 // An stdcall caller is expected to clean up its arguments; the callee
2740 // isn't going to do that.
2741 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2742 return false;
2743
Chad Rosier871f6642011-05-18 19:59:50 +00002744 // Do not sibcall optimize vararg calls unless all arguments are passed via
Chad Rosiera1660892011-05-20 00:59:28 +00002745 // registers.
Chad Rosier871f6642011-05-18 19:59:50 +00002746 if (isVarArg && !Outs.empty()) {
Chad Rosiera1660892011-05-20 00:59:28 +00002747
2748 // Optimizing for varargs on Win64 is unlikely to be safe without
2749 // additional testing.
2750 if (Subtarget->isTargetWin64())
2751 return false;
2752
Chad Rosier871f6642011-05-18 19:59:50 +00002753 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002754 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002755 getTargetMachine(), ArgLocs, *DAG.getContext());
Chad Rosier871f6642011-05-18 19:59:50 +00002756
Chad Rosier871f6642011-05-18 19:59:50 +00002757 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
2758 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
2759 if (!ArgLocs[i].isRegLoc())
2760 return false;
2761 }
2762
Chad Rosier30450e82011-12-22 22:35:21 +00002763 // If the call result is in ST0 / ST1, it needs to be popped off the x87
2764 // stack. Therefore, if it's not used by the call it is not safe to optimize
2765 // this into a sibcall.
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002766 bool Unused = false;
2767 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2768 if (!Ins[i].Used) {
2769 Unused = true;
2770 break;
2771 }
2772 }
2773 if (Unused) {
2774 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002775 CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002776 getTargetMachine(), RVLocs, *DAG.getContext());
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002777 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002778 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002779 CCValAssign &VA = RVLocs[i];
2780 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2781 return false;
2782 }
2783 }
2784
Evan Cheng13617962010-04-30 01:12:32 +00002785 // If the calling conventions do not match, then we'd better make sure the
2786 // results are returned in the same way as what the caller expects.
2787 if (!CCMatch) {
2788 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopher471e4222011-06-08 23:55:35 +00002789 CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002790 getTargetMachine(), RVLocs1, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002791 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2792
2793 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopher471e4222011-06-08 23:55:35 +00002794 CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002795 getTargetMachine(), RVLocs2, *DAG.getContext());
Evan Cheng13617962010-04-30 01:12:32 +00002796 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2797
2798 if (RVLocs1.size() != RVLocs2.size())
2799 return false;
2800 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2801 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2802 return false;
2803 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2804 return false;
2805 if (RVLocs1[i].isRegLoc()) {
2806 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2807 return false;
2808 } else {
2809 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2810 return false;
2811 }
2812 }
2813 }
2814
Evan Chenga6bff982010-01-30 01:22:00 +00002815 // If the callee takes no arguments then go on to check the results of the
2816 // call.
2817 if (!Outs.empty()) {
2818 // Check if stack adjustment is needed. For now, do not do this if any
2819 // argument is passed on the stack.
2820 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002821 CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
Craig Topper0fbf3642012-04-23 03:28:34 +00002822 getTargetMachine(), ArgLocs, *DAG.getContext());
NAKAMURA Takumi3f4be4f2011-02-05 15:11:32 +00002823
2824 // Allocate shadow area for Win64
2825 if (Subtarget->isTargetWin64()) {
2826 CCInfo.AllocateStack(32, 8);
2827 }
2828
Duncan Sands45907662010-10-31 13:21:44 +00002829 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Stuart Hastings6db2c2f2011-05-17 16:59:46 +00002830 if (CCInfo.getNextStackOffset()) {
Evan Chengb2c92902010-02-02 02:22:50 +00002831 MachineFunction &MF = DAG.getMachineFunction();
2832 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2833 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002834
2835 // Check if the arguments are already laid out in the right way as
2836 // the caller's fixed stack objects.
2837 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002838 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2839 const X86InstrInfo *TII =
Roman Divacky59324292012-09-05 22:26:57 +00002840 ((const X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002841 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2842 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002843 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002844 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002845 if (VA.getLocInfo() == CCValAssign::Indirect)
2846 return false;
2847 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002848 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2849 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002850 return false;
2851 }
2852 }
2853 }
Evan Cheng9c044672010-05-29 01:35:22 +00002854
2855 // If the tailcall address may be in a register, then make sure it's
2856 // possible to register allocate for it. In 32-bit, the call address can
2857 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002858 // callee-saved registers are restored. These happen to be the same
2859 // registers used to pass 'inreg' arguments so watch out for those.
2860 if (!Subtarget->is64Bit() &&
2861 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002862 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002863 unsigned NumInRegs = 0;
2864 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2865 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002866 if (!VA.isRegLoc())
2867 continue;
2868 unsigned Reg = VA.getLocReg();
2869 switch (Reg) {
2870 default: break;
2871 case X86::EAX: case X86::EDX: case X86::ECX:
2872 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002873 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002874 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002875 }
2876 }
2877 }
Evan Chenga6bff982010-01-30 01:22:00 +00002878 }
Evan Chengb1712452010-01-27 06:25:16 +00002879
Evan Cheng86809cc2010-02-03 03:28:02 +00002880 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002881}
2882
Dan Gohman3df24e62008-09-03 23:12:08 +00002883FastISel *
Bob Wilsond49edb72012-08-03 04:06:28 +00002884X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
2885 const TargetLibraryInfo *libInfo) const {
2886 return X86::createFastISel(funcInfo, libInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002887}
2888
2889
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002890//===----------------------------------------------------------------------===//
2891// Other Lowering Hooks
2892//===----------------------------------------------------------------------===//
2893
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002894static bool MayFoldLoad(SDValue Op) {
2895 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2896}
2897
2898static bool MayFoldIntoStore(SDValue Op) {
2899 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2900}
2901
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002902static bool isTargetShuffle(unsigned Opcode) {
2903 switch(Opcode) {
2904 default: return false;
2905 case X86ISD::PSHUFD:
2906 case X86ISD::PSHUFHW:
2907 case X86ISD::PSHUFLW:
Craig Topperb3982da2011-12-31 23:50:21 +00002908 case X86ISD::SHUFP:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002909 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002910 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002911 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002912 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002913 case X86ISD::MOVLPS:
2914 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002915 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002916 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002917 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002918 case X86ISD::MOVSS:
2919 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002920 case X86ISD::UNPCKL:
2921 case X86ISD::UNPCKH:
Craig Topper316cd2a2011-11-30 06:25:25 +00002922 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +00002923 case X86ISD::VPERM2X128:
Craig Topperbdcbcb32012-05-06 18:54:26 +00002924 case X86ISD::VPERMI:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002925 return true;
2926 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002927}
2928
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002929static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002930 SDValue V1, SelectionDAG &DAG) {
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002931 switch(Opc) {
2932 default: llvm_unreachable("Unknown x86 shuffle node");
2933 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002934 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002935 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002936 return DAG.getNode(Opc, dl, VT, V1);
2937 }
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002938}
2939
2940static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002941 SDValue V1, unsigned TargetMask,
2942 SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002943 switch(Opc) {
2944 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002945 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002946 case X86ISD::PSHUFHW:
2947 case X86ISD::PSHUFLW:
Craig Topper316cd2a2011-11-30 06:25:25 +00002948 case X86ISD::VPERMILP:
Craig Topper8325c112012-04-16 00:41:45 +00002949 case X86ISD::VPERMI:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002950 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2951 }
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002952}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002953
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002954static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Craig Topper3d092db2012-03-21 02:14:01 +00002955 SDValue V1, SDValue V2, unsigned TargetMask,
2956 SelectionDAG &DAG) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002957 switch(Opc) {
2958 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002959 case X86ISD::PALIGN:
Craig Topperb3982da2011-12-31 23:50:21 +00002960 case X86ISD::SHUFP:
Craig Topperec24e612011-11-30 07:47:51 +00002961 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002962 return DAG.getNode(Opc, dl, VT, V1, V2,
2963 DAG.getConstant(TargetMask, MVT::i8));
2964 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002965}
2966
2967static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2968 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2969 switch(Opc) {
2970 default: llvm_unreachable("Unknown x86 shuffle node");
2971 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002972 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002973 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002974 case X86ISD::MOVLPS:
2975 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002976 case X86ISD::MOVSS:
2977 case X86ISD::MOVSD:
Craig Topper34671b82011-12-06 08:21:25 +00002978 case X86ISD::UNPCKL:
2979 case X86ISD::UNPCKH:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002980 return DAG.getNode(Opc, dl, VT, V1, V2);
2981 }
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002982}
2983
Dan Gohmand858e902010-04-17 15:26:15 +00002984SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002985 MachineFunction &MF = DAG.getMachineFunction();
2986 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2987 int ReturnAddrIndex = FuncInfo->getRAIndex();
2988
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002989 if (ReturnAddrIndex == 0) {
2990 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002991 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002992 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002993 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002994 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002995 }
2996
Evan Cheng25ab6902006-09-08 06:48:29 +00002997 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002998}
2999
3000
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003001bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3002 bool hasSymbolicDisplacement) {
3003 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00003004 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00003005 return false;
3006
3007 // If we don't have a symbolic displacement - we don't have any extra
3008 // restrictions.
3009 if (!hasSymbolicDisplacement)
3010 return true;
3011
3012 // FIXME: Some tweaks might be needed for medium code model.
3013 if (M != CodeModel::Small && M != CodeModel::Kernel)
3014 return false;
3015
3016 // For small code model we assume that latest object is 16MB before end of 31
3017 // bits boundary. We may also accept pretty large negative constants knowing
3018 // that all objects are in the positive half of address space.
3019 if (M == CodeModel::Small && Offset < 16*1024*1024)
3020 return true;
3021
3022 // For kernel code model we know that all object resist in the negative half
3023 // of 32bits address space. We may not accept negative offsets, since they may
3024 // be just off and we may accept pretty large positive ones.
3025 if (M == CodeModel::Kernel && Offset > 0)
3026 return true;
3027
3028 return false;
3029}
3030
Evan Chengef41ff62011-06-23 17:54:54 +00003031/// isCalleePop - Determines whether the callee is required to pop its
3032/// own arguments. Callee pop is necessary to support tail calls.
3033bool X86::isCalleePop(CallingConv::ID CallingConv,
3034 bool is64Bit, bool IsVarArg, bool TailCallOpt) {
3035 if (IsVarArg)
3036 return false;
3037
3038 switch (CallingConv) {
3039 default:
3040 return false;
3041 case CallingConv::X86_StdCall:
3042 return !is64Bit;
3043 case CallingConv::X86_FastCall:
3044 return !is64Bit;
3045 case CallingConv::X86_ThisCall:
3046 return !is64Bit;
3047 case CallingConv::Fast:
3048 return TailCallOpt;
3049 case CallingConv::GHC:
3050 return TailCallOpt;
3051 }
3052}
3053
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003054/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
3055/// specific condition code, returning the condition code and the LHS/RHS of the
3056/// comparison to make.
3057static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
3058 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00003059 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003060 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3061 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
3062 // X > -1 -> X == 0, jump !sign.
3063 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003064 return X86::COND_NS;
Craig Topper69947b92012-04-23 06:57:04 +00003065 }
3066 if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003067 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003068 return X86::COND_S;
Craig Topper69947b92012-04-23 06:57:04 +00003069 }
3070 if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00003071 // X < 1 -> X <= 0
3072 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003073 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00003074 }
Chris Lattnerf9570512006-09-13 03:22:10 +00003075 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003076
Evan Chengd9558e02006-01-06 00:43:03 +00003077 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003078 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003079 case ISD::SETEQ: return X86::COND_E;
3080 case ISD::SETGT: return X86::COND_G;
3081 case ISD::SETGE: return X86::COND_GE;
3082 case ISD::SETLT: return X86::COND_L;
3083 case ISD::SETLE: return X86::COND_LE;
3084 case ISD::SETNE: return X86::COND_NE;
3085 case ISD::SETULT: return X86::COND_B;
3086 case ISD::SETUGT: return X86::COND_A;
3087 case ISD::SETULE: return X86::COND_BE;
3088 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00003089 }
Chris Lattner4c78e022008-12-23 23:42:27 +00003090 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003091
Chris Lattner4c78e022008-12-23 23:42:27 +00003092 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00003093
Chris Lattner4c78e022008-12-23 23:42:27 +00003094 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00003095 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
3096 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00003097 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
3098 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00003099 }
3100
Chris Lattner4c78e022008-12-23 23:42:27 +00003101 switch (SetCCOpcode) {
3102 default: break;
3103 case ISD::SETOLT:
3104 case ISD::SETOLE:
3105 case ISD::SETUGT:
3106 case ISD::SETUGE:
3107 std::swap(LHS, RHS);
3108 break;
3109 }
3110
3111 // On a floating point condition, the flags are set as follows:
3112 // ZF PF CF op
3113 // 0 | 0 | 0 | X > Y
3114 // 0 | 0 | 1 | X < Y
3115 // 1 | 0 | 0 | X == Y
3116 // 1 | 1 | 1 | unordered
3117 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003118 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00003119 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003120 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00003121 case ISD::SETOLT: // flipped
3122 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003123 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00003124 case ISD::SETOLE: // flipped
3125 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003126 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003127 case ISD::SETUGT: // flipped
3128 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003129 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00003130 case ISD::SETUGE: // flipped
3131 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003132 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00003133 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00003134 case ISD::SETNE: return X86::COND_NE;
3135 case ISD::SETUO: return X86::COND_P;
3136 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00003137 case ISD::SETOEQ:
3138 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00003139 }
Evan Chengd9558e02006-01-06 00:43:03 +00003140}
3141
Evan Cheng4a460802006-01-11 00:33:36 +00003142/// hasFPCMov - is there a floating point cmov for the specific X86 condition
3143/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00003144/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00003145static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00003146 switch (X86CC) {
3147 default:
3148 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003149 case X86::COND_B:
3150 case X86::COND_BE:
3151 case X86::COND_E:
3152 case X86::COND_P:
3153 case X86::COND_A:
3154 case X86::COND_AE:
3155 case X86::COND_NE:
3156 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00003157 return true;
3158 }
3159}
3160
Evan Chengeb2f9692009-10-27 19:56:55 +00003161/// isFPImmLegal - Returns true if the target can instruction select the
3162/// specified FP immediate natively. If false, the legalizer will
3163/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003164bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00003165 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
3166 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
3167 return true;
3168 }
3169 return false;
3170}
3171
Nate Begeman9008ca62009-04-27 18:41:29 +00003172/// isUndefOrInRange - Return true if Val is undef or if its value falls within
3173/// the specified range (L, H].
3174static bool isUndefOrInRange(int Val, int Low, int Hi) {
3175 return (Val < 0) || (Val >= Low && Val < Hi);
3176}
3177
3178/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
3179/// specified value.
3180static bool isUndefOrEqual(int Val, int CmpVal) {
3181 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003182 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00003184}
3185
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00003186/// isSequentialOrUndefInRange - Return true if every element in Mask, beginning
Bruno Cardoso Lopes4002d7e2011-08-12 21:54:42 +00003187/// from position Pos and ending in Pos+Size, falls within the specified
3188/// sequential range (L, L+Pos]. or is undef.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003189static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
Craig Topperb6072642012-05-03 07:26:59 +00003190 unsigned Pos, unsigned Size, int Low) {
3191 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003192 if (!isUndefOrEqual(Mask[i], Low))
3193 return false;
3194 return true;
3195}
3196
Nate Begeman9008ca62009-04-27 18:41:29 +00003197/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
3198/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
3199/// the second operand.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003200static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003201 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00003202 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00003204 return (Mask[0] < 2 && Mask[1] < 2);
3205 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003206}
3207
Nate Begeman9008ca62009-04-27 18:41:29 +00003208/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
3209/// is suitable for input to PSHUFHW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003210static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3211 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng0188ecb2006-03-22 18:59:22 +00003212 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003213
Nate Begeman9008ca62009-04-27 18:41:29 +00003214 // Lower quadword copied in order or undef.
Craig Topperc612d792012-01-02 09:17:37 +00003215 if (!isSequentialOrUndefInRange(Mask, 0, 4, 0))
3216 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003217
Evan Cheng506d3df2006-03-29 23:07:14 +00003218 // Upper quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003219 for (unsigned i = 4; i != 8; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003220 if (!isUndefOrInRange(Mask[i], 4, 8))
Evan Cheng506d3df2006-03-29 23:07:14 +00003221 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003222
Craig Toppera9a568a2012-05-02 08:03:44 +00003223 if (VT == MVT::v16i16) {
3224 // Lower quadword copied in order or undef.
3225 if (!isSequentialOrUndefInRange(Mask, 8, 4, 8))
3226 return false;
3227
3228 // Upper quadword shuffled.
3229 for (unsigned i = 12; i != 16; ++i)
3230 if (!isUndefOrInRange(Mask[i], 12, 16))
3231 return false;
3232 }
3233
Evan Cheng506d3df2006-03-29 23:07:14 +00003234 return true;
3235}
3236
Nate Begeman9008ca62009-04-27 18:41:29 +00003237/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
3238/// is suitable for input to PSHUFLW.
Craig Toppera9a568a2012-05-02 08:03:44 +00003239static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
3240 if (VT != MVT::v8i16 && (!HasAVX2 || VT != MVT::v16i16))
Evan Cheng506d3df2006-03-29 23:07:14 +00003241 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003242
Rafael Espindola15684b22009-04-24 12:40:33 +00003243 // Upper quadword copied in order.
Craig Topperc612d792012-01-02 09:17:37 +00003244 if (!isSequentialOrUndefInRange(Mask, 4, 4, 4))
3245 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003246
Rafael Espindola15684b22009-04-24 12:40:33 +00003247 // Lower quadword shuffled.
Craig Topperc612d792012-01-02 09:17:37 +00003248 for (unsigned i = 0; i != 4; ++i)
Craig Toppera9a568a2012-05-02 08:03:44 +00003249 if (!isUndefOrInRange(Mask[i], 0, 4))
Rafael Espindola15684b22009-04-24 12:40:33 +00003250 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003251
Craig Toppera9a568a2012-05-02 08:03:44 +00003252 if (VT == MVT::v16i16) {
3253 // Upper quadword copied in order.
3254 if (!isSequentialOrUndefInRange(Mask, 12, 4, 12))
3255 return false;
3256
3257 // Lower quadword shuffled.
3258 for (unsigned i = 8; i != 12; ++i)
3259 if (!isUndefOrInRange(Mask[i], 8, 12))
3260 return false;
3261 }
3262
Rafael Espindola15684b22009-04-24 12:40:33 +00003263 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003264}
3265
Nate Begemana09008b2009-10-19 02:17:23 +00003266/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
3267/// is suitable for input to PALIGNR.
Craig Topper0e2037b2012-01-20 05:53:00 +00003268static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT,
3269 const X86Subtarget *Subtarget) {
3270 if ((VT.getSizeInBits() == 128 && !Subtarget->hasSSSE3()) ||
3271 (VT.getSizeInBits() == 256 && !Subtarget->hasAVX2()))
Bruno Cardoso Lopes9065d4b2011-07-29 01:30:59 +00003272 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003273
Craig Topper0e2037b2012-01-20 05:53:00 +00003274 unsigned NumElts = VT.getVectorNumElements();
3275 unsigned NumLanes = VT.getSizeInBits()/128;
3276 unsigned NumLaneElts = NumElts/NumLanes;
3277
3278 // Do not handle 64-bit element shuffles with palignr.
3279 if (NumLaneElts == 2)
Nate Begemana09008b2009-10-19 02:17:23 +00003280 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003281
Craig Topper0e2037b2012-01-20 05:53:00 +00003282 for (unsigned l = 0; l != NumElts; l+=NumLaneElts) {
3283 unsigned i;
3284 for (i = 0; i != NumLaneElts; ++i) {
3285 if (Mask[i+l] >= 0)
3286 break;
3287 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00003288
Craig Topper0e2037b2012-01-20 05:53:00 +00003289 // Lane is all undef, go to next lane
3290 if (i == NumLaneElts)
3291 continue;
Nate Begemana09008b2009-10-19 02:17:23 +00003292
Craig Topper0e2037b2012-01-20 05:53:00 +00003293 int Start = Mask[i+l];
Nate Begemana09008b2009-10-19 02:17:23 +00003294
Craig Topper0e2037b2012-01-20 05:53:00 +00003295 // Make sure its in this lane in one of the sources
3296 if (!isUndefOrInRange(Start, l, l+NumLaneElts) &&
3297 !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts))
Nate Begemana09008b2009-10-19 02:17:23 +00003298 return false;
Craig Topper0e2037b2012-01-20 05:53:00 +00003299
3300 // If not lane 0, then we must match lane 0
3301 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l))
3302 return false;
3303
3304 // Correct second source to be contiguous with first source
3305 if (Start >= (int)NumElts)
3306 Start -= NumElts - NumLaneElts;
3307
3308 // Make sure we're shifting in the right direction.
3309 if (Start <= (int)(i+l))
3310 return false;
3311
3312 Start -= i;
3313
3314 // Check the rest of the elements to see if they are consecutive.
3315 for (++i; i != NumLaneElts; ++i) {
3316 int Idx = Mask[i+l];
3317
3318 // Make sure its in this lane
3319 if (!isUndefOrInRange(Idx, l, l+NumLaneElts) &&
3320 !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts))
3321 return false;
3322
3323 // If not lane 0, then we must match lane 0
3324 if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l))
3325 return false;
3326
3327 if (Idx >= (int)NumElts)
3328 Idx -= NumElts - NumLaneElts;
3329
3330 if (!isUndefOrEqual(Idx, Start+i))
3331 return false;
3332
3333 }
Nate Begemana09008b2009-10-19 02:17:23 +00003334 }
Craig Topper0e2037b2012-01-20 05:53:00 +00003335
Nate Begemana09008b2009-10-19 02:17:23 +00003336 return true;
3337}
3338
Craig Topper1a7700a2012-01-19 08:19:12 +00003339/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3340/// the two vector operands have swapped position.
3341static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask,
3342 unsigned NumElems) {
3343 for (unsigned i = 0; i != NumElems; ++i) {
3344 int idx = Mask[i];
3345 if (idx < 0)
3346 continue;
3347 else if (idx < (int)NumElems)
3348 Mask[i] = idx + NumElems;
3349 else
3350 Mask[i] = idx - NumElems;
3351 }
3352}
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003353
Craig Topper1a7700a2012-01-19 08:19:12 +00003354/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
3355/// specifies a shuffle of elements that is suitable for input to 128/256-bit
3356/// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be
3357/// reverse of what x86 shuffles want.
3358static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX,
3359 bool Commuted = false) {
3360 if (!HasAVX && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003361 return false;
3362
Craig Topper1a7700a2012-01-19 08:19:12 +00003363 unsigned NumElems = VT.getVectorNumElements();
3364 unsigned NumLanes = VT.getSizeInBits()/128;
3365 unsigned NumLaneElems = NumElems/NumLanes;
3366
3367 if (NumLaneElems != 2 && NumLaneElems != 4)
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003368 return false;
3369
3370 // VSHUFPSY divides the resulting vector into 4 chunks.
3371 // The sources are also splitted into 4 chunks, and each destination
3372 // chunk must come from a different source chunk.
3373 //
3374 // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0
3375 // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9
3376 //
3377 // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4,
3378 // Y3..Y0, Y3..Y0, X3..X0, X3..X0
3379 //
Craig Topper9d7025b2011-11-27 21:41:12 +00003380 // VSHUFPDY divides the resulting vector into 4 chunks.
3381 // The sources are also splitted into 4 chunks, and each destination
3382 // chunk must come from a different source chunk.
3383 //
3384 // SRC1 => X3 X2 X1 X0
3385 // SRC2 => Y3 Y2 Y1 Y0
3386 //
3387 // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0
3388 //
Craig Topper1a7700a2012-01-19 08:19:12 +00003389 unsigned HalfLaneElems = NumLaneElems/2;
3390 for (unsigned l = 0; l != NumElems; l += NumLaneElems) {
3391 for (unsigned i = 0; i != NumLaneElems; ++i) {
3392 int Idx = Mask[i+l];
3393 unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0);
3394 if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems))
3395 return false;
3396 // For VSHUFPSY, the mask of the second half must be the same as the
3397 // first but with the appropriate offsets. This works in the same way as
3398 // VPERMILPS works with masks.
3399 if (NumElems != 8 || l == 0 || Mask[i] < 0)
3400 continue;
3401 if (!isUndefOrEqual(Idx, Mask[i]+l))
3402 return false;
Craig Topper1ff73d72011-12-06 04:59:07 +00003403 }
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00003404 }
3405
3406 return true;
3407}
3408
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003409/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
3410/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003411static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003412 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003413 return false;
3414
Craig Topper7a9a28b2012-08-12 02:23:29 +00003415 unsigned NumElems = VT.getVectorNumElements();
3416
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003417 if (NumElems != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003418 return false;
3419
Evan Cheng2064a2b2006-03-28 06:50:32 +00003420 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Craig Topperdd637ae2012-02-19 05:41:45 +00003421 return isUndefOrEqual(Mask[0], 6) &&
3422 isUndefOrEqual(Mask[1], 7) &&
3423 isUndefOrEqual(Mask[2], 2) &&
3424 isUndefOrEqual(Mask[3], 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003425}
3426
Nate Begeman0b10b912009-11-07 23:17:15 +00003427/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
3428/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
3429/// <2, 3, 2, 3>
Craig Topperdd637ae2012-02-19 05:41:45 +00003430static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003431 if (!VT.is128BitVector())
Bruno Cardoso Lopes61260052011-07-29 02:05:28 +00003432 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003433
Craig Topper7a9a28b2012-08-12 02:23:29 +00003434 unsigned NumElems = VT.getVectorNumElements();
3435
Nate Begeman0b10b912009-11-07 23:17:15 +00003436 if (NumElems != 4)
3437 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00003438
Craig Topperdd637ae2012-02-19 05:41:45 +00003439 return isUndefOrEqual(Mask[0], 2) &&
3440 isUndefOrEqual(Mask[1], 3) &&
3441 isUndefOrEqual(Mask[2], 2) &&
3442 isUndefOrEqual(Mask[3], 3);
Nate Begeman0b10b912009-11-07 23:17:15 +00003443}
3444
Evan Cheng5ced1d82006-04-06 23:23:56 +00003445/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3446/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Craig Topperdd637ae2012-02-19 05:41:45 +00003447static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003448 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003449 return false;
3450
Craig Topperdd637ae2012-02-19 05:41:45 +00003451 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003452
Evan Cheng5ced1d82006-04-06 23:23:56 +00003453 if (NumElems != 2 && NumElems != 4)
3454 return false;
3455
Chad Rosier238ae312012-04-30 17:47:15 +00003456 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003457 if (!isUndefOrEqual(Mask[i], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003458 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003459
Chad Rosier238ae312012-04-30 17:47:15 +00003460 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003461 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003462 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003463
3464 return true;
3465}
3466
Nate Begeman0b10b912009-11-07 23:17:15 +00003467/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3468/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
Craig Topperdd637ae2012-02-19 05:41:45 +00003469static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003470 if (!VT.is128BitVector())
3471 return false;
3472
Craig Topperdd637ae2012-02-19 05:41:45 +00003473 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003474
Craig Topper7a9a28b2012-08-12 02:23:29 +00003475 if (NumElems != 2 && NumElems != 4)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003476 return false;
3477
Chad Rosier238ae312012-04-30 17:47:15 +00003478 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003479 if (!isUndefOrEqual(Mask[i], i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003480 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003481
Chad Rosier238ae312012-04-30 17:47:15 +00003482 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
3483 if (!isUndefOrEqual(Mask[i + e], i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003484 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003485
3486 return true;
3487}
3488
Elena Demikhovsky15963732012-06-26 08:04:10 +00003489//
3490// Some special combinations that can be optimized.
3491//
3492static
3493SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp,
3494 SelectionDAG &DAG) {
3495 EVT VT = SVOp->getValueType(0);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003496 DebugLoc dl = SVOp->getDebugLoc();
3497
3498 if (VT != MVT::v8i32 && VT != MVT::v8f32)
3499 return SDValue();
3500
3501 ArrayRef<int> Mask = SVOp->getMask();
3502
3503 // These are the special masks that may be optimized.
3504 static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14};
3505 static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15};
3506 bool MatchEvenMask = true;
3507 bool MatchOddMask = true;
3508 for (int i=0; i<8; ++i) {
3509 if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i]))
3510 MatchEvenMask = false;
3511 if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i]))
3512 MatchOddMask = false;
3513 }
Elena Demikhovsky15963732012-06-26 08:04:10 +00003514
Elena Demikhovsky32510202012-09-04 12:49:02 +00003515 if (!MatchEvenMask && !MatchOddMask)
Elena Demikhovsky15963732012-06-26 08:04:10 +00003516 return SDValue();
Elena Demikhovsky32510202012-09-04 12:49:02 +00003517
Elena Demikhovsky15963732012-06-26 08:04:10 +00003518 SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT);
3519
Elena Demikhovsky32510202012-09-04 12:49:02 +00003520 SDValue Op0 = SVOp->getOperand(0);
3521 SDValue Op1 = SVOp->getOperand(1);
3522
3523 if (MatchEvenMask) {
3524 // Shift the second operand right to 32 bits.
3525 static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 };
3526 Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask);
3527 } else {
3528 // Shift the first operand left to 32 bits.
3529 static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 };
3530 Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask);
3531 }
3532 static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15};
3533 return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask);
Elena Demikhovsky15963732012-06-26 08:04:10 +00003534}
3535
Evan Cheng0038e592006-03-28 00:39:58 +00003536/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3537/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003538static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003539 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003540 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003541
3542 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3543 "Unsupported vector type for unpckh");
3544
Craig Topper6347e862011-11-21 06:57:39 +00003545 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003546 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng0038e592006-03-28 00:39:58 +00003547 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003548
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003549 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3550 // independently on 128-bit lanes.
3551 unsigned NumLanes = VT.getSizeInBits()/128;
3552 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003553
Craig Topper94438ba2011-12-16 08:06:31 +00003554 for (unsigned l = 0; l != NumLanes; ++l) {
3555 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3556 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003557 i += 2, ++j) {
3558 int BitI = Mask[i];
3559 int BitI1 = Mask[i+1];
3560 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003561 return false;
David Greenea20244d2011-03-02 17:23:43 +00003562 if (V2IsSplat) {
3563 if (!isUndefOrEqual(BitI1, NumElts))
3564 return false;
3565 } else {
3566 if (!isUndefOrEqual(BitI1, j + NumElts))
3567 return false;
3568 }
Evan Cheng39623da2006-04-20 08:58:49 +00003569 }
Evan Cheng0038e592006-03-28 00:39:58 +00003570 }
David Greenea20244d2011-03-02 17:23:43 +00003571
Evan Cheng0038e592006-03-28 00:39:58 +00003572 return true;
3573}
3574
Evan Cheng4fcb9222006-03-28 02:43:26 +00003575/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3576/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003577static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT,
Craig Topper6347e862011-11-21 06:57:39 +00003578 bool HasAVX2, bool V2IsSplat = false) {
Craig Topper94438ba2011-12-16 08:06:31 +00003579 unsigned NumElts = VT.getVectorNumElements();
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003580
3581 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3582 "Unsupported vector type for unpckh");
3583
Craig Topper6347e862011-11-21 06:57:39 +00003584 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
Craig Topper6fa583d2011-11-21 08:26:50 +00003585 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng4fcb9222006-03-28 02:43:26 +00003586 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003587
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003588 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3589 // independently on 128-bit lanes.
3590 unsigned NumLanes = VT.getSizeInBits()/128;
3591 unsigned NumLaneElts = NumElts/NumLanes;
3592
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003593 for (unsigned l = 0; l != NumLanes; ++l) {
Craig Topper94438ba2011-12-16 08:06:31 +00003594 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3595 i != (l+1)*NumLaneElts; i += 2, ++j) {
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003596 int BitI = Mask[i];
3597 int BitI1 = Mask[i+1];
3598 if (!isUndefOrEqual(BitI, j))
Evan Cheng39623da2006-04-20 08:58:49 +00003599 return false;
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003600 if (V2IsSplat) {
3601 if (isUndefOrEqual(BitI1, NumElts))
3602 return false;
3603 } else {
3604 if (!isUndefOrEqual(BitI1, j+NumElts))
3605 return false;
3606 }
Evan Cheng39623da2006-04-20 08:58:49 +00003607 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003608 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003609 return true;
3610}
3611
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003612/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3613/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3614/// <0, 0, 1, 1>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003615static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT,
Craig Topper94438ba2011-12-16 08:06:31 +00003616 bool HasAVX2) {
3617 unsigned NumElts = VT.getVectorNumElements();
3618
3619 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3620 "Unsupported vector type for unpckh");
3621
3622 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3623 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003624 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003625
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003626 // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern
3627 // FIXME: Need a better way to get rid of this, there's no latency difference
3628 // between UNPCKLPD and MOVDDUP, the later should always be checked first and
3629 // the former later. We should also remove the "_undef" special mask.
Craig Topper94438ba2011-12-16 08:06:31 +00003630 if (NumElts == 4 && VT.getSizeInBits() == 256)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003631 return false;
3632
Bruno Cardoso Lopes4ea49682011-07-26 22:03:40 +00003633 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3634 // independently on 128-bit lanes.
Craig Topper94438ba2011-12-16 08:06:31 +00003635 unsigned NumLanes = VT.getSizeInBits()/128;
3636 unsigned NumLaneElts = NumElts/NumLanes;
David Greenea20244d2011-03-02 17:23:43 +00003637
Craig Topper94438ba2011-12-16 08:06:31 +00003638 for (unsigned l = 0; l != NumLanes; ++l) {
3639 for (unsigned i = l*NumLaneElts, j = l*NumLaneElts;
3640 i != (l+1)*NumLaneElts;
David Greenea20244d2011-03-02 17:23:43 +00003641 i += 2, ++j) {
3642 int BitI = Mask[i];
3643 int BitI1 = Mask[i+1];
3644
3645 if (!isUndefOrEqual(BitI, j))
3646 return false;
3647 if (!isUndefOrEqual(BitI1, j))
3648 return false;
3649 }
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003650 }
David Greenea20244d2011-03-02 17:23:43 +00003651
Rafael Espindola15684b22009-04-24 12:40:33 +00003652 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003653}
3654
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003655/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3656/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3657/// <2, 2, 3, 3>
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003658static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX2) {
Craig Topper94438ba2011-12-16 08:06:31 +00003659 unsigned NumElts = VT.getVectorNumElements();
3660
3661 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3662 "Unsupported vector type for unpckh");
3663
3664 if (VT.getSizeInBits() == 256 && NumElts != 4 && NumElts != 8 &&
3665 (!HasAVX2 || (NumElts != 16 && NumElts != 32)))
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003666 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003667
Craig Topper94438ba2011-12-16 08:06:31 +00003668 // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate
3669 // independently on 128-bit lanes.
3670 unsigned NumLanes = VT.getSizeInBits()/128;
3671 unsigned NumLaneElts = NumElts/NumLanes;
3672
3673 for (unsigned l = 0; l != NumLanes; ++l) {
3674 for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2;
3675 i != (l+1)*NumLaneElts; i += 2, ++j) {
3676 int BitI = Mask[i];
3677 int BitI1 = Mask[i+1];
3678 if (!isUndefOrEqual(BitI, j))
3679 return false;
3680 if (!isUndefOrEqual(BitI1, j))
3681 return false;
3682 }
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003683 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003684 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003685}
3686
Evan Cheng017dcc62006-04-21 01:05:10 +00003687/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3688/// specifies a shuffle of elements that is suitable for input to MOVSS,
3689/// MOVSD, and MOVD, i.e. setting the lowest element.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003690static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003691 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003692 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003693 if (!VT.is128BitVector())
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00003694 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003695
Craig Topperc612d792012-01-02 09:17:37 +00003696 unsigned NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003697
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003699 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003700
Craig Topperc612d792012-01-02 09:17:37 +00003701 for (unsigned i = 1; i != NumElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003702 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003703 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003704
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003705 return true;
3706}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003707
Craig Topper70b883b2011-11-28 10:14:51 +00003708/// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003709/// as permutations between 128-bit chunks or halves. As an example: this
3710/// shuffle bellow:
3711/// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15>
3712/// The first half comes from the second half of V1 and the second half from the
3713/// the second half of V2.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003714static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003715 if (!HasAVX || !VT.is256BitVector())
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003716 return false;
3717
3718 // The shuffle result is divided into half A and half B. In total the two
3719 // sources have 4 halves, namely: C, D, E, F. The final values of A and
3720 // B must come from C, D, E or F.
Craig Topperc612d792012-01-02 09:17:37 +00003721 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003722 bool MatchA = false, MatchB = false;
3723
3724 // Check if A comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003725 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003726 if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) {
3727 MatchA = true;
3728 break;
3729 }
3730 }
3731
3732 // Check if B comes from one of C, D, E, F.
Craig Topperc612d792012-01-02 09:17:37 +00003733 for (unsigned Half = 0; Half != 4; ++Half) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003734 if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) {
3735 MatchB = true;
3736 break;
3737 }
3738 }
3739
3740 return MatchA && MatchB;
3741}
3742
Craig Topper70b883b2011-11-28 10:14:51 +00003743/// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle
3744/// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions.
Craig Topperd93e4c32011-12-11 19:12:35 +00003745static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003746 EVT VT = SVOp->getValueType(0);
3747
Craig Topperc612d792012-01-02 09:17:37 +00003748 unsigned HalfSize = VT.getVectorNumElements()/2;
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003749
Craig Topperc612d792012-01-02 09:17:37 +00003750 unsigned FstHalf = 0, SndHalf = 0;
3751 for (unsigned i = 0; i < HalfSize; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003752 if (SVOp->getMaskElt(i) > 0) {
3753 FstHalf = SVOp->getMaskElt(i)/HalfSize;
3754 break;
3755 }
3756 }
Craig Topperc612d792012-01-02 09:17:37 +00003757 for (unsigned i = HalfSize; i < HalfSize*2; ++i) {
Bruno Cardoso Lopes53cae132011-08-12 21:48:26 +00003758 if (SVOp->getMaskElt(i) > 0) {
3759 SndHalf = SVOp->getMaskElt(i)/HalfSize;
3760 break;
3761 }
3762 }
3763
3764 return (FstHalf | (SndHalf << 4));
3765}
3766
Craig Topper70b883b2011-11-28 10:14:51 +00003767/// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003768/// specifies a shuffle of elements that is suitable for input to VPERMILPD*.
3769/// Note that VPERMIL mask matching is different depending whether theunderlying
3770/// type is 32 or 64. In the VPERMILPS the high half of the mask should point
3771/// to the same elements of the low, but to the higher half of the source.
3772/// In VPERMILPD the two lanes could be shuffled independently of each other
Craig Topperdbd98a42012-02-07 06:28:42 +00003773/// with the same restriction that lanes can't be crossed. Also handles PSHUFDY.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003774static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper70b883b2011-11-28 10:14:51 +00003775 if (!HasAVX)
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003776 return false;
3777
Craig Topperc612d792012-01-02 09:17:37 +00003778 unsigned NumElts = VT.getVectorNumElements();
Craig Topper70b883b2011-11-28 10:14:51 +00003779 // Only match 256-bit with 32/64-bit types
3780 if (VT.getSizeInBits() != 256 || (NumElts != 4 && NumElts != 8))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003781 return false;
3782
Craig Topperc612d792012-01-02 09:17:37 +00003783 unsigned NumLanes = VT.getSizeInBits()/128;
3784 unsigned LaneSize = NumElts/NumLanes;
Craig Topper1a7700a2012-01-19 08:19:12 +00003785 for (unsigned l = 0; l != NumElts; l += LaneSize) {
Craig Topperc612d792012-01-02 09:17:37 +00003786 for (unsigned i = 0; i != LaneSize; ++i) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003787 if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize))
Craig Topper70b883b2011-11-28 10:14:51 +00003788 return false;
Craig Topper1a7700a2012-01-19 08:19:12 +00003789 if (NumElts != 8 || l == 0)
Craig Topper70b883b2011-11-28 10:14:51 +00003790 continue;
3791 // VPERMILPS handling
3792 if (Mask[i] < 0)
3793 continue;
Craig Topper1a7700a2012-01-19 08:19:12 +00003794 if (!isUndefOrEqual(Mask[i+l], Mask[i]+l))
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00003795 return false;
3796 }
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00003797 }
3798
3799 return true;
3800}
3801
Craig Topper5aaffa82012-02-19 02:53:47 +00003802/// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse
Evan Cheng017dcc62006-04-21 01:05:10 +00003803/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003804/// element of vector 2 and the other elements to come from vector 1 in order.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003805static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003806 bool V2IsSplat = false, bool V2IsUndef = false) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003807 if (!VT.is128BitVector())
Craig Topper97327dc2012-03-18 22:50:10 +00003808 return false;
Craig Topper7a9a28b2012-08-12 02:23:29 +00003809
3810 unsigned NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003811 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003812 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003813
Nate Begeman9008ca62009-04-27 18:41:29 +00003814 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003815 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003816
Craig Topperc612d792012-01-02 09:17:37 +00003817 for (unsigned i = 1; i != NumOps; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003818 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3819 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3820 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003821 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003822
Evan Cheng39623da2006-04-20 08:58:49 +00003823 return true;
3824}
3825
Evan Chengd9539472006-04-14 21:59:03 +00003826/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3827/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003828/// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7>
Craig Topperdd637ae2012-02-19 05:41:45 +00003829static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003830 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003831 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003832 return false;
3833
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003834 unsigned NumElems = VT.getVectorNumElements();
3835
3836 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3837 (VT.getSizeInBits() == 256 && NumElems != 8))
3838 return false;
3839
3840 // "i+1" is the value the indexed mask element must have
Craig Topperdd637ae2012-02-19 05:41:45 +00003841 for (unsigned i = 0; i != NumElems; i += 2)
3842 if (!isUndefOrEqual(Mask[i], i+1) ||
3843 !isUndefOrEqual(Mask[i+1], i+1))
Nate Begeman9008ca62009-04-27 18:41:29 +00003844 return false;
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003845
3846 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003847}
3848
3849/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3850/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003851/// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6>
Craig Topperdd637ae2012-02-19 05:41:45 +00003852static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT,
Craig Topper5aaffa82012-02-19 02:53:47 +00003853 const X86Subtarget *Subtarget) {
Craig Topperd0a31172012-01-10 06:37:29 +00003854 if (!Subtarget->hasSSE3())
Evan Chengd9539472006-04-14 21:59:03 +00003855 return false;
3856
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003857 unsigned NumElems = VT.getVectorNumElements();
3858
3859 if ((VT.getSizeInBits() == 128 && NumElems != 4) ||
3860 (VT.getSizeInBits() == 256 && NumElems != 8))
3861 return false;
3862
3863 // "i" is the value the indexed mask element must have
Craig Topperc612d792012-01-02 09:17:37 +00003864 for (unsigned i = 0; i != NumElems; i += 2)
Craig Topperdd637ae2012-02-19 05:41:45 +00003865 if (!isUndefOrEqual(Mask[i], i) ||
3866 !isUndefOrEqual(Mask[i+1], i))
Nate Begeman9008ca62009-04-27 18:41:29 +00003867 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003868
Bruno Cardoso Lopes9123c6f2011-07-26 02:39:28 +00003869 return true;
Evan Chengd9539472006-04-14 21:59:03 +00003870}
3871
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003872/// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand
3873/// specifies a shuffle of elements that is suitable for input to 256-bit
3874/// version of MOVDDUP.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00003875static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasAVX) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003876 if (!HasAVX || !VT.is256BitVector())
3877 return false;
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003878
Craig Topper7a9a28b2012-08-12 02:23:29 +00003879 unsigned NumElts = VT.getVectorNumElements();
3880 if (NumElts != 4)
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003881 return false;
3882
Craig Topperc612d792012-01-02 09:17:37 +00003883 for (unsigned i = 0; i != NumElts/2; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003884 if (!isUndefOrEqual(Mask[i], 0))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003885 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003886 for (unsigned i = NumElts/2; i != NumElts; ++i)
Craig Topperbeabc6c2011-12-05 06:56:46 +00003887 if (!isUndefOrEqual(Mask[i], NumElts/2))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00003888 return false;
3889 return true;
3890}
3891
Evan Cheng0b457f02008-09-25 20:50:48 +00003892/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003893/// specifies a shuffle of elements that is suitable for input to 128-bit
3894/// version of MOVDDUP.
Craig Topperdd637ae2012-02-19 05:41:45 +00003895static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00003896 if (!VT.is128BitVector())
Bruno Cardoso Lopes06ef9232011-08-25 21:40:34 +00003897 return false;
3898
Craig Topperc612d792012-01-02 09:17:37 +00003899 unsigned e = VT.getVectorNumElements() / 2;
3900 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003901 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003902 return false;
Craig Topperc612d792012-01-02 09:17:37 +00003903 for (unsigned i = 0; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00003904 if (!isUndefOrEqual(Mask[e+i], i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003905 return false;
3906 return true;
3907}
3908
David Greenec38a03e2011-02-03 15:50:00 +00003909/// isVEXTRACTF128Index - Return true if the specified
3910/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3911/// suitable for input to VEXTRACTF128.
3912bool X86::isVEXTRACTF128Index(SDNode *N) {
3913 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3914 return false;
3915
3916 // The index should be aligned on a 128-bit boundary.
3917 uint64_t Index =
3918 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3919
3920 unsigned VL = N->getValueType(0).getVectorNumElements();
3921 unsigned VBits = N->getValueType(0).getSizeInBits();
3922 unsigned ElSize = VBits / VL;
3923 bool Result = (Index * ElSize) % 128 == 0;
3924
3925 return Result;
3926}
3927
David Greeneccacdc12011-02-04 16:08:29 +00003928/// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR
3929/// operand specifies a subvector insert that is suitable for input to
3930/// VINSERTF128.
3931bool X86::isVINSERTF128Index(SDNode *N) {
3932 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
3933 return false;
3934
3935 // The index should be aligned on a 128-bit boundary.
3936 uint64_t Index =
3937 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
3938
3939 unsigned VL = N->getValueType(0).getVectorNumElements();
3940 unsigned VBits = N->getValueType(0).getSizeInBits();
3941 unsigned ElSize = VBits / VL;
3942 bool Result = (Index * ElSize) % 128 == 0;
3943
3944 return Result;
3945}
3946
Evan Cheng63d33002006-03-22 08:01:21 +00003947/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003948/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Craig Topper1a7700a2012-01-19 08:19:12 +00003949/// Handles 128-bit and 256-bit.
Craig Topper5aaffa82012-02-19 02:53:47 +00003950static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) {
Craig Topper1a7700a2012-01-19 08:19:12 +00003951 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003952
Craig Topper1a7700a2012-01-19 08:19:12 +00003953 assert((VT.is128BitVector() || VT.is256BitVector()) &&
3954 "Unsupported vector type for PSHUF/SHUFP");
3955
3956 // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate
3957 // independently on 128-bit lanes.
3958 unsigned NumElts = VT.getVectorNumElements();
3959 unsigned NumLanes = VT.getSizeInBits()/128;
3960 unsigned NumLaneElts = NumElts/NumLanes;
3961
3962 assert((NumLaneElts == 2 || NumLaneElts == 4) &&
3963 "Only supports 2 or 4 elements per lane");
3964
3965 unsigned Shift = (NumLaneElts == 4) ? 1 : 0;
Evan Chengb9df0ca2006-03-22 02:53:00 +00003966 unsigned Mask = 0;
Craig Topper1a7700a2012-01-19 08:19:12 +00003967 for (unsigned i = 0; i != NumElts; ++i) {
3968 int Elt = N->getMaskElt(i);
3969 if (Elt < 0) continue;
Craig Topper6b28d352012-05-03 07:12:59 +00003970 Elt &= NumLaneElts - 1;
3971 unsigned ShAmt = (i << Shift) % 8;
Craig Topper1a7700a2012-01-19 08:19:12 +00003972 Mask |= Elt << ShAmt;
Evan Cheng36b27f32006-03-28 23:41:33 +00003973 }
Craig Topper1a7700a2012-01-19 08:19:12 +00003974
Evan Cheng63d33002006-03-22 08:01:21 +00003975 return Mask;
3976}
3977
Evan Cheng506d3df2006-03-29 23:07:14 +00003978/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003979/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00003980static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00003981 EVT VT = N->getValueType(0);
3982
3983 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
3984 "Unsupported vector type for PSHUFHW");
3985
3986 unsigned NumElts = VT.getVectorNumElements();
3987
Evan Cheng506d3df2006-03-29 23:07:14 +00003988 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00003989 for (unsigned l = 0; l != NumElts; l += 8) {
3990 // 8 nodes per lane, but we only care about the last 4.
3991 for (unsigned i = 0; i < 4; ++i) {
3992 int Elt = N->getMaskElt(l+i+4);
3993 if (Elt < 0) continue;
3994 Elt &= 0x3; // only 2-bits.
3995 Mask |= Elt << (i * 2);
3996 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003997 }
Craig Topper6b28d352012-05-03 07:12:59 +00003998
Evan Cheng506d3df2006-03-29 23:07:14 +00003999 return Mask;
4000}
4001
4002/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00004003/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Craig Topperdd637ae2012-02-19 05:41:45 +00004004static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) {
Craig Topper6b28d352012-05-03 07:12:59 +00004005 EVT VT = N->getValueType(0);
4006
4007 assert((VT == MVT::v8i16 || VT == MVT::v16i16) &&
4008 "Unsupported vector type for PSHUFHW");
4009
4010 unsigned NumElts = VT.getVectorNumElements();
4011
Evan Cheng506d3df2006-03-29 23:07:14 +00004012 unsigned Mask = 0;
Craig Topper6b28d352012-05-03 07:12:59 +00004013 for (unsigned l = 0; l != NumElts; l += 8) {
4014 // 8 nodes per lane, but we only care about the first 4.
4015 for (unsigned i = 0; i < 4; ++i) {
4016 int Elt = N->getMaskElt(l+i);
4017 if (Elt < 0) continue;
4018 Elt &= 0x3; // only 2-bits
4019 Mask |= Elt << (i * 2);
4020 }
Evan Cheng506d3df2006-03-29 23:07:14 +00004021 }
Craig Topper6b28d352012-05-03 07:12:59 +00004022
Evan Cheng506d3df2006-03-29 23:07:14 +00004023 return Mask;
4024}
4025
Nate Begemana09008b2009-10-19 02:17:23 +00004026/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
4027/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
Craig Topperd93e4c32011-12-11 19:12:35 +00004028static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) {
4029 EVT VT = SVOp->getValueType(0);
4030 unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3;
Nate Begemana09008b2009-10-19 02:17:23 +00004031
Craig Topper0e2037b2012-01-20 05:53:00 +00004032 unsigned NumElts = VT.getVectorNumElements();
4033 unsigned NumLanes = VT.getSizeInBits()/128;
4034 unsigned NumLaneElts = NumElts/NumLanes;
4035
4036 int Val = 0;
4037 unsigned i;
4038 for (i = 0; i != NumElts; ++i) {
Nate Begemana09008b2009-10-19 02:17:23 +00004039 Val = SVOp->getMaskElt(i);
4040 if (Val >= 0)
4041 break;
4042 }
Craig Topper0e2037b2012-01-20 05:53:00 +00004043 if (Val >= (int)NumElts)
4044 Val -= NumElts - NumLaneElts;
4045
Eli Friedman63f8dde2011-07-25 21:36:45 +00004046 assert(Val - i > 0 && "PALIGNR imm should be positive");
Nate Begemana09008b2009-10-19 02:17:23 +00004047 return (Val - i) * EltSize;
4048}
4049
David Greenec38a03e2011-02-03 15:50:00 +00004050/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
4051/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
4052/// instructions.
4053unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
4054 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4055 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
4056
4057 uint64_t Index =
4058 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4059
4060 EVT VecVT = N->getOperand(0).getValueType();
4061 EVT ElVT = VecVT.getVectorElementType();
4062
4063 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greenec38a03e2011-02-03 15:50:00 +00004064 return Index / NumElemsPerChunk;
4065}
4066
David Greeneccacdc12011-02-04 16:08:29 +00004067/// getInsertVINSERTF128Immediate - Return the appropriate immediate
4068/// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128
4069/// instructions.
4070unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) {
4071 if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4072 llvm_unreachable("Illegal insert subvector for VINSERTF128");
4073
4074 uint64_t Index =
NAKAMURA Takumi27635382011-02-05 15:10:54 +00004075 cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
David Greeneccacdc12011-02-04 16:08:29 +00004076
4077 EVT VecVT = N->getValueType(0);
4078 EVT ElVT = VecVT.getVectorElementType();
4079
4080 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
David Greeneccacdc12011-02-04 16:08:29 +00004081 return Index / NumElemsPerChunk;
4082}
4083
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004084/// getShuffleCLImmediate - Return the appropriate immediate to shuffle
4085/// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions.
4086/// Handles 256-bit.
4087static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) {
4088 EVT VT = N->getValueType(0);
4089
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004090 unsigned NumElts = VT.getVectorNumElements();
4091
Craig Topper095c5282012-04-15 23:48:57 +00004092 assert((VT.is256BitVector() && NumElts == 4) &&
4093 "Unsupported vector type for VPERMQ/VPERMPD");
4094
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004095 unsigned Mask = 0;
4096 for (unsigned i = 0; i != NumElts; ++i) {
4097 int Elt = N->getMaskElt(i);
Craig Topper095c5282012-04-15 23:48:57 +00004098 if (Elt < 0)
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00004099 continue;
4100 Mask |= Elt << (i*2);
4101 }
4102
4103 return Mask;
4104}
Evan Cheng37b73872009-07-30 08:33:02 +00004105/// isZeroNode - Returns true if Elt is a constant zero or a floating point
4106/// constant +0.0.
4107bool X86::isZeroNode(SDValue Elt) {
4108 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00004109 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00004110 (isa<ConstantFPSDNode>(Elt) &&
4111 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
4112}
4113
Nate Begeman9008ca62009-04-27 18:41:29 +00004114/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
4115/// their permute mask.
4116static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
4117 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004118 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004119 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004120 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00004121
Nate Begeman5a5ca152009-04-29 05:20:52 +00004122 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00004123 int Idx = SVOp->getMaskElt(i);
4124 if (Idx >= 0) {
4125 if (Idx < (int)NumElems)
4126 Idx += NumElems;
4127 else
4128 Idx -= NumElems;
4129 }
4130 MaskVec.push_back(Idx);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004131 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004132 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
4133 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00004134}
4135
Evan Cheng533a0aa2006-04-19 20:35:22 +00004136/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
4137/// match movhlps. The lower half elements should come from upper half of
4138/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004139/// half of V2 (and in order).
Craig Topperdd637ae2012-02-19 05:41:45 +00004140static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004141 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004142 return false;
4143 if (VT.getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00004144 return false;
4145 for (unsigned i = 0, e = 2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004146 if (!isUndefOrEqual(Mask[i], i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004147 return false;
4148 for (unsigned i = 2; i != 4; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004149 if (!isUndefOrEqual(Mask[i], i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004150 return false;
4151 return true;
4152}
4153
Evan Cheng5ced1d82006-04-06 23:23:56 +00004154/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00004155/// is promoted to a vector. It also returns the LoadSDNode by reference if
4156/// required.
4157static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00004158 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
4159 return false;
4160 N = N->getOperand(0).getNode();
4161 if (!ISD::isNON_EXTLoad(N))
4162 return false;
4163 if (LD)
4164 *LD = cast<LoadSDNode>(N);
4165 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004166}
4167
Dan Gohman65fd6562011-11-03 21:49:52 +00004168// Test whether the given value is a vector value which will be legalized
4169// into a load.
4170static bool WillBeConstantPoolLoad(SDNode *N) {
4171 if (N->getOpcode() != ISD::BUILD_VECTOR)
4172 return false;
4173
4174 // Check for any non-constant elements.
4175 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4176 switch (N->getOperand(i).getNode()->getOpcode()) {
4177 case ISD::UNDEF:
4178 case ISD::ConstantFP:
4179 case ISD::Constant:
4180 break;
4181 default:
4182 return false;
4183 }
4184
4185 // Vectors of all-zeros and all-ones are materialized with special
4186 // instructions rather than being loaded.
4187 return !ISD::isBuildVectorAllZeros(N) &&
4188 !ISD::isBuildVectorAllOnes(N);
4189}
4190
Evan Cheng533a0aa2006-04-19 20:35:22 +00004191/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
4192/// match movlp{s|d}. The lower half elements should come from lower half of
4193/// V1 (and in order), and the upper half elements should come from the upper
4194/// half of V2 (and in order). And since V1 will become the source of the
4195/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004196static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
Craig Topperdd637ae2012-02-19 05:41:45 +00004197 ArrayRef<int> Mask, EVT VT) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004198 if (!VT.is128BitVector())
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004199 return false;
4200
Evan Cheng466685d2006-10-09 20:57:25 +00004201 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004202 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00004203 // Is V2 is a vector load, don't do this transformation. We will try to use
4204 // load folding shufps op.
Dan Gohman65fd6562011-11-03 21:49:52 +00004205 if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2))
Evan Cheng23425f52006-10-09 21:39:25 +00004206 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004207
Bruno Cardoso Lopes59353b42011-08-11 18:59:13 +00004208 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00004209
Evan Cheng533a0aa2006-04-19 20:35:22 +00004210 if (NumElems != 2 && NumElems != 4)
4211 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00004212 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004213 if (!isUndefOrEqual(Mask[i], i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004214 return false;
Chad Rosier238ae312012-04-30 17:47:15 +00004215 for (unsigned i = NumElems/2, e = NumElems; i != e; ++i)
Craig Topperdd637ae2012-02-19 05:41:45 +00004216 if (!isUndefOrEqual(Mask[i], i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00004217 return false;
4218 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004219}
4220
Evan Cheng39623da2006-04-20 08:58:49 +00004221/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
4222/// all the same.
4223static bool isSplatVector(SDNode *N) {
4224 if (N->getOpcode() != ISD::BUILD_VECTOR)
4225 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004226
Dan Gohman475871a2008-07-27 21:46:04 +00004227 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00004228 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
4229 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00004230 return false;
4231 return true;
4232}
4233
Evan Cheng213d2cf2007-05-17 18:45:50 +00004234/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00004235/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00004236/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00004237static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00004238 SDValue V1 = N->getOperand(0);
4239 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004240 unsigned NumElems = N->getValueType(0).getVectorNumElements();
4241 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004242 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00004243 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004244 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00004245 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
4246 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004247 if (Opc != ISD::BUILD_VECTOR ||
4248 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00004249 return false;
4250 } else if (Idx >= 0) {
4251 unsigned Opc = V1.getOpcode();
4252 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
4253 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00004254 if (Opc != ISD::BUILD_VECTOR ||
4255 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00004256 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00004257 }
4258 }
4259 return true;
4260}
4261
4262/// getZeroVector - Returns a vector of specified type with all zero elements.
4263///
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004264static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget,
Craig Topper12216172012-01-13 08:12:35 +00004265 SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004266 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004267 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004268
Dale Johannesen0488fb62010-09-30 23:57:10 +00004269 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004270 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00004271 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004272 if (Size == 128) { // SSE
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004273 if (Subtarget->hasSSE2()) { // SSE2
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004274 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4275 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4276 } else { // SSE1
4277 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4278 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4279 }
Craig Topper9d352402012-04-23 07:24:41 +00004280 } else if (Size == 256) { // AVX
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004281 if (Subtarget->hasAVX2()) { // AVX2
Craig Topper12216172012-01-13 08:12:35 +00004282 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
4283 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4284 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4285 } else {
4286 // 256-bit logic and arithmetic instructions in AVX are all
4287 // floating-point, no support for integer ops. Emit fp zeroed vectors.
4288 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
4289 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4290 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
4291 }
Craig Topper9d352402012-04-23 07:24:41 +00004292 } else
4293 llvm_unreachable("Unexpected vector type");
4294
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004295 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004296}
4297
Chris Lattner8a594482007-11-25 00:24:49 +00004298/// getOnesVector - Returns a vector of specified type with all bits set.
Craig Topper745a86b2011-11-19 22:34:59 +00004299/// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4300/// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4301/// Then bitcast to their original type, ensuring they get CSE'd.
4302static SDValue getOnesVector(EVT VT, bool HasAVX2, SelectionDAG &DAG,
4303 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004304 assert(VT.isVector() && "Expected a vector type");
Craig Topper9d352402012-04-23 07:24:41 +00004305 unsigned Size = VT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00004306
Owen Anderson825b72b2009-08-11 20:47:22 +00004307 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Craig Topper745a86b2011-11-19 22:34:59 +00004308 SDValue Vec;
Craig Topper9d352402012-04-23 07:24:41 +00004309 if (Size == 256) {
Craig Topper745a86b2011-11-19 22:34:59 +00004310 if (HasAVX2) { // AVX2
4311 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4312 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, 8);
4313 } else { // AVX
4314 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper4c7972d2012-04-22 18:15:59 +00004315 Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
Craig Topper745a86b2011-11-19 22:34:59 +00004316 }
Craig Topper9d352402012-04-23 07:24:41 +00004317 } else if (Size == 128) {
Craig Topper745a86b2011-11-19 22:34:59 +00004318 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Craig Topper9d352402012-04-23 07:24:41 +00004319 } else
4320 llvm_unreachable("Unexpected vector type");
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +00004321
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004322 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00004323}
4324
Evan Cheng39623da2006-04-20 08:58:49 +00004325/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
4326/// that point to V2 points to its first element.
Craig Topper39a9e482012-02-11 06:24:48 +00004327static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00004328 for (unsigned i = 0; i != NumElems; ++i) {
Craig Topper39a9e482012-02-11 06:24:48 +00004329 if (Mask[i] > (int)NumElems) {
4330 Mask[i] = NumElems;
Evan Cheng39623da2006-04-20 08:58:49 +00004331 }
Evan Cheng39623da2006-04-20 08:58:49 +00004332 }
Evan Cheng39623da2006-04-20 08:58:49 +00004333}
4334
Evan Cheng017dcc62006-04-21 01:05:10 +00004335/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
4336/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00004337static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004338 SDValue V2) {
4339 unsigned NumElems = VT.getVectorNumElements();
4340 SmallVector<int, 8> Mask;
4341 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00004342 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004343 Mask.push_back(i);
4344 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00004345}
4346
Nate Begeman9008ca62009-04-27 18:41:29 +00004347/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004348static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004349 SDValue V2) {
4350 unsigned NumElems = VT.getVectorNumElements();
4351 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00004352 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004353 Mask.push_back(i);
4354 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00004355 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004356 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00004357}
4358
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004359/// getUnpackh - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00004360static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00004361 SDValue V2) {
4362 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00004363 SmallVector<int, 8> Mask;
Chad Rosier238ae312012-04-30 17:47:15 +00004364 for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 Mask.push_back(i + Half);
4366 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00004367 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004368 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004369}
4370
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004371// PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004372// a generic shuffle instruction because the target has no such instructions.
4373// Generate shuffles which repeat i16 and i8 several times until they can be
4374// represented by v4f32 and then be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004375static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004376 EVT VT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00004377 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004378 DebugLoc dl = V.getDebugLoc();
Rafael Espindola15684b22009-04-24 12:40:33 +00004379
Nate Begeman9008ca62009-04-27 18:41:29 +00004380 while (NumElems > 4) {
4381 if (EltNo < NumElems/2) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004382 V = getUnpackl(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004383 } else {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004384 V = getUnpackh(DAG, dl, VT, V, V);
Nate Begeman9008ca62009-04-27 18:41:29 +00004385 EltNo -= NumElems/2;
4386 }
4387 NumElems >>= 1;
4388 }
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004389 return V;
4390}
Eric Christopherfd179292009-08-27 18:07:15 +00004391
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004392/// getLegalSplat - Generate a legal splat with supported x86 shuffles
4393static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) {
4394 EVT VT = V.getValueType();
4395 DebugLoc dl = V.getDebugLoc();
Craig Topper9d352402012-04-23 07:24:41 +00004396 unsigned Size = VT.getSizeInBits();
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004397
Craig Topper9d352402012-04-23 07:24:41 +00004398 if (Size == 128) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004399 V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004400 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004401 V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32),
4402 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004403 } else if (Size == 256) {
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004404 // To use VPERMILPS to splat scalars, the second half of indicies must
4405 // refer to the higher part, which is a duplication of the lower one,
4406 // because VPERMILPS can only handle in-lane permutations.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004407 int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo,
4408 EltNo+4, EltNo+4, EltNo+4, EltNo+4 };
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004409
4410 V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V);
4411 V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32),
4412 &SplatMask[0]);
Craig Topper9d352402012-04-23 07:24:41 +00004413 } else
4414 llvm_unreachable("Vector size not supported");
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004415
4416 return DAG.getNode(ISD::BITCAST, dl, VT, V);
4417}
4418
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004419/// PromoteSplat - Splat is promoted to target supported vector shuffles.
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004420static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
4421 EVT SrcVT = SV->getValueType(0);
4422 SDValue V1 = SV->getOperand(0);
4423 DebugLoc dl = SV->getDebugLoc();
4424
4425 int EltNo = SV->getSplatIndex();
4426 int NumElems = SrcVT.getVectorNumElements();
4427 unsigned Size = SrcVT.getSizeInBits();
4428
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004429 assert(((Size == 128 && NumElems > 4) || Size == 256) &&
4430 "Unknown how to promote splat for type");
4431
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004432 // Extract the 128-bit part containing the splat element and update
4433 // the splat element index when it refers to the higher register.
4434 if (Size == 256) {
Craig Topper7d1e3dc2012-04-30 05:17:10 +00004435 V1 = Extract128BitVector(V1, EltNo, DAG, dl);
4436 if (EltNo >= NumElems/2)
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004437 EltNo -= NumElems/2;
4438 }
4439
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00004440 // All i16 and i8 vector types can't be used directly by a generic shuffle
4441 // instruction because the target has no such instruction. Generate shuffles
4442 // which repeat i16 and i8 several times until they fit in i32, and then can
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004443 // be manipulated by target suported shuffles.
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004444 EVT EltVT = SrcVT.getVectorElementType();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004445 if (EltVT == MVT::i8 || EltVT == MVT::i16)
Bruno Cardoso Lopes5f1d8ab2011-08-11 02:49:44 +00004446 V1 = PromoteSplati8i16(V1, DAG, EltNo);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004447
4448 // Recreate the 256-bit vector and place the same 128-bit vector
4449 // into the low and high part. This is necessary because we want
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00004450 // to use VPERM* to shuffle the vectors
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004451 if (Size == 256) {
Craig Topper4c7972d2012-04-22 18:15:59 +00004452 V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1);
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00004453 }
4454
4455 return getLegalSplat(DAG, V1, EltNo);
Evan Chengc575ca22006-04-17 20:43:08 +00004456}
4457
Evan Chengba05f722006-04-21 23:03:30 +00004458/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00004459/// vector of zero or undef vector. This produces a shuffle where the low
4460/// element of V2 is swizzled into the zero/undef vector, landing at element
4461/// 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 +00004462static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Craig Topper12216172012-01-13 08:12:35 +00004463 bool IsZero,
4464 const X86Subtarget *Subtarget,
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004465 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004466 EVT VT = V2.getValueType();
Craig Topper12216172012-01-13 08:12:35 +00004467 SDValue V1 = IsZero
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004468 ? getZeroVector(VT, Subtarget, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00004469 unsigned NumElems = VT.getVectorNumElements();
4470 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00004471 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004472 // If this is the insertion idx, put the low elt of V2 here.
4473 MaskVec.push_back(i == Idx ? NumElems : i);
4474 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00004475}
4476
Craig Toppera1ffc682012-03-20 06:42:26 +00004477/// getTargetShuffleMask - Calculates the shuffle mask corresponding to the
4478/// target specific opcode. Returns true if the Mask could be calculated.
Craig Topper89f4e662012-03-20 07:17:59 +00004479/// Sets IsUnary to true if only uses one source.
Craig Topperd978c542012-05-06 19:46:21 +00004480static bool getTargetShuffleMask(SDNode *N, MVT VT,
Craig Topper89f4e662012-03-20 07:17:59 +00004481 SmallVectorImpl<int> &Mask, bool &IsUnary) {
Craig Toppera1ffc682012-03-20 06:42:26 +00004482 unsigned NumElems = VT.getVectorNumElements();
4483 SDValue ImmN;
4484
Craig Topper89f4e662012-03-20 07:17:59 +00004485 IsUnary = false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004486 switch(N->getOpcode()) {
4487 case X86ISD::SHUFP:
4488 ImmN = N->getOperand(N->getNumOperands()-1);
4489 DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4490 break;
4491 case X86ISD::UNPCKH:
4492 DecodeUNPCKHMask(VT, Mask);
4493 break;
4494 case X86ISD::UNPCKL:
4495 DecodeUNPCKLMask(VT, Mask);
4496 break;
4497 case X86ISD::MOVHLPS:
4498 DecodeMOVHLPSMask(NumElems, Mask);
4499 break;
4500 case X86ISD::MOVLHPS:
4501 DecodeMOVLHPSMask(NumElems, Mask);
4502 break;
4503 case X86ISD::PSHUFD:
4504 case X86ISD::VPERMILP:
4505 ImmN = N->getOperand(N->getNumOperands()-1);
4506 DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004507 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004508 break;
4509 case X86ISD::PSHUFHW:
4510 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004511 DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004512 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004513 break;
4514 case X86ISD::PSHUFLW:
4515 ImmN = N->getOperand(N->getNumOperands()-1);
Craig Toppera9a568a2012-05-02 08:03:44 +00004516 DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper89f4e662012-03-20 07:17:59 +00004517 IsUnary = true;
Craig Toppera1ffc682012-03-20 06:42:26 +00004518 break;
Craig Topperbdcbcb32012-05-06 18:54:26 +00004519 case X86ISD::VPERMI:
4520 ImmN = N->getOperand(N->getNumOperands()-1);
4521 DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4522 IsUnary = true;
4523 break;
Craig Toppera1ffc682012-03-20 06:42:26 +00004524 case X86ISD::MOVSS:
4525 case X86ISD::MOVSD: {
4526 // The index 0 always comes from the first element of the second source,
4527 // this is why MOVSS and MOVSD are used in the first place. The other
4528 // elements come from the other positions of the first source vector
4529 Mask.push_back(NumElems);
4530 for (unsigned i = 1; i != NumElems; ++i) {
4531 Mask.push_back(i);
4532 }
4533 break;
4534 }
4535 case X86ISD::VPERM2X128:
4536 ImmN = N->getOperand(N->getNumOperands()-1);
4537 DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
Craig Topper2091df32012-04-17 05:54:54 +00004538 if (Mask.empty()) return false;
Craig Toppera1ffc682012-03-20 06:42:26 +00004539 break;
4540 case X86ISD::MOVDDUP:
4541 case X86ISD::MOVLHPD:
4542 case X86ISD::MOVLPD:
4543 case X86ISD::MOVLPS:
4544 case X86ISD::MOVSHDUP:
4545 case X86ISD::MOVSLDUP:
4546 case X86ISD::PALIGN:
4547 // Not yet implemented
4548 return false;
4549 default: llvm_unreachable("unknown target shuffle node");
4550 }
4551
4552 return true;
4553}
4554
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004555/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4556/// element of the result of the vector shuffle.
Craig Topper3d092db2012-03-21 02:14:01 +00004557static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
Benjamin Kramer050db522011-03-26 12:38:19 +00004558 unsigned Depth) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004559 if (Depth == 6)
4560 return SDValue(); // Limit search depth.
4561
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004562 SDValue V = SDValue(N, 0);
4563 EVT VT = V.getValueType();
4564 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004565
4566 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
4567 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
Craig Topper3d092db2012-03-21 02:14:01 +00004568 int Elt = SV->getMaskElt(Index);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004569
Craig Topper3d092db2012-03-21 02:14:01 +00004570 if (Elt < 0)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004571 return DAG.getUNDEF(VT.getVectorElementType());
4572
Craig Topperd156dc12012-02-06 07:17:51 +00004573 unsigned NumElems = VT.getVectorNumElements();
Craig Topper3d092db2012-03-21 02:14:01 +00004574 SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
4575 : SV->getOperand(1);
4576 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00004577 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004578
4579 // Recurse into target specific vector shuffles to find scalars.
4580 if (isTargetShuffle(Opcode)) {
Craig Topperd978c542012-05-06 19:46:21 +00004581 MVT ShufVT = V.getValueType().getSimpleVT();
4582 unsigned NumElems = ShufVT.getVectorNumElements();
Craig Toppera1ffc682012-03-20 06:42:26 +00004583 SmallVector<int, 16> ShuffleMask;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004584 SDValue ImmN;
Craig Topper89f4e662012-03-20 07:17:59 +00004585 bool IsUnary;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004586
Craig Topperd978c542012-05-06 19:46:21 +00004587 if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
Craig Toppera1ffc682012-03-20 06:42:26 +00004588 return SDValue();
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004589
Craig Topper3d092db2012-03-21 02:14:01 +00004590 int Elt = ShuffleMask[Index];
4591 if (Elt < 0)
Craig Topperd978c542012-05-06 19:46:21 +00004592 return DAG.getUNDEF(ShufVT.getVectorElementType());
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004593
Craig Topper3d092db2012-03-21 02:14:01 +00004594 SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
Craig Topperd978c542012-05-06 19:46:21 +00004595 : N->getOperand(1);
Craig Topper3d092db2012-03-21 02:14:01 +00004596 return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00004597 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004598 }
4599
4600 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004601 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004602 V = V.getOperand(0);
4603 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004604 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004605
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00004606 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004607 return SDValue();
4608 }
4609
4610 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
4611 return (Index == 0) ? V.getOperand(0)
Craig Topper3d092db2012-03-21 02:14:01 +00004612 : DAG.getUNDEF(VT.getVectorElementType());
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004613
4614 if (V.getOpcode() == ISD::BUILD_VECTOR)
4615 return V.getOperand(Index);
4616
4617 return SDValue();
4618}
4619
4620/// getNumOfConsecutiveZeros - Return the number of elements of a vector
4621/// shuffle operation which come from a consecutively from a zero. The
Chris Lattner7a2bdde2011-04-15 05:18:47 +00004622/// search can start in two different directions, from left or right.
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004623static
Craig Topper3d092db2012-03-21 02:14:01 +00004624unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, unsigned NumElems,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004625 bool ZerosFromLeft, SelectionDAG &DAG) {
Craig Topper3d092db2012-03-21 02:14:01 +00004626 unsigned i;
4627 for (i = 0; i != NumElems; ++i) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004628 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Craig Topper3d092db2012-03-21 02:14:01 +00004629 SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004630 if (!(Elt.getNode() &&
4631 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
4632 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004633 }
4634
4635 return i;
4636}
4637
Craig Topper3d092db2012-03-21 02:14:01 +00004638/// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE)
4639/// correspond consecutively to elements from one of the vector operands,
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004640/// starting from its index OpIdx. Also tell OpNum which source vector operand.
4641static
Craig Topper3d092db2012-03-21 02:14:01 +00004642bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp,
4643 unsigned MaskI, unsigned MaskE, unsigned OpIdx,
4644 unsigned NumElems, unsigned &OpNum) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004645 bool SeenV1 = false;
4646 bool SeenV2 = false;
4647
Craig Topper3d092db2012-03-21 02:14:01 +00004648 for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004649 int Idx = SVOp->getMaskElt(i);
4650 // Ignore undef indicies
4651 if (Idx < 0)
4652 continue;
4653
Craig Topper3d092db2012-03-21 02:14:01 +00004654 if (Idx < (int)NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004655 SeenV1 = true;
4656 else
4657 SeenV2 = true;
4658
4659 // Only accept consecutive elements from the same vector
4660 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
4661 return false;
4662 }
4663
4664 OpNum = SeenV1 ? 0 : 1;
4665 return true;
4666}
4667
4668/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
4669/// logical left shift of a vector.
4670static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4671 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4672 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4673 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4674 false /* check zeros from right */, DAG);
4675 unsigned OpSrc;
4676
4677 if (!NumZeros)
4678 return false;
4679
4680 // Considering the elements in the mask that are not consecutive zeros,
4681 // check if they consecutively come from only one of the source vectors.
4682 //
4683 // V1 = {X, A, B, C} 0
4684 // \ \ \ /
4685 // vector_shuffle V1, V2 <1, 2, 3, X>
4686 //
4687 if (!isShuffleMaskConsecutive(SVOp,
4688 0, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004689 NumElems-NumZeros, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004690 NumZeros, // Where to start looking in the src vector
4691 NumElems, // Number of elements in vector
4692 OpSrc)) // Which source operand ?
4693 return false;
4694
4695 isLeft = false;
4696 ShAmt = NumZeros;
4697 ShVal = SVOp->getOperand(OpSrc);
4698 return true;
4699}
4700
4701/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
4702/// logical left shift of a vector.
4703static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
4704 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
4705 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
4706 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
4707 true /* check zeros from left */, DAG);
4708 unsigned OpSrc;
4709
4710 if (!NumZeros)
4711 return false;
4712
4713 // Considering the elements in the mask that are not consecutive zeros,
4714 // check if they consecutively come from only one of the source vectors.
4715 //
4716 // 0 { A, B, X, X } = V2
4717 // / \ / /
4718 // vector_shuffle V1, V2 <X, X, 4, 5>
4719 //
4720 if (!isShuffleMaskConsecutive(SVOp,
4721 NumZeros, // Mask Start Index
Craig Topper3d092db2012-03-21 02:14:01 +00004722 NumElems, // Mask End Index(exclusive)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004723 0, // Where to start looking in the src vector
4724 NumElems, // Number of elements in vector
4725 OpSrc)) // Which source operand ?
4726 return false;
4727
4728 isLeft = true;
4729 ShAmt = NumZeros;
4730 ShVal = SVOp->getOperand(OpSrc);
4731 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004732}
4733
4734/// isVectorShift - Returns true if the shuffle can be implemented as a
4735/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00004736static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00004737 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004738 // Although the logic below support any bitwidth size, there are no
4739 // shift instructions which handle more than 128-bit vectors.
Craig Topper7a9a28b2012-08-12 02:23:29 +00004740 if (!SVOp->getValueType(0).is128BitVector())
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00004741 return false;
4742
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004743 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
4744 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
4745 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00004746
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00004747 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00004748}
4749
Evan Chengc78d3b42006-04-24 18:01:45 +00004750/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
4751///
Dan Gohman475871a2008-07-27 21:46:04 +00004752static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00004753 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00004754 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004755 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004756 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004757 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00004758 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004759
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004760 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004761 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004762 bool First = true;
4763 for (unsigned i = 0; i < 16; ++i) {
4764 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
4765 if (ThisIsNonZero && First) {
4766 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004767 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004768 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004769 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004770 First = false;
4771 }
4772
4773 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00004774 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004775 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
4776 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004777 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004778 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00004779 }
4780 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004781 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
4782 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
4783 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00004784 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00004785 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00004786 } else
4787 ThisElt = LastElt;
4788
Gabor Greifba36cb52008-08-28 21:40:38 +00004789 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00004791 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00004792 }
4793 }
4794
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004795 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00004796}
4797
Bill Wendlinga348c562007-03-22 18:42:45 +00004798/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00004799///
Dan Gohman475871a2008-07-27 21:46:04 +00004800static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00004801 unsigned NumNonZero, unsigned NumZero,
4802 SelectionDAG &DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004803 const X86Subtarget* Subtarget,
Dan Gohmand858e902010-04-17 15:26:15 +00004804 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00004805 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00004806 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00004807
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004808 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004809 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00004810 bool First = true;
4811 for (unsigned i = 0; i < 8; ++i) {
4812 bool isNonZero = (NonZeros & (1 << i)) != 0;
4813 if (isNonZero) {
4814 if (First) {
4815 if (NumZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00004816 V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00004817 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004818 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00004819 First = false;
4820 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004821 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004822 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00004823 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00004824 }
4825 }
4826
4827 return V;
4828}
4829
Evan Chengf26ffe92008-05-29 08:22:04 +00004830/// getVShift - Return a vector logical shift node.
4831///
Owen Andersone50ed302009-08-10 22:56:29 +00004832static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00004833 unsigned NumBits, SelectionDAG &DAG,
4834 const TargetLowering &TLI, DebugLoc dl) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00004835 assert(VT.is128BitVector() && "Unknown type for VShift");
Dale Johannesen0488fb62010-09-30 23:57:10 +00004836 EVT ShVT = MVT::v2i64;
Craig Toppered2e13d2012-01-22 19:15:14 +00004837 unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004838 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
4839 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004840 DAG.getNode(Opc, dl, ShVT, SrcOp,
Owen Anderson95771af2011-02-25 21:41:48 +00004841 DAG.getConstant(NumBits,
4842 TLI.getShiftAmountTy(SrcOp.getValueType()))));
Evan Chengf26ffe92008-05-29 08:22:04 +00004843}
4844
Dan Gohman475871a2008-07-27 21:46:04 +00004845SDValue
Evan Chengc3630942009-12-09 21:00:30 +00004846X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004847 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004848
Evan Chengc3630942009-12-09 21:00:30 +00004849 // Check if the scalar load can be widened into a vector load. And if
4850 // the address is "base + cst" see if the cst can be "absorbed" into
4851 // the shuffle mask.
4852 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4853 SDValue Ptr = LD->getBasePtr();
4854 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4855 return SDValue();
4856 EVT PVT = LD->getValueType(0);
4857 if (PVT != MVT::i32 && PVT != MVT::f32)
4858 return SDValue();
4859
4860 int FI = -1;
4861 int64_t Offset = 0;
4862 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4863 FI = FINode->getIndex();
4864 Offset = 0;
Chris Lattner0a9481f2011-02-13 22:25:43 +00004865 } else if (DAG.isBaseWithConstantOffset(Ptr) &&
Evan Chengc3630942009-12-09 21:00:30 +00004866 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4867 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4868 Offset = Ptr.getConstantOperandVal(1);
4869 Ptr = Ptr.getOperand(0);
4870 } else {
4871 return SDValue();
4872 }
4873
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004874 // FIXME: 256-bit vector instructions don't require a strict alignment,
4875 // improve this code to support it better.
4876 unsigned RequiredAlign = VT.getSizeInBits()/8;
Evan Chengc3630942009-12-09 21:00:30 +00004877 SDValue Chain = LD->getChain();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004878 // Make sure the stack object alignment is at least 16 or 32.
Evan Chengc3630942009-12-09 21:00:30 +00004879 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004880 if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
Evan Chengc3630942009-12-09 21:00:30 +00004881 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004882 // Can't change the alignment. FIXME: It's possible to compute
4883 // the exact stack offset and reference FI + adjust offset instead.
4884 // If someone *really* cares about this. That's the way to implement it.
4885 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004886 } else {
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004887 MFI->setObjectAlignment(FI, RequiredAlign);
Evan Chengc3630942009-12-09 21:00:30 +00004888 }
4889 }
4890
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004891 // (Offset % 16 or 32) must be multiple of 4. Then address is then
Evan Chengc3630942009-12-09 21:00:30 +00004892 // Ptr + (Offset & ~15).
4893 if (Offset < 0)
4894 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004895 if ((Offset % RequiredAlign) & 3)
Evan Chengc3630942009-12-09 21:00:30 +00004896 return SDValue();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004897 int64_t StartOffset = Offset & ~(RequiredAlign-1);
Evan Chengc3630942009-12-09 21:00:30 +00004898 if (StartOffset)
4899 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4900 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4901
4902 int EltNo = (Offset - StartOffset) >> 2;
Craig Topper66ddd152012-04-27 22:54:43 +00004903 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004904
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004905 EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
4906 SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
Chris Lattner51abfe42010-09-21 06:02:19 +00004907 LD->getPointerInfo().getWithOffset(StartOffset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004908 false, false, false, 0);
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004909
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004910 SmallVector<int, 8> Mask;
Craig Topper66ddd152012-04-27 22:54:43 +00004911 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopesac5f13f2011-08-02 16:06:18 +00004912 Mask.push_back(EltNo);
4913
Craig Toppercc3000632012-01-30 07:50:31 +00004914 return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
Evan Chengc3630942009-12-09 21:00:30 +00004915 }
4916
4917 return SDValue();
4918}
4919
Michael J. Spencerec38de22010-10-10 22:04:20 +00004920/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4921/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004922/// load which has the same value as a build_vector whose operands are 'elts'.
4923///
4924/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004925///
Nate Begeman1449f292010-03-24 22:19:06 +00004926/// FIXME: we'd also like to handle the case where the last elements are zero
4927/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4928/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004929static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004930 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004931 EVT EltVT = VT.getVectorElementType();
4932 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004933
Nate Begemanfdea31a2010-03-24 20:49:50 +00004934 LoadSDNode *LDBase = NULL;
4935 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004936
Nate Begeman1449f292010-03-24 22:19:06 +00004937 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004938 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004939 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004940 for (unsigned i = 0; i < NumElems; ++i) {
4941 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004942
Nate Begemanfdea31a2010-03-24 20:49:50 +00004943 if (!Elt.getNode() ||
4944 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4945 return SDValue();
4946 if (!LDBase) {
4947 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4948 return SDValue();
4949 LDBase = cast<LoadSDNode>(Elt.getNode());
4950 LastLoadedElt = i;
4951 continue;
4952 }
4953 if (Elt.getOpcode() == ISD::UNDEF)
4954 continue;
4955
4956 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4957 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4958 return SDValue();
4959 LastLoadedElt = i;
4960 }
Nate Begeman1449f292010-03-24 22:19:06 +00004961
4962 // If we have found an entire vector of loads and undefs, then return a large
4963 // load of the entire vector width starting at the base pointer. If we found
4964 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004965 if (LastLoadedElt == NumElems - 1) {
4966 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004967 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004968 LDBase->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004969 LDBase->isVolatile(), LDBase->isNonTemporal(),
4970 LDBase->isInvariant(), 0);
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(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004973 LDBase->isVolatile(), LDBase->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004974 LDBase->isInvariant(), LDBase->getAlignment());
Craig Topper69947b92012-04-23 06:57:04 +00004975 }
4976 if (NumElems == 4 && LastLoadedElt == 1 &&
4977 DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004978 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4979 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Eli Friedman322ea082011-09-14 23:42:45 +00004980 SDValue ResNode =
4981 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, 2, MVT::i64,
4982 LDBase->getPointerInfo(),
4983 LDBase->getAlignment(),
4984 false/*isVolatile*/, true/*ReadMem*/,
4985 false/*WriteMem*/);
Manman Ren2b7a2e82012-08-31 23:16:57 +00004986
4987 // Make sure the newly-created LOAD is in the same position as LDBase in
4988 // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
4989 // update uses of LDBase's output chain to use the TokenFactor.
4990 if (LDBase->hasAnyUseOfValue(1)) {
4991 SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
4992 SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
4993 DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
4994 DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
4995 SDValue(ResNode.getNode(), 1));
4996 }
4997
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004998 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004999 }
5000 return SDValue();
5001}
5002
Nadav Rotem9d68b062012-04-08 12:54:54 +00005003/// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5004/// to generate a splat value for the following cases:
5005/// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005006/// 2. A splat shuffle which uses a scalar_to_vector node which comes from
Nadav Rotem9d68b062012-04-08 12:54:54 +00005007/// a scalar load, or a constant.
5008/// The VBROADCAST node is returned when a pattern is found,
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005009/// or SDValue() otherwise.
Nadav Rotem154819d2012-04-09 07:45:58 +00005010SDValue
5011X86TargetLowering::LowerVectorBroadcast(SDValue &Op, SelectionDAG &DAG) const {
Craig Toppera9376332012-01-10 08:23:59 +00005012 if (!Subtarget->hasAVX())
5013 return SDValue();
5014
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005015 EVT VT = Op.getValueType();
Nadav Rotem154819d2012-04-09 07:45:58 +00005016 DebugLoc dl = Op.getDebugLoc();
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005017
Craig Topper5da8a802012-05-04 05:49:51 +00005018 assert((VT.is128BitVector() || VT.is256BitVector()) &&
5019 "Unsupported vector type for broadcast.");
5020
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005021 SDValue Ld;
Nadav Rotem9d68b062012-04-08 12:54:54 +00005022 bool ConstSplatVal;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005023
Nadav Rotem9d68b062012-04-08 12:54:54 +00005024 switch (Op.getOpcode()) {
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005025 default:
5026 // Unknown pattern found.
5027 return SDValue();
5028
5029 case ISD::BUILD_VECTOR: {
5030 // The BUILD_VECTOR node must be a splat.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005031 if (!isSplatVector(Op.getNode()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005032 return SDValue();
5033
Nadav Rotem9d68b062012-04-08 12:54:54 +00005034 Ld = Op.getOperand(0);
5035 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5036 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005037
5038 // The suspected load node has several users. Make sure that all
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005039 // of its users are from the BUILD_VECTOR node.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005040 // Constants may have multiple users.
5041 if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005042 return SDValue();
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005043 break;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005044 }
5045
5046 case ISD::VECTOR_SHUFFLE: {
5047 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5048
5049 // Shuffles must have a splat mask where the first element is
5050 // broadcasted.
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005051 if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005052 return SDValue();
5053
5054 SDValue Sc = Op.getOperand(0);
Nadav Rotemb88e8dd2012-05-10 12:50:02 +00005055 if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
Elena Demikhovsky8f40f7b2012-07-01 06:12:26 +00005056 Sc.getOpcode() != ISD::BUILD_VECTOR) {
5057
5058 if (!Subtarget->hasAVX2())
5059 return SDValue();
5060
5061 // Use the register form of the broadcast instruction available on AVX2.
5062 if (VT.is256BitVector())
5063 Sc = Extract128BitVector(Sc, 0, DAG, dl);
5064 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5065 }
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005066
5067 Ld = Sc.getOperand(0);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005068 ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
Nadav Rotem154819d2012-04-09 07:45:58 +00005069 Ld.getOpcode() == ISD::ConstantFP);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005070
5071 // The scalar_to_vector node and the suspected
5072 // load node must have exactly one user.
Nadav Rotem9d68b062012-04-08 12:54:54 +00005073 // Constants may have multiple users.
5074 if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse()))
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005075 return SDValue();
5076 break;
5077 }
5078 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005079
Craig Topper7a9a28b2012-08-12 02:23:29 +00005080 bool Is256 = VT.is256BitVector();
Nadav Rotem9d68b062012-04-08 12:54:54 +00005081
5082 // Handle the broadcasting a single constant scalar from the constant pool
5083 // into a vector. On Sandybridge it is still better to load a constant vector
5084 // from the constant pool and not to broadcast it from a scalar.
5085 if (ConstSplatVal && Subtarget->hasAVX2()) {
5086 EVT CVT = Ld.getValueType();
5087 assert(!CVT.isVector() && "Must not broadcast a vector type");
5088 unsigned ScalarSize = CVT.getSizeInBits();
5089
Craig Topper5da8a802012-05-04 05:49:51 +00005090 if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) {
Nadav Rotem9d68b062012-04-08 12:54:54 +00005091 const Constant *C = 0;
5092 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5093 C = CI->getConstantIntValue();
5094 else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5095 C = CF->getConstantFPValue();
5096
5097 assert(C && "Invalid constant type");
5098
Nadav Rotem154819d2012-04-09 07:45:58 +00005099 SDValue CP = DAG.getConstantPool(C, getPointerTy());
Nadav Rotem9d68b062012-04-08 12:54:54 +00005100 unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
Nadav Rotem154819d2012-04-09 07:45:58 +00005101 Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP,
Craig Topper6643d9c2012-05-04 06:18:33 +00005102 MachinePointerInfo::getConstantPool(),
5103 false, false, false, Alignment);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005104
Nadav Rotem9d68b062012-04-08 12:54:54 +00005105 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5106 }
5107 }
5108
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005109 bool IsLoad = ISD::isNormalLoad(Ld.getNode());
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005110 unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5111
Nadav Rotem4fc8a5d2012-05-19 19:57:37 +00005112 // Handle AVX2 in-register broadcasts.
5113 if (!IsLoad && Subtarget->hasAVX2() &&
5114 (ScalarSize == 32 || (Is256 && ScalarSize == 64)))
5115 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5116
5117 // The scalar source must be a normal load.
5118 if (!IsLoad)
5119 return SDValue();
5120
Craig Topper5da8a802012-05-04 05:49:51 +00005121 if (ScalarSize == 32 || (Is256 && ScalarSize == 64))
Nadav Rotem9d68b062012-04-08 12:54:54 +00005122 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005123
Craig Toppera9376332012-01-10 08:23:59 +00005124 // The integer check is needed for the 64-bit into 128-bit so it doesn't match
Craig Topper5da8a802012-05-04 05:49:51 +00005125 // double since there is no vbroadcastsd xmm
Craig Toppera9376332012-01-10 08:23:59 +00005126 if (Subtarget->hasAVX2() && Ld.getValueType().isInteger()) {
Craig Topper5da8a802012-05-04 05:49:51 +00005127 if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
Nadav Rotem9d68b062012-04-08 12:54:54 +00005128 return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
Craig Toppera9376332012-01-10 08:23:59 +00005129 }
Nadav Rotemcbbe33f2011-11-18 02:49:55 +00005130
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005131 // Unsupported broadcast.
5132 return SDValue();
5133}
5134
Michael Liao7091b242012-08-14 21:24:47 +00005135// LowerVectorFpExtend - Recognize the scalarized FP_EXTEND from v2f32 to v2f64
5136// and convert it into X86ISD::VFPEXT due to the current ISD::FP_EXTEND has the
5137// constraint of matching input/output vector elements.
5138SDValue
5139X86TargetLowering::LowerVectorFpExtend(SDValue &Op, SelectionDAG &DAG) const {
5140 DebugLoc DL = Op.getDebugLoc();
5141 SDNode *N = Op.getNode();
5142 EVT VT = Op.getValueType();
5143 unsigned NumElts = Op.getNumOperands();
5144
5145 // Check supported types and sub-targets.
5146 //
5147 // Only v2f32 -> v2f64 needs special handling.
5148 if (VT != MVT::v2f64 || !Subtarget->hasSSE2())
5149 return SDValue();
5150
5151 SDValue VecIn;
5152 EVT VecInVT;
5153 SmallVector<int, 8> Mask;
5154 EVT SrcVT = MVT::Other;
5155
5156 // Check the patterns could be translated into X86vfpext.
5157 for (unsigned i = 0; i < NumElts; ++i) {
5158 SDValue In = N->getOperand(i);
5159 unsigned Opcode = In.getOpcode();
5160
5161 // Skip if the element is undefined.
5162 if (Opcode == ISD::UNDEF) {
5163 Mask.push_back(-1);
5164 continue;
5165 }
5166
5167 // Quit if one of the elements is not defined from 'fpext'.
5168 if (Opcode != ISD::FP_EXTEND)
5169 return SDValue();
5170
5171 // Check how the source of 'fpext' is defined.
5172 SDValue L2In = In.getOperand(0);
5173 EVT L2InVT = L2In.getValueType();
5174
5175 // Check the original type
5176 if (SrcVT == MVT::Other)
5177 SrcVT = L2InVT;
5178 else if (SrcVT != L2InVT) // Quit if non-homogenous typed.
5179 return SDValue();
5180
5181 // Check whether the value being 'fpext'ed is extracted from the same
5182 // source.
5183 Opcode = L2In.getOpcode();
5184
5185 // Quit if it's not extracted with a constant index.
5186 if (Opcode != ISD::EXTRACT_VECTOR_ELT ||
5187 !isa<ConstantSDNode>(L2In.getOperand(1)))
5188 return SDValue();
5189
5190 SDValue ExtractedFromVec = L2In.getOperand(0);
5191
5192 if (VecIn.getNode() == 0) {
5193 VecIn = ExtractedFromVec;
5194 VecInVT = ExtractedFromVec.getValueType();
5195 } else if (VecIn != ExtractedFromVec) // Quit if built from more than 1 vec.
5196 return SDValue();
5197
5198 Mask.push_back(cast<ConstantSDNode>(L2In.getOperand(1))->getZExtValue());
5199 }
5200
Michael Liao24438b82012-08-20 17:59:18 +00005201 // Quit if all operands of BUILD_VECTOR are undefined.
5202 if (!VecIn.getNode())
5203 return SDValue();
5204
Michael Liao7091b242012-08-14 21:24:47 +00005205 // Fill the remaining mask as undef.
5206 for (unsigned i = NumElts; i < VecInVT.getVectorNumElements(); ++i)
5207 Mask.push_back(-1);
5208
5209 return DAG.getNode(X86ISD::VFPEXT, DL, VT,
5210 DAG.getVectorShuffle(VecInVT, DL,
5211 VecIn, DAG.getUNDEF(VecInVT),
5212 &Mask[0]));
5213}
5214
Evan Chengc3630942009-12-09 21:00:30 +00005215SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005216X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005217 DebugLoc dl = Op.getDebugLoc();
David Greenea5f26012011-02-07 19:36:54 +00005218
David Greenef125a292011-02-08 19:04:41 +00005219 EVT VT = Op.getValueType();
5220 EVT ExtVT = VT.getVectorElementType();
David Greenef125a292011-02-08 19:04:41 +00005221 unsigned NumElems = Op.getNumOperands();
5222
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005223 // Vectors containing all zeros can be matched by pxor and xorps later
5224 if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5225 // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
5226 // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
Craig Topper07a27622012-01-22 03:07:48 +00005227 if (VT == MVT::v4i32 || VT == MVT::v8i32)
Chris Lattner8a594482007-11-25 00:24:49 +00005228 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005229
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005230 return getZeroVector(VT, Subtarget, DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00005231 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005232
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005233 // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
Craig Topper745a86b2011-11-19 22:34:59 +00005234 // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
5235 // vpcmpeqd on 256-bit vectors.
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005236 if (ISD::isBuildVectorAllOnes(Op.getNode())) {
Craig Topper07a27622012-01-22 03:07:48 +00005237 if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasAVX2()))
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005238 return Op;
5239
Craig Topper07a27622012-01-22 03:07:48 +00005240 return getOnesVector(VT, Subtarget->hasAVX2(), DAG, dl);
Bruno Cardoso Lopes531f19f2011-08-01 19:51:53 +00005241 }
5242
Nadav Rotem154819d2012-04-09 07:45:58 +00005243 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00005244 if (Broadcast.getNode())
5245 return Broadcast;
Nadav Rotemf8c10e52011-11-15 22:50:37 +00005246
Michael Liao7091b242012-08-14 21:24:47 +00005247 SDValue FpExt = LowerVectorFpExtend(Op, DAG);
5248 if (FpExt.getNode())
5249 return FpExt;
5250
Owen Andersone50ed302009-08-10 22:56:29 +00005251 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005252
Evan Cheng0db9fe62006-04-25 20:13:52 +00005253 unsigned NumZero = 0;
5254 unsigned NumNonZero = 0;
5255 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005256 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005257 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005258 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005259 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00005260 if (Elt.getOpcode() == ISD::UNDEF)
5261 continue;
5262 Values.insert(Elt);
5263 if (Elt.getOpcode() != ISD::Constant &&
5264 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005265 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00005266 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00005267 NumZero++;
5268 else {
5269 NonZeros |= (1 << i);
5270 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005271 }
5272 }
5273
Chris Lattner97a2a562010-08-26 05:24:29 +00005274 // All undef vector. Return an UNDEF. All zero vectors were handled above.
5275 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00005276 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005277
Chris Lattner67f453a2008-03-09 05:42:06 +00005278 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00005279 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005280 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00005281 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00005282
Chris Lattner62098042008-03-09 01:05:04 +00005283 // If this is an insertion of an i64 value on x86-32, and if the top bits of
5284 // the value are obviously zero, truncate the value to i32 and do the
5285 // insertion that way. Only do this if the value is non-constant or if the
5286 // value is a constant being inserted into element 0. It is cheaper to do
5287 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00005288 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00005289 (!IsAllConstants || Idx == 0)) {
5290 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00005291 // Handle SSE only.
5292 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
5293 EVT VecVT = MVT::v4i32;
5294 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00005295
Chris Lattner62098042008-03-09 01:05:04 +00005296 // Truncate the value (which may itself be a constant) to i32, and
5297 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00005298 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00005299 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Craig Topper12216172012-01-13 08:12:35 +00005300 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005301
Chris Lattner62098042008-03-09 01:05:04 +00005302 // Now we have our 32-bit value zero extended in the low element of
5303 // a vector. If Idx != 0, swizzle it into place.
5304 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005305 SmallVector<int, 4> Mask;
5306 Mask.push_back(Idx);
5307 for (unsigned i = 1; i != VecElts; ++i)
5308 Mask.push_back(i);
Craig Topperdf966f62012-04-22 19:17:57 +00005309 Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT),
Nate Begeman9008ca62009-04-27 18:41:29 +00005310 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00005311 }
Craig Topper07a27622012-01-22 03:07:48 +00005312 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Chris Lattner62098042008-03-09 01:05:04 +00005313 }
5314 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005315
Chris Lattner19f79692008-03-08 22:59:52 +00005316 // If we have a constant or non-constant insertion into the low element of
5317 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
5318 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00005319 // depending on what the source datatype is.
5320 if (Idx == 0) {
Craig Topperd62c16e2011-12-29 03:20:51 +00005321 if (NumZero == 0)
Eli Friedman10415532009-06-06 06:05:10 +00005322 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Craig Topperd62c16e2011-12-29 03:20:51 +00005323
5324 if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005325 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005326 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005327 SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
Nadav Rotem394a1f52012-01-11 14:07:51 +00005328 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
5329 Item, DAG.getIntPtrConstant(0));
Elena Demikhovsky021c0a22011-12-28 08:14:01 +00005330 }
Craig Topper7a9a28b2012-08-12 02:23:29 +00005331 assert(VT.is128BitVector() && "Expected an SSE value type!");
Eli Friedman10415532009-06-06 06:05:10 +00005332 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
5333 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Craig Topper12216172012-01-13 08:12:35 +00005334 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topperd62c16e2011-12-29 03:20:51 +00005335 }
5336
5337 if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005338 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Craig Topper3224e6b2011-12-29 03:09:33 +00005339 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
Craig Topper7a9a28b2012-08-12 02:23:29 +00005340 if (VT.is256BitVector()) {
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005341 SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +00005342 Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
Craig Topper19ec2a92011-12-29 03:34:54 +00005343 } else {
Craig Topper7a9a28b2012-08-12 02:23:29 +00005344 assert(VT.is128BitVector() && "Expected an SSE value type!");
Craig Topper12216172012-01-13 08:12:35 +00005345 Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
Craig Topper19ec2a92011-12-29 03:34:54 +00005346 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005347 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00005348 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005349 }
Evan Chengf26ffe92008-05-29 08:22:04 +00005350
5351 // Is it a vector logical left shift?
5352 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00005353 X86::isZeroNode(Op.getOperand(0)) &&
5354 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005355 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00005356 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00005357 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005358 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00005359 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005360 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005361
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005362 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00005363 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005364
Chris Lattner19f79692008-03-08 22:59:52 +00005365 // Otherwise, if this is a vector with i32 or f32 elements, and the element
5366 // is a non-constant being inserted into an element other than the low one,
5367 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
5368 // movd/movss) to move this into the low element, then shuffle it into
5369 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005370 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00005371 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00005372
Evan Cheng0db9fe62006-04-25 20:13:52 +00005373 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Craig Topper12216172012-01-13 08:12:35 +00005374 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00005375 SmallVector<int, 8> MaskVec;
Craig Topper31a207a2012-05-04 06:39:13 +00005376 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005377 MaskVec.push_back(i == Idx ? 0 : 1);
5378 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005379 }
5380 }
5381
Chris Lattner67f453a2008-03-09 05:42:06 +00005382 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00005383 if (Values.size() == 1) {
5384 if (EVTBits == 32) {
5385 // Instead of a shuffle like this:
5386 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
5387 // Check if it's possible to issue this instead.
5388 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
5389 unsigned Idx = CountTrailingZeros_32(NonZeros);
5390 SDValue Item = Op.getOperand(Idx);
5391 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
5392 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
5393 }
Dan Gohman475871a2008-07-27 21:46:04 +00005394 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00005395 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005396
Dan Gohmana3941172007-07-24 22:55:08 +00005397 // A vector full of immediates; various special cases are already
5398 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00005399 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00005400 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00005401
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005402 // For AVX-length vectors, build the individual 128-bit pieces and use
5403 // shuffles to put them in place.
Craig Topper7a9a28b2012-08-12 02:23:29 +00005404 if (VT.is256BitVector()) {
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005405 SmallVector<SDValue, 32> V;
Craig Topperfa5b70e2012-02-03 06:32:21 +00005406 for (unsigned i = 0; i != NumElems; ++i)
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005407 V.push_back(Op.getOperand(i));
5408
5409 EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
5410
5411 // Build both the lower and upper subvector.
5412 SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2);
5413 SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2],
5414 NumElems/2);
5415
5416 // Recreate the wider vector with the lower and upper part.
Craig Topper4c7972d2012-04-22 18:15:59 +00005417 return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
Bruno Cardoso Lopes6683efb2011-07-22 00:15:07 +00005418 }
5419
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00005420 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00005421 if (EVTBits == 64) {
5422 if (NumNonZero == 1) {
5423 // One half is zero or undef.
5424 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00005425 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00005426 Op.getOperand(Idx));
Craig Topper12216172012-01-13 08:12:35 +00005427 return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00005428 }
Dan Gohman475871a2008-07-27 21:46:04 +00005429 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00005430 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005431
5432 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00005433 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005434 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005435 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005436 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005437 }
5438
Bill Wendling826f36f2007-03-28 00:57:11 +00005439 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005440 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005441 Subtarget, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00005442 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005443 }
5444
5445 // If element VT is == 32 bits, turn it into a number of shuffles.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005446 SmallVector<SDValue, 8> V(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005447 if (NumElems == 4 && NumZero > 0) {
5448 for (unsigned i = 0; i < 4; ++i) {
5449 bool isZero = !(NonZeros & (1 << i));
5450 if (isZero)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00005451 V[i] = getZeroVector(VT, Subtarget, DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005452 else
Dale Johannesenace16102009-02-03 19:33:06 +00005453 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005454 }
5455
5456 for (unsigned i = 0; i < 2; ++i) {
5457 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
5458 default: break;
5459 case 0:
5460 V[i] = V[i*2]; // Must be a zero vector.
5461 break;
5462 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00005463 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005464 break;
5465 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00005466 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005467 break;
5468 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00005469 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005470 break;
5471 }
5472 }
5473
Benjamin Kramer9c683542012-01-30 15:16:21 +00005474 bool Reverse1 = (NonZeros & 0x3) == 2;
5475 bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
5476 int MaskVec[] = {
5477 Reverse1 ? 1 : 0,
5478 Reverse1 ? 0 : 1,
Benjamin Kramer630ecf02012-01-30 20:01:35 +00005479 static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
5480 static_cast<int>(Reverse2 ? NumElems : NumElems+1)
Benjamin Kramer9c683542012-01-30 15:16:21 +00005481 };
Nate Begeman9008ca62009-04-27 18:41:29 +00005482 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005483 }
5484
Craig Topper7a9a28b2012-08-12 02:23:29 +00005485 if (Values.size() > 1 && VT.is128BitVector()) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00005486 // Check for a build vector of consecutive loads.
5487 for (unsigned i = 0; i < NumElems; ++i)
5488 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005489
Nate Begemanfdea31a2010-03-24 20:49:50 +00005490 // Check for elements which are consecutive loads.
5491 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
5492 if (LD.getNode())
5493 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005494
5495 // For SSE 4.1, use insertps to put the high elements into the low element.
Craig Topperd0a31172012-01-10 06:37:29 +00005496 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00005497 SDValue Result;
5498 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
5499 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
5500 else
5501 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00005502
Chris Lattner24faf612010-08-28 17:59:08 +00005503 for (unsigned i = 1; i < NumElems; ++i) {
5504 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
5505 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00005506 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00005507 }
5508 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00005509 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00005510
Chris Lattner6e80e442010-08-28 17:15:43 +00005511 // Otherwise, expand into a number of unpckl*, start by extending each of
5512 // our (non-undef) elements to the full vector width with the element in the
5513 // bottom slot of the vector (which generates no code for SSE).
5514 for (unsigned i = 0; i < NumElems; ++i) {
5515 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
5516 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
5517 else
5518 V[i] = DAG.getUNDEF(VT);
5519 }
5520
5521 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005522 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
5523 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
5524 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00005525 unsigned EltStride = NumElems >> 1;
5526 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00005527 for (unsigned i = 0; i < EltStride; ++i) {
5528 // If V[i+EltStride] is undef and this is the first round of mixing,
5529 // then it is safe to just drop this shuffle: V[i] is already in the
5530 // right place, the one element (since it's the first round) being
5531 // inserted as undef can be dropped. This isn't safe for successive
5532 // rounds because they will permute elements within both vectors.
5533 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
5534 EltStride == NumElems/2)
5535 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00005536
Chris Lattner6e80e442010-08-28 17:15:43 +00005537 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00005538 }
Chris Lattner6e80e442010-08-28 17:15:43 +00005539 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005540 }
5541 return V[0];
5542 }
Dan Gohman475871a2008-07-27 21:46:04 +00005543 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005544}
5545
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005546// LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction
5547// to create 256-bit vectors from two other 128-bit ones.
5548static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5549 DebugLoc dl = Op.getDebugLoc();
5550 EVT ResVT = Op.getValueType();
5551
Craig Topper7a9a28b2012-08-12 02:23:29 +00005552 assert(ResVT.is256BitVector() && "Value type must be 256-bit wide");
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005553
5554 SDValue V1 = Op.getOperand(0);
5555 SDValue V2 = Op.getOperand(1);
5556 unsigned NumElems = ResVT.getVectorNumElements();
5557
Craig Topper4c7972d2012-04-22 18:15:59 +00005558 return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005559}
5560
5561SDValue
5562X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005563 assert(Op.getNumOperands() == 2);
Bruno Cardoso Lopes8af24512011-08-01 21:54:02 +00005564
5565 // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors
5566 // from two other 128-bit ones.
5567 return LowerAVXCONCAT_VECTORS(Op, DAG);
5568}
5569
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005570// Try to lower a shuffle node into a simple blend instruction.
Craig Topper1842ba02012-04-23 06:38:28 +00005571static SDValue LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp,
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005572 const X86Subtarget *Subtarget,
Nadav Rotem91794872012-04-11 11:05:21 +00005573 SelectionDAG &DAG) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005574 SDValue V1 = SVOp->getOperand(0);
5575 SDValue V2 = SVOp->getOperand(1);
5576 DebugLoc dl = SVOp->getDebugLoc();
Craig Topper708e44f2012-04-23 07:36:33 +00005577 MVT VT = SVOp->getValueType(0).getSimpleVT();
Craig Topper1842ba02012-04-23 06:38:28 +00005578 unsigned NumElems = VT.getVectorNumElements();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005579
Nadav Roteme6113782012-04-11 06:40:27 +00005580 if (!Subtarget->hasSSE41())
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005581 return SDValue();
5582
Craig Topper1842ba02012-04-23 06:38:28 +00005583 unsigned ISDNo = 0;
Nadav Roteme6113782012-04-11 06:40:27 +00005584 MVT OpTy;
5585
Craig Topper708e44f2012-04-23 07:36:33 +00005586 switch (VT.SimpleTy) {
Nadav Roteme6113782012-04-11 06:40:27 +00005587 default: return SDValue();
5588 case MVT::v8i16:
Craig Topper1842ba02012-04-23 06:38:28 +00005589 ISDNo = X86ISD::BLENDPW;
5590 OpTy = MVT::v8i16;
5591 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005592 case MVT::v4i32:
5593 case MVT::v4f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005594 ISDNo = X86ISD::BLENDPS;
5595 OpTy = MVT::v4f32;
5596 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005597 case MVT::v2i64:
5598 case MVT::v2f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005599 ISDNo = X86ISD::BLENDPD;
5600 OpTy = MVT::v2f64;
5601 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005602 case MVT::v8i32:
5603 case MVT::v8f32:
Craig Topper1842ba02012-04-23 06:38:28 +00005604 if (!Subtarget->hasAVX())
5605 return SDValue();
5606 ISDNo = X86ISD::BLENDPS;
5607 OpTy = MVT::v8f32;
5608 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005609 case MVT::v4i64:
5610 case MVT::v4f64:
Craig Topper1842ba02012-04-23 06:38:28 +00005611 if (!Subtarget->hasAVX())
5612 return SDValue();
5613 ISDNo = X86ISD::BLENDPD;
5614 OpTy = MVT::v4f64;
5615 break;
Nadav Roteme6113782012-04-11 06:40:27 +00005616 }
5617 assert(ISDNo && "Invalid Op Number");
5618
5619 unsigned MaskVals = 0;
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005620
Craig Topper1842ba02012-04-23 06:38:28 +00005621 for (unsigned i = 0; i != NumElems; ++i) {
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005622 int EltIdx = SVOp->getMaskElt(i);
Craig Topper1842ba02012-04-23 06:38:28 +00005623 if (EltIdx == (int)i || EltIdx < 0)
Nadav Roteme6113782012-04-11 06:40:27 +00005624 MaskVals |= (1<<i);
Craig Topper1842ba02012-04-23 06:38:28 +00005625 else if (EltIdx == (int)(i + NumElems))
Nadav Roteme6113782012-04-11 06:40:27 +00005626 continue; // Bit is set to zero;
Craig Topper1842ba02012-04-23 06:38:28 +00005627 else
5628 return SDValue();
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005629 }
5630
Nadav Roteme6113782012-04-11 06:40:27 +00005631 V1 = DAG.getNode(ISD::BITCAST, dl, OpTy, V1);
5632 V2 = DAG.getNode(ISD::BITCAST, dl, OpTy, V2);
5633 SDValue Ret = DAG.getNode(ISDNo, dl, OpTy, V1, V2,
5634 DAG.getConstant(MaskVals, MVT::i32));
5635 return DAG.getNode(ISD::BITCAST, dl, VT, Ret);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00005636}
5637
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638// v8i16 shuffles - Prefer shuffles in the following order:
5639// 1. [all] pshuflw, pshufhw, optional move
5640// 2. [ssse3] 1 x pshufb
5641// 3. [ssse3] 2 x pshufb + 1 x por
5642// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005643SDValue
5644X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
5645 SelectionDAG &DAG) const {
5646 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00005647 SDValue V1 = SVOp->getOperand(0);
5648 SDValue V2 = SVOp->getOperand(1);
5649 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00005650 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00005651
Nate Begemanb9a47b82009-02-23 08:49:38 +00005652 // Determine if more than 1 of the words in each of the low and high quadwords
5653 // of the result come from the same quadword of one of the two inputs. Undef
5654 // mask values count as coming from any quadword, for better codegen.
Benjamin Kramer003fad92011-10-15 13:28:31 +00005655 unsigned LoQuad[] = { 0, 0, 0, 0 };
5656 unsigned HiQuad[] = { 0, 0, 0, 0 };
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005657 std::bitset<4> InputQuads;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005658 for (unsigned i = 0; i < 8; ++i) {
Benjamin Kramer003fad92011-10-15 13:28:31 +00005659 unsigned *Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00005660 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005661 MaskVals.push_back(EltIdx);
5662 if (EltIdx < 0) {
5663 ++Quad[0];
5664 ++Quad[1];
5665 ++Quad[2];
5666 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00005667 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005668 }
5669 ++Quad[EltIdx / 4];
5670 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00005671 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005672
Nate Begemanb9a47b82009-02-23 08:49:38 +00005673 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005674 unsigned MaxQuad = 1;
5675 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005676 if (LoQuad[i] > MaxQuad) {
5677 BestLoQuad = i;
5678 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005679 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005680 }
5681
Nate Begemanb9a47b82009-02-23 08:49:38 +00005682 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00005683 MaxQuad = 1;
5684 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005685 if (HiQuad[i] > MaxQuad) {
5686 BestHiQuad = i;
5687 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00005688 }
5689 }
5690
Nate Begemanb9a47b82009-02-23 08:49:38 +00005691 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00005692 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00005693 // single pshufb instruction is necessary. If There are more than 2 input
5694 // quads, disable the next transformation since it does not help SSSE3.
5695 bool V1Used = InputQuads[0] || InputQuads[1];
5696 bool V2Used = InputQuads[2] || InputQuads[3];
Craig Topperd0a31172012-01-10 06:37:29 +00005697 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005698 if (InputQuads.count() == 2 && V1Used && V2Used) {
Benjamin Kramer699ddcb2012-02-06 12:06:18 +00005699 BestLoQuad = InputQuads[0] ? 0 : 1;
5700 BestHiQuad = InputQuads[2] ? 2 : 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005701 }
5702 if (InputQuads.count() > 2) {
5703 BestLoQuad = -1;
5704 BestHiQuad = -1;
5705 }
5706 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005707
Nate Begemanb9a47b82009-02-23 08:49:38 +00005708 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
5709 // the shuffle mask. If a quad is scored as -1, that means that it contains
5710 // words from all 4 input quadwords.
5711 SDValue NewV;
5712 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005713 int MaskV[] = {
5714 BestLoQuad < 0 ? 0 : BestLoQuad,
5715 BestHiQuad < 0 ? 1 : BestHiQuad
5716 };
Eric Christopherfd179292009-08-27 18:07:15 +00005717 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005718 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
5719 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
5720 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00005721
Nate Begemanb9a47b82009-02-23 08:49:38 +00005722 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
5723 // source words for the shuffle, to aid later transformations.
5724 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00005725 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00005726 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005727 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00005728 if (idx != (int)i)
5729 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005730 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00005731 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005732 AllWordsInNewV = false;
5733 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00005734 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00005735
Nate Begemanb9a47b82009-02-23 08:49:38 +00005736 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
5737 if (AllWordsInNewV) {
5738 for (int i = 0; i != 8; ++i) {
5739 int idx = MaskVals[i];
5740 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00005741 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005742 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005743 if ((idx != i) && idx < 4)
5744 pshufhw = false;
5745 if ((idx != i) && idx > 3)
5746 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00005747 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005748 V1 = NewV;
5749 V2Used = false;
5750 BestLoQuad = 0;
5751 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00005752 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005753
Nate Begemanb9a47b82009-02-23 08:49:38 +00005754 // If we've eliminated the use of V2, and the new mask is a pshuflw or
5755 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00005756 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005757 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
5758 unsigned TargetMask = 0;
5759 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00005760 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Craig Topperdd637ae2012-02-19 05:41:45 +00005761 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
5762 TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp):
5763 getShufflePSHUFLWImmediate(SVOp);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00005764 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005765 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00005766 }
Evan Cheng14b32e12007-12-11 01:46:18 +00005767 }
Eric Christopherfd179292009-08-27 18:07:15 +00005768
Nate Begemanb9a47b82009-02-23 08:49:38 +00005769 // If we have SSSE3, and all words of the result are from 1 input vector,
5770 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
5771 // is present, fall back to case 4.
Craig Topperd0a31172012-01-10 06:37:29 +00005772 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005773 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005774
Nate Begemanb9a47b82009-02-23 08:49:38 +00005775 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00005776 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00005777 // mask, and elements that come from V1 in the V2 mask, so that the two
5778 // results can be OR'd together.
5779 bool TwoInputs = V1Used && V2Used;
5780 for (unsigned i = 0; i != 8; ++i) {
5781 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005782 int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx;
5783 int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1;
5784 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5785 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005786 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005787 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005788 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005789 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005790 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005791 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005792 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00005793
Nate Begemanb9a47b82009-02-23 08:49:38 +00005794 // Calculate the shuffle mask for the second input, shuffle it, and
5795 // OR it with the first shuffled input.
5796 pshufbMask.clear();
5797 for (unsigned i = 0; i != 8; ++i) {
5798 int EltIdx = MaskVals[i] * 2;
Craig Topperbe97ae92012-05-18 07:07:36 +00005799 int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16;
5800 int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15;
5801 pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8));
5802 pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005803 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005804 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00005805 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005806 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005807 MVT::v16i8, &pshufbMask[0], 16));
5808 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005809 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005810 }
5811
5812 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
5813 // and update MaskVals with new element order.
Benjamin Kramer9c683542012-01-30 15:16:21 +00005814 std::bitset<8> InOrder;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005815 if (BestLoQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005816 int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005817 for (int i = 0; i != 4; ++i) {
5818 int idx = MaskVals[i];
5819 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005820 InOrder.set(i);
5821 } else if ((idx / 4) == BestLoQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005822 MaskV[i] = idx & 3;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005823 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005824 }
5825 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005826 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005827 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005828
Craig Topperdd637ae2012-02-19 05:41:45 +00005829 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5830 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005831 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005832 NewV.getOperand(0),
5833 getShufflePSHUFLWImmediate(SVOp), DAG);
5834 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005835 }
Eric Christopherfd179292009-08-27 18:07:15 +00005836
Nate Begemanb9a47b82009-02-23 08:49:38 +00005837 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
5838 // and update MaskVals with the new element order.
5839 if (BestHiQuad >= 0) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005840 int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 };
Nate Begemanb9a47b82009-02-23 08:49:38 +00005841 for (unsigned i = 4; i != 8; ++i) {
5842 int idx = MaskVals[i];
5843 if (idx < 0) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005844 InOrder.set(i);
5845 } else if ((idx / 4) == BestHiQuad) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00005846 MaskV[i] = (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005847 InOrder.set(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005848 }
5849 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005850 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00005851 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005852
Craig Topperdd637ae2012-02-19 05:41:45 +00005853 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) {
5854 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode());
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00005855 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
Craig Topperdd637ae2012-02-19 05:41:45 +00005856 NewV.getOperand(0),
5857 getShufflePSHUFHWImmediate(SVOp), DAG);
5858 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00005859 }
Eric Christopherfd179292009-08-27 18:07:15 +00005860
Nate Begemanb9a47b82009-02-23 08:49:38 +00005861 // In case BestHi & BestLo were both -1, which means each quadword has a word
5862 // from each of the four input quadwords, calculate the InOrder bitvector now
5863 // before falling through to the insert/extract cleanup.
5864 if (BestLoQuad == -1 && BestHiQuad == -1) {
5865 NewV = V1;
5866 for (int i = 0; i != 8; ++i)
5867 if (MaskVals[i] < 0 || MaskVals[i] == i)
5868 InOrder.set(i);
5869 }
Eric Christopherfd179292009-08-27 18:07:15 +00005870
Nate Begemanb9a47b82009-02-23 08:49:38 +00005871 // The other elements are put in the right place using pextrw and pinsrw.
5872 for (unsigned i = 0; i != 8; ++i) {
5873 if (InOrder[i])
5874 continue;
5875 int EltIdx = MaskVals[i];
5876 if (EltIdx < 0)
5877 continue;
Craig Topper6643d9c2012-05-04 06:18:33 +00005878 SDValue ExtOp = (EltIdx < 8) ?
5879 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
5880 DAG.getIntPtrConstant(EltIdx)) :
5881 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005882 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00005883 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005884 DAG.getIntPtrConstant(i));
5885 }
5886 return NewV;
5887}
5888
5889// v16i8 shuffles - Prefer shuffles in the following order:
5890// 1. [ssse3] 1 x pshufb
5891// 2. [ssse3] 2 x pshufb + 1 x por
5892// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
5893static
Nate Begeman9008ca62009-04-27 18:41:29 +00005894SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00005895 SelectionDAG &DAG,
5896 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005897 SDValue V1 = SVOp->getOperand(0);
5898 SDValue V2 = SVOp->getOperand(1);
5899 DebugLoc dl = SVOp->getDebugLoc();
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005900 ArrayRef<int> MaskVals = SVOp->getMask();
Eric Christopherfd179292009-08-27 18:07:15 +00005901
Nate Begemanb9a47b82009-02-23 08:49:38 +00005902 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00005903 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00005904 // present, fall back to case 3.
Eric Christopherfd179292009-08-27 18:07:15 +00005905
Nate Begemanb9a47b82009-02-23 08:49:38 +00005906 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
Craig Topperd0a31172012-01-10 06:37:29 +00005907 if (TLI.getSubtarget()->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00005908 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00005909
Nate Begemanb9a47b82009-02-23 08:49:38 +00005910 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00005911 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005912 //
5913 // Otherwise, we have elements from both input vectors, and must zero out
5914 // elements that come from V2 in the first mask, and V1 in the second mask
5915 // so that we can OR them together.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005916 for (unsigned i = 0; i != 16; ++i) {
5917 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005918 if (EltIdx < 0 || EltIdx >= 16)
5919 EltIdx = 0x80;
Owen Anderson825b72b2009-08-11 20:47:22 +00005920 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005921 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005922 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00005923 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005924 MVT::v16i8, &pshufbMask[0], 16));
Michael Liao265bcb12012-08-31 20:12:31 +00005925
5926 // As PSHUFB will zero elements with negative indices, it's safe to ignore
5927 // the 2nd operand if it's undefined or zero.
5928 if (V2.getOpcode() == ISD::UNDEF ||
5929 ISD::isBuildVectorAllZeros(V2.getNode()))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005930 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00005931
Nate Begemanb9a47b82009-02-23 08:49:38 +00005932 // Calculate the shuffle mask for the second input, shuffle it, and
5933 // OR it with the first shuffled input.
5934 pshufbMask.clear();
5935 for (unsigned i = 0; i != 16; ++i) {
5936 int EltIdx = MaskVals[i];
Craig Topperb82b5ab2012-05-18 06:42:06 +00005937 EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16;
Craig Topper85b9e562012-05-22 06:09:38 +00005938 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005939 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005940 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00005941 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005942 MVT::v16i8, &pshufbMask[0], 16));
5943 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005944 }
Eric Christopherfd179292009-08-27 18:07:15 +00005945
Nate Begemanb9a47b82009-02-23 08:49:38 +00005946 // No SSSE3 - Calculate in place words and then fix all out of place words
5947 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
5948 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005949 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
5950 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Craig Topperb82b5ab2012-05-18 06:42:06 +00005951 SDValue NewV = V1;
Nate Begemanb9a47b82009-02-23 08:49:38 +00005952 for (int i = 0; i != 8; ++i) {
5953 int Elt0 = MaskVals[i*2];
5954 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00005955
Nate Begemanb9a47b82009-02-23 08:49:38 +00005956 // This word of the result is all undef, skip it.
5957 if (Elt0 < 0 && Elt1 < 0)
5958 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005959
Nate Begemanb9a47b82009-02-23 08:49:38 +00005960 // This word of the result is already in the correct place, skip it.
Craig Topperb82b5ab2012-05-18 06:42:06 +00005961 if ((Elt0 == i*2) && (Elt1 == i*2+1))
Nate Begemanb9a47b82009-02-23 08:49:38 +00005962 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00005963
Nate Begemanb9a47b82009-02-23 08:49:38 +00005964 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
5965 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
5966 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00005967
5968 // If Elt0 and Elt1 are defined, are consecutive, and can be load
5969 // using a single extract together, load it and store it.
5970 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005971 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005972 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00005973 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00005974 DAG.getIntPtrConstant(i));
5975 continue;
5976 }
5977
Nate Begemanb9a47b82009-02-23 08:49:38 +00005978 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00005979 // source byte is not also odd, shift the extracted word left 8 bits
5980 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00005981 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005982 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005983 DAG.getIntPtrConstant(Elt1 / 2));
5984 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005985 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Owen Anderson95771af2011-02-25 21:41:48 +00005986 DAG.getConstant(8,
5987 TLI.getShiftAmountTy(InsElt.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00005988 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005989 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
5990 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00005991 }
5992 // If Elt0 is defined, extract it from the appropriate source. If the
5993 // source byte is not also even, shift the extracted word right 8 bits. If
5994 // Elt1 was also defined, OR the extracted values together before
5995 // inserting them in the result.
5996 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005997 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00005998 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
5999 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006000 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Owen Anderson95771af2011-02-25 21:41:48 +00006001 DAG.getConstant(8,
6002 TLI.getShiftAmountTy(InsElt0.getValueType())));
Mon P Wang6b3ef692009-03-11 18:47:57 +00006003 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006004 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
6005 DAG.getConstant(0x00FF, MVT::i16));
6006 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00006007 : InsElt0;
6008 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006009 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00006010 DAG.getIntPtrConstant(i));
6011 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006012 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00006013}
6014
Elena Demikhovsky41789462012-09-06 12:42:01 +00006015// v32i8 shuffles - Translate to VPSHUFB if possible.
6016static
6017SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp,
6018 SelectionDAG &DAG,
6019 const X86TargetLowering &TLI) {
6020 EVT VT = SVOp->getValueType(0);
6021 SDValue V1 = SVOp->getOperand(0);
6022 SDValue V2 = SVOp->getOperand(1);
6023 DebugLoc dl = SVOp->getDebugLoc();
6024 ArrayRef<int> MaskVals = SVOp->getMask();
6025
6026 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
6027
6028 if (VT != MVT::v32i8 || !TLI.getSubtarget()->hasAVX2() || !V2IsUndef)
6029 return SDValue();
6030
6031 SmallVector<SDValue,32> pshufbMask;
6032 for (unsigned i = 0; i != 32; i++) {
6033 int EltIdx = MaskVals[i];
6034 if (EltIdx < 0 || EltIdx >= 32)
6035 EltIdx = 0x80;
6036 else {
6037 if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16))
6038 // Cross lane is not allowed.
6039 return SDValue();
6040 EltIdx &= 0xf;
6041 }
6042 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
6043 }
6044 return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1,
6045 DAG.getNode(ISD::BUILD_VECTOR, dl,
6046 MVT::v32i8, &pshufbMask[0], 32));
6047}
6048
Evan Cheng7a831ce2007-12-15 03:00:47 +00006049/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006050/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00006051/// done when every pair / quad of shuffle mask elements point to elements in
6052/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00006053/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00006054static
Nate Begeman9008ca62009-04-27 18:41:29 +00006055SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006056 SelectionDAG &DAG, DebugLoc dl) {
Craig Topper11ac1f82012-05-04 04:08:44 +00006057 MVT VT = SVOp->getValueType(0).getSimpleVT();
Nate Begeman9008ca62009-04-27 18:41:29 +00006058 unsigned NumElems = VT.getVectorNumElements();
Craig Topper11ac1f82012-05-04 04:08:44 +00006059 MVT NewVT;
6060 unsigned Scale;
6061 switch (VT.SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +00006062 default: llvm_unreachable("Unexpected!");
Craig Topperf3640d72012-05-04 04:44:49 +00006063 case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break;
6064 case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break;
6065 case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break;
6066 case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break;
6067 case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break;
6068 case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00006069 }
6070
Nate Begeman9008ca62009-04-27 18:41:29 +00006071 SmallVector<int, 8> MaskVec;
Craig Topper11ac1f82012-05-04 04:08:44 +00006072 for (unsigned i = 0; i != NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006073 int StartIdx = -1;
Craig Topper11ac1f82012-05-04 04:08:44 +00006074 for (unsigned j = 0; j != Scale; ++j) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006075 int EltIdx = SVOp->getMaskElt(i+j);
6076 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00006077 continue;
Craig Topper11ac1f82012-05-04 04:08:44 +00006078 if (StartIdx < 0)
6079 StartIdx = (EltIdx / Scale);
6080 if (EltIdx != (int)(StartIdx*Scale + j))
Dan Gohman475871a2008-07-27 21:46:04 +00006081 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006082 }
Craig Topper11ac1f82012-05-04 04:08:44 +00006083 MaskVec.push_back(StartIdx);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006084 }
6085
Craig Topper11ac1f82012-05-04 04:08:44 +00006086 SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0));
6087 SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1));
Nate Begeman9008ca62009-04-27 18:41:29 +00006088 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00006089}
6090
Evan Chengd880b972008-05-09 21:53:03 +00006091/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006092///
Owen Andersone50ed302009-08-10 22:56:29 +00006093static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00006094 SDValue SrcOp, SelectionDAG &DAG,
6095 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006096 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006097 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00006098 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006099 LD = dyn_cast<LoadSDNode>(SrcOp);
6100 if (!LD) {
6101 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
6102 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00006103 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00006104 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00006105 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006106 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00006107 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006108 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00006109 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006110 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006111 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
6112 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6113 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00006114 SrcOp.getOperand(0)
6115 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006116 }
6117 }
6118 }
6119
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006120 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00006121 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006122 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00006123 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00006124}
6125
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006126/// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles
6127/// which could not be matched by any known target speficic shuffle
6128static SDValue
6129LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Elena Demikhovsky15963732012-06-26 08:04:10 +00006130
6131 SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG);
6132 if (NewOp.getNode())
6133 return NewOp;
6134
Craig Topper8f35c132012-01-20 09:29:03 +00006135 EVT VT = SVOp->getValueType(0);
Bruno Cardoso Lopes3b865982011-08-16 18:21:54 +00006136
Craig Topper8f35c132012-01-20 09:29:03 +00006137 unsigned NumElems = VT.getVectorNumElements();
6138 unsigned NumLaneElems = NumElems / 2;
6139
Craig Topper8f35c132012-01-20 09:29:03 +00006140 DebugLoc dl = SVOp->getDebugLoc();
6141 MVT EltVT = VT.getVectorElementType().getSimpleVT();
Craig Topper9a2b6e12012-04-06 07:45:23 +00006142 EVT NVT = MVT::getVectorVT(EltVT, NumLaneElems);
Craig Topper8ae97ba2012-05-21 06:40:16 +00006143 SDValue Output[2];
Craig Topper8f35c132012-01-20 09:29:03 +00006144
Craig Topper9a2b6e12012-04-06 07:45:23 +00006145 SmallVector<int, 16> Mask;
Craig Topper8f35c132012-01-20 09:29:03 +00006146 for (unsigned l = 0; l < 2; ++l) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006147 // Build a shuffle mask for the output, discovering on the fly which
6148 // input vectors to use as shuffle operands (recorded in InputUsed).
6149 // If building a suitable shuffle vector proves too hard, then bail
Craig Topper8ae97ba2012-05-21 06:40:16 +00006150 // out with UseBuildVector set.
6151 bool UseBuildVector = false;
Benjamin Kramer9e5512a2012-04-06 13:33:52 +00006152 int InputUsed[2] = { -1, -1 }; // Not yet discovered.
Craig Topper9a2b6e12012-04-06 07:45:23 +00006153 unsigned LaneStart = l * NumLaneElems;
6154 for (unsigned i = 0; i != NumLaneElems; ++i) {
6155 // The mask element. This indexes into the input.
6156 int Idx = SVOp->getMaskElt(i+LaneStart);
6157 if (Idx < 0) {
6158 // the mask element does not index into any input vector.
6159 Mask.push_back(-1);
6160 continue;
6161 }
Craig Topper8f35c132012-01-20 09:29:03 +00006162
Craig Topper9a2b6e12012-04-06 07:45:23 +00006163 // The input vector this mask element indexes into.
6164 int Input = Idx / NumLaneElems;
Craig Topper8f35c132012-01-20 09:29:03 +00006165
Craig Topper9a2b6e12012-04-06 07:45:23 +00006166 // Turn the index into an offset from the start of the input vector.
6167 Idx -= Input * NumLaneElems;
6168
6169 // Find or create a shuffle vector operand to hold this input.
6170 unsigned OpNo;
6171 for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) {
6172 if (InputUsed[OpNo] == Input)
6173 // This input vector is already an operand.
6174 break;
6175 if (InputUsed[OpNo] < 0) {
6176 // Create a new operand for this input vector.
6177 InputUsed[OpNo] = Input;
6178 break;
6179 }
6180 }
6181
6182 if (OpNo >= array_lengthof(InputUsed)) {
Craig Topper8ae97ba2012-05-21 06:40:16 +00006183 // More than two input vectors used! Give up on trying to create a
6184 // shuffle vector. Insert all elements into a BUILD_VECTOR instead.
6185 UseBuildVector = true;
6186 break;
Craig Topper9a2b6e12012-04-06 07:45:23 +00006187 }
6188
6189 // Add the mask index for the new shuffle vector.
6190 Mask.push_back(Idx + OpNo * NumLaneElems);
6191 }
6192
Craig Topper8ae97ba2012-05-21 06:40:16 +00006193 if (UseBuildVector) {
6194 SmallVector<SDValue, 16> SVOps;
6195 for (unsigned i = 0; i != NumLaneElems; ++i) {
6196 // The mask element. This indexes into the input.
6197 int Idx = SVOp->getMaskElt(i+LaneStart);
6198 if (Idx < 0) {
6199 SVOps.push_back(DAG.getUNDEF(EltVT));
6200 continue;
6201 }
6202
6203 // The input vector this mask element indexes into.
6204 int Input = Idx / NumElems;
6205
6206 // Turn the index into an offset from the start of the input vector.
6207 Idx -= Input * NumElems;
6208
6209 // Extract the vector element by hand.
6210 SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6211 SVOp->getOperand(Input),
6212 DAG.getIntPtrConstant(Idx)));
6213 }
6214
6215 // Construct the output using a BUILD_VECTOR.
6216 Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0],
6217 SVOps.size());
6218 } else if (InputUsed[0] < 0) {
Craig Topper9a2b6e12012-04-06 07:45:23 +00006219 // No input vectors were used! The result is undefined.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006220 Output[l] = DAG.getUNDEF(NVT);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006221 } else {
6222 SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006223 (InputUsed[0] % 2) * NumLaneElems,
6224 DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006225 // If only one input was used, use an undefined vector for the other.
6226 SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) :
6227 Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2),
Craig Topperb14940a2012-04-22 20:55:18 +00006228 (InputUsed[1] % 2) * NumLaneElems, DAG, dl);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006229 // At least one input vector was used. Create a new shuffle vector.
Craig Topper8ae97ba2012-05-21 06:40:16 +00006230 Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]);
Craig Topper9a2b6e12012-04-06 07:45:23 +00006231 }
6232
6233 Mask.clear();
6234 }
Craig Topper8f35c132012-01-20 09:29:03 +00006235
6236 // Concatenate the result back
Craig Topper8ae97ba2012-05-21 06:40:16 +00006237 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]);
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006238}
6239
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006240/// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with
6241/// 4 elements, and match them with several different shuffle types.
Dan Gohman475871a2008-07-27 21:46:04 +00006242static SDValue
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006243LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006244 SDValue V1 = SVOp->getOperand(0);
6245 SDValue V2 = SVOp->getOperand(1);
6246 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006247 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00006248
Craig Topper7a9a28b2012-08-12 02:23:29 +00006249 assert(VT.is128BitVector() && "Unsupported vector size");
Bruno Cardoso Lopes589b8972011-07-22 00:14:53 +00006250
Benjamin Kramer9c683542012-01-30 15:16:21 +00006251 std::pair<int, int> Locs[4];
6252 int Mask1[] = { -1, -1, -1, -1 };
Benjamin Kramered4c8c62012-01-15 13:16:05 +00006253 SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end());
Nate Begeman9008ca62009-04-27 18:41:29 +00006254
Evan Chengace3c172008-07-22 21:13:36 +00006255 unsigned NumHi = 0;
6256 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00006257 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006258 int Idx = PermMask[i];
6259 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006260 Locs[i] = std::make_pair(-1, -1);
6261 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00006262 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
6263 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006264 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00006265 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006266 NumLo++;
6267 } else {
6268 Locs[i] = std::make_pair(1, NumHi);
6269 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00006270 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006271 NumHi++;
6272 }
6273 }
6274 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006275
Evan Chengace3c172008-07-22 21:13:36 +00006276 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006277 // If no more than two elements come from either vector. This can be
6278 // implemented with two shuffles. First shuffle gather the elements.
6279 // The second shuffle, which takes the first shuffle as both of its
6280 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00006281 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006282
Benjamin Kramer9c683542012-01-30 15:16:21 +00006283 int Mask2[] = { -1, -1, -1, -1 };
Eric Christopherfd179292009-08-27 18:07:15 +00006284
Benjamin Kramer9c683542012-01-30 15:16:21 +00006285 for (unsigned i = 0; i != 4; ++i)
6286 if (Locs[i].first != -1) {
Evan Chengace3c172008-07-22 21:13:36 +00006287 unsigned Idx = (i < 2) ? 0 : 4;
6288 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006289 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006290 }
Evan Chengace3c172008-07-22 21:13:36 +00006291
Nate Begeman9008ca62009-04-27 18:41:29 +00006292 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Craig Topper69947b92012-04-23 06:57:04 +00006293 }
6294
6295 if (NumLo == 3 || NumHi == 3) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006296 // Otherwise, we must have three elements from one vector, call it X, and
6297 // one element from the other, call it Y. First, use a shufps to build an
6298 // intermediate vector with the one element from Y and the element from X
6299 // that will be in the same half in the final destination (the indexes don't
6300 // matter). Then, use a shufps to build the final vector, taking the half
6301 // containing the element from Y from the intermediate, and the other half
6302 // from X.
6303 if (NumHi == 3) {
6304 // Normalize it so the 3 elements come from V1.
Craig Topperbeabc6c2011-12-05 06:56:46 +00006305 CommuteVectorShuffleMask(PermMask, 4);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006306 std::swap(V1, V2);
6307 }
6308
6309 // Find the element from V2.
6310 unsigned HiIndex;
6311 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006312 int Val = PermMask[HiIndex];
6313 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006314 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006315 if (Val >= 4)
6316 break;
6317 }
6318
Nate Begeman9008ca62009-04-27 18:41:29 +00006319 Mask1[0] = PermMask[HiIndex];
6320 Mask1[1] = -1;
6321 Mask1[2] = PermMask[HiIndex^1];
6322 Mask1[3] = -1;
6323 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006324
6325 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006326 Mask1[0] = PermMask[0];
6327 Mask1[1] = PermMask[1];
6328 Mask1[2] = HiIndex & 1 ? 6 : 4;
6329 Mask1[3] = HiIndex & 1 ? 4 : 6;
6330 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00006331 }
Craig Topper69947b92012-04-23 06:57:04 +00006332
6333 Mask1[0] = HiIndex & 1 ? 2 : 0;
6334 Mask1[1] = HiIndex & 1 ? 0 : 2;
6335 Mask1[2] = PermMask[2];
6336 Mask1[3] = PermMask[3];
6337 if (Mask1[2] >= 0)
6338 Mask1[2] += 4;
6339 if (Mask1[3] >= 0)
6340 Mask1[3] += 4;
6341 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006342 }
6343
6344 // Break it into (shuffle shuffle_hi, shuffle_lo).
Benjamin Kramer9c683542012-01-30 15:16:21 +00006345 int LoMask[] = { -1, -1, -1, -1 };
6346 int HiMask[] = { -1, -1, -1, -1 };
Nate Begeman9008ca62009-04-27 18:41:29 +00006347
Benjamin Kramer9c683542012-01-30 15:16:21 +00006348 int *MaskPtr = LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00006349 unsigned MaskIdx = 0;
6350 unsigned LoIdx = 0;
6351 unsigned HiIdx = 2;
6352 for (unsigned i = 0; i != 4; ++i) {
6353 if (i == 2) {
Benjamin Kramer9c683542012-01-30 15:16:21 +00006354 MaskPtr = HiMask;
Evan Chengace3c172008-07-22 21:13:36 +00006355 MaskIdx = 1;
6356 LoIdx = 0;
6357 HiIdx = 2;
6358 }
Nate Begeman9008ca62009-04-27 18:41:29 +00006359 int Idx = PermMask[i];
6360 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00006361 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00006362 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00006363 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006364 MaskPtr[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006365 LoIdx++;
6366 } else {
6367 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006368 MaskPtr[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00006369 HiIdx++;
6370 }
6371 }
6372
Nate Begeman9008ca62009-04-27 18:41:29 +00006373 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
6374 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
Benjamin Kramer9c683542012-01-30 15:16:21 +00006375 int MaskOps[] = { -1, -1, -1, -1 };
6376 for (unsigned i = 0; i != 4; ++i)
6377 if (Locs[i].first != -1)
6378 MaskOps[i] = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00006379 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00006380}
6381
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006382static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006383 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006384 V = V.getOperand(0);
6385 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6386 V = V.getOperand(0);
Evan Cheng7bc389b2011-11-08 00:31:58 +00006387 if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR &&
6388 V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF)
6389 // BUILD_VECTOR (load), undef
6390 V = V.getOperand(0);
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006391 if (MayFoldLoad(V))
6392 return true;
6393 return false;
6394}
6395
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006396// FIXME: the version above should always be used. Since there's
6397// a bug where several vector shuffles can't be folded because the
6398// DAG is not updated during lowering and a node claims to have two
6399// uses while it only has one, use this version, and let isel match
6400// another instruction if the load really happens to have more than
6401// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00006402// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006403static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006404 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006405 V = V.getOperand(0);
6406 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
6407 V = V.getOperand(0);
6408 if (ISD::isNormalLoad(V.getNode()))
6409 return true;
6410 return false;
6411}
6412
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006413static
Evan Cheng835580f2010-10-07 20:50:20 +00006414SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
6415 EVT VT = Op.getValueType();
6416
6417 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006418 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
6419 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00006420 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
6421 V1, DAG));
6422}
6423
6424static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006425SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
Craig Topper1accb7e2012-01-10 06:54:16 +00006426 bool HasSSE2) {
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006427 SDValue V1 = Op.getOperand(0);
6428 SDValue V2 = Op.getOperand(1);
6429 EVT VT = Op.getValueType();
6430
6431 assert(VT != MVT::v2i64 && "unsupported shuffle type");
6432
Craig Topper1accb7e2012-01-10 06:54:16 +00006433 if (HasSSE2 && VT == MVT::v2f64)
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006434 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
6435
Evan Cheng0899f5c2011-08-31 02:05:24 +00006436 // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1)
6437 return DAG.getNode(ISD::BITCAST, dl, VT,
6438 getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32,
6439 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1),
6440 DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG));
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006441}
6442
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006443static
6444SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
6445 SDValue V1 = Op.getOperand(0);
6446 SDValue V2 = Op.getOperand(1);
6447 EVT VT = Op.getValueType();
6448
6449 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
6450 "unsupported shuffle type");
6451
6452 if (V2.getOpcode() == ISD::UNDEF)
6453 V2 = V1;
6454
6455 // v4i32 or v4f32
6456 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
6457}
6458
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006459static
Craig Topper1accb7e2012-01-10 06:54:16 +00006460SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006461 SDValue V1 = Op.getOperand(0);
6462 SDValue V2 = Op.getOperand(1);
6463 EVT VT = Op.getValueType();
6464 unsigned NumElems = VT.getVectorNumElements();
6465
6466 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
6467 // operand of these instructions is only memory, so check if there's a
6468 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
6469 // same masks.
6470 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006471
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00006472 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00006473 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006474 CanFoldLoad = true;
6475
6476 // When V1 is a load, it can be folded later into a store in isel, example:
6477 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
6478 // turns into:
6479 // (MOVLPSmr addr:$src1, VR128:$src2)
6480 // So, recognize this potential and also use MOVLPS or MOVLPD
Evan Cheng7bc389b2011-11-08 00:31:58 +00006481 else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006482 CanFoldLoad = true;
6483
Dan Gohman65fd6562011-11-03 21:49:52 +00006484 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006485 if (CanFoldLoad) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006486 if (HasSSE2 && NumElems == 2)
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006487 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
6488
6489 if (NumElems == 4)
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00006490 // If we don't care about the second element, proceed to use movss.
Dan Gohman65fd6562011-11-03 21:49:52 +00006491 if (SVOp->getMaskElt(1) != -1)
6492 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006493 }
6494
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006495 // movl and movlp will both match v2i64, but v2i64 is never matched by
6496 // movl earlier because we make it strict to avoid messing with the movlp load
6497 // folding logic (see the code above getMOVLP call). Match it here then,
6498 // this is horrible, but will stay like this until we move all shuffle
6499 // matching to x86 specific nodes. Note that for the 1st condition all
6500 // types are matched with movsd.
Craig Topper1accb7e2012-01-10 06:54:16 +00006501 if (HasSSE2) {
Bruno Cardoso Lopes5ca0d142011-09-14 02:36:14 +00006502 // FIXME: isMOVLMask should be checked and matched before getMOVLP,
6503 // as to remove this logic from here, as much as possible
Craig Topper5aaffa82012-02-19 02:53:47 +00006504 if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT))
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006505 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006506 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopes57d6a5e2011-08-31 03:04:20 +00006507 }
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006508
6509 assert(VT != MVT::v4i32 && "unsupported shuffle type");
6510
6511 // Invert the operand order and use SHUFPS to match it.
Craig Topperb3982da2011-12-31 23:50:21 +00006512 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006513 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00006514}
6515
Nadav Rotem154819d2012-04-09 07:45:58 +00006516SDValue
6517X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006518 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
6519 EVT VT = Op.getValueType();
6520 DebugLoc dl = Op.getDebugLoc();
6521 SDValue V1 = Op.getOperand(0);
6522 SDValue V2 = Op.getOperand(1);
6523
6524 if (isZeroShuffle(SVOp))
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +00006525 return getZeroVector(VT, Subtarget, DAG, dl);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006526
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006527 // Handle splat operations
6528 if (SVOp->isSplat()) {
Bruno Cardoso Lopes9283b662011-07-21 01:55:42 +00006529 unsigned NumElem = VT.getVectorNumElements();
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006530 int Size = VT.getSizeInBits();
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006531
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006532 // Use vbroadcast whenever the splat comes from a foldable load
Nadav Rotem154819d2012-04-09 07:45:58 +00006533 SDValue Broadcast = LowerVectorBroadcast(Op, DAG);
Nadav Rotem9d68b062012-04-08 12:54:54 +00006534 if (Broadcast.getNode())
6535 return Broadcast;
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +00006536
Bruno Cardoso Lopes6a32adc2011-07-25 23:05:25 +00006537 // Handle splats by matching through known shuffle masks
Bruno Cardoso Lopesd8b7dd52011-08-23 22:06:37 +00006538 if ((Size == 128 && NumElem <= 4) ||
6539 (Size == 256 && NumElem < 8))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006540 return SDValue();
6541
Bruno Cardoso Lopes8a5b2622011-08-17 02:29:13 +00006542 // All remaning splats are promoted to target supported vector shuffles.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006543 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006544 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006545
6546 // If the shuffle can be profitably rewritten as a narrower shuffle, then
6547 // do it!
Craig Topperf3640d72012-05-04 04:44:49 +00006548 if (VT == MVT::v8i16 || VT == MVT::v16i8 ||
6549 VT == MVT::v16i16 || VT == MVT::v32i8) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006550 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6551 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006552 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006553 } else if ((VT == MVT::v4i32 ||
Craig Topper1accb7e2012-01-10 06:54:16 +00006554 (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006555 // FIXME: Figure out a cleaner way to do this.
6556 // Try to make use of movq to zero out the top part.
6557 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
6558 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
6559 if (NewOp.getNode()) {
Craig Topper5aaffa82012-02-19 02:53:47 +00006560 EVT NewVT = NewOp.getValueType();
6561 if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(),
6562 NewVT, true, false))
6563 return getVZextMovL(VT, NewVT, NewOp.getOperand(0),
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006564 DAG, Subtarget, dl);
6565 }
6566 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
6567 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
Craig Topper5aaffa82012-02-19 02:53:47 +00006568 if (NewOp.getNode()) {
6569 EVT NewVT = NewOp.getValueType();
6570 if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT))
6571 return getVZextMovL(VT, NewVT, NewOp.getOperand(1),
6572 DAG, Subtarget, dl);
6573 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006574 }
6575 }
6576 return SDValue();
6577}
6578
Dan Gohman475871a2008-07-27 21:46:04 +00006579SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006580X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00006581 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00006582 SDValue V1 = Op.getOperand(0);
6583 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00006584 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006585 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00006586 unsigned NumElems = VT.getVectorNumElements();
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006587 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006588 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00006589 bool V1IsSplat = false;
6590 bool V2IsSplat = false;
Craig Topper1accb7e2012-01-10 06:54:16 +00006591 bool HasSSE2 = Subtarget->hasSSE2();
Craig Topperbeabc6c2011-12-05 06:56:46 +00006592 bool HasAVX = Subtarget->hasAVX();
Craig Topper6347e862011-11-21 06:57:39 +00006593 bool HasAVX2 = Subtarget->hasAVX2();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006594 MachineFunction &MF = DAG.getMachineFunction();
6595 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006596
Craig Topper3426a3e2011-11-14 06:46:21 +00006597 assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles");
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00006598
Elena Demikhovsky16db7102012-01-12 20:33:10 +00006599 if (V1IsUndef && V2IsUndef)
6600 return DAG.getUNDEF(VT);
6601
6602 assert(!V1IsUndef && "Op 1 of shuffle should not be undef");
Craig Topper38034c52011-11-26 22:55:48 +00006603
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006604 // Vector shuffle lowering takes 3 steps:
6605 //
6606 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
6607 // narrowing and commutation of operands should be handled.
6608 // 2) Matching of shuffles with known shuffle masks to x86 target specific
6609 // shuffle nodes.
6610 // 3) Rewriting of unmatched masks into new generic shuffle operations,
6611 // so the shuffle can be broken into other shuffles and the legalizer can
6612 // try the lowering again.
6613 //
Craig Topper3426a3e2011-11-14 06:46:21 +00006614 // The general idea is that no vector_shuffle operation should be left to
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006615 // be matched during isel, all of them must be converted to a target specific
6616 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00006617
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006618 // Normalize the input vectors. Here splats, zeroed vectors, profitable
6619 // narrowing and commutation of operands should be handled. The actual code
6620 // doesn't include all of those, work in progress...
Nadav Rotem154819d2012-04-09 07:45:58 +00006621 SDValue NewOp = NormalizeVectorShuffle(Op, DAG);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00006622 if (NewOp.getNode())
6623 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00006624
Craig Topper5aaffa82012-02-19 02:53:47 +00006625 SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end());
6626
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006627 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
6628 // unpckh_undef). Only use pshufd if speed is more important than size.
Craig Topper5aaffa82012-02-19 02:53:47 +00006629 if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006630 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper5aaffa82012-02-19 02:53:47 +00006631 if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006632 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00006633
Craig Topperdd637ae2012-02-19 05:41:45 +00006634 if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() &&
Bruno Cardoso Lopes0c4b9ff2011-09-15 18:27:36 +00006635 V2IsUndef && RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00006636 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006637
Craig Topperdd637ae2012-02-19 05:41:45 +00006638 if (isMOVHLPS_v_undef_Mask(M, VT))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006639 return getMOVHighToLow(Op, dl, DAG);
6640
6641 // Use to match splats
Craig Topper5aaffa82012-02-19 02:53:47 +00006642 if (HasSSE2 && isUNPCKHMask(M, VT, HasAVX2) && V2IsUndef &&
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006643 (VT == MVT::v2f64 || VT == MVT::v2i64))
Craig Topper34671b82011-12-06 08:21:25 +00006644 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00006645
Craig Topper5aaffa82012-02-19 02:53:47 +00006646 if (isPSHUFDMask(M, VT)) {
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006647 // The actual implementation will match the mask in the if above and then
6648 // during isel it can match several different instructions, not only pshufd
6649 // as its name says, sad but true, emulate the behavior for now...
Craig Topperdd637ae2012-02-19 05:41:45 +00006650 if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
6651 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006652
Craig Topper5aaffa82012-02-19 02:53:47 +00006653 unsigned TargetMask = getShuffleSHUFImmediate(SVOp);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006654
Craig Topperdbd98a42012-02-07 06:28:42 +00006655 if (HasAVX && (VT == MVT::v4f32 || VT == MVT::v2f64))
6656 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, DAG);
6657
Craig Topper1accb7e2012-01-10 06:54:16 +00006658 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006659 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
6660
Craig Topperb3982da2011-12-31 23:50:21 +00006661 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1,
Bruno Cardoso Lopes07b7f672011-08-25 02:58:26 +00006662 TargetMask, DAG);
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00006663 }
Eric Christopherfd179292009-08-27 18:07:15 +00006664
Evan Chengf26ffe92008-05-29 08:22:04 +00006665 // Check if this can be converted into a logical shift.
6666 bool isLeft = false;
6667 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00006668 SDValue ShVal;
Craig Topper1accb7e2012-01-10 06:54:16 +00006669 bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00006670 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006671 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00006672 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006673 EVT EltVT = VT.getVectorElementType();
6674 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006675 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006676 }
Eric Christopherfd179292009-08-27 18:07:15 +00006677
Craig Topper5aaffa82012-02-19 02:53:47 +00006678 if (isMOVLMask(M, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006679 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00006680 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Craig Topperdd637ae2012-02-19 05:41:45 +00006681 if (!isMOVLPMask(M, VT)) {
Craig Topper1accb7e2012-01-10 06:54:16 +00006682 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006683 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
6684
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00006685 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00006686 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
6687 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00006688 }
Eric Christopherfd179292009-08-27 18:07:15 +00006689
Nate Begeman9008ca62009-04-27 18:41:29 +00006690 // FIXME: fold these into legal mask.
Craig Topperdd637ae2012-02-19 05:41:45 +00006691 if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasAVX2))
Craig Topper1accb7e2012-01-10 06:54:16 +00006692 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00006693
Craig Topperdd637ae2012-02-19 05:41:45 +00006694 if (isMOVHLPSMask(M, VT))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006695 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00006696
Craig Topperdd637ae2012-02-19 05:41:45 +00006697 if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006698 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00006699
Craig Topperdd637ae2012-02-19 05:41:45 +00006700 if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget))
Dale Johannesen0488fb62010-09-30 23:57:10 +00006701 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00006702
Craig Topperdd637ae2012-02-19 05:41:45 +00006703 if (isMOVLPMask(M, VT))
Craig Topper1accb7e2012-01-10 06:54:16 +00006704 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006705
Craig Topperdd637ae2012-02-19 05:41:45 +00006706 if (ShouldXformToMOVHLPS(M, VT) ||
6707 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT))
Nate Begeman9008ca62009-04-27 18:41:29 +00006708 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006709
Evan Chengf26ffe92008-05-29 08:22:04 +00006710 if (isShift) {
Craig Toppered2e13d2012-01-22 19:15:14 +00006711 // No better options. Use a vshldq / vsrldq.
Dan Gohman8a55ce42009-09-23 21:02:20 +00006712 EVT EltVT = VT.getVectorElementType();
6713 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00006714 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00006715 }
Eric Christopherfd179292009-08-27 18:07:15 +00006716
Evan Cheng9eca5e82006-10-25 21:49:50 +00006717 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00006718 // FIXME: This should also accept a bitcast of a splat? Be careful, not
6719 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00006720 V1IsSplat = isSplatVector(V1.getNode());
6721 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00006722
Chris Lattner8a594482007-11-25 00:24:49 +00006723 // Canonicalize the splat or undef, if present, to be on the RHS.
Craig Topper39a9e482012-02-11 06:24:48 +00006724 if (!V2IsUndef && V1IsSplat && !V2IsSplat) {
6725 CommuteVectorShuffleMask(M, NumElems);
6726 std::swap(V1, V2);
Evan Cheng9bbbb982006-10-25 20:48:19 +00006727 std::swap(V1IsSplat, V2IsSplat);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006728 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00006729 }
6730
Craig Topperbeabc6c2011-12-05 06:56:46 +00006731 if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00006732 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00006733 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00006734 return V1;
6735 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
6736 // the instruction selector will not match, so get a canonical MOVL with
6737 // swapped operands to undo the commute.
6738 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00006739 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006740
Craig Topperbeabc6c2011-12-05 06:56:46 +00006741 if (isUNPCKLMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006742 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006743
Craig Topperbeabc6c2011-12-05 06:56:46 +00006744 if (isUNPCKHMask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006745 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00006746
Evan Cheng9bbbb982006-10-25 20:48:19 +00006747 if (V2IsSplat) {
6748 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006749 // element then try to match unpck{h|l} again. If match, return a
Craig Topper39a9e482012-02-11 06:24:48 +00006750 // new vector_shuffle with the corrected mask.p
6751 SmallVector<int, 8> NewMask(M.begin(), M.end());
6752 NormalizeMask(NewMask, NumElems);
Craig Topper69947b92012-04-23 06:57:04 +00006753 if (isUNPCKLMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006754 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00006755 if (isUNPCKHMask(NewMask, VT, HasAVX2, true))
Craig Topper39a9e482012-02-11 06:24:48 +00006756 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006757 }
6758
Evan Cheng9eca5e82006-10-25 21:49:50 +00006759 if (Commuted) {
6760 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00006761 // FIXME: this seems wrong.
Craig Topper39a9e482012-02-11 06:24:48 +00006762 CommuteVectorShuffleMask(M, NumElems);
6763 std::swap(V1, V2);
6764 std::swap(V1IsSplat, V2IsSplat);
6765 Commuted = false;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006766
Craig Topper39a9e482012-02-11 06:24:48 +00006767 if (isUNPCKLMask(M, VT, HasAVX2))
6768 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00006769
Craig Topper39a9e482012-02-11 06:24:48 +00006770 if (isUNPCKHMask(M, VT, HasAVX2))
6771 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00006772 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006773
Nate Begeman9008ca62009-04-27 18:41:29 +00006774 // Normalize the node to match x86 shuffle ops if needed
Craig Topper1a7700a2012-01-19 08:19:12 +00006775 if (!V2IsUndef && (isSHUFPMask(M, VT, HasAVX, /* Commuted */ true)))
Nate Begeman9008ca62009-04-27 18:41:29 +00006776 return CommuteVectorShuffle(SVOp, DAG);
6777
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006778 // The checks below are all present in isShuffleMaskLegal, but they are
6779 // inlined here right now to enable us to directly emit target specific
6780 // nodes, and remove one by one until they don't return Op anymore.
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00006781
Craig Topper0e2037b2012-01-20 05:53:00 +00006782 if (isPALIGNRMask(M, VT, Subtarget))
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006783 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
Craig Topperd93e4c32011-12-11 19:12:35 +00006784 getShufflePALIGNRImmediate(SVOp),
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00006785 DAG);
6786
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006787 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
6788 SVOp->getSplatIndex() == 0 && V2IsUndef) {
Craig Topperbeabc6c2011-12-05 06:56:46 +00006789 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Craig Topper34671b82011-12-06 08:21:25 +00006790 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00006791 }
6792
Craig Toppera9a568a2012-05-02 08:03:44 +00006793 if (isPSHUFHWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006794 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006795 getShufflePSHUFHWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006796 DAG);
6797
Craig Toppera9a568a2012-05-02 08:03:44 +00006798 if (isPSHUFLWMask(M, VT, HasAVX2))
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006799 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006800 getShufflePSHUFLWImmediate(SVOp),
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00006801 DAG);
6802
Craig Topper1a7700a2012-01-19 08:19:12 +00006803 if (isSHUFPMask(M, VT, HasAVX))
Craig Topperb3982da2011-12-31 23:50:21 +00006804 return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2,
Craig Topper5aaffa82012-02-19 02:53:47 +00006805 getShuffleSHUFImmediate(SVOp), DAG);
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00006806
Craig Topper94438ba2011-12-16 08:06:31 +00006807 if (isUNPCKL_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006808 return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG);
Craig Topper94438ba2011-12-16 08:06:31 +00006809 if (isUNPCKH_v_undef_Mask(M, VT, HasAVX2))
Craig Topper34671b82011-12-06 08:21:25 +00006810 return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG);
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00006811
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006812 //===--------------------------------------------------------------------===//
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006813 // Generate target specific nodes for 128 or 256-bit shuffles only
6814 // supported in the AVX instruction set.
6815 //
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006816
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006817 // Handle VMOVDDUPY permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006818 if (V2IsUndef && isMOVDDUPYMask(M, VT, HasAVX))
Bruno Cardoso Lopes6292ece2011-08-25 21:40:37 +00006819 return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG);
6820
Craig Topper70b883b2011-11-28 10:14:51 +00006821 // Handle VPERMILPS/D* permutations
Craig Topperdbd98a42012-02-07 06:28:42 +00006822 if (isVPERMILPMask(M, VT, HasAVX)) {
6823 if (HasAVX2 && VT == MVT::v8i32)
6824 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006825 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topper316cd2a2011-11-30 06:25:25 +00006826 return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1,
Craig Topper5aaffa82012-02-19 02:53:47 +00006827 getShuffleSHUFImmediate(SVOp), DAG);
Craig Topperdbd98a42012-02-07 06:28:42 +00006828 }
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006829
Craig Topper70b883b2011-11-28 10:14:51 +00006830 // Handle VPERM2F128/VPERM2I128 permutations
Craig Topperbeabc6c2011-12-05 06:56:46 +00006831 if (isVPERM2X128Mask(M, VT, HasAVX))
Craig Topperec24e612011-11-30 07:47:51 +00006832 return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1,
Craig Topper70b883b2011-11-28 10:14:51 +00006833 V2, getShuffleVPERM2X128Immediate(SVOp), DAG);
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006834
Craig Topper1842ba02012-04-23 06:38:28 +00006835 SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG);
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006836 if (BlendOp.getNode())
6837 return BlendOp;
Craig Topper095c5282012-04-15 23:48:57 +00006838
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006839 if (V2IsUndef && HasAVX2 && (VT == MVT::v8i32 || VT == MVT::v8f32)) {
Craig Topper095c5282012-04-15 23:48:57 +00006840 SmallVector<SDValue, 8> permclMask;
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006841 for (unsigned i = 0; i != 8; ++i) {
Craig Topper095c5282012-04-15 23:48:57 +00006842 permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32));
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006843 }
Craig Topper92040742012-04-16 06:43:40 +00006844 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32,
6845 &permclMask[0], 8);
6846 // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32
Craig Topper8325c112012-04-16 00:41:45 +00006847 return DAG.getNode(X86ISD::VPERMV, dl, VT,
Craig Topper92040742012-04-16 06:43:40 +00006848 DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1);
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006849 }
Craig Topper095c5282012-04-15 23:48:57 +00006850
Craig Topper8325c112012-04-16 00:41:45 +00006851 if (V2IsUndef && HasAVX2 && (VT == MVT::v4i64 || VT == MVT::v4f64))
6852 return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1,
Elena Demikhovsky73c504a2012-04-15 11:18:59 +00006853 getShuffleCLImmediate(SVOp), DAG);
6854
Nadav Roteme80aa7c2012-04-09 08:33:21 +00006855
Bruno Cardoso Lopescea34e42011-07-27 00:56:34 +00006856 //===--------------------------------------------------------------------===//
6857 // Since no target specific shuffle was selected for this generic one,
6858 // lower it into other known shuffles. FIXME: this isn't true yet, but
6859 // this is the plan.
6860 //
Bruno Cardoso Lopes65b74e12011-07-21 01:55:47 +00006861
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006862 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
6863 if (VT == MVT::v8i16) {
6864 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
6865 if (NewOp.getNode())
6866 return NewOp;
6867 }
6868
6869 if (VT == MVT::v16i8) {
6870 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
6871 if (NewOp.getNode())
6872 return NewOp;
6873 }
6874
Elena Demikhovsky41789462012-09-06 12:42:01 +00006875 if (VT == MVT::v32i8) {
6876 SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, DAG, *this);
6877 if (NewOp.getNode())
6878 return NewOp;
6879 }
6880
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006881 // Handle all 128-bit wide vectors with 4 elements, and match them with
6882 // several different shuffle types.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006883 if (NumElems == 4 && VT.is128BitVector())
Bruno Cardoso Lopes9b4ad122011-07-27 00:56:37 +00006884 return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG);
6885
Bruno Cardoso Lopesd0888342011-07-22 00:14:56 +00006886 // Handle general 256-bit shuffles
6887 if (VT.is256BitVector())
6888 return LowerVECTOR_SHUFFLE_256(SVOp, DAG);
6889
Dan Gohman475871a2008-07-27 21:46:04 +00006890 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006891}
6892
Dan Gohman475871a2008-07-27 21:46:04 +00006893SDValue
6894X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006895 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006896 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006897 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006898
Craig Topper7a9a28b2012-08-12 02:23:29 +00006899 if (!Op.getOperand(0).getValueType().is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006900 return SDValue();
6901
Duncan Sands83ec4b62008-06-06 12:08:01 +00006902 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006903 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006904 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006905 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006906 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006907 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006908 }
6909
6910 if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00006911 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
6912 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
6913 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006914 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
6915 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006916 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006917 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00006918 Op.getOperand(0)),
6919 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006920 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006921 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00006922 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00006923 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00006924 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00006925 }
6926
6927 if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00006928 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
6929 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006930 // result has a single use which is a store or a bitcast to i32. And in
6931 // the case of a store, it's not worth it if the index is a constant 0,
6932 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00006933 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00006934 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006935 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00006936 if ((User->getOpcode() != ISD::STORE ||
6937 (isa<ConstantSDNode>(Op.getOperand(1)) &&
6938 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006939 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00006941 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006943 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00006944 Op.getOperand(0)),
6945 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006946 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Craig Topper69947b92012-04-23 06:57:04 +00006947 }
6948
6949 if (VT == MVT::i32 || VT == MVT::i64) {
Pete Coopera77214a2011-11-14 19:38:42 +00006950 // ExtractPS/pextrq works with constant index.
Mon P Wangf0fcdd82009-01-15 21:10:20 +00006951 if (isa<ConstantSDNode>(Op.getOperand(1)))
6952 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00006953 }
Dan Gohman475871a2008-07-27 21:46:04 +00006954 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00006955}
6956
6957
Dan Gohman475871a2008-07-27 21:46:04 +00006958SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006959X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
6960 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006961 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00006962 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006963
David Greene74a579d2011-02-10 16:57:36 +00006964 SDValue Vec = Op.getOperand(0);
6965 EVT VecVT = Vec.getValueType();
6966
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00006967 // If this is a 256-bit vector result, first extract the 128-bit vector and
6968 // then extract the element from the 128-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00006969 if (VecVT.is256BitVector()) {
David Greene74a579d2011-02-10 16:57:36 +00006970 DebugLoc dl = Op.getNode()->getDebugLoc();
6971 unsigned NumElems = VecVT.getVectorNumElements();
6972 SDValue Idx = Op.getOperand(1);
David Greene74a579d2011-02-10 16:57:36 +00006973 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
6974
6975 // Get the 128-bit vector.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006976 Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greene74a579d2011-02-10 16:57:36 +00006977
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006978 if (IdxVal >= NumElems/2)
6979 IdxVal -= NumElems/2;
David Greene74a579d2011-02-10 16:57:36 +00006980 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
Craig Topper7d1e3dc2012-04-30 05:17:10 +00006981 DAG.getConstant(IdxVal, MVT::i32));
David Greene74a579d2011-02-10 16:57:36 +00006982 }
6983
Craig Topper7a9a28b2012-08-12 02:23:29 +00006984 assert(VecVT.is128BitVector() && "Unexpected vector length");
David Greene74a579d2011-02-10 16:57:36 +00006985
Craig Topperd0a31172012-01-10 06:37:29 +00006986 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006987 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006988 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00006989 return Res;
6990 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00006991
Owen Andersone50ed302009-08-10 22:56:29 +00006992 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006993 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006994 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006995 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00006996 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006997 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00006998 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00006999 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
7000 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007001 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007002 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00007003 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007004 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00007005 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00007006 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007007 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00007008 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00007009 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00007010 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Craig Topper69947b92012-04-23 06:57:04 +00007011 }
7012
7013 if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007014 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007015 if (Idx == 0)
7016 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00007017
Evan Cheng0db9fe62006-04-25 20:13:52 +00007018 // SHUFPS the element to the lowest double word, then movss.
Jeffrey Yasskina44defe2011-07-27 06:22:51 +00007019 int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007020 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007021 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007022 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007023 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007024 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007025 }
7026
7027 if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007028 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
7029 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
7030 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007031 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007032 if (Idx == 0)
7033 return Op;
7034
7035 // UNPCKHPD the element to the lowest double word, then movsd.
7036 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
7037 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00007038 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00007039 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00007040 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00007041 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00007042 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00007043 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007044 }
7045
Dan Gohman475871a2008-07-27 21:46:04 +00007046 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007047}
7048
Dan Gohman475871a2008-07-27 21:46:04 +00007049SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007050X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
7051 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007052 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007053 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007054 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007055
Dan Gohman475871a2008-07-27 21:46:04 +00007056 SDValue N0 = Op.getOperand(0);
7057 SDValue N1 = Op.getOperand(1);
7058 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00007059
Craig Topper7a9a28b2012-08-12 02:23:29 +00007060 if (!VT.is128BitVector())
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007061 return SDValue();
7062
Dan Gohman8a55ce42009-09-23 21:02:20 +00007063 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00007064 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007065 unsigned Opc;
7066 if (VT == MVT::v8i16)
7067 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007068 else if (VT == MVT::v16i8)
7069 Opc = X86ISD::PINSRB;
7070 else
7071 Opc = X86ISD::PINSRB;
7072
Nate Begeman14d12ca2008-02-11 04:19:36 +00007073 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
7074 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007075 if (N1.getValueType() != MVT::i32)
7076 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7077 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007078 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00007079 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007080 }
7081
7082 if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00007083 // Bits [7:6] of the constant are the source select. This will always be
7084 // zero here. The DAG Combiner may combine an extract_elt index into these
7085 // bits. For example (insert (extract, 3), 2) could be matched by putting
7086 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00007087 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00007088 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00007089 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00007090 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007091 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00007092 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00007093 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00007094 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Craig Topper69947b92012-04-23 06:57:04 +00007095 }
7096
7097 if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00007098 // PINSR* works with constant index.
7099 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00007100 }
Dan Gohman475871a2008-07-27 21:46:04 +00007101 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007102}
7103
Dan Gohman475871a2008-07-27 21:46:04 +00007104SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007105X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007106 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00007107 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00007108
David Greene6b381262011-02-09 15:32:06 +00007109 DebugLoc dl = Op.getDebugLoc();
7110 SDValue N0 = Op.getOperand(0);
7111 SDValue N1 = Op.getOperand(1);
7112 SDValue N2 = Op.getOperand(2);
7113
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007114 // If this is a 256-bit vector result, first extract the 128-bit vector,
7115 // insert the element into the extracted half and then place it back.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007116 if (VT.is256BitVector()) {
David Greene6b381262011-02-09 15:32:06 +00007117 if (!isa<ConstantSDNode>(N2))
7118 return SDValue();
7119
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007120 // Get the desired 128-bit vector half.
David Greene6b381262011-02-09 15:32:06 +00007121 unsigned NumElems = VT.getVectorNumElements();
7122 unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue();
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007123 SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007124
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007125 // Insert the element into the desired half.
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007126 bool Upper = IdxVal >= NumElems/2;
7127 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
7128 DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32));
David Greene6b381262011-02-09 15:32:06 +00007129
Bruno Cardoso Lopes0b0a09f2011-07-29 01:31:02 +00007130 // Insert the changed part back to the 256-bit vector
Craig Topper7d1e3dc2012-04-30 05:17:10 +00007131 return Insert128BitVector(N0, V, IdxVal, DAG, dl);
David Greene6b381262011-02-09 15:32:06 +00007132 }
7133
Craig Topperd0a31172012-01-10 06:37:29 +00007134 if (Subtarget->hasSSE41())
Nate Begeman14d12ca2008-02-11 04:19:36 +00007135 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
7136
Dan Gohman8a55ce42009-09-23 21:02:20 +00007137 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00007138 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00007139
Dan Gohman8a55ce42009-09-23 21:02:20 +00007140 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00007141 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
7142 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00007143 if (N1.getValueType() != MVT::i32)
7144 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
7145 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007146 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00007147 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007148 }
Dan Gohman475871a2008-07-27 21:46:04 +00007149 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007150}
7151
Dan Gohman475871a2008-07-27 21:46:04 +00007152SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007153X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007154 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007155 DebugLoc dl = Op.getDebugLoc();
David Greene2fcdfb42011-02-10 23:11:29 +00007156 EVT OpVT = Op.getValueType();
7157
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007158 // If this is a 256-bit vector result, first insert into a 128-bit
7159 // vector and then insert into the 256-bit vector.
Craig Topper7a9a28b2012-08-12 02:23:29 +00007160 if (!OpVT.is128BitVector()) {
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007161 // Insert into a 128-bit vector.
7162 EVT VT128 = EVT::getVectorVT(*Context,
7163 OpVT.getVectorElementType(),
7164 OpVT.getVectorNumElements() / 2);
7165
7166 Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
7167
7168 // Insert the 128-bit vector.
Craig Topperb14940a2012-04-22 20:55:18 +00007169 return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
Bruno Cardoso Lopes233fa392011-07-25 23:05:16 +00007170 }
7171
Craig Topperd77d2fe2012-04-29 20:22:05 +00007172 if (OpVT == MVT::v1i64 &&
Chris Lattnerf172ecd2010-07-04 23:07:25 +00007173 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00007174 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00007175
Owen Anderson825b72b2009-08-11 20:47:22 +00007176 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Craig Topper7a9a28b2012-08-12 02:23:29 +00007177 assert(OpVT.is128BitVector() && "Expected an SSE type!");
Craig Topperd77d2fe2012-04-29 20:22:05 +00007178 return DAG.getNode(ISD::BITCAST, dl, OpVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00007179 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00007180}
7181
David Greene91585092011-01-26 15:38:49 +00007182// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
7183// a simple subregister reference or explicit instructions to grab
7184// upper bits of a vector.
7185SDValue
7186X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7187 if (Subtarget->hasAVX()) {
David Greenea5f26012011-02-07 19:36:54 +00007188 DebugLoc dl = Op.getNode()->getDebugLoc();
7189 SDValue Vec = Op.getNode()->getOperand(0);
7190 SDValue Idx = Op.getNode()->getOperand(1);
7191
Craig Topper7a9a28b2012-08-12 02:23:29 +00007192 if (Op.getNode()->getValueType(0).is128BitVector() &&
7193 Vec.getNode()->getValueType(0).is256BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007194 isa<ConstantSDNode>(Idx)) {
7195 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7196 return Extract128BitVector(Vec, IdxVal, DAG, dl);
David Greenea5f26012011-02-07 19:36:54 +00007197 }
David Greene91585092011-01-26 15:38:49 +00007198 }
7199 return SDValue();
7200}
7201
David Greenecfe33c42011-01-26 19:13:22 +00007202// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
7203// simple superregister reference or explicit instructions to insert
7204// the upper bits of a vector.
7205SDValue
7206X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
7207 if (Subtarget->hasAVX()) {
7208 DebugLoc dl = Op.getNode()->getDebugLoc();
7209 SDValue Vec = Op.getNode()->getOperand(0);
7210 SDValue SubVec = Op.getNode()->getOperand(1);
7211 SDValue Idx = Op.getNode()->getOperand(2);
7212
Craig Topper7a9a28b2012-08-12 02:23:29 +00007213 if (Op.getNode()->getValueType(0).is256BitVector() &&
7214 SubVec.getNode()->getValueType(0).is128BitVector() &&
Craig Topperb14940a2012-04-22 20:55:18 +00007215 isa<ConstantSDNode>(Idx)) {
7216 unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
7217 return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
David Greenecfe33c42011-01-26 19:13:22 +00007218 }
7219 }
7220 return SDValue();
7221}
7222
Bill Wendling056292f2008-09-16 21:48:12 +00007223// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
7224// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
7225// one of the above mentioned nodes. It has to be wrapped because otherwise
7226// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
7227// be used to form addressing mode. These wrapped nodes will be selected
7228// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00007229SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007230X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007231 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007232
Chris Lattner41621a22009-06-26 19:22:52 +00007233 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7234 // global base reg.
7235 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007236 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007237 CodeModel::Model M = getTargetMachine().getCodeModel();
7238
Chris Lattner4f066492009-07-11 20:29:19 +00007239 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007240 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007241 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007242 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007243 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007244 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007245 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007246
Evan Cheng1606e8e2009-03-13 07:51:59 +00007247 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00007248 CP->getAlignment(),
7249 CP->getOffset(), OpFlag);
7250 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00007251 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007252 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00007253 if (OpFlag) {
7254 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007255 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007256 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007257 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007258 }
7259
7260 return Result;
7261}
7262
Dan Gohmand858e902010-04-17 15:26:15 +00007263SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007264 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00007265
Chris Lattner18c59872009-06-27 04:16:01 +00007266 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7267 // global base reg.
7268 unsigned char OpFlag = 0;
7269 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007270 CodeModel::Model M = getTargetMachine().getCodeModel();
7271
Chris Lattner4f066492009-07-11 20:29:19 +00007272 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007273 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00007274 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00007275 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007276 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00007277 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00007278 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00007279
Chris Lattner18c59872009-06-27 04:16:01 +00007280 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
7281 OpFlag);
7282 DebugLoc DL = JT->getDebugLoc();
7283 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007284
Chris Lattner18c59872009-06-27 04:16:01 +00007285 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00007286 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00007287 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7288 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007289 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007290 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007291
Chris Lattner18c59872009-06-27 04:16:01 +00007292 return Result;
7293}
7294
7295SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007296X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00007297 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00007298
Chris Lattner18c59872009-06-27 04:16:01 +00007299 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7300 // global base reg.
7301 unsigned char OpFlag = 0;
7302 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007303 CodeModel::Model M = getTargetMachine().getCodeModel();
7304
Chris Lattner4f066492009-07-11 20:29:19 +00007305 if (Subtarget->isPICStyleRIPRel() &&
Eli Friedman586272d2011-08-11 01:48:05 +00007306 (M == CodeModel::Small || M == CodeModel::Kernel)) {
7307 if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
7308 OpFlag = X86II::MO_GOTPCREL;
Chris Lattnere4df7562009-07-09 03:15:51 +00007309 WrapperKind = X86ISD::WrapperRIP;
Eli Friedman586272d2011-08-11 01:48:05 +00007310 } else if (Subtarget->isPICStyleGOT()) {
7311 OpFlag = X86II::MO_GOT;
7312 } else if (Subtarget->isPICStyleStubPIC()) {
7313 OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
7314 } else if (Subtarget->isPICStyleStubNoDynamic()) {
7315 OpFlag = X86II::MO_DARWIN_NONLAZY;
7316 }
Eric Christopherfd179292009-08-27 18:07:15 +00007317
Chris Lattner18c59872009-06-27 04:16:01 +00007318 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00007319
Chris Lattner18c59872009-06-27 04:16:01 +00007320 DebugLoc DL = Op.getDebugLoc();
7321 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00007322
7323
Chris Lattner18c59872009-06-27 04:16:01 +00007324 // With PIC, the address is actually $g + Offset.
7325 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00007326 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00007327 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7328 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007329 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00007330 Result);
7331 }
Eric Christopherfd179292009-08-27 18:07:15 +00007332
Eli Friedman586272d2011-08-11 01:48:05 +00007333 // For symbols that require a load from a stub to get the address, emit the
7334 // load.
7335 if (isGlobalStubReference(OpFlag))
7336 Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007337 MachinePointerInfo::getGOT(), false, false, false, 0);
Eli Friedman586272d2011-08-11 01:48:05 +00007338
Chris Lattner18c59872009-06-27 04:16:01 +00007339 return Result;
7340}
7341
Dan Gohman475871a2008-07-27 21:46:04 +00007342SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007343X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00007344 // Create the TargetBlockAddressAddress node.
7345 unsigned char OpFlags =
7346 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00007347 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00007348 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00007349 DebugLoc dl = Op.getDebugLoc();
7350 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
7351 /*isTarget=*/true, OpFlags);
7352
Dan Gohmanf705adb2009-10-30 01:28:02 +00007353 if (Subtarget->isPICStyleRIPRel() &&
7354 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00007355 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7356 else
7357 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007358
Dan Gohman29cbade2009-11-20 23:18:13 +00007359 // With PIC, the address is actually $g + Offset.
7360 if (isGlobalRelativeToPICBase(OpFlags)) {
7361 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7362 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
7363 Result);
7364 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00007365
7366 return Result;
7367}
7368
7369SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00007370X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00007371 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00007372 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00007373 // Create the TargetGlobalAddress node, folding in the constant
7374 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00007375 unsigned char OpFlags =
7376 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007377 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00007378 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007379 if (OpFlags == X86II::MO_NO_FLAG &&
7380 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00007381 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00007382 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00007383 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007384 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00007385 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007386 }
Eric Christopherfd179292009-08-27 18:07:15 +00007387
Chris Lattner4f066492009-07-11 20:29:19 +00007388 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007389 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00007390 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
7391 else
7392 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00007393
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007394 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00007395 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007396 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
7397 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00007398 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007399 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007400
Chris Lattner36c25012009-07-10 07:34:39 +00007401 // For globals that require a load from a stub to get the address, emit the
7402 // load.
7403 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00007404 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Pete Cooperd752e0f2011-11-08 18:42:53 +00007405 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007406
Dan Gohman6520e202008-10-18 02:06:02 +00007407 // If there was a non-zero offset that we didn't fold, create an explicit
7408 // addition for it.
7409 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00007410 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00007411 DAG.getConstant(Offset, getPointerTy()));
7412
Evan Cheng0db9fe62006-04-25 20:13:52 +00007413 return Result;
7414}
7415
Evan Chengda43bcf2008-09-24 00:05:32 +00007416SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007417X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00007418 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007419 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007420 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007421}
7422
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007423static SDValue
7424GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00007425 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007426 unsigned char OperandFlags, bool LocalDynamic = false) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007427 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007428 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007429 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007430 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007431 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007432 GA->getOffset(),
7433 OperandFlags);
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007434
7435 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7436 : X86ISD::TLSADDR;
7437
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007438 if (InFlag) {
7439 SDValue Ops[] = { Chain, TGA, *InFlag };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007440 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007441 } else {
7442 SDValue Ops[] = { Chain, TGA };
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007443 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007444 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007445
7446 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00007447 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00007448
Rafael Espindola15f1b662009-04-24 12:59:40 +00007449 SDValue Flag = Chain.getValue(1);
7450 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00007451}
7452
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007453// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007454static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007455LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007456 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00007457 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00007458 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
7459 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007460 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00007461 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007462 InFlag = Chain.getValue(1);
7463
Chris Lattnerb903bed2009-06-26 21:20:29 +00007464 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007465}
7466
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007467// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00007468static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007469LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007470 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007471 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
7472 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007473}
7474
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007475static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7476 SelectionDAG &DAG,
7477 const EVT PtrVT,
7478 bool is64Bit) {
7479 DebugLoc dl = GA->getDebugLoc();
7480
7481 // Get the start address of the TLS block for this module.
7482 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7483 .getInfo<X86MachineFunctionInfo>();
7484 MFI->incNumLocalDynamicTLSAccesses();
7485
7486 SDValue Base;
7487 if (is64Bit) {
7488 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7489 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7490 } else {
7491 SDValue InFlag;
7492 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7493 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7494 InFlag = Chain.getValue(1);
7495 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7496 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7497 }
7498
7499 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7500 // of Base.
7501
7502 // Build x@dtpoff.
7503 unsigned char OperandFlags = X86II::MO_DTPOFF;
7504 unsigned WrapperKind = X86ISD::Wrapper;
7505 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7506 GA->getValueType(0),
7507 GA->getOffset(), OperandFlags);
7508 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7509
7510 // Add x@dtpoff with the base.
7511 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
7512}
7513
Hans Wennborg228756c2012-05-11 10:11:01 +00007514// Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00007515static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00007516 const EVT PtrVT, TLSModel::Model model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007517 bool is64Bit, bool isPIC) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00007518 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00007519
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007520 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
7521 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
7522 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00007523
Michael J. Spencerec38de22010-10-10 22:04:20 +00007524 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00007525 DAG.getIntPtrConstant(0),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007526 MachinePointerInfo(Ptr),
7527 false, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00007528
Chris Lattnerb903bed2009-06-26 21:20:29 +00007529 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00007530 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
7531 // initialexec.
7532 unsigned WrapperKind = X86ISD::Wrapper;
7533 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00007534 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Hans Wennborg228756c2012-05-11 10:11:01 +00007535 } else if (model == TLSModel::InitialExec) {
7536 if (is64Bit) {
7537 OperandFlags = X86II::MO_GOTTPOFF;
7538 WrapperKind = X86ISD::WrapperRIP;
7539 } else {
7540 OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
7541 }
Chris Lattner18c59872009-06-27 04:16:01 +00007542 } else {
Hans Wennborg228756c2012-05-11 10:11:01 +00007543 llvm_unreachable("Unexpected model");
Chris Lattnerb903bed2009-06-26 21:20:29 +00007544 }
Eric Christopherfd179292009-08-27 18:07:15 +00007545
Hans Wennborg228756c2012-05-11 10:11:01 +00007546 // emit "addl x@ntpoff,%eax" (local exec)
7547 // or "addl x@indntpoff,%eax" (initial exec)
7548 // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
Michael J. Spencerec38de22010-10-10 22:04:20 +00007549 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00007550 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00007551 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00007552 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007553
Hans Wennborg228756c2012-05-11 10:11:01 +00007554 if (model == TLSModel::InitialExec) {
7555 if (isPIC && !is64Bit) {
7556 Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
7557 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT),
7558 Offset);
Hans Wennborg228756c2012-05-11 10:11:01 +00007559 }
Rafael Espindola94e3b382012-06-29 04:22:35 +00007560
7561 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
7562 MachinePointerInfo::getGOT(), false, false, false,
7563 0);
Hans Wennborg228756c2012-05-11 10:11:01 +00007564 }
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00007565
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007566 // The address of the thread local variable is the add of the thread
7567 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00007568 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007569}
7570
Dan Gohman475871a2008-07-27 21:46:04 +00007571SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007572X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00007573
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007574 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00007575 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00007576
Eric Christopher30ef0e52010-06-03 04:07:48 +00007577 if (Subtarget->isTargetELF()) {
Chandler Carruth34797132012-04-08 17:20:55 +00007578 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007579
Eric Christopher30ef0e52010-06-03 04:07:48 +00007580 switch (model) {
7581 case TLSModel::GeneralDynamic:
Eric Christopher30ef0e52010-06-03 04:07:48 +00007582 if (Subtarget->is64Bit())
7583 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
7584 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Hans Wennborgf0234fc2012-06-01 16:27:21 +00007585 case TLSModel::LocalDynamic:
7586 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7587 Subtarget->is64Bit());
Eric Christopher30ef0e52010-06-03 04:07:48 +00007588 case TLSModel::InitialExec:
7589 case TLSModel::LocalExec:
7590 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
Hans Wennborg228756c2012-05-11 10:11:01 +00007591 Subtarget->is64Bit(),
7592 getTargetMachine().getRelocationModel() == Reloc::PIC_);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007593 }
Craig Toppere8eb1162012-04-23 03:26:18 +00007594 llvm_unreachable("Unknown TLS model.");
7595 }
7596
7597 if (Subtarget->isTargetDarwin()) {
Eric Christopher30ef0e52010-06-03 04:07:48 +00007598 // Darwin only has one model of TLS. Lower to that.
7599 unsigned char OpFlag = 0;
7600 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
7601 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007602
Eric Christopher30ef0e52010-06-03 04:07:48 +00007603 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
7604 // global base reg.
7605 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
7606 !Subtarget->is64Bit();
7607 if (PIC32)
7608 OpFlag = X86II::MO_TLVP_PIC_BASE;
7609 else
7610 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007611 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00007612 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00007613 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00007614 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00007615 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007616
Eric Christopher30ef0e52010-06-03 04:07:48 +00007617 // With PIC32, the address is actually $g + Offset.
7618 if (PIC32)
7619 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
7620 DAG.getNode(X86ISD::GlobalBaseReg,
7621 DebugLoc(), getPointerTy()),
7622 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007623
Eric Christopher30ef0e52010-06-03 04:07:48 +00007624 // Lowering the machine isd will make sure everything is in the right
7625 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007626 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007627 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00007628 SDValue Args[] = { Chain, Offset };
7629 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007630
Eric Christopher30ef0e52010-06-03 04:07:48 +00007631 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
7632 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7633 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007634
Eric Christopher30ef0e52010-06-03 04:07:48 +00007635 // And our return value (tls address) is in the standard call return value
7636 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00007637 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Evan Chengfd230df2011-10-19 22:22:54 +00007638 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(),
7639 Chain.getValue(1));
Craig Toppere8eb1162012-04-23 03:26:18 +00007640 }
7641
7642 if (Subtarget->isTargetWindows()) {
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007643 // Just use the implicit TLS architecture
7644 // Need to generate someting similar to:
7645 // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
7646 // ; from TEB
7647 // mov ecx, dword [rel _tls_index]: Load index (from C runtime)
7648 // mov rcx, qword [rdx+rcx*8]
7649 // mov eax, .tls$:tlsvar
7650 // [rax+rcx] contains the address
7651 // Windows 64bit: gs:0x58
7652 // Windows 32bit: fs:__tls_array
7653
7654 // If GV is an alias then use the aliasee for determining
7655 // thread-localness.
7656 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
7657 GV = GA->resolveAliasedGlobal(false);
7658 DebugLoc dl = GA->getDebugLoc();
7659 SDValue Chain = DAG.getEntryNode();
7660
7661 // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
7662 // %gs:0x58 (64-bit).
7663 Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
7664 ? Type::getInt8PtrTy(*DAG.getContext(),
7665 256)
7666 : Type::getInt32PtrTy(*DAG.getContext(),
7667 257));
7668
7669 SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain,
7670 Subtarget->is64Bit()
7671 ? DAG.getIntPtrConstant(0x58)
7672 : DAG.getExternalSymbol("_tls_array",
7673 getPointerTy()),
7674 MachinePointerInfo(Ptr),
7675 false, false, false, 0);
7676
7677 // Load the _tls_index variable
7678 SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy());
7679 if (Subtarget->is64Bit())
7680 IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain,
7681 IDX, MachinePointerInfo(), MVT::i32,
7682 false, false, 0);
7683 else
7684 IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(),
7685 false, false, false, 0);
7686
7687 SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()),
Craig Topper0fbf3642012-04-23 03:28:34 +00007688 getPointerTy());
Anton Korobeynikovd4a19b62012-02-11 17:26:53 +00007689 IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale);
7690
7691 SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX);
7692 res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(),
7693 false, false, false, 0);
7694
7695 // Get the offset of start of .tls section
7696 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7697 GA->getValueType(0),
7698 GA->getOffset(), X86II::MO_SECREL);
7699 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA);
7700
7701 // The address of the thread local variable is the add of the thread
7702 // pointer with the offset of the variable.
7703 return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00007704 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00007705
David Blaikie4d6ccb52012-01-20 21:51:11 +00007706 llvm_unreachable("TLS not implemented for this target.");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007707}
7708
Evan Cheng0db9fe62006-04-25 20:13:52 +00007709
Chad Rosierb90d2a92012-01-03 23:19:12 +00007710/// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
7711/// and take a 2 x i32 value to shift plus a shift amount.
7712SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{
Dan Gohman4c1fa612008-03-03 22:22:09 +00007713 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00007714 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007715 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007716 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007717 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00007718 SDValue ShOpLo = Op.getOperand(0);
7719 SDValue ShOpHi = Op.getOperand(1);
7720 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00007721 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00007722 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00007723 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00007724
Dan Gohman475871a2008-07-27 21:46:04 +00007725 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007726 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007727 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
7728 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007729 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007730 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
7731 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007732 }
Evan Chenge3413162006-01-09 18:33:28 +00007733
Owen Anderson825b72b2009-08-11 20:47:22 +00007734 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
7735 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00007736 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00007737 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00007738
Dan Gohman475871a2008-07-27 21:46:04 +00007739 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00007741 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
7742 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00007743
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007744 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00007745 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7746 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007747 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00007748 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
7749 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00007750 }
7751
Dan Gohman475871a2008-07-27 21:46:04 +00007752 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00007753 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007754}
Evan Chenga3195e82006-01-12 22:54:21 +00007755
Dan Gohmand858e902010-04-17 15:26:15 +00007756SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
7757 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007758 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00007759
Dale Johannesen0488fb62010-09-30 23:57:10 +00007760 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00007761 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00007762
Owen Anderson825b72b2009-08-11 20:47:22 +00007763 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00007764 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007765
Eli Friedman36df4992009-05-27 00:47:34 +00007766 // These are really Legal; return the operand so the caller accepts it as
7767 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00007768 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00007769 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00007770 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00007771 Subtarget->is64Bit()) {
7772 return Op;
7773 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007774
Bruno Cardoso Lopesa511b8e2011-08-09 17:39:01 +00007775 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007776 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00007777 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00007778 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007779 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00007780 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00007781 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007782 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00007783 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00007784 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
7785}
Evan Cheng0db9fe62006-04-25 20:13:52 +00007786
Owen Andersone50ed302009-08-10 22:56:29 +00007787SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007788 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00007789 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007790 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00007791 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00007792 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00007793 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007794 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007795 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00007796 else
Owen Anderson825b72b2009-08-11 20:47:22 +00007797 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007798
Chris Lattner492a43e2010-09-22 01:28:21 +00007799 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00007800
Stuart Hastings84be9582011-06-02 15:57:11 +00007801 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
7802 MachineMemOperand *MMO;
7803 if (FI) {
7804 int SSFI = FI->getIndex();
7805 MMO =
7806 DAG.getMachineFunction()
7807 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
7808 MachineMemOperand::MOLoad, ByteSize, ByteSize);
7809 } else {
7810 MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
7811 StackSlot = StackSlot.getOperand(1);
7812 }
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007813 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00007814 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
7815 X86ISD::FILD, DL,
7816 Tys, Ops, array_lengthof(Ops),
7817 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007818
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00007819 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007820 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007821 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007822
7823 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
7824 // shouldn't be necessary except that RFP cannot be live across
7825 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007826 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007827 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
7828 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007829 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00007830 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007831 SDValue Ops[] = {
7832 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
7833 };
Chris Lattner492a43e2010-09-22 01:28:21 +00007834 MachineMemOperand *MMO =
7835 DAG.getMachineFunction()
7836 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00007837 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00007838
Chris Lattner492a43e2010-09-22 01:28:21 +00007839 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
7840 Ops, array_lengthof(Ops),
7841 Op.getValueType(), MMO);
7842 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00007843 MachinePointerInfo::getFixedStack(SSFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007844 false, false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007845 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007846
Evan Cheng0db9fe62006-04-25 20:13:52 +00007847 return Result;
7848}
7849
Bill Wendling8b8a6362009-01-17 03:56:04 +00007850// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007851SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
7852 SelectionDAG &DAG) const {
Bill Wendling397ae212012-01-05 02:13:20 +00007853 // This algorithm is not obvious. Here it is what we're trying to output:
Bill Wendling8b8a6362009-01-17 03:56:04 +00007854 /*
Bill Wendling397ae212012-01-05 02:13:20 +00007855 movq %rax, %xmm0
7856 punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
7857 subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
7858 #ifdef __SSE3__
Chad Rosiera20e1e72012-08-01 18:39:17 +00007859 haddpd %xmm0, %xmm0
Bill Wendling397ae212012-01-05 02:13:20 +00007860 #else
Chad Rosiera20e1e72012-08-01 18:39:17 +00007861 pshufd $0x4e, %xmm0, %xmm1
Bill Wendling397ae212012-01-05 02:13:20 +00007862 addpd %xmm1, %xmm0
7863 #endif
Bill Wendling8b8a6362009-01-17 03:56:04 +00007864 */
Dale Johannesen040225f2008-10-21 23:07:49 +00007865
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007866 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00007867 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00007868
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007869 // Build some magic constants.
Chris Lattner7302d802012-02-06 21:56:39 +00007870 const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
7871 Constant *C0 = ConstantDataVector::get(*Context, CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007872 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007873
Chris Lattner97484792012-01-25 09:56:22 +00007874 SmallVector<Constant*,2> CV1;
7875 CV1.push_back(
Chris Lattner4ca829e2012-01-25 06:02:56 +00007876 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Chris Lattner97484792012-01-25 09:56:22 +00007877 CV1.push_back(
7878 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
7879 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00007880 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007881
Bill Wendling397ae212012-01-05 02:13:20 +00007882 // Load the 64-bit value into an XMM register.
7883 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
7884 Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007885 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00007886 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007887 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007888 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32,
7889 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1),
7890 CLod0);
7891
Owen Anderson825b72b2009-08-11 20:47:22 +00007892 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00007893 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007894 false, false, false, 16);
Bill Wendling397ae212012-01-05 02:13:20 +00007895 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007896 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling397ae212012-01-05 02:13:20 +00007897 SDValue Result;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007898
Craig Topperd0a31172012-01-10 06:37:29 +00007899 if (Subtarget->hasSSE3()) {
Bill Wendling397ae212012-01-05 02:13:20 +00007900 // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
7901 Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
7902 } else {
7903 SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub);
7904 SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
7905 S2F, 0x4E, DAG);
7906 Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
7907 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle),
7908 Sub);
7909 }
7910
7911 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007912 DAG.getIntPtrConstant(0));
7913}
7914
Bill Wendling8b8a6362009-01-17 03:56:04 +00007915// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00007916SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
7917 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007918 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007919 // FP constant to bias correct the final result.
7920 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00007921 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007922
7923 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00007924 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
Eli Friedman6cdc1f42011-08-02 18:38:35 +00007925 Op.getOperand(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00007926
Eli Friedmanf3704762011-08-29 21:15:46 +00007927 // Zero out the upper parts of the register.
Craig Topper12216172012-01-13 08:12:35 +00007928 Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
Eli Friedmanf3704762011-08-29 21:15:46 +00007929
Owen Anderson825b72b2009-08-11 20:47:22 +00007930 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007931 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007932 DAG.getIntPtrConstant(0));
7933
7934 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007935 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007936 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007937 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007938 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007939 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00007940 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007941 MVT::v2f64, Bias)));
7942 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007943 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00007944 DAG.getIntPtrConstant(0));
7945
7946 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00007947 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00007948
7949 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00007950 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00007951
Craig Topper69947b92012-04-23 06:57:04 +00007952 if (DestVT.bitsLT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007953 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00007954 DAG.getIntPtrConstant(0));
Craig Topper69947b92012-04-23 06:57:04 +00007955 if (DestVT.bitsGT(MVT::f64))
Dale Johannesenace16102009-02-03 19:33:06 +00007956 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00007957
7958 // Handle final rounding.
7959 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00007960}
7961
Dan Gohmand858e902010-04-17 15:26:15 +00007962SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
7963 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00007964 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007965 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00007966
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007967 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00007968 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
7969 // the optimization here.
7970 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00007971 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00007972
Owen Andersone50ed302009-08-10 22:56:29 +00007973 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007974 EVT DstVT = Op.getValueType();
7975 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007976 return LowerUINT_TO_FP_i64(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007977 if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00007978 return LowerUINT_TO_FP_i32(Op, DAG);
Craig Topper69947b92012-04-23 06:57:04 +00007979 if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
Bill Wendling397ae212012-01-05 02:13:20 +00007980 return SDValue();
Eli Friedman948e95a2009-05-23 09:59:16 +00007981
7982 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00007983 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007984 if (SrcVT == MVT::i32) {
7985 SDValue WordOff = DAG.getConstant(4, getPointerTy());
7986 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
7987 getPointerTy(), StackSlot, WordOff);
7988 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007989 StackSlot, MachinePointerInfo(),
7990 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007991 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007992 OffsetSlot, MachinePointerInfo(),
7993 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00007994 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
7995 return Fild;
7996 }
7997
7998 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
7999 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendlingf6c07472012-01-10 19:41:30 +00008000 StackSlot, MachinePointerInfo(),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008001 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008002 // For i64 source, we need to add the appropriate power of 2 if the input
8003 // was negative. This is the same as the optimization in
8004 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
8005 // we must be careful to do the computation in x87 extended precision, not
8006 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00008007 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
8008 MachineMemOperand *MMO =
8009 DAG.getMachineFunction()
8010 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8011 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008012
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008013 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
8014 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00008015 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
8016 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008017
8018 APInt FF(32, 0x5F800000ULL);
8019
8020 // Check whether the sign bit is set.
8021 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
8022 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
8023 ISD::SETLT);
8024
8025 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
8026 SDValue FudgePtr = DAG.getConstantPool(
8027 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
8028 getPointerTy());
8029
8030 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
8031 SDValue Zero = DAG.getIntPtrConstant(0);
8032 SDValue Four = DAG.getIntPtrConstant(4);
8033 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
8034 Zero, Four);
8035 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
8036
8037 // Load the value out, extending it from f32 to f80.
8038 // FIXME: Avoid the extend by constructing the right constant pool?
Stuart Hastingsa9011292011-02-16 16:23:55 +00008039 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00008040 FudgePtr, MachinePointerInfo::getConstantPool(),
8041 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00008042 // Extend everything to 80 bits to force it to be done on x87.
8043 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
8044 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00008045}
8046
Dan Gohman475871a2008-07-27 21:46:04 +00008047std::pair<SDValue,SDValue> X86TargetLowering::
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008048FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned, bool IsReplace) const {
Chris Lattner07290932010-09-22 01:05:16 +00008049 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00008050
Owen Andersone50ed302009-08-10 22:56:29 +00008051 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00008052
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008053 if (!IsSigned && !isIntegerTypeFTOL(DstTy)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008054 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
8055 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00008056 }
8057
Owen Anderson825b72b2009-08-11 20:47:22 +00008058 assert(DstTy.getSimpleVT() <= MVT::i64 &&
8059 DstTy.getSimpleVT() >= MVT::i16 &&
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008060 "Unknown FP_TO_INT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00008061
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008062 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00008063 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00008064 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008065 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00008066 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008067 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00008068 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008069 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00008070
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008071 // We lower FP->int64 either into FISTP64 followed by a load from a temporary
8072 // stack slot, or into the FTOL runtime function.
Evan Cheng87c89352007-10-15 20:11:21 +00008073 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00008074 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00008075 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008076 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00008077
Evan Cheng0db9fe62006-04-25 20:13:52 +00008078 unsigned Opc;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008079 if (!IsSigned && isIntegerTypeFTOL(DstTy))
8080 Opc = X86ISD::WIN_FTOL;
8081 else
8082 switch (DstTy.getSimpleVT().SimpleTy) {
8083 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
8084 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
8085 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
8086 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
8087 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008088
Dan Gohman475871a2008-07-27 21:46:04 +00008089 SDValue Chain = DAG.getEntryNode();
8090 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00008091 EVT TheVT = Op.getOperand(0).getValueType();
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008092 // FIXME This causes a redundant load/store if the SSE-class value is already
8093 // in memory, such as if it is on the callstack.
Chris Lattner492a43e2010-09-22 01:28:21 +00008094 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008095 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00008096 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00008097 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00008098 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008099 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00008100 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00008101 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00008102 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008103
Chris Lattner492a43e2010-09-22 01:28:21 +00008104 MachineMemOperand *MMO =
8105 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8106 MachineMemOperand::MOLoad, MemSize, MemSize);
8107 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
8108 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008109 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00008110 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008111 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
8112 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008113
Chris Lattner07290932010-09-22 01:05:16 +00008114 MachineMemOperand *MMO =
8115 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8116 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008117
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008118 if (Opc != X86ISD::WIN_FTOL) {
8119 // Build the FP_TO_INT*_IN_MEM
8120 SDValue Ops[] = { Chain, Value, StackSlot };
8121 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
8122 Ops, 3, DstTy, MMO);
8123 return std::make_pair(FIST, StackSlot);
8124 } else {
8125 SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL,
8126 DAG.getVTList(MVT::Other, MVT::Glue),
8127 Chain, Value);
8128 SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX,
8129 MVT::i32, ftol.getValue(1));
8130 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX,
8131 MVT::i32, eax.getValue(2));
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008132 SDValue Ops[] = { eax, edx };
8133 SDValue pair = IsReplace
8134 ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, 2)
8135 : DAG.getMergeValues(Ops, 2, DL);
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008136 return std::make_pair(pair, SDValue());
8137 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008138}
8139
Dan Gohmand858e902010-04-17 15:26:15 +00008140SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
8141 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00008142 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00008143 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00008144
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008145 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8146 /*IsSigned=*/ true, /*IsReplace=*/ false);
Dan Gohman475871a2008-07-27 21:46:04 +00008147 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00008148 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
8149 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00008150
Michael J. Spencer1a2d0612012-02-24 19:01:22 +00008151 if (StackSlot.getNode())
8152 // Load the result.
8153 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8154 FIST, StackSlot, MachinePointerInfo(),
8155 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008156
8157 // The node is the result.
8158 return FIST;
Chris Lattner27a6c732007-11-24 07:07:01 +00008159}
8160
Dan Gohmand858e902010-04-17 15:26:15 +00008161SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
8162 SelectionDAG &DAG) const {
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008163 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
8164 /*IsSigned=*/ false, /*IsReplace=*/ false);
Eli Friedman948e95a2009-05-23 09:59:16 +00008165 SDValue FIST = Vals.first, StackSlot = Vals.second;
8166 assert(FIST.getNode() && "Unexpected failure");
8167
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +00008168 if (StackSlot.getNode())
8169 // Load the result.
8170 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
8171 FIST, StackSlot, MachinePointerInfo(),
8172 false, false, false, 0);
Craig Topper69947b92012-04-23 06:57:04 +00008173
8174 // The node is the result.
8175 return FIST;
Eli Friedman948e95a2009-05-23 09:59:16 +00008176}
8177
Dan Gohmand858e902010-04-17 15:26:15 +00008178SDValue X86TargetLowering::LowerFABS(SDValue Op,
8179 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008180 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008181 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008182 EVT VT = Op.getValueType();
8183 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008184 if (VT.isVector())
8185 EltVT = VT.getVectorElementType();
Chris Lattner4ca829e2012-01-25 06:02:56 +00008186 Constant *C;
Owen Anderson825b72b2009-08-11 20:47:22 +00008187 if (EltVT == MVT::f64) {
Chad Rosiera20e1e72012-08-01 18:39:17 +00008188 C = ConstantVector::getSplat(2,
Chris Lattner4ca829e2012-01-25 06:02:56 +00008189 ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00008190 } else {
Chris Lattner4ca829e2012-01-25 06:02:56 +00008191 C = ConstantVector::getSplat(4,
8192 ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
Evan Cheng0db9fe62006-04-25 20:13:52 +00008193 }
Evan Cheng1606e8e2009-03-13 07:51:59 +00008194 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008195 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008196 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008197 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008198 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008199}
8200
Dan Gohmand858e902010-04-17 15:26:15 +00008201SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008202 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008203 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008204 EVT VT = Op.getValueType();
8205 EVT EltVT = VT;
Chad Rosiera860b182011-12-15 01:02:25 +00008206 unsigned NumElts = VT == MVT::f64 ? 2 : 4;
8207 if (VT.isVector()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008208 EltVT = VT.getVectorElementType();
Chad Rosiera860b182011-12-15 01:02:25 +00008209 NumElts = VT.getVectorNumElements();
8210 }
Chris Lattner4ca829e2012-01-25 06:02:56 +00008211 Constant *C;
8212 if (EltVT == MVT::f64)
8213 C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
8214 else
8215 C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
8216 C = ConstantVector::getSplat(NumElts, C);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008217 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008218 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008219 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008220 false, false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00008221 if (VT.isVector()) {
Craig Topper7a9a28b2012-08-12 02:23:29 +00008222 MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008223 return DAG.getNode(ISD::BITCAST, dl, VT,
Chad Rosiera860b182011-12-15 01:02:25 +00008224 DAG.getNode(ISD::XOR, dl, XORVT,
Craig Topper69947b92012-04-23 06:57:04 +00008225 DAG.getNode(ISD::BITCAST, dl, XORVT,
8226 Op.getOperand(0)),
8227 DAG.getNode(ISD::BITCAST, dl, XORVT, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00008228 }
Craig Topper69947b92012-04-23 06:57:04 +00008229
8230 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008231}
8232
Dan Gohmand858e902010-04-17 15:26:15 +00008233SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00008234 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00008235 SDValue Op0 = Op.getOperand(0);
8236 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008237 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008238 EVT VT = Op.getValueType();
8239 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00008240
8241 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008242 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008243 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008244 SrcVT = VT;
8245 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008246 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008247 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00008248 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008249 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00008250 }
8251
8252 // At this point the operands and the result should have the same
8253 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00008254
Evan Cheng68c47cb2007-01-05 07:55:56 +00008255 // First get the sign bit of second operand.
Chad Rosier01d426e2011-12-15 01:16:09 +00008256 SmallVector<Constant*,4> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00008257 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008258 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
8259 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008260 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008261 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
8262 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8263 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8264 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008265 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008266 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008267 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008268 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008269 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008270 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008271 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008272
8273 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00008274 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008275 // Op0 is MVT::f32, Op1 is MVT::f64.
8276 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
8277 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
8278 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008279 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00008280 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00008281 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00008282 }
8283
Evan Cheng73d6cf12007-01-05 21:37:56 +00008284 // Clear first operand sign bit.
8285 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00008286 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008287 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
8288 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008289 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00008290 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
8291 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8292 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
8293 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00008294 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00008295 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00008296 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008297 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008298 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008299 false, false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00008300 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00008301
8302 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00008303 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008304}
8305
Stuart Hastings4fd0dee2011-06-01 04:39:42 +00008306SDValue X86TargetLowering::LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) const {
8307 SDValue N0 = Op.getOperand(0);
8308 DebugLoc dl = Op.getDebugLoc();
8309 EVT VT = Op.getValueType();
8310
8311 // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
8312 SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
8313 DAG.getConstant(1, VT));
8314 return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT));
8315}
8316
Dan Gohman076aee32009-03-04 19:44:21 +00008317/// Emit nodes that will be selected as "test Op0,Op0", or something
8318/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008319SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008320 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008321 DebugLoc dl = Op.getDebugLoc();
8322
Dan Gohman31125812009-03-07 01:58:32 +00008323 // CF and OF aren't always set the way we want. Determine which
8324 // of these we need.
8325 bool NeedCF = false;
8326 bool NeedOF = false;
8327 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008328 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00008329 case X86::COND_A: case X86::COND_AE:
8330 case X86::COND_B: case X86::COND_BE:
8331 NeedCF = true;
8332 break;
8333 case X86::COND_G: case X86::COND_GE:
8334 case X86::COND_L: case X86::COND_LE:
8335 case X86::COND_O: case X86::COND_NO:
8336 NeedOF = true;
8337 break;
Dan Gohman31125812009-03-07 01:58:32 +00008338 }
8339
Dan Gohman076aee32009-03-04 19:44:21 +00008340 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00008341 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
8342 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008343 if (Op.getResNo() != 0 || NeedOF || NeedCF)
8344 // Emit a CMP with 0, which is the TEST pattern.
8345 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8346 DAG.getConstant(0, Op.getValueType()));
8347
8348 unsigned Opcode = 0;
8349 unsigned NumOperands = 0;
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008350
8351 // Truncate operations may prevent the merge of the SETCC instruction
8352 // and the arithmetic intruction before it. Attempt to truncate the operands
8353 // of the arithmetic instruction and use a reduced bit-width instruction.
8354 bool NeedTruncation = false;
8355 SDValue ArithOp = Op;
8356 if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
8357 SDValue Arith = Op->getOperand(0);
8358 // Both the trunc and the arithmetic op need to have one user each.
8359 if (Arith->hasOneUse())
8360 switch (Arith.getOpcode()) {
8361 default: break;
8362 case ISD::ADD:
8363 case ISD::SUB:
8364 case ISD::AND:
8365 case ISD::OR:
8366 case ISD::XOR: {
8367 NeedTruncation = true;
8368 ArithOp = Arith;
8369 }
8370 }
8371 }
8372
8373 // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
8374 // which may be the result of a CAST. We use the variable 'Op', which is the
8375 // non-casted variable when we check for possible users.
8376 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008377 case ISD::ADD:
8378 // Due to an isel shortcoming, be conservative if this add is likely to be
8379 // selected as part of a load-modify-store instruction. When the root node
8380 // in a match is a store, isel doesn't know how to remap non-chain non-flag
8381 // uses of other nodes in the match, such as the ADD in this case. This
8382 // leads to the ADD being left around and reselected, with the result being
8383 // two adds in the output. Alas, even if none our users are stores, that
8384 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
8385 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
8386 // climbing the DAG back to the root, and it doesn't seem to be worth the
8387 // effort.
8388 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Pete Cooper2d496892011-11-15 21:57:53 +00008389 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8390 if (UI->getOpcode() != ISD::CopyToReg &&
8391 UI->getOpcode() != ISD::SETCC &&
8392 UI->getOpcode() != ISD::STORE)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008393 goto default_case;
8394
8395 if (ConstantSDNode *C =
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008396 dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008397 // An add of one will be selected as an INC.
8398 if (C->getAPIntValue() == 1) {
8399 Opcode = X86ISD::INC;
8400 NumOperands = 1;
8401 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00008402 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008403
8404 // An add of negative one (subtract of one) will be selected as a DEC.
8405 if (C->getAPIntValue().isAllOnesValue()) {
8406 Opcode = X86ISD::DEC;
8407 NumOperands = 1;
8408 break;
8409 }
Dan Gohman076aee32009-03-04 19:44:21 +00008410 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008411
8412 // Otherwise use a regular EFLAGS-setting add.
8413 Opcode = X86ISD::ADD;
8414 NumOperands = 2;
8415 break;
8416 case ISD::AND: {
8417 // If the primary and result isn't used, don't bother using X86ISD::AND,
8418 // because a TEST instruction will be better.
8419 bool NonFlagUse = false;
8420 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8421 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
8422 SDNode *User = *UI;
8423 unsigned UOpNo = UI.getOperandNo();
8424 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
8425 // Look pass truncate.
8426 UOpNo = User->use_begin().getOperandNo();
8427 User = *User->use_begin();
8428 }
8429
8430 if (User->getOpcode() != ISD::BRCOND &&
8431 User->getOpcode() != ISD::SETCC &&
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008432 !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008433 NonFlagUse = true;
8434 break;
8435 }
Dan Gohman076aee32009-03-04 19:44:21 +00008436 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008437
8438 if (!NonFlagUse)
8439 break;
8440 }
8441 // FALL THROUGH
8442 case ISD::SUB:
8443 case ISD::OR:
8444 case ISD::XOR:
8445 // Due to the ISEL shortcoming noted above, be conservative if this op is
8446 // likely to be selected as part of a load-modify-store instruction.
8447 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
8448 UE = Op.getNode()->use_end(); UI != UE; ++UI)
8449 if (UI->getOpcode() == ISD::STORE)
8450 goto default_case;
8451
8452 // Otherwise use a regular EFLAGS-setting instruction.
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008453 switch (ArithOp.getOpcode()) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008454 default: llvm_unreachable("unexpected operator!");
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008455 case ISD::SUB: Opcode = X86ISD::SUB; break;
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008456 case ISD::OR: Opcode = X86ISD::OR; break;
8457 case ISD::XOR: Opcode = X86ISD::XOR; break;
8458 case ISD::AND: Opcode = X86ISD::AND; break;
8459 }
8460
8461 NumOperands = 2;
8462 break;
8463 case X86ISD::ADD:
8464 case X86ISD::SUB:
8465 case X86ISD::INC:
8466 case X86ISD::DEC:
8467 case X86ISD::OR:
8468 case X86ISD::XOR:
8469 case X86ISD::AND:
8470 return SDValue(Op.getNode(), 1);
8471 default:
8472 default_case:
8473 break;
Dan Gohman076aee32009-03-04 19:44:21 +00008474 }
8475
Nadav Rotemb9d6b842012-08-18 17:53:03 +00008476 // If we found that truncation is beneficial, perform the truncation and
8477 // update 'Op'.
8478 if (NeedTruncation) {
8479 EVT VT = Op.getValueType();
8480 SDValue WideVal = Op->getOperand(0);
8481 EVT WideVT = WideVal.getValueType();
8482 unsigned ConvertedOp = 0;
8483 // Use a target machine opcode to prevent further DAGCombine
8484 // optimizations that may separate the arithmetic operations
8485 // from the setcc node.
8486 switch (WideVal.getOpcode()) {
8487 default: break;
8488 case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
8489 case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
8490 case ISD::AND: ConvertedOp = X86ISD::AND; break;
8491 case ISD::OR: ConvertedOp = X86ISD::OR; break;
8492 case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
8493 }
8494
8495 if (ConvertedOp) {
8496 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8497 if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
8498 SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
8499 SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
8500 Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
8501 }
8502 }
8503 }
8504
Bill Wendlingc25ccf82010-06-28 21:08:32 +00008505 if (Opcode == 0)
8506 // Emit a CMP with 0, which is the TEST pattern.
8507 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
8508 DAG.getConstant(0, Op.getValueType()));
8509
8510 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
8511 SmallVector<SDValue, 4> Ops;
8512 for (unsigned i = 0; i != NumOperands; ++i)
8513 Ops.push_back(Op.getOperand(i));
8514
8515 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
8516 DAG.ReplaceAllUsesWith(Op, New);
8517 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00008518}
8519
8520/// Emit nodes that will be selected as "cmp Op0,Op1", or something
8521/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00008522SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00008523 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00008524 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
8525 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00008526 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00008527
8528 DebugLoc dl = Op0.getDebugLoc();
Manman Ren39ad5682012-08-08 00:51:41 +00008529 if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
8530 Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
8531 // Use SUB instead of CMP to enable CSE between SUB and CMP.
8532 SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
8533 SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
8534 Op0, Op1);
8535 return SDValue(Sub.getNode(), 1);
8536 }
Owen Anderson825b72b2009-08-11 20:47:22 +00008537 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00008538}
8539
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008540/// Convert a comparison if required by the subtarget.
8541SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
8542 SelectionDAG &DAG) const {
8543 // If the subtarget does not support the FUCOMI instruction, floating-point
8544 // comparisons have to be converted.
8545 if (Subtarget->hasCMov() ||
8546 Cmp.getOpcode() != X86ISD::CMP ||
8547 !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
8548 !Cmp.getOperand(1).getValueType().isFloatingPoint())
8549 return Cmp;
8550
8551 // The instruction selector will select an FUCOM instruction instead of
8552 // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
8553 // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
8554 // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
8555 DebugLoc dl = Cmp.getDebugLoc();
8556 SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
8557 SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
8558 SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
8559 DAG.getConstant(8, MVT::i8));
8560 SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
8561 return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
8562}
8563
Evan Chengd40d03e2010-01-06 19:38:29 +00008564/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
8565/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00008566SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
8567 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008568 SDValue Op0 = And.getOperand(0);
8569 SDValue Op1 = And.getOperand(1);
8570 if (Op0.getOpcode() == ISD::TRUNCATE)
8571 Op0 = Op0.getOperand(0);
8572 if (Op1.getOpcode() == ISD::TRUNCATE)
8573 Op1 = Op1.getOperand(0);
8574
Evan Chengd40d03e2010-01-06 19:38:29 +00008575 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008576 if (Op1.getOpcode() == ISD::SHL)
8577 std::swap(Op0, Op1);
8578 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00008579 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
8580 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008581 // If we looked past a truncate, check that it's only truncating away
8582 // known zeros.
8583 unsigned BitWidth = Op0.getValueSizeInBits();
8584 unsigned AndBitWidth = And.getValueSizeInBits();
8585 if (BitWidth > AndBitWidth) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00008586 APInt Zeros, Ones;
8587 DAG.ComputeMaskedBits(Op0, Zeros, Ones);
Dan Gohman6b13cbc2010-06-24 02:07:59 +00008588 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
8589 return SDValue();
8590 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008591 LHS = Op1;
8592 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00008593 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008594 } else if (Op1.getOpcode() == ISD::Constant) {
8595 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
Benjamin Kramerf238f502011-11-23 13:54:17 +00008596 uint64_t AndRHSVal = AndRHS->getZExtValue();
Evan Cheng2c755ba2010-02-27 07:36:59 +00008597 SDValue AndLHS = Op0;
Benjamin Kramerf238f502011-11-23 13:54:17 +00008598
8599 if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
Evan Chengd40d03e2010-01-06 19:38:29 +00008600 LHS = AndLHS.getOperand(0);
8601 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008602 }
Benjamin Kramerf238f502011-11-23 13:54:17 +00008603
8604 // Use BT if the immediate can't be encoded in a TEST instruction.
8605 if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
8606 LHS = AndLHS;
8607 RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());
8608 }
Evan Chengd40d03e2010-01-06 19:38:29 +00008609 }
Evan Cheng0488db92007-09-25 01:57:46 +00008610
Evan Chengd40d03e2010-01-06 19:38:29 +00008611 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00008612 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00008613 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00008614 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00008615 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00008616 // Also promote i16 to i32 for performance / code size reason.
8617 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00008618 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00008619 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00008620
Evan Chengd40d03e2010-01-06 19:38:29 +00008621 // If the operand types disagree, extend the shift amount to match. Since
8622 // BT ignores high bits (like shifts) we can use anyextend.
8623 if (LHS.getValueType() != RHS.getValueType())
8624 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008625
Evan Chengd40d03e2010-01-06 19:38:29 +00008626 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
8627 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
8628 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8629 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00008630 }
8631
Evan Cheng54de3ea2010-01-05 06:52:31 +00008632 return SDValue();
8633}
8634
Dan Gohmand858e902010-04-17 15:26:15 +00008635SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Duncan Sands28b77e92011-09-06 19:07:46 +00008636
8637 if (Op.getValueType().isVector()) return LowerVSETCC(Op, DAG);
8638
Evan Cheng54de3ea2010-01-05 06:52:31 +00008639 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
8640 SDValue Op0 = Op.getOperand(0);
8641 SDValue Op1 = Op.getOperand(1);
8642 DebugLoc dl = Op.getDebugLoc();
8643 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
8644
8645 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00008646 // Lower (X & (1 << N)) == 0 to BT(X, N).
8647 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
8648 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Andrew Trickf6c39412011-03-23 23:11:02 +00008649 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008650 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00008651 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00008652 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
8653 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
8654 if (NewSetCC.getNode())
8655 return NewSetCC;
8656 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00008657
Chris Lattner481eebc2010-12-19 21:23:48 +00008658 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
8659 // these.
8660 if (Op1.getOpcode() == ISD::Constant &&
Andrew Trickf6c39412011-03-23 23:11:02 +00008661 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
Evan Cheng2c755ba2010-02-27 07:36:59 +00008662 cast<ConstantSDNode>(Op1)->isNullValue()) &&
8663 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008664
Chris Lattner481eebc2010-12-19 21:23:48 +00008665 // If the input is a setcc, then reuse the input setcc or use a new one with
8666 // the inverted condition.
8667 if (Op0.getOpcode() == X86ISD::SETCC) {
8668 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
8669 bool Invert = (CC == ISD::SETNE) ^
8670 cast<ConstantSDNode>(Op1)->isNullValue();
8671 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008672
Evan Cheng2c755ba2010-02-27 07:36:59 +00008673 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00008674 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
8675 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
8676 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00008677 }
8678
Evan Chenge5b51ac2010-04-17 06:13:15 +00008679 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00008680 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00008681 if (X86CC == X86::COND_INVALID)
8682 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008683
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008684 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008685 EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00008686 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00008687 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00008688}
8689
Craig Topper89af15e2011-09-18 08:03:58 +00008690// Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008691// ones, and then concatenate the result back.
Craig Topper89af15e2011-09-18 08:03:58 +00008692static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008693 EVT VT = Op.getValueType();
8694
Craig Topper7a9a28b2012-08-12 02:23:29 +00008695 assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008696 "Unsupported value type for operation");
8697
Craig Topper66ddd152012-04-27 22:54:43 +00008698 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008699 DebugLoc dl = Op.getDebugLoc();
8700 SDValue CC = Op.getOperand(2);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008701
8702 // Extract the LHS vectors
8703 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +00008704 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
8705 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008706
8707 // Extract the RHS vectors
8708 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +00008709 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
8710 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008711
8712 // Issue the operation on the smaller types and concatenate the result back
8713 MVT EltVT = VT.getVectorElementType().getSimpleVT();
8714 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
8715 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
8716 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
8717 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
8718}
8719
8720
Dan Gohmand858e902010-04-17 15:26:15 +00008721SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008722 SDValue Cond;
8723 SDValue Op0 = Op.getOperand(0);
8724 SDValue Op1 = Op.getOperand(1);
8725 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00008726 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00008727 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
8728 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008729 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00008730
8731 if (isFP) {
Craig Topper523908d2012-08-13 02:34:03 +00008732#ifndef NDEBUG
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008733 EVT EltVT = Op0.getValueType().getVectorElementType();
Craig Topper523908d2012-08-13 02:34:03 +00008734 assert(EltVT == MVT::f32 || EltVT == MVT::f64);
8735#endif
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008736
Craig Topper523908d2012-08-13 02:34:03 +00008737 unsigned SSECC;
Nate Begeman30a0de92008-07-17 16:51:19 +00008738 bool Swap = false;
8739
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008740 // SSE Condition code mapping:
8741 // 0 - EQ
8742 // 1 - LT
8743 // 2 - LE
8744 // 3 - UNORD
8745 // 4 - NEQ
8746 // 5 - NLT
8747 // 6 - NLE
8748 // 7 - ORD
Nate Begeman30a0de92008-07-17 16:51:19 +00008749 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008750 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begemanfb8ead02008-07-25 19:05:58 +00008751 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00008752 case ISD::SETEQ: SSECC = 0; break;
Bruno Cardoso Lopes8e03a822011-09-12 19:30:40 +00008753 case ISD::SETOGT:
8754 case ISD::SETGT: Swap = true; // Fallthrough
Bruno Cardoso Lopes457d53d2011-09-12 21:24:07 +00008755 case ISD::SETLT:
8756 case ISD::SETOLT: SSECC = 1; break;
8757 case ISD::SETOGE:
8758 case ISD::SETGE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008759 case ISD::SETLE:
8760 case ISD::SETOLE: SSECC = 2; break;
8761 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008762 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00008763 case ISD::SETNE: SSECC = 4; break;
Craig Topper523908d2012-08-13 02:34:03 +00008764 case ISD::SETULE: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008765 case ISD::SETUGE: SSECC = 5; break;
Craig Topper523908d2012-08-13 02:34:03 +00008766 case ISD::SETULT: Swap = true; // Fallthrough
Nate Begeman30a0de92008-07-17 16:51:19 +00008767 case ISD::SETUGT: SSECC = 6; break;
8768 case ISD::SETO: SSECC = 7; break;
Craig Topper523908d2012-08-13 02:34:03 +00008769 case ISD::SETUEQ:
8770 case ISD::SETONE: SSECC = 8; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008771 }
8772 if (Swap)
8773 std::swap(Op0, Op1);
8774
Nate Begemanfb8ead02008-07-25 19:05:58 +00008775 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00008776 if (SSECC == 8) {
Craig Topper523908d2012-08-13 02:34:03 +00008777 unsigned CC0, CC1;
8778 unsigned CombineOpc;
Nate Begemanfb8ead02008-07-25 19:05:58 +00008779 if (SetCCOpcode == ISD::SETUEQ) {
Craig Topper523908d2012-08-13 02:34:03 +00008780 CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
8781 } else {
8782 assert(SetCCOpcode == ISD::SETONE);
8783 CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
Craig Topper69947b92012-04-23 06:57:04 +00008784 }
Craig Topper523908d2012-08-13 02:34:03 +00008785
8786 SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8787 DAG.getConstant(CC0, MVT::i8));
8788 SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8789 DAG.getConstant(CC1, MVT::i8));
8790 return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008791 }
8792 // Handle all other FP comparisons here.
Craig Topper1906d322012-01-22 23:36:02 +00008793 return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1,
8794 DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00008795 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008796
Bruno Cardoso Lopes2ac81112011-08-22 20:31:04 +00008797 // Break 256-bit integer vector compare into smaller ones.
Craig Topper7a9a28b2012-08-12 02:23:29 +00008798 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper89af15e2011-09-18 08:03:58 +00008799 return Lower256IntVSETCC(Op, DAG);
Bruno Cardoso Lopes0f0e0a02011-08-09 00:46:57 +00008800
Nate Begeman30a0de92008-07-17 16:51:19 +00008801 // We are handling one of the integer comparisons here. Since SSE only has
8802 // GT and EQ comparisons for integer, swapping operands and multiple
8803 // operations may be required for some comparisons.
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008804 unsigned Opc;
Nate Begeman30a0de92008-07-17 16:51:19 +00008805 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00008806
Nate Begeman30a0de92008-07-17 16:51:19 +00008807 switch (SetCCOpcode) {
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008808 default: llvm_unreachable("Unexpected SETCC condition");
Nate Begeman30a0de92008-07-17 16:51:19 +00008809 case ISD::SETNE: Invert = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008810 case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008811 case ISD::SETLT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008812 case ISD::SETGT: Opc = X86ISD::PCMPGT; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008813 case ISD::SETGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008814 case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008815 case ISD::SETULT: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008816 case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008817 case ISD::SETUGE: Swap = true;
Craig Topper67609fd2012-01-22 22:42:16 +00008818 case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00008819 }
8820 if (Swap)
8821 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008822
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008823 // Check that the operation in question is available (most are plain SSE2,
8824 // but PCMPGTQ and PCMPEQQ have different requirements).
Craig Topper2f1b2ec2012-08-13 03:42:38 +00008825 if (VT == MVT::v2i64) {
8826 if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42())
8827 return SDValue();
8828 if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41())
8829 return SDValue();
8830 }
Eli Friedman7d3e2b72011-09-28 21:00:25 +00008831
Nate Begeman30a0de92008-07-17 16:51:19 +00008832 // Since SSE has no unsigned integer comparisons, we need to flip the sign
8833 // bits of the inputs before performing those operations.
8834 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00008835 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00008836 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
8837 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00008838 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00008839 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
8840 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00008841 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
8842 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00008843 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008844
Dale Johannesenace16102009-02-03 19:33:06 +00008845 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00008846
8847 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00008848 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00008849 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00008850
Nate Begeman30a0de92008-07-17 16:51:19 +00008851 return Result;
8852}
Evan Cheng0488db92007-09-25 01:57:46 +00008853
Evan Cheng370e5342008-12-03 08:38:43 +00008854// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00008855static bool isX86LogicalCmp(SDValue Op) {
8856 unsigned Opc = Op.getNode()->getOpcode();
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008857 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
8858 Opc == X86ISD::SAHF)
Dan Gohman076aee32009-03-04 19:44:21 +00008859 return true;
8860 if (Op.getResNo() == 1 &&
8861 (Opc == X86ISD::ADD ||
8862 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00008863 Opc == X86ISD::ADC ||
8864 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00008865 Opc == X86ISD::SMUL ||
8866 Opc == X86ISD::UMUL ||
8867 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00008868 Opc == X86ISD::DEC ||
8869 Opc == X86ISD::OR ||
8870 Opc == X86ISD::XOR ||
8871 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00008872 return true;
8873
Chris Lattner9637d5b2010-12-05 07:49:54 +00008874 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
8875 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008876
Dan Gohman076aee32009-03-04 19:44:21 +00008877 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00008878}
8879
Chris Lattnera2b56002010-12-05 01:23:24 +00008880static bool isZero(SDValue V) {
8881 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8882 return C && C->isNullValue();
8883}
8884
Chris Lattner96908b12010-12-05 02:00:51 +00008885static bool isAllOnes(SDValue V) {
8886 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
8887 return C && C->isAllOnesValue();
8888}
8889
Evan Chengb64dd5f2012-08-07 22:21:00 +00008890static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
8891 if (V.getOpcode() != ISD::TRUNCATE)
8892 return false;
8893
8894 SDValue VOp0 = V.getOperand(0);
8895 unsigned InBits = VOp0.getValueSizeInBits();
8896 unsigned Bits = V.getValueSizeInBits();
8897 return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
8898}
8899
Dan Gohmand858e902010-04-17 15:26:15 +00008900SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00008901 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00008902 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00008903 SDValue Op1 = Op.getOperand(1);
8904 SDValue Op2 = Op.getOperand(2);
8905 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008906 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00008907
Dan Gohman1a492952009-10-20 16:22:37 +00008908 if (Cond.getOpcode() == ISD::SETCC) {
8909 SDValue NewCond = LowerSETCC(Cond, DAG);
8910 if (NewCond.getNode())
8911 Cond = NewCond;
8912 }
Evan Cheng734503b2006-09-11 02:19:56 +00008913
Chris Lattnera2b56002010-12-05 01:23:24 +00008914 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008915 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00008916 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00008917 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008918 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00008919 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
8920 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008921 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008922
Chris Lattnera2b56002010-12-05 01:23:24 +00008923 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008924
8925 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00008926 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
8927 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00008928
8929 SDValue CmpOp0 = Cmp.getOperand(0);
Manman Rened579842012-05-07 18:06:23 +00008930 // Apply further optimizations for special cases
8931 // (select (x != 0), -1, 0) -> neg & sbb
8932 // (select (x == 0), 0, -1) -> neg & sbb
8933 if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y))
Chad Rosiera20e1e72012-08-01 18:39:17 +00008934 if (YC->isNullValue() &&
Manman Rened579842012-05-07 18:06:23 +00008935 (isAllOnes(Op1) == (CondCode == X86::COND_NE))) {
8936 SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
Chad Rosiera20e1e72012-08-01 18:39:17 +00008937 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
8938 DAG.getConstant(0, CmpOp0.getValueType()),
Manman Rened579842012-05-07 18:06:23 +00008939 CmpOp0);
8940 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8941 DAG.getConstant(X86::COND_B, MVT::i8),
8942 SDValue(Neg.getNode(), 1));
8943 return Res;
8944 }
8945
Chris Lattnera2b56002010-12-05 01:23:24 +00008946 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
8947 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00008948 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008949
Chris Lattner96908b12010-12-05 02:00:51 +00008950 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00008951 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
8952 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008953
Chris Lattner96908b12010-12-05 02:00:51 +00008954 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
8955 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008956
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008957 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00008958 if (N2C == 0 || !N2C->isNullValue())
8959 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
8960 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00008961 }
8962 }
8963
Chris Lattnera2b56002010-12-05 01:23:24 +00008964 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00008965 if (Cond.getOpcode() == ISD::AND &&
8966 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
8967 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008968 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00008969 Cond = Cond.getOperand(0);
8970 }
8971
Evan Cheng3f41d662007-10-08 22:16:29 +00008972 // If condition flag is set by a X86ISD::CMP, then use it as the condition
8973 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00008974 unsigned CondOpcode = Cond.getOpcode();
8975 if (CondOpcode == X86ISD::SETCC ||
8976 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00008977 CC = Cond.getOperand(0);
8978
Dan Gohman475871a2008-07-27 21:46:04 +00008979 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00008980 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00008981 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00008982
Evan Cheng3f41d662007-10-08 22:16:29 +00008983 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008984 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00008985 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00008986 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00008987
Chris Lattnerd1980a52009-03-12 06:52:53 +00008988 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
8989 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00008990 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00008991 addTest = false;
8992 }
Dan Gohman65fd6562011-11-03 21:49:52 +00008993 } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
8994 CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
8995 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
8996 Cond.getOperand(0).getValueType() != MVT::i8)) {
8997 SDValue LHS = Cond.getOperand(0);
8998 SDValue RHS = Cond.getOperand(1);
8999 unsigned X86Opcode;
9000 unsigned X86Cond;
9001 SDVTList VTs;
9002 switch (CondOpcode) {
9003 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9004 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9005 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9006 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9007 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9008 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9009 default: llvm_unreachable("unexpected overflowing operator");
9010 }
9011 if (CondOpcode == ISD::UMULO)
9012 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9013 MVT::i32);
9014 else
9015 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9016
9017 SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
9018
9019 if (CondOpcode == ISD::UMULO)
9020 Cond = X86Op.getValue(2);
9021 else
9022 Cond = X86Op.getValue(1);
9023
9024 CC = DAG.getConstant(X86Cond, MVT::i8);
9025 addTest = false;
Evan Cheng0488db92007-09-25 01:57:46 +00009026 }
9027
9028 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009029 // Look pass the truncate if the high bits are known zero.
9030 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9031 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009032
9033 // We know the result of AND is compared against zero. Try to match
9034 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009035 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00009036 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00009037 if (NewSetCC.getNode()) {
9038 CC = NewSetCC.getOperand(0);
9039 Cond = NewSetCC.getOperand(1);
9040 addTest = false;
9041 }
9042 }
9043 }
9044
9045 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009046 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009047 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009048 }
9049
Benjamin Kramere915ff32010-12-22 23:09:28 +00009050 // a < b ? -1 : 0 -> RES = ~setcc_carry
9051 // a < b ? 0 : -1 -> RES = setcc_carry
9052 // a >= b ? -1 : 0 -> RES = setcc_carry
9053 // a >= b ? 0 : -1 -> RES = ~setcc_carry
Manman Ren39ad5682012-08-08 00:51:41 +00009054 if (Cond.getOpcode() == X86ISD::SUB) {
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009055 Cond = ConvertCmpIfNecessary(Cond, DAG);
Benjamin Kramere915ff32010-12-22 23:09:28 +00009056 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
9057
9058 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
9059 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
9060 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
9061 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
9062 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
9063 return DAG.getNOT(DL, Res, Res.getValueType());
9064 return Res;
9065 }
9066 }
9067
Evan Cheng0488db92007-09-25 01:57:46 +00009068 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
9069 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00009070 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00009071 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00009072 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00009073}
9074
Evan Cheng370e5342008-12-03 08:38:43 +00009075// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
9076// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
9077// from the AND / OR.
9078static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
9079 Opc = Op.getOpcode();
9080 if (Opc != ISD::OR && Opc != ISD::AND)
9081 return false;
9082 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9083 Op.getOperand(0).hasOneUse() &&
9084 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
9085 Op.getOperand(1).hasOneUse());
9086}
9087
Evan Cheng961d6d42009-02-02 08:19:07 +00009088// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
9089// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00009090static bool isXor1OfSetCC(SDValue Op) {
9091 if (Op.getOpcode() != ISD::XOR)
9092 return false;
9093 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9094 if (N1C && N1C->getAPIntValue() == 1) {
9095 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
9096 Op.getOperand(0).hasOneUse();
9097 }
9098 return false;
9099}
9100
Dan Gohmand858e902010-04-17 15:26:15 +00009101SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00009102 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00009103 SDValue Chain = Op.getOperand(0);
9104 SDValue Cond = Op.getOperand(1);
9105 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009106 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009107 SDValue CC;
Dan Gohman65fd6562011-11-03 21:49:52 +00009108 bool Inverted = false;
Evan Cheng734503b2006-09-11 02:19:56 +00009109
Dan Gohman1a492952009-10-20 16:22:37 +00009110 if (Cond.getOpcode() == ISD::SETCC) {
Dan Gohman65fd6562011-11-03 21:49:52 +00009111 // Check for setcc([su]{add,sub,mul}o == 0).
9112 if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
9113 isa<ConstantSDNode>(Cond.getOperand(1)) &&
9114 cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() &&
9115 Cond.getOperand(0).getResNo() == 1 &&
9116 (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
9117 Cond.getOperand(0).getOpcode() == ISD::UADDO ||
9118 Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
9119 Cond.getOperand(0).getOpcode() == ISD::USUBO ||
9120 Cond.getOperand(0).getOpcode() == ISD::SMULO ||
9121 Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
9122 Inverted = true;
9123 Cond = Cond.getOperand(0);
9124 } else {
9125 SDValue NewCond = LowerSETCC(Cond, DAG);
9126 if (NewCond.getNode())
9127 Cond = NewCond;
9128 }
Dan Gohman1a492952009-10-20 16:22:37 +00009129 }
Chris Lattnere55484e2008-12-25 05:34:37 +00009130#if 0
9131 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00009132 else if (Cond.getOpcode() == X86ISD::ADD ||
9133 Cond.getOpcode() == X86ISD::SUB ||
9134 Cond.getOpcode() == X86ISD::SMUL ||
9135 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00009136 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00009137#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00009138
Evan Chengad9c0a32009-12-15 00:53:42 +00009139 // Look pass (and (setcc_carry (cmp ...)), 1).
9140 if (Cond.getOpcode() == ISD::AND &&
9141 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
9142 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00009143 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00009144 Cond = Cond.getOperand(0);
9145 }
9146
Evan Cheng3f41d662007-10-08 22:16:29 +00009147 // If condition flag is set by a X86ISD::CMP, then use it as the condition
9148 // setting operand in place of the X86ISD::SETCC.
Dan Gohman65fd6562011-11-03 21:49:52 +00009149 unsigned CondOpcode = Cond.getOpcode();
9150 if (CondOpcode == X86ISD::SETCC ||
9151 CondOpcode == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00009152 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00009153
Dan Gohman475871a2008-07-27 21:46:04 +00009154 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00009155 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00009156 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00009157 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00009158 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00009159 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00009160 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00009161 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009162 default: break;
9163 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00009164 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00009165 // These can only come from an arithmetic instruction with overflow,
9166 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00009167 Cond = Cond.getNode()->getOperand(1);
9168 addTest = false;
9169 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00009170 }
Evan Cheng0488db92007-09-25 01:57:46 +00009171 }
Dan Gohman65fd6562011-11-03 21:49:52 +00009172 }
9173 CondOpcode = Cond.getOpcode();
9174 if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
9175 CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
9176 ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
9177 Cond.getOperand(0).getValueType() != MVT::i8)) {
9178 SDValue LHS = Cond.getOperand(0);
9179 SDValue RHS = Cond.getOperand(1);
9180 unsigned X86Opcode;
9181 unsigned X86Cond;
9182 SDVTList VTs;
9183 switch (CondOpcode) {
9184 case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
9185 case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
9186 case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
9187 case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
9188 case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
9189 case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
9190 default: llvm_unreachable("unexpected overflowing operator");
9191 }
9192 if (Inverted)
9193 X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
9194 if (CondOpcode == ISD::UMULO)
9195 VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
9196 MVT::i32);
9197 else
9198 VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
9199
9200 SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
9201
9202 if (CondOpcode == ISD::UMULO)
9203 Cond = X86Op.getValue(2);
9204 else
9205 Cond = X86Op.getValue(1);
9206
9207 CC = DAG.getConstant(X86Cond, MVT::i8);
9208 addTest = false;
Evan Cheng370e5342008-12-03 08:38:43 +00009209 } else {
9210 unsigned CondOpc;
9211 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
9212 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00009213 if (CondOpc == ISD::OR) {
9214 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
9215 // two branches instead of an explicit OR instruction with a
9216 // separate test.
9217 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009218 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00009219 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009220 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009221 Chain, Dest, CC, Cmp);
9222 CC = Cond.getOperand(1).getOperand(0);
9223 Cond = Cmp;
9224 addTest = false;
9225 }
9226 } else { // ISD::AND
9227 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
9228 // two branches instead of an explicit AND instruction with a
9229 // separate test. However, we only do this if this block doesn't
9230 // have a fall-through edge, because this requires an explicit
9231 // jmp when the condition is false.
9232 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00009233 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00009234 Op.getNode()->hasOneUse()) {
9235 X86::CondCode CCode =
9236 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9237 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009238 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00009239 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00009240 // Look for an unconditional branch following this conditional branch.
9241 // We need this because we need to reverse the successors in order
9242 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00009243 if (User->getOpcode() == ISD::BR) {
9244 SDValue FalseBB = User->getOperand(1);
9245 SDNode *NewBR =
9246 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00009247 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00009248 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00009249 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00009250
Dale Johannesene4d209d2009-02-03 20:21:25 +00009251 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00009252 Chain, Dest, CC, Cmp);
9253 X86::CondCode CCode =
9254 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
9255 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009256 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00009257 Cond = Cmp;
9258 addTest = false;
9259 }
9260 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009261 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00009262 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
9263 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
9264 // It should be transformed during dag combiner except when the condition
9265 // is set by a arithmetics with overflow node.
9266 X86::CondCode CCode =
9267 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
9268 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00009269 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00009270 Cond = Cond.getOperand(0).getOperand(1);
9271 addTest = false;
Dan Gohman65fd6562011-11-03 21:49:52 +00009272 } else if (Cond.getOpcode() == ISD::SETCC &&
9273 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
9274 // For FCMP_OEQ, we can emit
9275 // two branches instead of an explicit AND instruction with a
9276 // separate test. However, we only do this if this block doesn't
9277 // have a fall-through edge, because this requires an explicit
9278 // jmp when the condition is false.
9279 if (Op.getNode()->hasOneUse()) {
9280 SDNode *User = *Op.getNode()->use_begin();
9281 // Look for an unconditional branch following this conditional branch.
9282 // We need this because we need to reverse the successors in order
9283 // to implement FCMP_OEQ.
9284 if (User->getOpcode() == ISD::BR) {
9285 SDValue FalseBB = User->getOperand(1);
9286 SDNode *NewBR =
9287 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9288 assert(NewBR == User);
9289 (void)NewBR;
9290 Dest = FalseBB;
9291
9292 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9293 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009294 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009295 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9296 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9297 Chain, Dest, CC, Cmp);
9298 CC = DAG.getConstant(X86::COND_P, MVT::i8);
9299 Cond = Cmp;
9300 addTest = false;
9301 }
9302 }
9303 } else if (Cond.getOpcode() == ISD::SETCC &&
9304 cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
9305 // For FCMP_UNE, we can emit
9306 // two branches instead of an explicit AND instruction with a
9307 // separate test. However, we only do this if this block doesn't
9308 // have a fall-through edge, because this requires an explicit
9309 // jmp when the condition is false.
9310 if (Op.getNode()->hasOneUse()) {
9311 SDNode *User = *Op.getNode()->use_begin();
9312 // Look for an unconditional branch following this conditional branch.
9313 // We need this because we need to reverse the successors in order
9314 // to implement FCMP_UNE.
9315 if (User->getOpcode() == ISD::BR) {
9316 SDValue FalseBB = User->getOperand(1);
9317 SDNode *NewBR =
9318 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
9319 assert(NewBR == User);
9320 (void)NewBR;
9321
9322 SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
9323 Cond.getOperand(0), Cond.getOperand(1));
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009324 Cmp = ConvertCmpIfNecessary(Cmp, DAG);
Dan Gohman65fd6562011-11-03 21:49:52 +00009325 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
9326 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
9327 Chain, Dest, CC, Cmp);
9328 CC = DAG.getConstant(X86::COND_NP, MVT::i8);
9329 Cond = Cmp;
9330 addTest = false;
9331 Dest = FalseBB;
9332 }
9333 }
Dan Gohman279c22e2008-10-21 03:29:32 +00009334 }
Evan Cheng0488db92007-09-25 01:57:46 +00009335 }
9336
9337 if (addTest) {
Evan Chengb64dd5f2012-08-07 22:21:00 +00009338 // Look pass the truncate if the high bits are known zero.
9339 if (isTruncWithZeroHighBitsInput(Cond, DAG))
9340 Cond = Cond.getOperand(0);
Evan Chengd40d03e2010-01-06 19:38:29 +00009341
9342 // We know the result of AND is compared against zero. Try to match
9343 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00009344 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00009345 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
9346 if (NewSetCC.getNode()) {
9347 CC = NewSetCC.getOperand(0);
9348 Cond = NewSetCC.getOperand(1);
9349 addTest = false;
9350 }
9351 }
9352 }
9353
9354 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009355 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00009356 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00009357 }
Benjamin Kramer17c836c2012-04-27 12:07:43 +00009358 Cond = ConvertCmpIfNecessary(Cond, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009359 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00009360 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00009361}
9362
Anton Korobeynikove060b532007-04-17 19:34:00 +00009363
9364// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
9365// Calls to _alloca is needed to probe the stack when allocating more than 4k
9366// bytes in one go. Touching the stack at 4K increments is necessary to ensure
9367// that the guard pages used by the OS virtual memory manager are allocated in
9368// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00009369SDValue
9370X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00009371 SelectionDAG &DAG) const {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009372 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() ||
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009373 getTargetMachine().Options.EnableSegmentedStacks) &&
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009374 "This should be used only on Windows targets or when segmented stacks "
Rafael Espindola96428ce2011-09-06 18:43:08 +00009375 "are being used");
9376 assert(!Subtarget->isTargetEnvMacho() && "Not implemented");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009377 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009378
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009379 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009380 SDValue Chain = Op.getOperand(0);
9381 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009382 // FIXME: Ensure alignment here
9383
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009384 bool Is64Bit = Subtarget->is64Bit();
9385 EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009386
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009387 if (getTargetMachine().Options.EnableSegmentedStacks) {
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009388 MachineFunction &MF = DAG.getMachineFunction();
9389 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009390
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009391 if (Is64Bit) {
9392 // The 64 bit implementation of segmented stacks needs to clobber both r10
Rafael Espindola96428ce2011-09-06 18:43:08 +00009393 // r11. This makes it impossible to use it along with nested parameters.
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009394 const Function *F = MF.getFunction();
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009395
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009396 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
Craig Topper31a207a2012-05-04 06:39:13 +00009397 I != E; ++I)
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009398 if (I->hasNestAttr())
9399 report_fatal_error("Cannot use segmented stacks with functions that "
9400 "have nested arguments.");
9401 }
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00009402
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009403 const TargetRegisterClass *AddrRegClass =
9404 getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32);
9405 unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
9406 Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
9407 SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
9408 DAG.getRegister(Vreg, SPTy));
9409 SDValue Ops1[2] = { Value, Chain };
9410 return DAG.getMergeValues(Ops1, 2, dl);
9411 } else {
9412 SDValue Flag;
9413 unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009414
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009415 Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
9416 Flag = Chain.getValue(1);
9417 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00009418
Rafael Espindola151ab3e2011-08-30 19:47:04 +00009419 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
9420 Flag = Chain.getValue(1);
9421
9422 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
9423
9424 SDValue Ops1[2] = { Chain.getValue(0), Chain };
9425 return DAG.getMergeValues(Ops1, 2, dl);
9426 }
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00009427}
9428
Dan Gohmand858e902010-04-17 15:26:15 +00009429SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00009430 MachineFunction &MF = DAG.getMachineFunction();
9431 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
9432
Dan Gohman69de1932008-02-06 22:27:42 +00009433 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00009434 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00009435
Anton Korobeynikove7beda12010-10-03 22:52:07 +00009436 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00009437 // vastart just stores the address of the VarArgsFrameIndex slot into the
9438 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00009439 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9440 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009441 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
9442 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009443 }
9444
9445 // __va_list_tag:
9446 // gp_offset (0 - 6 * 8)
9447 // fp_offset (48 - 48 + 8 * 16)
9448 // overflow_arg_area (point to parameters coming in memory).
9449 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00009450 SmallVector<SDValue, 8> MemOps;
9451 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00009452 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009453 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009454 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
9455 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009456 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009457 MemOps.push_back(Store);
9458
9459 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00009460 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009461 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00009462 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00009463 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
9464 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00009465 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009466 MemOps.push_back(Store);
9467
9468 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00009469 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009470 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00009471 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
9472 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009473 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
9474 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00009475 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009476 MemOps.push_back(Store);
9477
9478 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00009479 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00009480 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00009481 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
9482 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00009483 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
9484 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00009485 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00009486 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00009487 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00009488}
9489
Dan Gohmand858e902010-04-17 15:26:15 +00009490SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00009491 assert(Subtarget->is64Bit() &&
9492 "LowerVAARG only handles 64-bit va_arg!");
9493 assert((Subtarget->isTargetLinux() ||
9494 Subtarget->isTargetDarwin()) &&
9495 "Unhandled target in LowerVAARG");
9496 assert(Op.getNode()->getNumOperands() == 4);
9497 SDValue Chain = Op.getOperand(0);
9498 SDValue SrcPtr = Op.getOperand(1);
9499 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
9500 unsigned Align = Op.getConstantOperandVal(3);
9501 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00009502
Dan Gohman320afb82010-10-12 18:00:49 +00009503 EVT ArgVT = Op.getNode()->getValueType(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009504 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Dan Gohman320afb82010-10-12 18:00:49 +00009505 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
9506 uint8_t ArgMode;
9507
9508 // Decide which area this value should be read from.
9509 // TODO: Implement the AMD64 ABI in its entirety. This simple
9510 // selection mechanism works only for the basic types.
9511 if (ArgVT == MVT::f80) {
9512 llvm_unreachable("va_arg for f80 not yet implemented");
9513 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
9514 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
9515 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
9516 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
9517 } else {
9518 llvm_unreachable("Unhandled argument type in LowerVAARG");
9519 }
9520
9521 if (ArgMode == 2) {
9522 // Sanity Check: Make sure using fp_offset makes sense.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00009523 assert(!getTargetMachine().Options.UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00009524 !(DAG.getMachineFunction()
9525 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Craig Topper1accb7e2012-01-10 06:54:16 +00009526 Subtarget->hasSSE1());
Dan Gohman320afb82010-10-12 18:00:49 +00009527 }
9528
9529 // Insert VAARG_64 node into the DAG
9530 // VAARG_64 returns two values: Variable Argument Address, Chain
9531 SmallVector<SDValue, 11> InstOps;
9532 InstOps.push_back(Chain);
9533 InstOps.push_back(SrcPtr);
9534 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
9535 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
9536 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
9537 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
9538 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
9539 VTs, &InstOps[0], InstOps.size(),
9540 MVT::i64,
9541 MachinePointerInfo(SV),
9542 /*Align=*/0,
9543 /*Volatile=*/false,
9544 /*ReadMem=*/true,
9545 /*WriteMem=*/true);
9546 Chain = VAARG.getValue(1);
9547
9548 // Load the next argument and return it
9549 return DAG.getLoad(ArgVT, dl,
9550 Chain,
9551 VAARG,
9552 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009553 false, false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00009554}
9555
Dan Gohmand858e902010-04-17 15:26:15 +00009556SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00009557 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00009558 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00009559 SDValue Chain = Op.getOperand(0);
9560 SDValue DstPtr = Op.getOperand(1);
9561 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00009562 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
9563 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00009564 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00009565
Chris Lattnere72f2022010-09-21 05:40:29 +00009566 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00009567 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00009568 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00009569 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00009570}
9571
Craig Topper80e46362012-01-23 06:16:53 +00009572// getTargetVShiftNOde - Handle vector element shifts where the shift amount
9573// may or may not be a constant. Takes immediate version of shift as input.
9574static SDValue getTargetVShiftNode(unsigned Opc, DebugLoc dl, EVT VT,
9575 SDValue SrcOp, SDValue ShAmt,
9576 SelectionDAG &DAG) {
9577 assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32");
9578
9579 if (isa<ConstantSDNode>(ShAmt)) {
Nadav Rotemd896e242012-07-15 20:27:43 +00009580 // Constant may be a TargetConstant. Use a regular constant.
9581 uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Craig Topper80e46362012-01-23 06:16:53 +00009582 switch (Opc) {
9583 default: llvm_unreachable("Unknown target vector shift node");
9584 case X86ISD::VSHLI:
9585 case X86ISD::VSRLI:
9586 case X86ISD::VSRAI:
Nadav Rotemd896e242012-07-15 20:27:43 +00009587 return DAG.getNode(Opc, dl, VT, SrcOp,
9588 DAG.getConstant(ShiftAmt, MVT::i32));
Craig Topper80e46362012-01-23 06:16:53 +00009589 }
9590 }
9591
9592 // Change opcode to non-immediate version
9593 switch (Opc) {
9594 default: llvm_unreachable("Unknown target vector shift node");
9595 case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
9596 case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
9597 case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
9598 }
9599
9600 // Need to build a vector containing shift amount
9601 // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0
9602 SDValue ShOps[4];
9603 ShOps[0] = ShAmt;
9604 ShOps[1] = DAG.getConstant(0, MVT::i32);
Craig Topper6d688152012-08-14 07:43:25 +00009605 ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32);
Craig Topper80e46362012-01-23 06:16:53 +00009606 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4);
Nadav Rotem65f489f2012-07-14 22:26:05 +00009607
9608 // The return type has to be a 128-bit type with the same element
9609 // type as the input type.
9610 MVT EltVT = VT.getVectorElementType().getSimpleVT();
9611 EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
9612
9613 ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt);
Craig Topper80e46362012-01-23 06:16:53 +00009614 return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
9615}
9616
Dan Gohman475871a2008-07-27 21:46:04 +00009617SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00009618X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00009619 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009620 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00009621 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00009622 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00009623 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00009624 case Intrinsic::x86_sse_comieq_ss:
9625 case Intrinsic::x86_sse_comilt_ss:
9626 case Intrinsic::x86_sse_comile_ss:
9627 case Intrinsic::x86_sse_comigt_ss:
9628 case Intrinsic::x86_sse_comige_ss:
9629 case Intrinsic::x86_sse_comineq_ss:
9630 case Intrinsic::x86_sse_ucomieq_ss:
9631 case Intrinsic::x86_sse_ucomilt_ss:
9632 case Intrinsic::x86_sse_ucomile_ss:
9633 case Intrinsic::x86_sse_ucomigt_ss:
9634 case Intrinsic::x86_sse_ucomige_ss:
9635 case Intrinsic::x86_sse_ucomineq_ss:
9636 case Intrinsic::x86_sse2_comieq_sd:
9637 case Intrinsic::x86_sse2_comilt_sd:
9638 case Intrinsic::x86_sse2_comile_sd:
9639 case Intrinsic::x86_sse2_comigt_sd:
9640 case Intrinsic::x86_sse2_comige_sd:
9641 case Intrinsic::x86_sse2_comineq_sd:
9642 case Intrinsic::x86_sse2_ucomieq_sd:
9643 case Intrinsic::x86_sse2_ucomilt_sd:
9644 case Intrinsic::x86_sse2_ucomile_sd:
9645 case Intrinsic::x86_sse2_ucomigt_sd:
9646 case Intrinsic::x86_sse2_ucomige_sd:
9647 case Intrinsic::x86_sse2_ucomineq_sd: {
Craig Topper6d688152012-08-14 07:43:25 +00009648 unsigned Opc;
9649 ISD::CondCode CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00009650 switch (IntNo) {
Craig Topper86c7c582012-01-30 01:10:15 +00009651 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009652 case Intrinsic::x86_sse_comieq_ss:
9653 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009654 Opc = X86ISD::COMI;
9655 CC = ISD::SETEQ;
9656 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009657 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009658 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009659 Opc = X86ISD::COMI;
9660 CC = ISD::SETLT;
9661 break;
9662 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009663 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009664 Opc = X86ISD::COMI;
9665 CC = ISD::SETLE;
9666 break;
9667 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009668 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009669 Opc = X86ISD::COMI;
9670 CC = ISD::SETGT;
9671 break;
9672 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009673 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009674 Opc = X86ISD::COMI;
9675 CC = ISD::SETGE;
9676 break;
9677 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009678 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009679 Opc = X86ISD::COMI;
9680 CC = ISD::SETNE;
9681 break;
9682 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009683 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009684 Opc = X86ISD::UCOMI;
9685 CC = ISD::SETEQ;
9686 break;
9687 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009688 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009689 Opc = X86ISD::UCOMI;
9690 CC = ISD::SETLT;
9691 break;
9692 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009693 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009694 Opc = X86ISD::UCOMI;
9695 CC = ISD::SETLE;
9696 break;
9697 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009698 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009699 Opc = X86ISD::UCOMI;
9700 CC = ISD::SETGT;
9701 break;
9702 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00009703 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00009704 Opc = X86ISD::UCOMI;
9705 CC = ISD::SETGE;
9706 break;
9707 case Intrinsic::x86_sse_ucomineq_ss:
9708 case Intrinsic::x86_sse2_ucomineq_sd:
9709 Opc = X86ISD::UCOMI;
9710 CC = ISD::SETNE;
9711 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00009712 }
Evan Cheng734503b2006-09-11 02:19:56 +00009713
Dan Gohman475871a2008-07-27 21:46:04 +00009714 SDValue LHS = Op.getOperand(1);
9715 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00009716 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00009717 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00009718 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
9719 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
9720 DAG.getConstant(X86CC, MVT::i8), Cond);
9721 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00009722 }
Craig Topper6d688152012-08-14 07:43:25 +00009723
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009724 // Arithmetic intrinsics.
Craig Topper5b209e82012-02-05 03:14:49 +00009725 case Intrinsic::x86_sse2_pmulu_dq:
9726 case Intrinsic::x86_avx2_pmulu_dq:
9727 return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(),
9728 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009729
9730 // SSE3/AVX horizontal add/sub intrinsics
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009731 case Intrinsic::x86_sse3_hadd_ps:
9732 case Intrinsic::x86_sse3_hadd_pd:
9733 case Intrinsic::x86_avx_hadd_ps_256:
9734 case Intrinsic::x86_avx_hadd_pd_256:
Duncan Sands04aa4ae2011-09-23 16:10:22 +00009735 case Intrinsic::x86_sse3_hsub_ps:
9736 case Intrinsic::x86_sse3_hsub_pd:
9737 case Intrinsic::x86_avx_hsub_ps_256:
9738 case Intrinsic::x86_avx_hsub_pd_256:
Craig Topper4bb3f342012-01-25 05:37:32 +00009739 case Intrinsic::x86_ssse3_phadd_w_128:
9740 case Intrinsic::x86_ssse3_phadd_d_128:
9741 case Intrinsic::x86_avx2_phadd_w:
9742 case Intrinsic::x86_avx2_phadd_d:
Craig Topper4bb3f342012-01-25 05:37:32 +00009743 case Intrinsic::x86_ssse3_phsub_w_128:
9744 case Intrinsic::x86_ssse3_phsub_d_128:
9745 case Intrinsic::x86_avx2_phsub_w:
Craig Topper6d688152012-08-14 07:43:25 +00009746 case Intrinsic::x86_avx2_phsub_d: {
9747 unsigned Opcode;
9748 switch (IntNo) {
9749 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9750 case Intrinsic::x86_sse3_hadd_ps:
9751 case Intrinsic::x86_sse3_hadd_pd:
9752 case Intrinsic::x86_avx_hadd_ps_256:
9753 case Intrinsic::x86_avx_hadd_pd_256:
9754 Opcode = X86ISD::FHADD;
9755 break;
9756 case Intrinsic::x86_sse3_hsub_ps:
9757 case Intrinsic::x86_sse3_hsub_pd:
9758 case Intrinsic::x86_avx_hsub_ps_256:
9759 case Intrinsic::x86_avx_hsub_pd_256:
9760 Opcode = X86ISD::FHSUB;
9761 break;
9762 case Intrinsic::x86_ssse3_phadd_w_128:
9763 case Intrinsic::x86_ssse3_phadd_d_128:
9764 case Intrinsic::x86_avx2_phadd_w:
9765 case Intrinsic::x86_avx2_phadd_d:
9766 Opcode = X86ISD::HADD;
9767 break;
9768 case Intrinsic::x86_ssse3_phsub_w_128:
9769 case Intrinsic::x86_ssse3_phsub_d_128:
9770 case Intrinsic::x86_avx2_phsub_w:
9771 case Intrinsic::x86_avx2_phsub_d:
9772 Opcode = X86ISD::HSUB;
9773 break;
9774 }
9775 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper4bb3f342012-01-25 05:37:32 +00009776 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009777 }
9778
9779 // AVX2 variable shift intrinsics
Craig Topper98fc7292011-11-19 17:46:46 +00009780 case Intrinsic::x86_avx2_psllv_d:
9781 case Intrinsic::x86_avx2_psllv_q:
9782 case Intrinsic::x86_avx2_psllv_d_256:
9783 case Intrinsic::x86_avx2_psllv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009784 case Intrinsic::x86_avx2_psrlv_d:
9785 case Intrinsic::x86_avx2_psrlv_q:
9786 case Intrinsic::x86_avx2_psrlv_d_256:
9787 case Intrinsic::x86_avx2_psrlv_q_256:
Craig Topper98fc7292011-11-19 17:46:46 +00009788 case Intrinsic::x86_avx2_psrav_d:
Craig Topper6d688152012-08-14 07:43:25 +00009789 case Intrinsic::x86_avx2_psrav_d_256: {
9790 unsigned Opcode;
9791 switch (IntNo) {
9792 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9793 case Intrinsic::x86_avx2_psllv_d:
9794 case Intrinsic::x86_avx2_psllv_q:
9795 case Intrinsic::x86_avx2_psllv_d_256:
9796 case Intrinsic::x86_avx2_psllv_q_256:
9797 Opcode = ISD::SHL;
9798 break;
9799 case Intrinsic::x86_avx2_psrlv_d:
9800 case Intrinsic::x86_avx2_psrlv_q:
9801 case Intrinsic::x86_avx2_psrlv_d_256:
9802 case Intrinsic::x86_avx2_psrlv_q_256:
9803 Opcode = ISD::SRL;
9804 break;
9805 case Intrinsic::x86_avx2_psrav_d:
9806 case Intrinsic::x86_avx2_psrav_d_256:
9807 Opcode = ISD::SRA;
9808 break;
9809 }
9810 return DAG.getNode(Opcode, dl, Op.getValueType(),
9811 Op.getOperand(1), Op.getOperand(2));
9812 }
9813
Craig Topper969ba282012-01-25 06:43:11 +00009814 case Intrinsic::x86_ssse3_pshuf_b_128:
9815 case Intrinsic::x86_avx2_pshuf_b:
9816 return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(),
9817 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009818
Craig Topper969ba282012-01-25 06:43:11 +00009819 case Intrinsic::x86_ssse3_psign_b_128:
9820 case Intrinsic::x86_ssse3_psign_w_128:
9821 case Intrinsic::x86_ssse3_psign_d_128:
9822 case Intrinsic::x86_avx2_psign_b:
9823 case Intrinsic::x86_avx2_psign_w:
9824 case Intrinsic::x86_avx2_psign_d:
9825 return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(),
9826 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009827
Craig Toppere566cd02012-01-26 07:18:03 +00009828 case Intrinsic::x86_sse41_insertps:
9829 return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(),
9830 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009831
Craig Toppere566cd02012-01-26 07:18:03 +00009832 case Intrinsic::x86_avx_vperm2f128_ps_256:
9833 case Intrinsic::x86_avx_vperm2f128_pd_256:
9834 case Intrinsic::x86_avx_vperm2f128_si_256:
9835 case Intrinsic::x86_avx2_vperm2i128:
9836 return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(),
9837 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
Craig Topper6d688152012-08-14 07:43:25 +00009838
Craig Topperffa6c402012-04-16 07:13:00 +00009839 case Intrinsic::x86_avx2_permd:
9840 case Intrinsic::x86_avx2_permps:
9841 // Operands intentionally swapped. Mask is last operand to intrinsic,
9842 // but second operand for node/intruction.
9843 return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
9844 Op.getOperand(2), Op.getOperand(1));
Craig Topper98fc7292011-11-19 17:46:46 +00009845
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009846 // ptest and testp intrinsics. The intrinsic these come from are designed to
9847 // return an integer value, not just an instruction so lower it to the ptest
9848 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00009849 case Intrinsic::x86_sse41_ptestz:
9850 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009851 case Intrinsic::x86_sse41_ptestnzc:
9852 case Intrinsic::x86_avx_ptestz_256:
9853 case Intrinsic::x86_avx_ptestc_256:
9854 case Intrinsic::x86_avx_ptestnzc_256:
9855 case Intrinsic::x86_avx_vtestz_ps:
9856 case Intrinsic::x86_avx_vtestc_ps:
9857 case Intrinsic::x86_avx_vtestnzc_ps:
9858 case Intrinsic::x86_avx_vtestz_pd:
9859 case Intrinsic::x86_avx_vtestc_pd:
9860 case Intrinsic::x86_avx_vtestnzc_pd:
9861 case Intrinsic::x86_avx_vtestz_ps_256:
9862 case Intrinsic::x86_avx_vtestc_ps_256:
9863 case Intrinsic::x86_avx_vtestnzc_ps_256:
9864 case Intrinsic::x86_avx_vtestz_pd_256:
9865 case Intrinsic::x86_avx_vtestc_pd_256:
9866 case Intrinsic::x86_avx_vtestnzc_pd_256: {
9867 bool IsTestPacked = false;
Craig Topper6d688152012-08-14 07:43:25 +00009868 unsigned X86CC;
Eric Christopher71c67532009-07-29 00:28:05 +00009869 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00009870 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009871 case Intrinsic::x86_avx_vtestz_ps:
9872 case Intrinsic::x86_avx_vtestz_pd:
9873 case Intrinsic::x86_avx_vtestz_ps_256:
9874 case Intrinsic::x86_avx_vtestz_pd_256:
9875 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009876 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009877 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009878 // ZF = 1
9879 X86CC = X86::COND_E;
9880 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009881 case Intrinsic::x86_avx_vtestc_ps:
9882 case Intrinsic::x86_avx_vtestc_pd:
9883 case Intrinsic::x86_avx_vtestc_ps_256:
9884 case Intrinsic::x86_avx_vtestc_pd_256:
9885 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00009886 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009887 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009888 // CF = 1
9889 X86CC = X86::COND_B;
9890 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009891 case Intrinsic::x86_avx_vtestnzc_ps:
9892 case Intrinsic::x86_avx_vtestnzc_pd:
9893 case Intrinsic::x86_avx_vtestnzc_ps_256:
9894 case Intrinsic::x86_avx_vtestnzc_pd_256:
9895 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00009896 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009897 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00009898 // ZF and CF = 0
9899 X86CC = X86::COND_A;
9900 break;
9901 }
Eric Christopherfd179292009-08-27 18:07:15 +00009902
Eric Christopher71c67532009-07-29 00:28:05 +00009903 SDValue LHS = Op.getOperand(1);
9904 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009905 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
9906 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00009907 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
9908 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
9909 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00009910 }
Evan Cheng5759f972008-05-04 09:15:50 +00009911
Craig Topper80e46362012-01-23 06:16:53 +00009912 // SSE/AVX shift intrinsics
9913 case Intrinsic::x86_sse2_psll_w:
9914 case Intrinsic::x86_sse2_psll_d:
9915 case Intrinsic::x86_sse2_psll_q:
9916 case Intrinsic::x86_avx2_psll_w:
9917 case Intrinsic::x86_avx2_psll_d:
9918 case Intrinsic::x86_avx2_psll_q:
Craig Topper80e46362012-01-23 06:16:53 +00009919 case Intrinsic::x86_sse2_psrl_w:
9920 case Intrinsic::x86_sse2_psrl_d:
9921 case Intrinsic::x86_sse2_psrl_q:
9922 case Intrinsic::x86_avx2_psrl_w:
9923 case Intrinsic::x86_avx2_psrl_d:
9924 case Intrinsic::x86_avx2_psrl_q:
Craig Topper80e46362012-01-23 06:16:53 +00009925 case Intrinsic::x86_sse2_psra_w:
9926 case Intrinsic::x86_sse2_psra_d:
9927 case Intrinsic::x86_avx2_psra_w:
Craig Topper6d688152012-08-14 07:43:25 +00009928 case Intrinsic::x86_avx2_psra_d: {
9929 unsigned Opcode;
9930 switch (IntNo) {
9931 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9932 case Intrinsic::x86_sse2_psll_w:
9933 case Intrinsic::x86_sse2_psll_d:
9934 case Intrinsic::x86_sse2_psll_q:
9935 case Intrinsic::x86_avx2_psll_w:
9936 case Intrinsic::x86_avx2_psll_d:
9937 case Intrinsic::x86_avx2_psll_q:
9938 Opcode = X86ISD::VSHL;
9939 break;
9940 case Intrinsic::x86_sse2_psrl_w:
9941 case Intrinsic::x86_sse2_psrl_d:
9942 case Intrinsic::x86_sse2_psrl_q:
9943 case Intrinsic::x86_avx2_psrl_w:
9944 case Intrinsic::x86_avx2_psrl_d:
9945 case Intrinsic::x86_avx2_psrl_q:
9946 Opcode = X86ISD::VSRL;
9947 break;
9948 case Intrinsic::x86_sse2_psra_w:
9949 case Intrinsic::x86_sse2_psra_d:
9950 case Intrinsic::x86_avx2_psra_w:
9951 case Intrinsic::x86_avx2_psra_d:
9952 Opcode = X86ISD::VSRA;
9953 break;
9954 }
9955 return DAG.getNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +00009956 Op.getOperand(1), Op.getOperand(2));
Craig Topper6d688152012-08-14 07:43:25 +00009957 }
9958
9959 // SSE/AVX immediate shift intrinsics
Evan Cheng5759f972008-05-04 09:15:50 +00009960 case Intrinsic::x86_sse2_pslli_w:
9961 case Intrinsic::x86_sse2_pslli_d:
9962 case Intrinsic::x86_sse2_pslli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009963 case Intrinsic::x86_avx2_pslli_w:
9964 case Intrinsic::x86_avx2_pslli_d:
9965 case Intrinsic::x86_avx2_pslli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009966 case Intrinsic::x86_sse2_psrli_w:
9967 case Intrinsic::x86_sse2_psrli_d:
9968 case Intrinsic::x86_sse2_psrli_q:
Craig Topper80e46362012-01-23 06:16:53 +00009969 case Intrinsic::x86_avx2_psrli_w:
9970 case Intrinsic::x86_avx2_psrli_d:
9971 case Intrinsic::x86_avx2_psrli_q:
Evan Cheng5759f972008-05-04 09:15:50 +00009972 case Intrinsic::x86_sse2_psrai_w:
9973 case Intrinsic::x86_sse2_psrai_d:
Craig Topper80e46362012-01-23 06:16:53 +00009974 case Intrinsic::x86_avx2_psrai_w:
Craig Topper6d688152012-08-14 07:43:25 +00009975 case Intrinsic::x86_avx2_psrai_d: {
9976 unsigned Opcode;
9977 switch (IntNo) {
9978 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
9979 case Intrinsic::x86_sse2_pslli_w:
9980 case Intrinsic::x86_sse2_pslli_d:
9981 case Intrinsic::x86_sse2_pslli_q:
9982 case Intrinsic::x86_avx2_pslli_w:
9983 case Intrinsic::x86_avx2_pslli_d:
9984 case Intrinsic::x86_avx2_pslli_q:
9985 Opcode = X86ISD::VSHLI;
9986 break;
9987 case Intrinsic::x86_sse2_psrli_w:
9988 case Intrinsic::x86_sse2_psrli_d:
9989 case Intrinsic::x86_sse2_psrli_q:
9990 case Intrinsic::x86_avx2_psrli_w:
9991 case Intrinsic::x86_avx2_psrli_d:
9992 case Intrinsic::x86_avx2_psrli_q:
9993 Opcode = X86ISD::VSRLI;
9994 break;
9995 case Intrinsic::x86_sse2_psrai_w:
9996 case Intrinsic::x86_sse2_psrai_d:
9997 case Intrinsic::x86_avx2_psrai_w:
9998 case Intrinsic::x86_avx2_psrai_d:
9999 Opcode = X86ISD::VSRAI;
10000 break;
10001 }
10002 return getTargetVShiftNode(Opcode, dl, Op.getValueType(),
Craig Topper80e46362012-01-23 06:16:53 +000010003 Op.getOperand(1), Op.getOperand(2), DAG);
Craig Topper6d688152012-08-14 07:43:25 +000010004 }
10005
Craig Topper4feb6472012-08-06 06:22:36 +000010006 case Intrinsic::x86_sse42_pcmpistria128:
10007 case Intrinsic::x86_sse42_pcmpestria128:
10008 case Intrinsic::x86_sse42_pcmpistric128:
10009 case Intrinsic::x86_sse42_pcmpestric128:
10010 case Intrinsic::x86_sse42_pcmpistrio128:
10011 case Intrinsic::x86_sse42_pcmpestrio128:
10012 case Intrinsic::x86_sse42_pcmpistris128:
10013 case Intrinsic::x86_sse42_pcmpestris128:
10014 case Intrinsic::x86_sse42_pcmpistriz128:
10015 case Intrinsic::x86_sse42_pcmpestriz128: {
10016 unsigned Opcode;
10017 unsigned X86CC;
10018 switch (IntNo) {
10019 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10020 case Intrinsic::x86_sse42_pcmpistria128:
10021 Opcode = X86ISD::PCMPISTRI;
10022 X86CC = X86::COND_A;
10023 break;
10024 case Intrinsic::x86_sse42_pcmpestria128:
10025 Opcode = X86ISD::PCMPESTRI;
10026 X86CC = X86::COND_A;
10027 break;
10028 case Intrinsic::x86_sse42_pcmpistric128:
10029 Opcode = X86ISD::PCMPISTRI;
10030 X86CC = X86::COND_B;
10031 break;
10032 case Intrinsic::x86_sse42_pcmpestric128:
10033 Opcode = X86ISD::PCMPESTRI;
10034 X86CC = X86::COND_B;
10035 break;
10036 case Intrinsic::x86_sse42_pcmpistrio128:
10037 Opcode = X86ISD::PCMPISTRI;
10038 X86CC = X86::COND_O;
10039 break;
10040 case Intrinsic::x86_sse42_pcmpestrio128:
10041 Opcode = X86ISD::PCMPESTRI;
10042 X86CC = X86::COND_O;
10043 break;
10044 case Intrinsic::x86_sse42_pcmpistris128:
10045 Opcode = X86ISD::PCMPISTRI;
10046 X86CC = X86::COND_S;
10047 break;
10048 case Intrinsic::x86_sse42_pcmpestris128:
10049 Opcode = X86ISD::PCMPESTRI;
10050 X86CC = X86::COND_S;
10051 break;
10052 case Intrinsic::x86_sse42_pcmpistriz128:
10053 Opcode = X86ISD::PCMPISTRI;
10054 X86CC = X86::COND_E;
10055 break;
10056 case Intrinsic::x86_sse42_pcmpestriz128:
10057 Opcode = X86ISD::PCMPESTRI;
10058 X86CC = X86::COND_E;
10059 break;
10060 }
10061 SmallVector<SDValue, 5> NewOps;
10062 NewOps.append(Op->op_begin()+1, Op->op_end());
10063 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10064 SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10065 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
10066 DAG.getConstant(X86CC, MVT::i8),
10067 SDValue(PCMP.getNode(), 1));
10068 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
10069 }
Craig Topper6d688152012-08-14 07:43:25 +000010070
Craig Topper4feb6472012-08-06 06:22:36 +000010071 case Intrinsic::x86_sse42_pcmpistri128:
10072 case Intrinsic::x86_sse42_pcmpestri128: {
10073 unsigned Opcode;
10074 if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
10075 Opcode = X86ISD::PCMPISTRI;
10076 else
10077 Opcode = X86ISD::PCMPESTRI;
10078
10079 SmallVector<SDValue, 5> NewOps;
10080 NewOps.append(Op->op_begin()+1, Op->op_end());
10081 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
10082 return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size());
10083 }
Craig Topper0e292372012-08-24 04:03:22 +000010084 case Intrinsic::x86_fma_vfmadd_ps:
10085 case Intrinsic::x86_fma_vfmadd_pd:
10086 case Intrinsic::x86_fma_vfmsub_ps:
10087 case Intrinsic::x86_fma_vfmsub_pd:
10088 case Intrinsic::x86_fma_vfnmadd_ps:
10089 case Intrinsic::x86_fma_vfnmadd_pd:
10090 case Intrinsic::x86_fma_vfnmsub_ps:
10091 case Intrinsic::x86_fma_vfnmsub_pd:
10092 case Intrinsic::x86_fma_vfmaddsub_ps:
10093 case Intrinsic::x86_fma_vfmaddsub_pd:
10094 case Intrinsic::x86_fma_vfmsubadd_ps:
10095 case Intrinsic::x86_fma_vfmsubadd_pd:
10096 case Intrinsic::x86_fma_vfmadd_ps_256:
10097 case Intrinsic::x86_fma_vfmadd_pd_256:
10098 case Intrinsic::x86_fma_vfmsub_ps_256:
10099 case Intrinsic::x86_fma_vfmsub_pd_256:
10100 case Intrinsic::x86_fma_vfnmadd_ps_256:
10101 case Intrinsic::x86_fma_vfnmadd_pd_256:
10102 case Intrinsic::x86_fma_vfnmsub_ps_256:
10103 case Intrinsic::x86_fma_vfnmsub_pd_256:
10104 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10105 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10106 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10107 case Intrinsic::x86_fma_vfmsubadd_pd_256: {
Craig Topper0e292372012-08-24 04:03:22 +000010108 unsigned Opc;
10109 switch (IntNo) {
10110 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
10111 case Intrinsic::x86_fma_vfmadd_ps:
10112 case Intrinsic::x86_fma_vfmadd_pd:
10113 case Intrinsic::x86_fma_vfmadd_ps_256:
10114 case Intrinsic::x86_fma_vfmadd_pd_256:
10115 Opc = X86ISD::FMADD;
10116 break;
10117 case Intrinsic::x86_fma_vfmsub_ps:
10118 case Intrinsic::x86_fma_vfmsub_pd:
10119 case Intrinsic::x86_fma_vfmsub_ps_256:
10120 case Intrinsic::x86_fma_vfmsub_pd_256:
10121 Opc = X86ISD::FMSUB;
10122 break;
10123 case Intrinsic::x86_fma_vfnmadd_ps:
10124 case Intrinsic::x86_fma_vfnmadd_pd:
10125 case Intrinsic::x86_fma_vfnmadd_ps_256:
10126 case Intrinsic::x86_fma_vfnmadd_pd_256:
10127 Opc = X86ISD::FNMADD;
10128 break;
10129 case Intrinsic::x86_fma_vfnmsub_ps:
10130 case Intrinsic::x86_fma_vfnmsub_pd:
10131 case Intrinsic::x86_fma_vfnmsub_ps_256:
10132 case Intrinsic::x86_fma_vfnmsub_pd_256:
10133 Opc = X86ISD::FNMSUB;
10134 break;
10135 case Intrinsic::x86_fma_vfmaddsub_ps:
10136 case Intrinsic::x86_fma_vfmaddsub_pd:
10137 case Intrinsic::x86_fma_vfmaddsub_ps_256:
10138 case Intrinsic::x86_fma_vfmaddsub_pd_256:
10139 Opc = X86ISD::FMADDSUB;
10140 break;
10141 case Intrinsic::x86_fma_vfmsubadd_ps:
10142 case Intrinsic::x86_fma_vfmsubadd_pd:
10143 case Intrinsic::x86_fma_vfmsubadd_ps_256:
10144 case Intrinsic::x86_fma_vfmsubadd_pd_256:
10145 Opc = X86ISD::FMSUBADD;
10146 break;
10147 }
10148
10149 return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1),
10150 Op.getOperand(2), Op.getOperand(3));
10151 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +000010152 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000010153}
Evan Cheng72261582005-12-20 06:22:03 +000010154
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010155SDValue
10156X86TargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const {
10157 DebugLoc dl = Op.getDebugLoc();
10158 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
10159 switch (IntNo) {
10160 default: return SDValue(); // Don't custom lower most intrinsics.
10161
10162 // RDRAND intrinsics.
10163 case Intrinsic::x86_rdrand_16:
10164 case Intrinsic::x86_rdrand_32:
10165 case Intrinsic::x86_rdrand_64: {
10166 // Emit the node with the right value type.
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010167 SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
10168 SDValue Result = DAG.getNode(X86ISD::RDRAND, dl, VTs, Op.getOperand(0));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010169
10170 // If the value returned by RDRAND was valid (CF=1), return 1. Otherwise
10171 // return the value from Rand, which is always 0, casted to i32.
10172 SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
10173 DAG.getConstant(1, Op->getValueType(1)),
10174 DAG.getConstant(X86::COND_B, MVT::i32),
10175 SDValue(Result.getNode(), 1) };
10176 SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
10177 DAG.getVTList(Op->getValueType(1), MVT::Glue),
10178 Ops, 4);
10179
10180 // Return { result, isValid, chain }.
10181 return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
Benjamin Kramerfeae00a2012-07-12 18:14:57 +000010182 SDValue(Result.getNode(), 2));
Benjamin Kramerb9bee042012-07-12 09:31:43 +000010183 }
10184 }
10185}
10186
Dan Gohmand858e902010-04-17 15:26:15 +000010187SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
10188 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +000010189 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10190 MFI->setReturnAddressIsTaken(true);
10191
Bill Wendling64e87322009-01-16 19:25:27 +000010192 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010193 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +000010194
10195 if (Depth > 0) {
10196 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
10197 SDValue Offset =
10198 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +000010199 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010200 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010201 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010202 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010203 MachinePointerInfo(), false, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +000010204 }
10205
10206 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +000010207 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010208 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010209 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +000010210}
10211
Dan Gohmand858e902010-04-17 15:26:15 +000010212SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +000010213 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10214 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +000010215
Owen Andersone50ed302009-08-10 22:56:29 +000010216 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010217 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +000010218 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10219 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +000010220 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +000010221 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +000010222 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
10223 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010224 false, false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +000010225 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +000010226}
10227
Dan Gohman475871a2008-07-27 21:46:04 +000010228SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000010229 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010230 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010231}
10232
Dan Gohmand858e902010-04-17 15:26:15 +000010233SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010234 SDValue Chain = Op.getOperand(0);
10235 SDValue Offset = Op.getOperand(1);
10236 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010237 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010238
Dan Gohmand8816272010-08-11 18:14:00 +000010239 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
10240 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
10241 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010242 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010243
Dan Gohmand8816272010-08-11 18:14:00 +000010244 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
10245 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +000010246 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +000010247 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
10248 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +000010249 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010250
Dale Johannesene4d209d2009-02-03 20:21:25 +000010251 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010252 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +000010253 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +000010254}
10255
Duncan Sands4a544a72011-09-06 13:37:06 +000010256SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
10257 SelectionDAG &DAG) const {
10258 return Op.getOperand(0);
10259}
10260
10261SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
10262 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010263 SDValue Root = Op.getOperand(0);
10264 SDValue Trmp = Op.getOperand(1); // trampoline
10265 SDValue FPtr = Op.getOperand(2); // nested function
10266 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010267 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010268
Dan Gohman69de1932008-02-06 22:27:42 +000010269 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010270
10271 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010272 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +000010273
10274 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +000010275 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
10276 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +000010277
Evan Cheng0e6a0522011-07-18 20:57:22 +000010278 const unsigned char N86R10 = X86_MC::getX86RegNum(X86::R10);
10279 const unsigned char N86R11 = X86_MC::getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +000010280
10281 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
10282
10283 // Load the pointer to the nested function into R11.
10284 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +000010285 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +000010286 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010287 Addr, MachinePointerInfo(TrmpAddr),
10288 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010289
Owen Anderson825b72b2009-08-11 20:47:22 +000010290 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10291 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010292 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
10293 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +000010294 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010295
10296 // Load the 'nest' parameter value into R10.
10297 // R10 is specified in X86CallingConv.td
10298 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +000010299 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10300 DAG.getConstant(10, MVT::i64));
10301 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010302 Addr, MachinePointerInfo(TrmpAddr, 10),
10303 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010304
Owen Anderson825b72b2009-08-11 20:47:22 +000010305 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10306 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010307 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
10308 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +000010309 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +000010310
10311 // Jump to the nested function.
10312 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +000010313 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10314 DAG.getConstant(20, MVT::i64));
10315 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010316 Addr, MachinePointerInfo(TrmpAddr, 20),
10317 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010318
10319 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +000010320 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
10321 DAG.getConstant(22, MVT::i64));
10322 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010323 MachinePointerInfo(TrmpAddr, 22),
10324 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +000010325
Duncan Sands4a544a72011-09-06 13:37:06 +000010326 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010327 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +000010328 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +000010329 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000010330 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +000010331 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010332
10333 switch (CC) {
10334 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000010335 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010336 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010337 case CallingConv::X86_StdCall: {
10338 // Pass 'nest' parameter in ECX.
10339 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010340 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010341
10342 // Check that ECX wasn't needed by an 'inreg' parameter.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010343 FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +000010344 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +000010345
Chris Lattner58d74912008-03-12 17:45:29 +000010346 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +000010347 unsigned InRegCount = 0;
10348 unsigned Idx = 1;
10349
10350 for (FunctionType::param_iterator I = FTy->param_begin(),
10351 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +000010352 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +000010353 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000010354 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010355
10356 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +000010357 report_fatal_error("Nest register in use - reduce number of inreg"
10358 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +000010359 }
10360 }
10361 break;
10362 }
10363 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +000010364 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +000010365 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +000010366 // Pass 'nest' parameter in EAX.
10367 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +000010368 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010369 break;
10370 }
10371
Dan Gohman475871a2008-07-27 21:46:04 +000010372 SDValue OutChains[4];
10373 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +000010374
Owen Anderson825b72b2009-08-11 20:47:22 +000010375 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10376 DAG.getConstant(10, MVT::i32));
10377 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010378
Chris Lattnera62fe662010-02-05 19:20:30 +000010379 // This is storing the opcode for MOV32ri.
10380 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Evan Cheng0e6a0522011-07-18 20:57:22 +000010381 const unsigned char N86Reg = X86_MC::getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +000010382 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +000010383 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +000010384 Trmp, MachinePointerInfo(TrmpAddr),
10385 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010386
Owen Anderson825b72b2009-08-11 20:47:22 +000010387 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10388 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010389 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
10390 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +000010391 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010392
Chris Lattnera62fe662010-02-05 19:20:30 +000010393 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +000010394 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10395 DAG.getConstant(5, MVT::i32));
10396 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000010397 MachinePointerInfo(TrmpAddr, 5),
10398 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010399
Owen Anderson825b72b2009-08-11 20:47:22 +000010400 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
10401 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +000010402 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
10403 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +000010404 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010405
Duncan Sands4a544a72011-09-06 13:37:06 +000010406 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
Duncan Sandsb116fac2007-07-27 20:02:49 +000010407 }
10408}
10409
Dan Gohmand858e902010-04-17 15:26:15 +000010410SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
10411 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010412 /*
10413 The rounding mode is in bits 11:10 of FPSR, and has the following
10414 settings:
10415 00 Round to nearest
10416 01 Round to -inf
10417 10 Round to +inf
10418 11 Round to 0
10419
10420 FLT_ROUNDS, on the other hand, expects the following:
10421 -1 Undefined
10422 0 Round to 0
10423 1 Round to nearest
10424 2 Round to +inf
10425 3 Round to -inf
10426
10427 To perform the conversion, we do:
10428 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
10429 */
10430
10431 MachineFunction &MF = DAG.getMachineFunction();
10432 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010433 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010434 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +000010435 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +000010436 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010437
10438 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +000010439 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +000010440 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010441
Michael J. Spencerec38de22010-10-10 22:04:20 +000010442
Chris Lattner2156b792010-09-22 01:11:26 +000010443 MachineMemOperand *MMO =
10444 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
10445 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010446
Chris Lattner2156b792010-09-22 01:11:26 +000010447 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
10448 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
10449 DAG.getVTList(MVT::Other),
10450 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010451
10452 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +000010453 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010454 MachinePointerInfo(), false, false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010455
10456 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +000010457 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +000010458 DAG.getNode(ISD::SRL, DL, MVT::i16,
10459 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010460 CWD, DAG.getConstant(0x800, MVT::i16)),
10461 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +000010462 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +000010463 DAG.getNode(ISD::SRL, DL, MVT::i16,
10464 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +000010465 CWD, DAG.getConstant(0x400, MVT::i16)),
10466 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010467
Dan Gohman475871a2008-07-27 21:46:04 +000010468 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +000010469 DAG.getNode(ISD::AND, DL, MVT::i16,
10470 DAG.getNode(ISD::ADD, DL, MVT::i16,
10471 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +000010472 DAG.getConstant(1, MVT::i16)),
10473 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010474
10475
Duncan Sands83ec4b62008-06-06 12:08:01 +000010476 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +000010477 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000010478}
10479
Dan Gohmand858e902010-04-17 15:26:15 +000010480SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010481 EVT VT = Op.getValueType();
10482 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010483 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010484 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010485
10486 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010487 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +000010488 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +000010489 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +000010490 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010491 }
Evan Cheng18efe262007-12-14 02:13:44 +000010492
Evan Cheng152804e2007-12-14 08:30:15 +000010493 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010494 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010495 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010496
10497 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010498 SDValue Ops[] = {
10499 Op,
10500 DAG.getConstant(NumBits+NumBits-1, OpVT),
10501 DAG.getConstant(X86::COND_E, MVT::i8),
10502 Op.getValue(1)
10503 };
10504 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +000010505
10506 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +000010507 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +000010508
Owen Anderson825b72b2009-08-11 20:47:22 +000010509 if (VT == MVT::i8)
10510 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +000010511 return Op;
10512}
10513
Chandler Carruthacc068e2011-12-24 10:55:54 +000010514SDValue X86TargetLowering::LowerCTLZ_ZERO_UNDEF(SDValue Op,
10515 SelectionDAG &DAG) const {
10516 EVT VT = Op.getValueType();
10517 EVT OpVT = VT;
10518 unsigned NumBits = VT.getSizeInBits();
10519 DebugLoc dl = Op.getDebugLoc();
10520
10521 Op = Op.getOperand(0);
10522 if (VT == MVT::i8) {
10523 // Zero extend to i32 since there is not an i8 bsr.
10524 OpVT = MVT::i32;
10525 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
10526 }
10527
10528 // Issue a bsr (scan bits in reverse).
10529 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
10530 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
10531
10532 // And xor with NumBits-1.
10533 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
10534
10535 if (VT == MVT::i8)
10536 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
10537 return Op;
10538}
10539
Dan Gohmand858e902010-04-17 15:26:15 +000010540SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000010541 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010542 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010543 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +000010544 Op = Op.getOperand(0);
Evan Cheng152804e2007-12-14 08:30:15 +000010545
10546 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Chandler Carruth77821022011-12-24 12:12:34 +000010547 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +000010548 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +000010549
10550 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010551 SDValue Ops[] = {
10552 Op,
Chandler Carruth77821022011-12-24 12:12:34 +000010553 DAG.getConstant(NumBits, VT),
Benjamin Kramer7f1a5602009-12-29 16:57:26 +000010554 DAG.getConstant(X86::COND_E, MVT::i8),
10555 Op.getValue(1)
10556 };
Chandler Carruth77821022011-12-24 12:12:34 +000010557 return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops));
Evan Cheng18efe262007-12-14 02:13:44 +000010558}
10559
Craig Topper13894fa2011-08-24 06:14:18 +000010560// Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
10561// ones, and then concatenate the result back.
10562static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +000010563 EVT VT = Op.getValueType();
Craig Topper13894fa2011-08-24 06:14:18 +000010564
Craig Topper7a9a28b2012-08-12 02:23:29 +000010565 assert(VT.is256BitVector() && VT.isInteger() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010566 "Unsupported value type for operation");
10567
Craig Topper66ddd152012-04-27 22:54:43 +000010568 unsigned NumElems = VT.getVectorNumElements();
Craig Topper13894fa2011-08-24 06:14:18 +000010569 DebugLoc dl = Op.getDebugLoc();
Craig Topper13894fa2011-08-24 06:14:18 +000010570
10571 // Extract the LHS vectors
10572 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010573 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10574 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010575
10576 // Extract the RHS vectors
10577 SDValue RHS = Op.getOperand(1);
Craig Topperb14940a2012-04-22 20:55:18 +000010578 SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
10579 SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
Craig Topper13894fa2011-08-24 06:14:18 +000010580
10581 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10582 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10583
10584 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
10585 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
10586 DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
10587}
10588
10589SDValue X86TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010590 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010591 Op.getValueType().isInteger() &&
10592 "Only handle AVX 256-bit vector integer operation");
10593 return Lower256IntArith(Op, DAG);
10594}
10595
10596SDValue X86TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) const {
Craig Topper7a9a28b2012-08-12 02:23:29 +000010597 assert(Op.getValueType().is256BitVector() &&
Craig Topper13894fa2011-08-24 06:14:18 +000010598 Op.getValueType().isInteger() &&
10599 "Only handle AVX 256-bit vector integer operation");
10600 return Lower256IntArith(Op, DAG);
10601}
10602
10603SDValue X86TargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
10604 EVT VT = Op.getValueType();
10605
10606 // Decompose 256-bit ops into smaller 128-bit ops.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010607 if (VT.is256BitVector() && !Subtarget->hasAVX2())
Craig Topper13894fa2011-08-24 06:14:18 +000010608 return Lower256IntArith(Op, DAG);
10609
Craig Topper5b209e82012-02-05 03:14:49 +000010610 assert((VT == MVT::v2i64 || VT == MVT::v4i64) &&
10611 "Only know how to lower V2I64/V4I64 multiply");
10612
Dale Johannesen6f38cb62009-02-07 19:59:05 +000010613 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +000010614
Craig Topper5b209e82012-02-05 03:14:49 +000010615 // Ahi = psrlqi(a, 32);
10616 // Bhi = psrlqi(b, 32);
10617 //
10618 // AloBlo = pmuludq(a, b);
10619 // AloBhi = pmuludq(a, Bhi);
10620 // AhiBlo = pmuludq(Ahi, b);
10621
10622 // AloBhi = psllqi(AloBhi, 32);
10623 // AhiBlo = psllqi(AhiBlo, 32);
10624 // return AloBlo + AloBhi + AhiBlo;
10625
Craig Topperaaa643c2011-11-09 07:28:55 +000010626 SDValue A = Op.getOperand(0);
10627 SDValue B = Op.getOperand(1);
10628
Craig Topper5b209e82012-02-05 03:14:49 +000010629 SDValue ShAmt = DAG.getConstant(32, MVT::i32);
Craig Topperaaa643c2011-11-09 07:28:55 +000010630
Craig Topper5b209e82012-02-05 03:14:49 +000010631 SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt);
10632 SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt);
Craig Topperaaa643c2011-11-09 07:28:55 +000010633
Craig Topper5b209e82012-02-05 03:14:49 +000010634 // Bit cast to 32-bit vectors for MULUDQ
10635 EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32;
10636 A = DAG.getNode(ISD::BITCAST, dl, MulVT, A);
10637 B = DAG.getNode(ISD::BITCAST, dl, MulVT, B);
10638 Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi);
10639 Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi);
Craig Topperaaa643c2011-11-09 07:28:55 +000010640
Craig Topper5b209e82012-02-05 03:14:49 +000010641 SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
10642 SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
10643 SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
Craig Topperaaa643c2011-11-09 07:28:55 +000010644
Craig Topper5b209e82012-02-05 03:14:49 +000010645 AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt);
10646 AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010647
Dale Johannesene4d209d2009-02-03 20:21:25 +000010648 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
Craig Topper5b209e82012-02-05 03:14:49 +000010649 return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +000010650}
10651
Nadav Rotem43012222011-05-11 08:12:09 +000010652SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
10653
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010654 EVT VT = Op.getValueType();
10655 DebugLoc dl = Op.getDebugLoc();
10656 SDValue R = Op.getOperand(0);
Nadav Rotem43012222011-05-11 08:12:09 +000010657 SDValue Amt = Op.getOperand(1);
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010658 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010659
Craig Topper1accb7e2012-01-10 06:54:16 +000010660 if (!Subtarget->hasSSE2())
Bruno Cardoso Lopes328a9d42011-08-08 21:31:08 +000010661 return SDValue();
10662
Nadav Rotem43012222011-05-11 08:12:09 +000010663 // Optimize shl/srl/sra with constant shift amount.
10664 if (isSplatVector(Amt.getNode())) {
10665 SDValue SclrAmt = Amt->getOperand(0);
10666 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) {
10667 uint64_t ShiftAmt = C->getZExtValue();
10668
Craig Toppered2e13d2012-01-22 19:15:14 +000010669 if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 ||
10670 (Subtarget->hasAVX2() &&
10671 (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) {
10672 if (Op.getOpcode() == ISD::SHL)
10673 return DAG.getNode(X86ISD::VSHLI, dl, VT, R,
10674 DAG.getConstant(ShiftAmt, MVT::i32));
10675 if (Op.getOpcode() == ISD::SRL)
10676 return DAG.getNode(X86ISD::VSRLI, dl, VT, R,
10677 DAG.getConstant(ShiftAmt, MVT::i32));
10678 if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64)
10679 return DAG.getNode(X86ISD::VSRAI, dl, VT, R,
10680 DAG.getConstant(ShiftAmt, MVT::i32));
Benjamin Kramerdade3c12011-10-30 17:31:21 +000010681 }
10682
Craig Toppered2e13d2012-01-22 19:15:14 +000010683 if (VT == MVT::v16i8) {
10684 if (Op.getOpcode() == ISD::SHL) {
10685 // Make a large shift.
10686 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R,
10687 DAG.getConstant(ShiftAmt, MVT::i32));
10688 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
10689 // Zero out the rightmost bits.
10690 SmallVector<SDValue, 16> V(16,
10691 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10692 MVT::i8));
10693 return DAG.getNode(ISD::AND, dl, VT, SHL,
10694 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010695 }
Craig Toppered2e13d2012-01-22 19:15:14 +000010696 if (Op.getOpcode() == ISD::SRL) {
10697 // Make a large shift.
10698 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R,
10699 DAG.getConstant(ShiftAmt, MVT::i32));
10700 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
10701 // Zero out the leftmost bits.
10702 SmallVector<SDValue, 16> V(16,
10703 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10704 MVT::i8));
10705 return DAG.getNode(ISD::AND, dl, VT, SRL,
10706 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16));
10707 }
10708 if (Op.getOpcode() == ISD::SRA) {
10709 if (ShiftAmt == 7) {
10710 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010711 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010712 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Toppered2e13d2012-01-22 19:15:14 +000010713 }
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010714
Craig Toppered2e13d2012-01-22 19:15:14 +000010715 // R s>> a === ((R u>> a) ^ m) - m
10716 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10717 SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt,
10718 MVT::i8));
10719 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16);
10720 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10721 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10722 return Res;
10723 }
Craig Topper731dfd02012-04-23 03:42:40 +000010724 llvm_unreachable("Unknown shift opcode.");
Eli Friedmanf6aa6b12011-11-01 21:18:39 +000010725 }
Craig Topper46154eb2011-11-11 07:39:23 +000010726
Craig Topper0d86d462011-11-20 00:12:05 +000010727 if (Subtarget->hasAVX2() && VT == MVT::v32i8) {
10728 if (Op.getOpcode() == ISD::SHL) {
10729 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010730 SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R,
10731 DAG.getConstant(ShiftAmt, MVT::i32));
10732 SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL);
Craig Topper0d86d462011-11-20 00:12:05 +000010733 // Zero out the rightmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010734 SmallVector<SDValue, 32> V(32,
10735 DAG.getConstant(uint8_t(-1U << ShiftAmt),
10736 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010737 return DAG.getNode(ISD::AND, dl, VT, SHL,
10738 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
Craig Topper46154eb2011-11-11 07:39:23 +000010739 }
Craig Topper0d86d462011-11-20 00:12:05 +000010740 if (Op.getOpcode() == ISD::SRL) {
10741 // Make a large shift.
Craig Toppered2e13d2012-01-22 19:15:14 +000010742 SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R,
10743 DAG.getConstant(ShiftAmt, MVT::i32));
10744 SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL);
Craig Topper0d86d462011-11-20 00:12:05 +000010745 // Zero out the leftmost bits.
Craig Toppered2e13d2012-01-22 19:15:14 +000010746 SmallVector<SDValue, 32> V(32,
10747 DAG.getConstant(uint8_t(-1U) >> ShiftAmt,
10748 MVT::i8));
Craig Topper0d86d462011-11-20 00:12:05 +000010749 return DAG.getNode(ISD::AND, dl, VT, SRL,
10750 DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32));
10751 }
10752 if (Op.getOpcode() == ISD::SRA) {
10753 if (ShiftAmt == 7) {
10754 // R s>> 7 === R s< 0
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000010755 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topper67609fd2012-01-22 22:42:16 +000010756 return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
Craig Topper0d86d462011-11-20 00:12:05 +000010757 }
10758
10759 // R s>> a === ((R u>> a) ^ m) - m
10760 SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
10761 SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt,
10762 MVT::i8));
10763 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32);
10764 Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
10765 Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
10766 return Res;
10767 }
Craig Topper731dfd02012-04-23 03:42:40 +000010768 llvm_unreachable("Unknown shift opcode.");
Craig Topper0d86d462011-11-20 00:12:05 +000010769 }
Nadav Rotem43012222011-05-11 08:12:09 +000010770 }
10771 }
10772
10773 // Lower SHL with variable shift amount.
Nadav Rotem43012222011-05-11 08:12:09 +000010774 if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010775 Op = DAG.getNode(X86ISD::VSHLI, dl, VT, Op.getOperand(1),
10776 DAG.getConstant(23, MVT::i32));
Nate Begeman51409212010-07-28 00:21:48 +000010777
Chris Lattner7302d802012-02-06 21:56:39 +000010778 const uint32_t CV[] = { 0x3f800000U, 0x3f800000U, 0x3f800000U, 0x3f800000U};
10779 Constant *C = ConstantDataVector::get(*Context, CV);
Nate Begeman51409212010-07-28 00:21:48 +000010780 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
10781 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +000010782 MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010783 false, false, false, 16);
Nate Begeman51409212010-07-28 00:21:48 +000010784
10785 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010786 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010787 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
10788 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
10789 }
Nadav Rotem43012222011-05-11 08:12:09 +000010790 if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) {
Craig Topper8b5a6b62012-01-17 08:23:44 +000010791 assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq.");
Lang Hames8b99c1e2011-12-17 01:08:46 +000010792
Nate Begeman51409212010-07-28 00:21:48 +000010793 // a = a << 5;
Craig Toppered2e13d2012-01-22 19:15:14 +000010794 Op = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, Op.getOperand(1),
10795 DAG.getConstant(5, MVT::i32));
10796 Op = DAG.getNode(ISD::BITCAST, dl, VT, Op);
Nate Begeman51409212010-07-28 00:21:48 +000010797
Lang Hames8b99c1e2011-12-17 01:08:46 +000010798 // Turn 'a' into a mask suitable for VSELECT
10799 SDValue VSelM = DAG.getConstant(0x80, VT);
10800 SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010801 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Nate Begeman51409212010-07-28 00:21:48 +000010802
Lang Hames8b99c1e2011-12-17 01:08:46 +000010803 SDValue CM1 = DAG.getConstant(0x0f, VT);
10804 SDValue CM2 = DAG.getConstant(0x3f, VT);
Nate Begeman51409212010-07-28 00:21:48 +000010805
Lang Hames8b99c1e2011-12-17 01:08:46 +000010806 // r = VSELECT(r, psllw(r & (char16)15, 4), a);
10807 SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1);
Craig Toppered2e13d2012-01-22 19:15:14 +000010808 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10809 DAG.getConstant(4, MVT::i32), DAG);
10810 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010811 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10812
Nate Begeman51409212010-07-28 00:21:48 +000010813 // a += a
10814 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010815 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010816 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010817
Lang Hames8b99c1e2011-12-17 01:08:46 +000010818 // r = VSELECT(r, psllw(r & (char16)63, 2), a);
10819 M = DAG.getNode(ISD::AND, dl, VT, R, CM2);
Craig Toppered2e13d2012-01-22 19:15:14 +000010820 M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M,
10821 DAG.getConstant(2, MVT::i32), DAG);
10822 M = DAG.getNode(ISD::BITCAST, dl, VT, M);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010823 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R);
10824
Nate Begeman51409212010-07-28 00:21:48 +000010825 // a += a
10826 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Lang Hames8b99c1e2011-12-17 01:08:46 +000010827 OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op);
Craig Topper67609fd2012-01-22 22:42:16 +000010828 OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM);
Michael J. Spencerec38de22010-10-10 22:04:20 +000010829
Lang Hames8b99c1e2011-12-17 01:08:46 +000010830 // return VSELECT(r, r+r, a);
10831 R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel,
Lang Hamesa0a25132011-12-15 18:57:27 +000010832 DAG.getNode(ISD::ADD, dl, VT, R, R), R);
Nate Begeman51409212010-07-28 00:21:48 +000010833 return R;
10834 }
Craig Topper46154eb2011-11-11 07:39:23 +000010835
10836 // Decompose 256-bit shifts into smaller 128-bit shifts.
Craig Topper7a9a28b2012-08-12 02:23:29 +000010837 if (VT.is256BitVector()) {
Craig Toppered2e13d2012-01-22 19:15:14 +000010838 unsigned NumElems = VT.getVectorNumElements();
Craig Topper46154eb2011-11-11 07:39:23 +000010839 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10840 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
10841
10842 // Extract the two vectors
Craig Topperb14940a2012-04-22 20:55:18 +000010843 SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
10844 SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010845
10846 // Recreate the shift amount vectors
10847 SDValue Amt1, Amt2;
10848 if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
10849 // Constant shift amount
10850 SmallVector<SDValue, 4> Amt1Csts;
10851 SmallVector<SDValue, 4> Amt2Csts;
Craig Toppered2e13d2012-01-22 19:15:14 +000010852 for (unsigned i = 0; i != NumElems/2; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010853 Amt1Csts.push_back(Amt->getOperand(i));
Craig Toppered2e13d2012-01-22 19:15:14 +000010854 for (unsigned i = NumElems/2; i != NumElems; ++i)
Craig Topper46154eb2011-11-11 07:39:23 +000010855 Amt2Csts.push_back(Amt->getOperand(i));
10856
10857 Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10858 &Amt1Csts[0], NumElems/2);
10859 Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT,
10860 &Amt2Csts[0], NumElems/2);
10861 } else {
10862 // Variable shift amount
Craig Topperb14940a2012-04-22 20:55:18 +000010863 Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
10864 Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
Craig Topper46154eb2011-11-11 07:39:23 +000010865 }
10866
10867 // Issue new vector shifts for the smaller types
10868 V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
10869 V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
10870
10871 // Concatenate the result back
10872 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
10873 }
10874
Nate Begeman51409212010-07-28 00:21:48 +000010875 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +000010876}
Mon P Wangaf9b9522008-12-18 21:42:19 +000010877
Dan Gohmand858e902010-04-17 15:26:15 +000010878SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +000010879 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
10880 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +000010881 // looks for this combo and may remove the "setcc" instruction if the "setcc"
10882 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +000010883 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +000010884 SDValue LHS = N->getOperand(0);
10885 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +000010886 unsigned BaseOp = 0;
10887 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010888 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +000010889 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010890 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +000010891 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +000010892 // A subtract of one will be selected as a INC. Note that INC doesn't
10893 // set CF, so we can't do this for UADDO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010894 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10895 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010896 BaseOp = X86ISD::INC;
10897 Cond = X86::COND_O;
10898 break;
10899 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010900 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +000010901 Cond = X86::COND_O;
10902 break;
10903 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010904 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +000010905 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010906 break;
10907 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +000010908 // A subtract of one will be selected as a DEC. Note that DEC doesn't
10909 // set CF, so we can't do this for USUBO.
Benjamin Kramerc175a4b2011-03-08 15:20:20 +000010910 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS))
10911 if (C->isOne()) {
Dan Gohman076aee32009-03-04 19:44:21 +000010912 BaseOp = X86ISD::DEC;
10913 Cond = X86::COND_O;
10914 break;
10915 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010916 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +000010917 Cond = X86::COND_O;
10918 break;
10919 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +000010920 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +000010921 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +000010922 break;
10923 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +000010924 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +000010925 Cond = X86::COND_O;
10926 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010927 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
10928 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
10929 MVT::i32);
10930 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010931
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010932 SDValue SetCC =
10933 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10934 DAG.getConstant(X86::COND_O, MVT::i32),
10935 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010936
Dan Gohman6e5fda22011-07-22 18:45:15 +000010937 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010938 }
Bill Wendling74c37652008-12-09 22:08:41 +000010939 }
Bill Wendling3fafd932008-11-26 22:37:40 +000010940
Bill Wendling61edeb52008-12-02 01:06:39 +000010941 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +000010942 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010943 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +000010944
Bill Wendling61edeb52008-12-02 01:06:39 +000010945 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +000010946 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
10947 DAG.getConstant(Cond, MVT::i32),
10948 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +000010949
Dan Gohman6e5fda22011-07-22 18:45:15 +000010950 return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
Bill Wendling41ea7e72008-11-24 19:21:46 +000010951}
10952
Chad Rosier30450e82011-12-22 22:35:21 +000010953SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
10954 SelectionDAG &DAG) const {
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010955 DebugLoc dl = Op.getDebugLoc();
Craig Toppera124f942011-11-21 01:12:36 +000010956 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
10957 EVT VT = Op.getValueType();
10958
Craig Toppered2e13d2012-01-22 19:15:14 +000010959 if (!Subtarget->hasSSE2() || !VT.isVector())
10960 return SDValue();
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010961
Craig Toppered2e13d2012-01-22 19:15:14 +000010962 unsigned BitsDiff = VT.getScalarType().getSizeInBits() -
10963 ExtraVT.getScalarType().getSizeInBits();
10964 SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32);
10965
10966 switch (VT.getSimpleVT().SimpleTy) {
10967 default: return SDValue();
10968 case MVT::v8i32:
10969 case MVT::v16i16:
10970 if (!Subtarget->hasAVX())
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000010971 return SDValue();
Craig Toppered2e13d2012-01-22 19:15:14 +000010972 if (!Subtarget->hasAVX2()) {
10973 // needs to be split
Craig Topper66ddd152012-04-27 22:54:43 +000010974 unsigned NumElems = VT.getVectorNumElements();
Craig Toppera124f942011-11-21 01:12:36 +000010975
Craig Toppered2e13d2012-01-22 19:15:14 +000010976 // Extract the LHS vectors
10977 SDValue LHS = Op.getOperand(0);
Craig Topperb14940a2012-04-22 20:55:18 +000010978 SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
10979 SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
Craig Toppera124f942011-11-21 01:12:36 +000010980
Craig Toppered2e13d2012-01-22 19:15:14 +000010981 MVT EltVT = VT.getVectorElementType().getSimpleVT();
10982 EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
Craig Toppera124f942011-11-21 01:12:36 +000010983
Craig Toppered2e13d2012-01-22 19:15:14 +000010984 EVT ExtraEltVT = ExtraVT.getVectorElementType();
Craig Topperb6072642012-05-03 07:26:59 +000010985 unsigned ExtraNumElems = ExtraVT.getVectorNumElements();
Craig Toppered2e13d2012-01-22 19:15:14 +000010986 ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT,
10987 ExtraNumElems/2);
10988 SDValue Extra = DAG.getValueType(ExtraVT);
Craig Toppera124f942011-11-21 01:12:36 +000010989
Craig Toppered2e13d2012-01-22 19:15:14 +000010990 LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra);
10991 LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra);
Craig Toppera124f942011-11-21 01:12:36 +000010992
Craig Toppered2e13d2012-01-22 19:15:14 +000010993 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2);;
10994 }
10995 // fall through
10996 case MVT::v4i32:
10997 case MVT::v8i16: {
10998 SDValue Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT,
10999 Op.getOperand(0), ShAmt, DAG);
11000 return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG);
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011001 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011002 }
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011003}
11004
11005
Eric Christopher9a9d2752010-07-22 02:48:34 +000011006SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
11007 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011008
Eric Christopher77ed1352011-07-08 00:04:56 +000011009 // Go ahead and emit the fence on x86-64 even if we asked for no-sse2.
11010 // There isn't any reason to disable it if the target processor supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011011 if (!Subtarget->hasSSE2() && !Subtarget->is64Bit()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +000011012 SDValue Chain = Op.getOperand(0);
Eric Christopher77ed1352011-07-08 00:04:56 +000011013 SDValue Zero = DAG.getConstant(0, MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +000011014 SDValue Ops[] = {
11015 DAG.getRegister(X86::ESP, MVT::i32), // Base
11016 DAG.getTargetConstant(1, MVT::i8), // Scale
11017 DAG.getRegister(0, MVT::i32), // Index
11018 DAG.getTargetConstant(0, MVT::i32), // Disp
11019 DAG.getRegister(0, MVT::i32), // Segment.
11020 Zero,
11021 Chain
11022 };
Michael J. Spencerec38de22010-10-10 22:04:20 +000011023 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +000011024 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11025 array_lengthof(Ops));
11026 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +000011027 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000011028
Eric Christopher9a9d2752010-07-22 02:48:34 +000011029 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +000011030 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +000011031 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011032
Chris Lattner132929a2010-08-14 17:26:09 +000011033 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11034 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
11035 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
11036 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +000011037
Chris Lattner132929a2010-08-14 17:26:09 +000011038 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
11039 if (!Op1 && !Op2 && !Op3 && Op4)
11040 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011041
Chris Lattner132929a2010-08-14 17:26:09 +000011042 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
11043 if (Op1 && !Op2 && !Op3 && !Op4)
11044 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +000011045
11046 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +000011047 // (MFENCE)>;
11048 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +000011049}
11050
Eli Friedman14648462011-07-27 22:21:52 +000011051SDValue X86TargetLowering::LowerATOMIC_FENCE(SDValue Op,
11052 SelectionDAG &DAG) const {
11053 DebugLoc dl = Op.getDebugLoc();
11054 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
11055 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
11056 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
11057 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
11058
11059 // The only fence that needs an instruction is a sequentially-consistent
11060 // cross-thread fence.
11061 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
11062 // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
11063 // no-sse2). There isn't any reason to disable it if the target processor
11064 // supports it.
Craig Topper1accb7e2012-01-10 06:54:16 +000011065 if (Subtarget->hasSSE2() || Subtarget->is64Bit())
Eli Friedman14648462011-07-27 22:21:52 +000011066 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
11067
11068 SDValue Chain = Op.getOperand(0);
11069 SDValue Zero = DAG.getConstant(0, MVT::i32);
11070 SDValue Ops[] = {
11071 DAG.getRegister(X86::ESP, MVT::i32), // Base
11072 DAG.getTargetConstant(1, MVT::i8), // Scale
11073 DAG.getRegister(0, MVT::i32), // Index
11074 DAG.getTargetConstant(0, MVT::i32), // Disp
11075 DAG.getRegister(0, MVT::i32), // Segment.
11076 Zero,
11077 Chain
11078 };
11079 SDNode *Res =
11080 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
11081 array_lengthof(Ops));
11082 return SDValue(Res, 0);
11083 }
11084
11085 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
11086 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
11087}
11088
11089
Dan Gohmand858e902010-04-17 15:26:15 +000011090SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +000011091 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011092 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +000011093 unsigned Reg = 0;
11094 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +000011095 switch(T.getSimpleVT().SimpleTy) {
Craig Topperabb94d02012-02-05 03:43:23 +000011096 default: llvm_unreachable("Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +000011097 case MVT::i8: Reg = X86::AL; size = 1; break;
11098 case MVT::i16: Reg = X86::AX; size = 2; break;
11099 case MVT::i32: Reg = X86::EAX; size = 4; break;
11100 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +000011101 assert(Subtarget->is64Bit() && "Node not type legal!");
11102 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +000011103 break;
Bill Wendling61edeb52008-12-02 01:06:39 +000011104 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011105 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +000011106 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +000011107 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011108 Op.getOperand(1),
11109 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +000011110 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +000011111 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011112 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011113 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
11114 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
11115 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +000011116 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +000011117 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +000011118 return cpOut;
11119}
11120
Duncan Sands1607f052008-12-01 11:39:25 +000011121SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +000011122 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +000011123 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011124 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011125 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +000011126 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011127 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011128 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
11129 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +000011130 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +000011131 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
11132 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +000011133 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +000011134 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +000011135 rdx.getValue(1)
11136 };
Dale Johannesene4d209d2009-02-03 20:21:25 +000011137 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011138}
11139
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011140SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +000011141 SelectionDAG &DAG) const {
11142 EVT SrcVT = Op.getOperand(0).getValueType();
11143 EVT DstVT = Op.getValueType();
Craig Topper1accb7e2012-01-10 06:54:16 +000011144 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
Chris Lattner2a786eb2010-12-19 20:19:20 +000011145 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +000011146 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +000011147 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011148 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +000011149 // i64 <=> MMX conversions are Legal.
11150 if (SrcVT==MVT::i64 && DstVT.isVector())
11151 return Op;
11152 if (DstVT==MVT::i64 && SrcVT.isVector())
11153 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +000011154 // MMX <=> MMX conversions are Legal.
11155 if (SrcVT.isVector() && DstVT.isVector())
11156 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +000011157 // All other conversions need to be expanded.
11158 return SDValue();
11159}
Chris Lattner5b856542010-12-20 00:59:46 +000011160
Dan Gohmand858e902010-04-17 15:26:15 +000011161SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011162 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +000011163 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000011164 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011165 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +000011166 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +000011167 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011168 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +000011169 Node->getOperand(0),
11170 Node->getOperand(1), negOp,
11171 cast<AtomicSDNode>(Node)->getSrcValue(),
Eli Friedman55ba8162011-07-29 03:05:32 +000011172 cast<AtomicSDNode>(Node)->getAlignment(),
11173 cast<AtomicSDNode>(Node)->getOrdering(),
11174 cast<AtomicSDNode>(Node)->getSynchScope());
Mon P Wang63307c32008-05-05 19:05:59 +000011175}
11176
Eli Friedman327236c2011-08-24 20:50:09 +000011177static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
11178 SDNode *Node = Op.getNode();
11179 DebugLoc dl = Node->getDebugLoc();
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011180 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
Eli Friedman327236c2011-08-24 20:50:09 +000011181
11182 // Convert seq_cst store -> xchg
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011183 // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
11184 // FIXME: On 32-bit, store -> fist or movq would be more efficient
11185 // (The only way to get a 16-byte store is cmpxchg16b)
11186 // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
11187 if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
11188 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Eli Friedman4317fe12011-08-24 21:17:30 +000011189 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
11190 cast<AtomicSDNode>(Node)->getMemoryVT(),
11191 Node->getOperand(0),
11192 Node->getOperand(1), Node->getOperand(2),
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011193 cast<AtomicSDNode>(Node)->getMemOperand(),
Eli Friedman4317fe12011-08-24 21:17:30 +000011194 cast<AtomicSDNode>(Node)->getOrdering(),
11195 cast<AtomicSDNode>(Node)->getSynchScope());
Eli Friedman327236c2011-08-24 20:50:09 +000011196 return Swap.getValue(1);
11197 }
11198 // Other atomic stores have a simple pattern.
11199 return Op;
11200}
11201
Chris Lattner5b856542010-12-20 00:59:46 +000011202static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
11203 EVT VT = Op.getNode()->getValueType(0);
11204
11205 // Let legalize expand this if it isn't a legal type yet.
11206 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
11207 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011208
Chris Lattner5b856542010-12-20 00:59:46 +000011209 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011210
Chris Lattner5b856542010-12-20 00:59:46 +000011211 unsigned Opc;
11212 bool ExtraOp = false;
11213 switch (Op.getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000011214 default: llvm_unreachable("Invalid code");
Chris Lattner5b856542010-12-20 00:59:46 +000011215 case ISD::ADDC: Opc = X86ISD::ADD; break;
11216 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
11217 case ISD::SUBC: Opc = X86ISD::SUB; break;
11218 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
11219 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011220
Chris Lattner5b856542010-12-20 00:59:46 +000011221 if (!ExtraOp)
11222 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11223 Op.getOperand(1));
11224 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
11225 Op.getOperand(1), Op.getOperand(2));
11226}
11227
Evan Cheng0db9fe62006-04-25 20:13:52 +000011228/// LowerOperation - Provide custom lowering hooks for some operations.
11229///
Dan Gohmand858e902010-04-17 15:26:15 +000011230SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +000011231 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000011232 default: llvm_unreachable("Should not custom lower this!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011233 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG);
Eric Christopher9a9d2752010-07-22 02:48:34 +000011234 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Eli Friedman14648462011-07-27 22:21:52 +000011235 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011236 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
11237 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Eli Friedman327236c2011-08-24 20:50:09 +000011238 case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011239 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +000011240 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011241 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
11242 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
11243 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +000011244 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +000011245 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011246 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
11247 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
11248 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011249 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +000011250 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +000011251 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011252 case ISD::SHL_PARTS:
11253 case ISD::SRA_PARTS:
Nadav Rotem43012222011-05-11 08:12:09 +000011254 case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011255 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +000011256 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011257 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +000011258 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011259 case ISD::FABS: return LowerFABS(Op, DAG);
11260 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +000011261 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Stuart Hastings4fd0dee2011-06-01 04:39:42 +000011262 case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +000011263 case ISD::SETCC: return LowerSETCC(Op, DAG);
11264 case ISD::SELECT: return LowerSELECT(Op, DAG);
11265 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011266 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011267 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +000011268 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +000011269 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011270 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011271 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +000011272 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
11273 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011274 case ISD::FRAME_TO_ARGS_OFFSET:
11275 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +000011276 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011277 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +000011278 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
11279 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +000011280 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011281 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
Chandler Carruthacc068e2011-12-24 10:55:54 +000011282 case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +000011283 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011284 case ISD::MUL: return LowerMUL(Op, DAG);
Nadav Rotem43012222011-05-11 08:12:09 +000011285 case ISD::SRA:
11286 case ISD::SRL:
11287 case ISD::SHL: return LowerShift(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +000011288 case ISD::SADDO:
11289 case ISD::UADDO:
11290 case ISD::SSUBO:
11291 case ISD::USUBO:
11292 case ISD::SMULO:
11293 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +000011294 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011295 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +000011296 case ISD::ADDC:
11297 case ISD::ADDE:
11298 case ISD::SUBC:
11299 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Craig Topper13894fa2011-08-24 06:14:18 +000011300 case ISD::ADD: return LowerADD(Op, DAG);
11301 case ISD::SUB: return LowerSUB(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +000011302 }
Chris Lattner27a6c732007-11-24 07:07:01 +000011303}
11304
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011305static void ReplaceATOMIC_LOAD(SDNode *Node,
11306 SmallVectorImpl<SDValue> &Results,
11307 SelectionDAG &DAG) {
11308 DebugLoc dl = Node->getDebugLoc();
11309 EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
11310
11311 // Convert wide load -> cmpxchg8b/cmpxchg16b
11312 // FIXME: On 32-bit, load -> fild or movq would be more efficient
11313 // (The only way to get a 16-byte load is cmpxchg16b)
11314 // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment.
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011315 SDValue Zero = DAG.getConstant(0, VT);
11316 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT,
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011317 Node->getOperand(0),
11318 Node->getOperand(1), Zero, Zero,
11319 cast<AtomicSDNode>(Node)->getMemOperand(),
11320 cast<AtomicSDNode>(Node)->getOrdering(),
11321 cast<AtomicSDNode>(Node)->getSynchScope());
11322 Results.push_back(Swap.getValue(0));
11323 Results.push_back(Swap.getValue(1));
11324}
11325
Craig Topperc0878702012-08-17 06:55:11 +000011326static void
Duncan Sands1607f052008-12-01 11:39:25 +000011327ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Craig Topperc0878702012-08-17 06:55:11 +000011328 SelectionDAG &DAG, unsigned NewOp) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011329 DebugLoc dl = Node->getDebugLoc();
Duncan Sands17001ce2011-10-18 12:44:00 +000011330 assert (Node->getValueType(0) == MVT::i64 &&
11331 "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +000011332
11333 SDValue Chain = Node->getOperand(0);
11334 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011335 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011336 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +000011337 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011338 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +000011339 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +000011340 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +000011341 SDValue Result =
11342 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
11343 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +000011344 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +000011345 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011346 Results.push_back(Result.getValue(2));
11347}
11348
Duncan Sands126d9072008-07-04 11:47:58 +000011349/// ReplaceNodeResults - Replace a node with an illegal result type
11350/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +000011351void X86TargetLowering::ReplaceNodeResults(SDNode *N,
11352 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +000011353 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +000011354 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +000011355 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +000011356 default:
Craig Topperabb94d02012-02-05 03:43:23 +000011357 llvm_unreachable("Do not know how to custom type legalize this operation!");
Nadav Rotemd0f3ef82011-07-14 11:11:14 +000011358 case ISD::SIGN_EXTEND_INREG:
Chris Lattner5b856542010-12-20 00:59:46 +000011359 case ISD::ADDC:
11360 case ISD::ADDE:
11361 case ISD::SUBC:
11362 case ISD::SUBE:
11363 // We don't want to expand or promote these.
11364 return;
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011365 case ISD::FP_TO_SINT:
11366 case ISD::FP_TO_UINT: {
11367 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
11368
11369 if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType()))
11370 return;
11371
Eli Friedman948e95a2009-05-23 09:59:16 +000011372 std::pair<SDValue,SDValue> Vals =
NAKAMURA Takumi9a68fdc2012-02-25 03:37:25 +000011373 FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
Duncan Sands1607f052008-12-01 11:39:25 +000011374 SDValue FIST = Vals.first, StackSlot = Vals.second;
11375 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +000011376 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +000011377 // Return a load from the stack slot.
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011378 if (StackSlot.getNode() != 0)
11379 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
11380 MachinePointerInfo(),
11381 false, false, false, 0));
11382 else
11383 Results.push_back(FIST);
Duncan Sands1607f052008-12-01 11:39:25 +000011384 }
11385 return;
11386 }
11387 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011388 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +000011389 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011390 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +000011391 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +000011392 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +000011393 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +000011394 eax.getValue(2));
11395 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
11396 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +000011397 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011398 Results.push_back(edx.getValue(1));
11399 return;
11400 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011401 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +000011402 EVT T = N->getValueType(0);
Benjamin Kramer2753ae32011-08-27 17:36:14 +000011403 assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
Eli Friedman43f51ae2011-08-26 21:21:21 +000011404 bool Regs64bit = T == MVT::i128;
11405 EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
Duncan Sands1607f052008-12-01 11:39:25 +000011406 SDValue cpInL, cpInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011407 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11408 DAG.getConstant(0, HalfT));
11409 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
11410 DAG.getConstant(1, HalfT));
11411 cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
11412 Regs64bit ? X86::RAX : X86::EAX,
11413 cpInL, SDValue());
11414 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
11415 Regs64bit ? X86::RDX : X86::EDX,
11416 cpInH, cpInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011417 SDValue swapInL, swapInH;
Eli Friedman43f51ae2011-08-26 21:21:21 +000011418 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11419 DAG.getConstant(0, HalfT));
11420 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
11421 DAG.getConstant(1, HalfT));
11422 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
11423 Regs64bit ? X86::RBX : X86::EBX,
11424 swapInL, cpInH.getValue(1));
11425 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
Chad Rosiera20e1e72012-08-01 18:39:17 +000011426 Regs64bit ? X86::RCX : X86::ECX,
Eli Friedman43f51ae2011-08-26 21:21:21 +000011427 swapInH, swapInL.getValue(1));
Duncan Sands1607f052008-12-01 11:39:25 +000011428 SDValue Ops[] = { swapInH.getValue(0),
11429 N->getOperand(1),
11430 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +000011431 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011432 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
Eli Friedman43f51ae2011-08-26 21:21:21 +000011433 unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
11434 X86ISD::LCMPXCHG8_DAG;
11435 SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys,
Andrew Trick1a2cf3b2010-10-11 19:02:04 +000011436 Ops, 3, T, MMO);
Eli Friedman43f51ae2011-08-26 21:21:21 +000011437 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
11438 Regs64bit ? X86::RAX : X86::EAX,
11439 HalfT, Result.getValue(1));
11440 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
11441 Regs64bit ? X86::RDX : X86::EDX,
11442 HalfT, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +000011443 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Eli Friedman43f51ae2011-08-26 21:21:21 +000011444 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +000011445 Results.push_back(cpOutH.getValue(1));
11446 return;
11447 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011448 case ISD::ATOMIC_LOAD_ADD:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011449 case ISD::ATOMIC_LOAD_AND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011450 case ISD::ATOMIC_LOAD_NAND:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011451 case ISD::ATOMIC_LOAD_OR:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011452 case ISD::ATOMIC_LOAD_SUB:
Dan Gohman0b1d4a72008-12-23 21:37:04 +000011453 case ISD::ATOMIC_LOAD_XOR:
Craig Topperc0878702012-08-17 06:55:11 +000011454 case ISD::ATOMIC_SWAP: {
11455 unsigned Opc;
11456 switch (N->getOpcode()) {
11457 default: llvm_unreachable("Unexpected opcode");
11458 case ISD::ATOMIC_LOAD_ADD:
11459 Opc = X86ISD::ATOMADD64_DAG;
11460 break;
11461 case ISD::ATOMIC_LOAD_AND:
11462 Opc = X86ISD::ATOMAND64_DAG;
11463 break;
11464 case ISD::ATOMIC_LOAD_NAND:
11465 Opc = X86ISD::ATOMNAND64_DAG;
11466 break;
11467 case ISD::ATOMIC_LOAD_OR:
11468 Opc = X86ISD::ATOMOR64_DAG;
11469 break;
11470 case ISD::ATOMIC_LOAD_SUB:
11471 Opc = X86ISD::ATOMSUB64_DAG;
11472 break;
11473 case ISD::ATOMIC_LOAD_XOR:
11474 Opc = X86ISD::ATOMXOR64_DAG;
11475 break;
11476 case ISD::ATOMIC_SWAP:
11477 Opc = X86ISD::ATOMSWAP64_DAG;
11478 break;
11479 }
11480 ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc);
Duncan Sands1607f052008-12-01 11:39:25 +000011481 return;
Craig Topperc0878702012-08-17 06:55:11 +000011482 }
Eli Friedmanf8f90f02011-08-24 22:33:28 +000011483 case ISD::ATOMIC_LOAD:
11484 ReplaceATOMIC_LOAD(N, Results, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +000011485 }
Evan Cheng0db9fe62006-04-25 20:13:52 +000011486}
11487
Evan Cheng72261582005-12-20 06:22:03 +000011488const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
11489 switch (Opcode) {
11490 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +000011491 case X86ISD::BSF: return "X86ISD::BSF";
11492 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +000011493 case X86ISD::SHLD: return "X86ISD::SHLD";
11494 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +000011495 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011496 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +000011497 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +000011498 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +000011499 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +000011500 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +000011501 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
11502 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
11503 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +000011504 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +000011505 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +000011506 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +000011507 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +000011508 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +000011509 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +000011510 case X86ISD::COMI: return "X86ISD::COMI";
11511 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +000011512 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +000011513 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Stuart Hastings865f0932011-06-03 23:53:54 +000011514 case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd";
11515 case X86ISD::FSETCCss: return "X86ISD::FSETCCss";
Evan Cheng72261582005-12-20 06:22:03 +000011516 case X86ISD::CMOV: return "X86ISD::CMOV";
11517 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +000011518 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +000011519 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
11520 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +000011521 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +000011522 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +000011523 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011524 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +000011525 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +000011526 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
11527 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +000011528 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +000011529 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000011530 case X86ISD::ANDNP: return "X86ISD::ANDNP";
Craig Topper31133842011-11-19 07:33:10 +000011531 case X86ISD::PSIGN: return "X86ISD::PSIGN";
Craig Toppere6a62772011-11-13 17:31:07 +000011532 case X86ISD::BLENDV: return "X86ISD::BLENDV";
Nadav Roteme6113782012-04-11 06:40:27 +000011533 case X86ISD::BLENDPW: return "X86ISD::BLENDPW";
11534 case X86ISD::BLENDPS: return "X86ISD::BLENDPS";
11535 case X86ISD::BLENDPD: return "X86ISD::BLENDPD";
Craig Topperfe033152011-12-06 09:31:36 +000011536 case X86ISD::HADD: return "X86ISD::HADD";
11537 case X86ISD::HSUB: return "X86ISD::HSUB";
Craig Toppere6a62772011-11-13 17:31:07 +000011538 case X86ISD::FHADD: return "X86ISD::FHADD";
11539 case X86ISD::FHSUB: return "X86ISD::FHSUB";
Evan Cheng8ca29322006-11-10 21:43:37 +000011540 case X86ISD::FMAX: return "X86ISD::FMAX";
11541 case X86ISD::FMIN: return "X86ISD::FMIN";
Nadav Rotemd60cb112012-08-19 13:06:16 +000011542 case X86ISD::FMAXC: return "X86ISD::FMAXC";
11543 case X86ISD::FMINC: return "X86ISD::FMINC";
Dan Gohman20382522007-07-10 00:05:58 +000011544 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
11545 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000011546 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Hans Wennborgf0234fc2012-06-01 16:27:21 +000011547 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +000011548 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +000011549 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000011550 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +000011551 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011552 case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r";
Evan Cheng7e2ff772008-05-08 00:57:18 +000011553 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
11554 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011555 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
11556 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
11557 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
11558 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
11559 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
11560 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +000011561 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
Michael Liaob7bf7262012-08-14 22:53:17 +000011562 case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL";
Evan Chengd880b972008-05-09 21:53:03 +000011563 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Michael Liao7091b242012-08-14 21:24:47 +000011564 case X86ISD::VFPEXT: return "X86ISD::VFPEXT";
Craig Toppered2e13d2012-01-22 19:15:14 +000011565 case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ";
11566 case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ";
Evan Chengf26ffe92008-05-29 08:22:04 +000011567 case X86ISD::VSHL: return "X86ISD::VSHL";
11568 case X86ISD::VSRL: return "X86ISD::VSRL";
Craig Toppered2e13d2012-01-22 19:15:14 +000011569 case X86ISD::VSRA: return "X86ISD::VSRA";
11570 case X86ISD::VSHLI: return "X86ISD::VSHLI";
11571 case X86ISD::VSRLI: return "X86ISD::VSRLI";
11572 case X86ISD::VSRAI: return "X86ISD::VSRAI";
Craig Topper1906d322012-01-22 23:36:02 +000011573 case X86ISD::CMPP: return "X86ISD::CMPP";
Craig Topper67609fd2012-01-22 22:42:16 +000011574 case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ";
11575 case X86ISD::PCMPGT: return "X86ISD::PCMPGT";
Bill Wendlingab55ebd2008-12-12 00:56:36 +000011576 case X86ISD::ADD: return "X86ISD::ADD";
11577 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +000011578 case X86ISD::ADC: return "X86ISD::ADC";
11579 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +000011580 case X86ISD::SMUL: return "X86ISD::SMUL";
11581 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +000011582 case X86ISD::INC: return "X86ISD::INC";
11583 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +000011584 case X86ISD::OR: return "X86ISD::OR";
11585 case X86ISD::XOR: return "X86ISD::XOR";
11586 case X86ISD::AND: return "X86ISD::AND";
Craig Topper54a11172011-10-14 07:06:56 +000011587 case X86ISD::ANDN: return "X86ISD::ANDN";
Craig Toppere6a62772011-11-13 17:31:07 +000011588 case X86ISD::BLSI: return "X86ISD::BLSI";
11589 case X86ISD::BLSMSK: return "X86ISD::BLSMSK";
11590 case X86ISD::BLSR: return "X86ISD::BLSR";
Evan Cheng73f24c92009-03-30 21:36:47 +000011591 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +000011592 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +000011593 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011594 case X86ISD::PALIGN: return "X86ISD::PALIGN";
11595 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
11596 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011597 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
Craig Topperb3982da2011-12-31 23:50:21 +000011598 case X86ISD::SHUFP: return "X86ISD::SHUFP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011599 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011600 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +000011601 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +000011602 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
11603 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011604 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
11605 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
11606 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +000011607 case X86ISD::MOVSD: return "X86ISD::MOVSD";
11608 case X86ISD::MOVSS: return "X86ISD::MOVSS";
Craig Topper34671b82011-12-06 08:21:25 +000011609 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
11610 case X86ISD::UNPCKH: return "X86ISD::UNPCKH";
Bruno Cardoso Lopes0e6d2302011-08-17 02:29:19 +000011611 case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST";
Craig Topper316cd2a2011-11-30 06:25:25 +000011612 case X86ISD::VPERMILP: return "X86ISD::VPERMILP";
Craig Topperec24e612011-11-30 07:47:51 +000011613 case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128";
Craig Topper8325c112012-04-16 00:41:45 +000011614 case X86ISD::VPERMV: return "X86ISD::VPERMV";
11615 case X86ISD::VPERMI: return "X86ISD::VPERMI";
Craig Topper5b209e82012-02-05 03:14:49 +000011616 case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +000011617 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +000011618 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +000011619 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Eli Friedman14648462011-07-27 22:21:52 +000011620 case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER";
Rafael Espindolad07b7ec2011-08-30 19:43:21 +000011621 case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA";
Michael J. Spencer1a2d0612012-02-24 19:01:22 +000011622 case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL";
Benjamin Kramer17c836c2012-04-27 12:07:43 +000011623 case X86ISD::SAHF: return "X86ISD::SAHF";
Benjamin Kramerb9bee042012-07-12 09:31:43 +000011624 case X86ISD::RDRAND: return "X86ISD::RDRAND";
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000011625 case X86ISD::FMADD: return "X86ISD::FMADD";
11626 case X86ISD::FMSUB: return "X86ISD::FMSUB";
11627 case X86ISD::FNMADD: return "X86ISD::FNMADD";
11628 case X86ISD::FNMSUB: return "X86ISD::FNMSUB";
11629 case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB";
11630 case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD";
Evan Cheng72261582005-12-20 06:22:03 +000011631 }
11632}
Evan Cheng3a03ebb2005-12-21 23:05:39 +000011633
Chris Lattnerc9addb72007-03-30 23:15:24 +000011634// isLegalAddressingMode - Return true if the addressing mode represented
11635// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +000011636bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011637 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +000011638 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011639 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +000011640 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +000011641
Chris Lattnerc9addb72007-03-30 23:15:24 +000011642 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011643 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011644 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000011645
Chris Lattnerc9addb72007-03-30 23:15:24 +000011646 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +000011647 unsigned GVFlags =
11648 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011649
Chris Lattnerdfed4132009-07-10 07:38:24 +000011650 // If a reference to this global requires an extra load, we can't fold it.
11651 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +000011652 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011653
Chris Lattnerdfed4132009-07-10 07:38:24 +000011654 // If BaseGV requires a register for the PIC base, we cannot also have a
11655 // BaseReg specified.
11656 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +000011657 return false;
Evan Cheng52787842007-08-01 23:46:47 +000011658
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011659 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +000011660 if ((M != CodeModel::Small || R != Reloc::Static) &&
11661 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +000011662 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +000011663 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011664
Chris Lattnerc9addb72007-03-30 23:15:24 +000011665 switch (AM.Scale) {
11666 case 0:
11667 case 1:
11668 case 2:
11669 case 4:
11670 case 8:
11671 // These scales always work.
11672 break;
11673 case 3:
11674 case 5:
11675 case 9:
11676 // These scales are formed with basereg+scalereg. Only accept if there is
11677 // no basereg yet.
11678 if (AM.HasBaseReg)
11679 return false;
11680 break;
11681 default: // Other stuff never works.
11682 return false;
11683 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011684
Chris Lattnerc9addb72007-03-30 23:15:24 +000011685 return true;
11686}
11687
11688
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011689bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011690 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +000011691 return false;
Evan Chenge127a732007-10-29 07:57:50 +000011692 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
11693 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011694 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +000011695 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011696 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +000011697}
11698
Evan Cheng70e10d32012-07-17 06:53:39 +000011699bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
11700 return Imm == (int32_t)Imm;
11701}
11702
11703bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
Evan Chenga9e13ba2012-07-17 18:54:11 +000011704 // Can also use sub to handle negated immediates.
Evan Cheng70e10d32012-07-17 06:53:39 +000011705 return Imm == (int32_t)Imm;
11706}
11707
Owen Andersone50ed302009-08-10 22:56:29 +000011708bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +000011709 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011710 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +000011711 unsigned NumBits1 = VT1.getSizeInBits();
11712 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +000011713 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011714 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +000011715 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +000011716}
Evan Cheng2bd122c2007-10-26 01:56:11 +000011717
Chris Lattnerdb125cf2011-07-18 04:54:35 +000011718bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011719 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011720 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011721}
11722
Owen Andersone50ed302009-08-10 22:56:29 +000011723bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +000011724 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +000011725 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +000011726}
11727
Owen Andersone50ed302009-08-10 22:56:29 +000011728bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +000011729 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +000011730 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +000011731}
11732
Evan Cheng60c07e12006-07-05 22:17:51 +000011733/// isShuffleMaskLegal - Targets can use this to indicate that they only
11734/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
11735/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
11736/// are assumed to be legal.
11737bool
Eric Christopherfd179292009-08-27 18:07:15 +000011738X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +000011739 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +000011740 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +000011741 if (VT.getSizeInBits() == 64)
Craig Topper1dc0fbc2011-12-05 07:27:14 +000011742 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +000011743
Nate Begemana09008b2009-10-19 02:17:23 +000011744 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +000011745 return (VT.getVectorNumElements() == 2 ||
11746 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
11747 isMOVLMask(M, VT) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011748 isSHUFPMask(M, VT, Subtarget->hasAVX()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +000011749 isPSHUFDMask(M, VT) ||
Craig Toppera9a568a2012-05-02 08:03:44 +000011750 isPSHUFHWMask(M, VT, Subtarget->hasAVX2()) ||
11751 isPSHUFLWMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper0e2037b2012-01-20 05:53:00 +000011752 isPALIGNRMask(M, VT, Subtarget) ||
Craig Topper6347e862011-11-21 06:57:39 +000011753 isUNPCKLMask(M, VT, Subtarget->hasAVX2()) ||
11754 isUNPCKHMask(M, VT, Subtarget->hasAVX2()) ||
Craig Topper94438ba2011-12-16 08:06:31 +000011755 isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasAVX2()) ||
11756 isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasAVX2()));
Evan Cheng60c07e12006-07-05 22:17:51 +000011757}
11758
Dan Gohman7d8143f2008-04-09 20:09:42 +000011759bool
Nate Begeman5a5ca152009-04-29 05:20:52 +000011760X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +000011761 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +000011762 unsigned NumElts = VT.getVectorNumElements();
11763 // FIXME: This collection of masks seems suspect.
11764 if (NumElts == 2)
11765 return true;
Craig Topper7a9a28b2012-08-12 02:23:29 +000011766 if (NumElts == 4 && VT.is128BitVector()) {
Nate Begeman9008ca62009-04-27 18:41:29 +000011767 return (isMOVLMask(Mask, VT) ||
11768 isCommutedMOVLMask(Mask, VT, true) ||
Craig Topper1a7700a2012-01-19 08:19:12 +000011769 isSHUFPMask(Mask, VT, Subtarget->hasAVX()) ||
11770 isSHUFPMask(Mask, VT, Subtarget->hasAVX(), /* Commuted */ true));
Evan Cheng60c07e12006-07-05 22:17:51 +000011771 }
11772 return false;
11773}
11774
11775//===----------------------------------------------------------------------===//
11776// X86 Scheduler Hooks
11777//===----------------------------------------------------------------------===//
11778
Mon P Wang63307c32008-05-05 19:05:59 +000011779// private utility function
11780MachineBasicBlock *
11781X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
11782 MachineBasicBlock *MBB,
11783 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011784 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011785 unsigned LoadOpc,
11786 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +000011787 unsigned notOpc,
11788 unsigned EAXreg,
Craig Topper44d23822012-02-22 05:59:10 +000011789 const TargetRegisterClass *RC,
Richard Smith42fc29e2012-04-13 22:47:00 +000011790 bool Invert) const {
Mon P Wang63307c32008-05-05 19:05:59 +000011791 // For the atomic bitwise operator, we generate
11792 // thisMBB:
11793 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000011794 // ld t1 = [bitinstr.addr]
11795 // op t2 = t1, [bitinstr.val]
Richard Smith42fc29e2012-04-13 22:47:00 +000011796 // not t3 = t2 (if Invert)
Mon P Wangab3e7472008-05-05 22:56:23 +000011797 // mov EAX = t1
Richard Smith42fc29e2012-04-13 22:47:00 +000011798 // lcs dest = [bitinstr.addr], t3 [EAX is implicit]
Mon P Wang63307c32008-05-05 19:05:59 +000011799 // bz newMBB
11800 // fallthrough -->nextMBB
11801 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11802 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011803 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000011804 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011805
Mon P Wang63307c32008-05-05 19:05:59 +000011806 /// First build the CFG
11807 MachineFunction *F = MBB->getParent();
11808 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000011809 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11810 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11811 F->insert(MBBIter, newMBB);
11812 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011813
Dan Gohman14152b42010-07-06 20:24:04 +000011814 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11815 nextMBB->splice(nextMBB->begin(), thisMBB,
11816 llvm::next(MachineBasicBlock::iterator(bInstr)),
11817 thisMBB->end());
11818 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011819
Mon P Wang63307c32008-05-05 19:05:59 +000011820 // Update thisMBB to fall through to newMBB
11821 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011822
Mon P Wang63307c32008-05-05 19:05:59 +000011823 // newMBB jumps to itself and fall through to nextMBB
11824 newMBB->addSuccessor(nextMBB);
11825 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011826
Mon P Wang63307c32008-05-05 19:05:59 +000011827 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011828 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011829 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +000011830 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000011831 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011832 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000011833 int numArgs = bInstr->getNumOperands() - 1;
11834 for (int i=0; i < numArgs; ++i)
11835 argOpers[i] = &bInstr->getOperand(i+1);
11836
11837 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011838 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011839 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000011840
Dale Johannesen140be2d2008-08-19 18:47:28 +000011841 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011842 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011843 for (int i=0; i <= lastAddrIndx; ++i)
11844 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011845
Dale Johannesen140be2d2008-08-19 18:47:28 +000011846 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +000011847 assert((argOpers[valArgIndx]->isReg() ||
11848 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000011849 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +000011850 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011851 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000011852 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011853 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Richard Smith42fc29e2012-04-13 22:47:00 +000011854 MIB.addReg(t1);
Mon P Wang63307c32008-05-05 19:05:59 +000011855 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000011856
Richard Smith42fc29e2012-04-13 22:47:00 +000011857 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11858 if (Invert) {
11859 MIB = BuildMI(newMBB, dl, TII->get(notOpc), t3).addReg(t2);
11860 }
11861 else
11862 t3 = t2;
11863
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011864 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Richard Smith2c651fe2012-04-16 18:43:53 +000011865 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +000011866
Dale Johannesene4d209d2009-02-03 20:21:25 +000011867 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +000011868 for (int i=0; i <= lastAddrIndx; ++i)
11869 (*MIB).addOperand(*argOpers[i]);
Richard Smith42fc29e2012-04-13 22:47:00 +000011870 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000011871 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000011872 (*MIB).setMemRefs(bInstr->memoperands_begin(),
11873 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +000011874
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000011875 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +000011876 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +000011877
Mon P Wang63307c32008-05-05 19:05:59 +000011878 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000011879 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000011880
Dan Gohman14152b42010-07-06 20:24:04 +000011881 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000011882 return nextMBB;
11883}
11884
Dale Johannesen1b54c7f2008-10-03 19:41:08 +000011885// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +000011886MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011887X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
11888 MachineBasicBlock *MBB,
11889 unsigned regOpcL,
11890 unsigned regOpcH,
11891 unsigned immOpcL,
11892 unsigned immOpcH,
Richard Smith42fc29e2012-04-13 22:47:00 +000011893 bool Invert) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011894 // For the atomic bitwise operator, we generate
11895 // thisMBB (instructions are in pairs, except cmpxchg8b)
11896 // ld t1,t2 = [bitinstr.addr]
11897 // newMBB:
11898 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
11899 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +000011900 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Richard Smith42fc29e2012-04-13 22:47:00 +000011901 // neg t7, t8 < t5, t6 (if Invert)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011902 // mov ECX, EBX <- t5, t6
11903 // mov EAX, EDX <- t1, t2
11904 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
11905 // mov t3, t4 <- EAX, EDX
11906 // bz newMBB
11907 // result in out1, out2
11908 // fallthrough -->nextMBB
11909
Craig Topperc9099502012-04-20 06:31:50 +000011910 const TargetRegisterClass *RC = &X86::GR32RegClass;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011911 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011912 const unsigned NotOpc = X86::NOT32r;
11913 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
11914 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
11915 MachineFunction::iterator MBBIter = MBB;
11916 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000011917
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011918 /// First build the CFG
11919 MachineFunction *F = MBB->getParent();
11920 MachineBasicBlock *thisMBB = MBB;
11921 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
11922 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
11923 F->insert(MBBIter, newMBB);
11924 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011925
Dan Gohman14152b42010-07-06 20:24:04 +000011926 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
11927 nextMBB->splice(nextMBB->begin(), thisMBB,
11928 llvm::next(MachineBasicBlock::iterator(bInstr)),
11929 thisMBB->end());
11930 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011931
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011932 // Update thisMBB to fall through to newMBB
11933 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011934
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011935 // newMBB jumps to itself and fall through to nextMBB
11936 newMBB->addSuccessor(nextMBB);
11937 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000011938
Dale Johannesene4d209d2009-02-03 20:21:25 +000011939 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011940 // Insert instructions into newMBB based on incoming instruction
11941 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011942 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000011943 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011944 MachineOperand& dest1Oper = bInstr->getOperand(0);
11945 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011946 MachineOperand* argOpers[2 + X86::AddrNumOperands];
11947 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011948 argOpers[i] = &bInstr->getOperand(i+2);
11949
Dan Gohman71ea4e52010-05-14 21:01:44 +000011950 // We use some of the operands multiple times, so conservatively just
11951 // clear any kill flags that might be present.
11952 if (argOpers[i]->isReg() && argOpers[i]->isUse())
11953 argOpers[i]->setIsKill(false);
11954 }
11955
Evan Chengad5b52f2010-01-08 19:14:57 +000011956 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000011957 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +000011958
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011959 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011960 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011961 for (int i=0; i <= lastAddrIndx; ++i)
11962 (*MIB).addOperand(*argOpers[i]);
11963 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011964 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +000011965 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +000011966 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011967 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +000011968 MachineOperand newOp3 = *(argOpers[3]);
11969 if (newOp3.isImm())
11970 newOp3.setImm(newOp3.getImm()+4);
11971 else
11972 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011973 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +000011974 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011975
11976 // t3/4 are defined later, at the bottom of the loop
11977 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
11978 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011979 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011980 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +000011981 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011982 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
11983
Evan Cheng306b4ca2010-01-08 23:41:50 +000011984 // The subsequent operations should be using the destination registers of
Richard Smith42fc29e2012-04-13 22:47:00 +000011985 // the PHI instructions.
11986 t1 = dest1Oper.getReg();
11987 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011988
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011989 int valArgIndx = lastAddrIndx + 1;
11990 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +000011991 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011992 "invalid operand");
11993 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
11994 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000011995 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000011996 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000011997 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000011998 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +000011999 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012000 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012001 (*MIB).addOperand(*argOpers[valArgIndx]);
12002 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012003 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012004 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +000012005 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012006 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +000012007 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012008 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012009 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +000012010 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +000012011 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012012 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012013
Richard Smith42fc29e2012-04-13 22:47:00 +000012014 unsigned t7, t8;
12015 if (Invert) {
12016 t7 = F->getRegInfo().createVirtualRegister(RC);
12017 t8 = F->getRegInfo().createVirtualRegister(RC);
12018 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t7).addReg(t5);
12019 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t8).addReg(t6);
12020 } else {
12021 t7 = t5;
12022 t8 = t6;
12023 }
12024
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012025 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012026 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012027 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012028 MIB.addReg(t2);
12029
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012030 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012031 MIB.addReg(t7);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012032 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Richard Smith42fc29e2012-04-13 22:47:00 +000012033 MIB.addReg(t8);
Scott Michelfdc40a02009-02-17 22:15:04 +000012034
Dale Johannesene4d209d2009-02-03 20:21:25 +000012035 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012036 for (int i=0; i <= lastAddrIndx; ++i)
12037 (*MIB).addOperand(*argOpers[i]);
12038
12039 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012040 (*MIB).setMemRefs(bInstr->memoperands_begin(),
12041 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012042
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012043 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012044 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012045 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012046 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012047
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012048 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012049 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012050
Dan Gohman14152b42010-07-06 20:24:04 +000012051 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000012052 return nextMBB;
12053}
12054
12055// private utility function
12056MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +000012057X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
12058 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000012059 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +000012060 // For the atomic min/max operator, we generate
12061 // thisMBB:
12062 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +000012063 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +000012064 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +000012065 // cmp t1, t2
12066 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +000012067 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +000012068 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
12069 // bz newMBB
12070 // fallthrough -->nextMBB
12071 //
12072 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12073 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012074 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +000012075 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +000012076
Mon P Wang63307c32008-05-05 19:05:59 +000012077 /// First build the CFG
12078 MachineFunction *F = MBB->getParent();
12079 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000012080 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
12081 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
12082 F->insert(MBBIter, newMBB);
12083 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012084
Dan Gohman14152b42010-07-06 20:24:04 +000012085 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
12086 nextMBB->splice(nextMBB->begin(), thisMBB,
12087 llvm::next(MachineBasicBlock::iterator(mInstr)),
12088 thisMBB->end());
12089 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012090
Mon P Wang63307c32008-05-05 19:05:59 +000012091 // Update thisMBB to fall through to newMBB
12092 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012093
Mon P Wang63307c32008-05-05 19:05:59 +000012094 // newMBB jumps to newMBB and fall through to nextMBB
12095 newMBB->addSuccessor(nextMBB);
12096 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +000012097
Dale Johannesene4d209d2009-02-03 20:21:25 +000012098 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +000012099 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012100 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +000012101 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +000012102 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012103 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +000012104 int numArgs = mInstr->getNumOperands() - 1;
12105 for (int i=0; i < numArgs; ++i)
12106 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +000012107
Mon P Wang63307c32008-05-05 19:05:59 +000012108 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000012109 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +000012110 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +000012111
Craig Topperc9099502012-04-20 06:31:50 +000012112 unsigned t1 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012113 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +000012114 for (int i=0; i <= lastAddrIndx; ++i)
12115 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +000012116
Mon P Wang63307c32008-05-05 19:05:59 +000012117 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +000012118 assert((argOpers[valArgIndx]->isReg() ||
12119 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +000012120 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +000012121
Craig Topperc9099502012-04-20 06:31:50 +000012122 unsigned t2 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dan Gohmand735b802008-10-03 15:45:36 +000012123 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012124 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +000012125 else
Dale Johannesene4d209d2009-02-03 20:21:25 +000012126 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +000012127 (*MIB).addOperand(*argOpers[valArgIndx]);
12128
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012129 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +000012130 MIB.addReg(t1);
12131
Dale Johannesene4d209d2009-02-03 20:21:25 +000012132 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +000012133 MIB.addReg(t1);
12134 MIB.addReg(t2);
12135
12136 // Generate movc
Craig Topperc9099502012-04-20 06:31:50 +000012137 unsigned t3 = F->getRegInfo().createVirtualRegister(&X86::GR32RegClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +000012138 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +000012139 MIB.addReg(t2);
12140 MIB.addReg(t1);
12141
12142 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +000012143 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +000012144 for (int i=0; i <= lastAddrIndx; ++i)
12145 (*MIB).addOperand(*argOpers[i]);
12146 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +000012147 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +000012148 (*MIB).setMemRefs(mInstr->memoperands_begin(),
12149 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +000012150
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000012151 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +000012152 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +000012153
Mon P Wang63307c32008-05-05 19:05:59 +000012154 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012155 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +000012156
Dan Gohman14152b42010-07-06 20:24:04 +000012157 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +000012158 return nextMBB;
12159}
12160
Eric Christopherf83a5de2009-08-27 18:08:16 +000012161// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012162// or XMM0_V32I8 in AVX all of this code can be replaced with that
12163// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012164MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +000012165X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +000012166 unsigned numArgs, bool memArg) const {
Craig Topperd0a31172012-01-10 06:37:29 +000012167 assert(Subtarget->hasSSE42() &&
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012168 "Target must have SSE4.2 or AVX features enabled");
12169
Eric Christopherb120ab42009-08-18 22:50:32 +000012170 DebugLoc dl = MI->getDebugLoc();
12171 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +000012172 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000012173 if (!Subtarget->hasAVX()) {
12174 if (memArg)
12175 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
12176 else
12177 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
12178 } else {
12179 if (memArg)
12180 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
12181 else
12182 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
12183 }
Eric Christopherb120ab42009-08-18 22:50:32 +000012184
Eric Christopher41c902f2010-11-30 08:20:21 +000012185 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +000012186 for (unsigned i = 0; i < numArgs; ++i) {
12187 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +000012188 if (!(Op.isReg() && Op.isImplicit()))
12189 MIB.addOperand(Op);
12190 }
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012191 BuildMI(*BB, MI, dl,
Craig Topper638aa682012-08-05 00:17:48 +000012192 TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +000012193 .addReg(X86::XMM0);
12194
Dan Gohman14152b42010-07-06 20:24:04 +000012195 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +000012196 return BB;
12197}
12198
12199MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +000012200X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +000012201 DebugLoc dl = MI->getDebugLoc();
12202 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012203
Eric Christopher228232b2010-11-30 07:20:12 +000012204 // Address into RAX/EAX, other two args into ECX, EDX.
12205 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
12206 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12207 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
12208 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +000012209 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012210
Eric Christopher228232b2010-11-30 07:20:12 +000012211 unsigned ValOps = X86::AddrNumOperands;
12212 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
12213 .addReg(MI->getOperand(ValOps).getReg());
12214 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
12215 .addReg(MI->getOperand(ValOps+1).getReg());
12216
12217 // The instruction doesn't actually take any operands though.
12218 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000012219
Eric Christopher228232b2010-11-30 07:20:12 +000012220 MI->eraseFromParent(); // The pseudo is gone now.
12221 return BB;
12222}
12223
12224MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +000012225X86TargetLowering::EmitVAARG64WithCustomInserter(
12226 MachineInstr *MI,
12227 MachineBasicBlock *MBB) const {
12228 // Emit va_arg instruction on X86-64.
12229
12230 // Operands to this pseudo-instruction:
12231 // 0 ) Output : destination address (reg)
12232 // 1-5) Input : va_list address (addr, i64mem)
12233 // 6 ) ArgSize : Size (in bytes) of vararg type
12234 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
12235 // 8 ) Align : Alignment of type
12236 // 9 ) EFLAGS (implicit-def)
12237
12238 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
12239 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
12240
12241 unsigned DestReg = MI->getOperand(0).getReg();
12242 MachineOperand &Base = MI->getOperand(1);
12243 MachineOperand &Scale = MI->getOperand(2);
12244 MachineOperand &Index = MI->getOperand(3);
12245 MachineOperand &Disp = MI->getOperand(4);
12246 MachineOperand &Segment = MI->getOperand(5);
12247 unsigned ArgSize = MI->getOperand(6).getImm();
12248 unsigned ArgMode = MI->getOperand(7).getImm();
12249 unsigned Align = MI->getOperand(8).getImm();
12250
12251 // Memory Reference
12252 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
12253 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
12254 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
12255
12256 // Machine Information
12257 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12258 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
12259 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
12260 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
12261 DebugLoc DL = MI->getDebugLoc();
12262
12263 // struct va_list {
12264 // i32 gp_offset
12265 // i32 fp_offset
12266 // i64 overflow_area (address)
12267 // i64 reg_save_area (address)
12268 // }
12269 // sizeof(va_list) = 24
12270 // alignment(va_list) = 8
12271
12272 unsigned TotalNumIntRegs = 6;
12273 unsigned TotalNumXMMRegs = 8;
12274 bool UseGPOffset = (ArgMode == 1);
12275 bool UseFPOffset = (ArgMode == 2);
12276 unsigned MaxOffset = TotalNumIntRegs * 8 +
12277 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
12278
12279 /* Align ArgSize to a multiple of 8 */
12280 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
12281 bool NeedsAlign = (Align > 8);
12282
12283 MachineBasicBlock *thisMBB = MBB;
12284 MachineBasicBlock *overflowMBB;
12285 MachineBasicBlock *offsetMBB;
12286 MachineBasicBlock *endMBB;
12287
12288 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
12289 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
12290 unsigned OffsetReg = 0;
12291
12292 if (!UseGPOffset && !UseFPOffset) {
12293 // If we only pull from the overflow region, we don't create a branch.
12294 // We don't need to alter control flow.
12295 OffsetDestReg = 0; // unused
12296 OverflowDestReg = DestReg;
12297
12298 offsetMBB = NULL;
12299 overflowMBB = thisMBB;
12300 endMBB = thisMBB;
12301 } else {
12302 // First emit code to check if gp_offset (or fp_offset) is below the bound.
12303 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
12304 // If not, pull from overflow_area. (branch to overflowMBB)
12305 //
12306 // thisMBB
12307 // | .
12308 // | .
12309 // offsetMBB overflowMBB
12310 // | .
12311 // | .
12312 // endMBB
12313
12314 // Registers for the PHI in endMBB
12315 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
12316 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
12317
12318 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12319 MachineFunction *MF = MBB->getParent();
12320 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12321 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12322 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12323
12324 MachineFunction::iterator MBBIter = MBB;
12325 ++MBBIter;
12326
12327 // Insert the new basic blocks
12328 MF->insert(MBBIter, offsetMBB);
12329 MF->insert(MBBIter, overflowMBB);
12330 MF->insert(MBBIter, endMBB);
12331
12332 // Transfer the remainder of MBB and its successor edges to endMBB.
12333 endMBB->splice(endMBB->begin(), thisMBB,
12334 llvm::next(MachineBasicBlock::iterator(MI)),
12335 thisMBB->end());
12336 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
12337
12338 // Make offsetMBB and overflowMBB successors of thisMBB
12339 thisMBB->addSuccessor(offsetMBB);
12340 thisMBB->addSuccessor(overflowMBB);
12341
12342 // endMBB is a successor of both offsetMBB and overflowMBB
12343 offsetMBB->addSuccessor(endMBB);
12344 overflowMBB->addSuccessor(endMBB);
12345
12346 // Load the offset value into a register
12347 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12348 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
12349 .addOperand(Base)
12350 .addOperand(Scale)
12351 .addOperand(Index)
12352 .addDisp(Disp, UseFPOffset ? 4 : 0)
12353 .addOperand(Segment)
12354 .setMemRefs(MMOBegin, MMOEnd);
12355
12356 // Check if there is enough room left to pull this argument.
12357 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
12358 .addReg(OffsetReg)
12359 .addImm(MaxOffset + 8 - ArgSizeA8);
12360
12361 // Branch to "overflowMBB" if offset >= max
12362 // Fall through to "offsetMBB" otherwise
12363 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
12364 .addMBB(overflowMBB);
12365 }
12366
12367 // In offsetMBB, emit code to use the reg_save_area.
12368 if (offsetMBB) {
12369 assert(OffsetReg != 0);
12370
12371 // Read the reg_save_area address.
12372 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
12373 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
12374 .addOperand(Base)
12375 .addOperand(Scale)
12376 .addOperand(Index)
12377 .addDisp(Disp, 16)
12378 .addOperand(Segment)
12379 .setMemRefs(MMOBegin, MMOEnd);
12380
12381 // Zero-extend the offset
12382 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
12383 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
12384 .addImm(0)
12385 .addReg(OffsetReg)
12386 .addImm(X86::sub_32bit);
12387
12388 // Add the offset to the reg_save_area to get the final address.
12389 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
12390 .addReg(OffsetReg64)
12391 .addReg(RegSaveReg);
12392
12393 // Compute the offset for the next argument
12394 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
12395 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
12396 .addReg(OffsetReg)
12397 .addImm(UseFPOffset ? 16 : 8);
12398
12399 // Store it back into the va_list.
12400 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
12401 .addOperand(Base)
12402 .addOperand(Scale)
12403 .addOperand(Index)
12404 .addDisp(Disp, UseFPOffset ? 4 : 0)
12405 .addOperand(Segment)
12406 .addReg(NextOffsetReg)
12407 .setMemRefs(MMOBegin, MMOEnd);
12408
12409 // Jump to endMBB
12410 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
12411 .addMBB(endMBB);
12412 }
12413
12414 //
12415 // Emit code to use overflow area
12416 //
12417
12418 // Load the overflow_area address into a register.
12419 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
12420 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
12421 .addOperand(Base)
12422 .addOperand(Scale)
12423 .addOperand(Index)
12424 .addDisp(Disp, 8)
12425 .addOperand(Segment)
12426 .setMemRefs(MMOBegin, MMOEnd);
12427
12428 // If we need to align it, do so. Otherwise, just copy the address
12429 // to OverflowDestReg.
12430 if (NeedsAlign) {
12431 // Align the overflow address
12432 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
12433 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
12434
12435 // aligned_addr = (addr + (align-1)) & ~(align-1)
12436 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
12437 .addReg(OverflowAddrReg)
12438 .addImm(Align-1);
12439
12440 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
12441 .addReg(TmpReg)
12442 .addImm(~(uint64_t)(Align-1));
12443 } else {
12444 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
12445 .addReg(OverflowAddrReg);
12446 }
12447
12448 // Compute the next overflow address after this argument.
12449 // (the overflow address should be kept 8-byte aligned)
12450 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
12451 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
12452 .addReg(OverflowDestReg)
12453 .addImm(ArgSizeA8);
12454
12455 // Store the new overflow address.
12456 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
12457 .addOperand(Base)
12458 .addOperand(Scale)
12459 .addOperand(Index)
12460 .addDisp(Disp, 8)
12461 .addOperand(Segment)
12462 .addReg(NextAddrReg)
12463 .setMemRefs(MMOBegin, MMOEnd);
12464
12465 // If we branched, emit the PHI to the front of endMBB.
12466 if (offsetMBB) {
12467 BuildMI(*endMBB, endMBB->begin(), DL,
12468 TII->get(X86::PHI), DestReg)
12469 .addReg(OffsetDestReg).addMBB(offsetMBB)
12470 .addReg(OverflowDestReg).addMBB(overflowMBB);
12471 }
12472
12473 // Erase the pseudo instruction
12474 MI->eraseFromParent();
12475
12476 return endMBB;
12477}
12478
12479MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +000012480X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
12481 MachineInstr *MI,
12482 MachineBasicBlock *MBB) const {
12483 // Emit code to save XMM registers to the stack. The ABI says that the
12484 // number of registers to save is given in %al, so it's theoretically
12485 // possible to do an indirect jump trick to avoid saving all of them,
12486 // however this code takes a simpler approach and just executes all
12487 // of the stores if %al is non-zero. It's less code, and it's probably
12488 // easier on the hardware branch predictor, and stores aren't all that
12489 // expensive anyway.
12490
12491 // Create the new basic blocks. One block contains all the XMM stores,
12492 // and one block is the final destination regardless of whether any
12493 // stores were performed.
12494 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
12495 MachineFunction *F = MBB->getParent();
12496 MachineFunction::iterator MBBIter = MBB;
12497 ++MBBIter;
12498 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
12499 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
12500 F->insert(MBBIter, XMMSaveMBB);
12501 F->insert(MBBIter, EndMBB);
12502
Dan Gohman14152b42010-07-06 20:24:04 +000012503 // Transfer the remainder of MBB and its successor edges to EndMBB.
12504 EndMBB->splice(EndMBB->begin(), MBB,
12505 llvm::next(MachineBasicBlock::iterator(MI)),
12506 MBB->end());
12507 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
12508
Dan Gohmand6708ea2009-08-15 01:38:56 +000012509 // The original block will now fall through to the XMM save block.
12510 MBB->addSuccessor(XMMSaveMBB);
12511 // The XMMSaveMBB will fall through to the end block.
12512 XMMSaveMBB->addSuccessor(EndMBB);
12513
12514 // Now add the instructions.
12515 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12516 DebugLoc DL = MI->getDebugLoc();
12517
12518 unsigned CountReg = MI->getOperand(0).getReg();
12519 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
12520 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
12521
12522 if (!Subtarget->isTargetWin64()) {
12523 // If %al is 0, branch around the XMM save block.
12524 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +000012525 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012526 MBB->addSuccessor(EndMBB);
12527 }
12528
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012529 unsigned MOVOpc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Dan Gohmand6708ea2009-08-15 01:38:56 +000012530 // In the XMM save block, save all the XMM argument registers.
12531 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
12532 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +000012533 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +000012534 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +000012535 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +000012536 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +000012537 /*Size=*/16, /*Align=*/16);
Bruno Cardoso Lopes5affa512011-08-31 03:04:09 +000012538 BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
Dan Gohmand6708ea2009-08-15 01:38:56 +000012539 .addFrameIndex(RegSaveFrameIndex)
12540 .addImm(/*Scale=*/1)
12541 .addReg(/*IndexReg=*/0)
12542 .addImm(/*Disp=*/Offset)
12543 .addReg(/*Segment=*/0)
12544 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +000012545 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +000012546 }
12547
Dan Gohman14152b42010-07-06 20:24:04 +000012548 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +000012549
12550 return EndMBB;
12551}
Mon P Wang63307c32008-05-05 19:05:59 +000012552
Lang Hames6e3f7e42012-02-03 01:13:49 +000012553// The EFLAGS operand of SelectItr might be missing a kill marker
12554// because there were multiple uses of EFLAGS, and ISel didn't know
12555// which to mark. Figure out whether SelectItr should have had a
12556// kill marker, and set it if it should. Returns the correct kill
12557// marker value.
12558static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
12559 MachineBasicBlock* BB,
12560 const TargetRegisterInfo* TRI) {
12561 // Scan forward through BB for a use/def of EFLAGS.
12562 MachineBasicBlock::iterator miI(llvm::next(SelectItr));
12563 for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
Lang Hames50a36f72012-02-02 07:48:37 +000012564 const MachineInstr& mi = *miI;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012565 if (mi.readsRegister(X86::EFLAGS))
Lang Hames50a36f72012-02-02 07:48:37 +000012566 return false;
Lang Hames6e3f7e42012-02-03 01:13:49 +000012567 if (mi.definesRegister(X86::EFLAGS))
12568 break; // Should have kill-flag - update below.
12569 }
12570
12571 // If we hit the end of the block, check whether EFLAGS is live into a
12572 // successor.
12573 if (miI == BB->end()) {
12574 for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
12575 sEnd = BB->succ_end();
12576 sItr != sEnd; ++sItr) {
12577 MachineBasicBlock* succ = *sItr;
12578 if (succ->isLiveIn(X86::EFLAGS))
12579 return false;
Lang Hames50a36f72012-02-02 07:48:37 +000012580 }
12581 }
12582
Lang Hames6e3f7e42012-02-03 01:13:49 +000012583 // We found a def, or hit the end of the basic block and EFLAGS wasn't live
12584 // out. SelectMI should have a kill flag on EFLAGS.
12585 SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
Lang Hames50a36f72012-02-02 07:48:37 +000012586 return true;
12587}
12588
Evan Cheng60c07e12006-07-05 22:17:51 +000012589MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +000012590X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012591 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +000012592 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12593 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +000012594
Chris Lattner52600972009-09-02 05:57:00 +000012595 // To "insert" a SELECT_CC instruction, we actually have to insert the
12596 // diamond control-flow pattern. The incoming instruction knows the
12597 // destination vreg to set, the condition code register to branch on, the
12598 // true/false values to select between, and a branch opcode to use.
12599 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12600 MachineFunction::iterator It = BB;
12601 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +000012602
Chris Lattner52600972009-09-02 05:57:00 +000012603 // thisMBB:
12604 // ...
12605 // TrueVal = ...
12606 // cmpTY ccX, r1, r2
12607 // bCC copy1MBB
12608 // fallthrough --> copy0MBB
12609 MachineBasicBlock *thisMBB = BB;
12610 MachineFunction *F = BB->getParent();
12611 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
12612 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +000012613 F->insert(It, copy0MBB);
12614 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +000012615
Bill Wendling730c07e2010-06-25 20:48:10 +000012616 // If the EFLAGS register isn't dead in the terminator, then claim that it's
12617 // live into the sink and copy blocks.
Lang Hames6e3f7e42012-02-03 01:13:49 +000012618 const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo();
12619 if (!MI->killsRegister(X86::EFLAGS) &&
12620 !checkAndUpdateEFLAGSKill(MI, BB, TRI)) {
12621 copy0MBB->addLiveIn(X86::EFLAGS);
12622 sinkMBB->addLiveIn(X86::EFLAGS);
Bill Wendling730c07e2010-06-25 20:48:10 +000012623 }
12624
Dan Gohman14152b42010-07-06 20:24:04 +000012625 // Transfer the remainder of BB and its successor edges to sinkMBB.
12626 sinkMBB->splice(sinkMBB->begin(), BB,
12627 llvm::next(MachineBasicBlock::iterator(MI)),
12628 BB->end());
12629 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
12630
12631 // Add the true and fallthrough blocks as its successors.
12632 BB->addSuccessor(copy0MBB);
12633 BB->addSuccessor(sinkMBB);
12634
12635 // Create the conditional branch instruction.
12636 unsigned Opc =
12637 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
12638 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
12639
Chris Lattner52600972009-09-02 05:57:00 +000012640 // copy0MBB:
12641 // %FalseValue = ...
12642 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000012643 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000012644
Chris Lattner52600972009-09-02 05:57:00 +000012645 // sinkMBB:
12646 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
12647 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000012648 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
12649 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000012650 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
12651 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
12652
Dan Gohman14152b42010-07-06 20:24:04 +000012653 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000012654 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000012655}
12656
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012657MachineBasicBlock *
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012658X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB,
12659 bool Is64Bit) const {
12660 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12661 DebugLoc DL = MI->getDebugLoc();
12662 MachineFunction *MF = BB->getParent();
12663 const BasicBlock *LLVM_BB = BB->getBasicBlock();
12664
Nick Lewycky8a8d4792011-12-02 22:16:29 +000012665 assert(getTargetMachine().Options.EnableSegmentedStacks);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012666
12667 unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
12668 unsigned TlsOffset = Is64Bit ? 0x70 : 0x30;
12669
12670 // BB:
12671 // ... [Till the alloca]
12672 // If stacklet is not large enough, jump to mallocMBB
12673 //
12674 // bumpMBB:
12675 // Allocate by subtracting from RSP
12676 // Jump to continueMBB
12677 //
12678 // mallocMBB:
12679 // Allocate by call to runtime
12680 //
12681 // continueMBB:
12682 // ...
12683 // [rest of original BB]
12684 //
12685
12686 MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12687 MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12688 MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
12689
12690 MachineRegisterInfo &MRI = MF->getRegInfo();
12691 const TargetRegisterClass *AddrRegClass =
12692 getRegClassFor(Is64Bit ? MVT::i64:MVT::i32);
12693
12694 unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12695 bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
12696 tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola66bf7432011-10-26 21:16:41 +000012697 SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012698 sizeVReg = MI->getOperand(1).getReg(),
12699 physSPReg = Is64Bit ? X86::RSP : X86::ESP;
12700
12701 MachineFunction::iterator MBBIter = BB;
12702 ++MBBIter;
12703
12704 MF->insert(MBBIter, bumpMBB);
12705 MF->insert(MBBIter, mallocMBB);
12706 MF->insert(MBBIter, continueMBB);
12707
12708 continueMBB->splice(continueMBB->begin(), BB, llvm::next
12709 (MachineBasicBlock::iterator(MI)), BB->end());
12710 continueMBB->transferSuccessorsAndUpdatePHIs(BB);
12711
12712 // Add code to the main basic block to check if the stack limit has been hit,
12713 // and if so, jump to mallocMBB otherwise to bumpMBB.
12714 BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
Rafael Espindola66bf7432011-10-26 21:16:41 +000012715 BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012716 .addReg(tmpSPVReg).addReg(sizeVReg);
12717 BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr))
Rafael Espindola014f7a32012-01-11 18:14:03 +000012718 .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012719 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012720 BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB);
12721
12722 // bumpMBB simply decreases the stack pointer, since we know the current
12723 // stacklet has enough space.
12724 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012725 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012726 BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
Rafael Espindola66bf7432011-10-26 21:16:41 +000012727 .addReg(SPLimitVReg);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012728 BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12729
12730 // Calls into a routine in libgcc to allocate more space from the heap.
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012731 const uint32_t *RegMask =
12732 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012733 if (Is64Bit) {
12734 BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
12735 .addReg(sizeVReg);
12736 BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012737 .addExternalSymbol("__morestack_allocate_stack_space")
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012738 .addRegMask(RegMask)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +000012739 .addReg(X86::RDI, RegState::Implicit)
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012740 .addReg(X86::RAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012741 } else {
12742 BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
12743 .addImm(12);
12744 BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
12745 BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012746 .addExternalSymbol("__morestack_allocate_stack_space")
12747 .addRegMask(RegMask)
12748 .addReg(X86::EAX, RegState::ImplicitDefine);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012749 }
12750
12751 if (!Is64Bit)
12752 BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
12753 .addImm(16);
12754
12755 BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
12756 .addReg(Is64Bit ? X86::RAX : X86::EAX);
12757 BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB);
12758
12759 // Set up the CFG correctly.
12760 BB->addSuccessor(bumpMBB);
12761 BB->addSuccessor(mallocMBB);
12762 mallocMBB->addSuccessor(continueMBB);
12763 bumpMBB->addSuccessor(continueMBB);
12764
12765 // Take care of the PHI nodes.
12766 BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
12767 MI->getOperand(0).getReg())
12768 .addReg(mallocPtrVReg).addMBB(mallocMBB)
12769 .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
12770
12771 // Delete the original pseudo instruction.
12772 MI->eraseFromParent();
12773
12774 // And we're done.
12775 return continueMBB;
12776}
12777
12778MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012779X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012780 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012781 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12782 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012783
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012784 assert(!Subtarget->isTargetEnvMacho());
12785
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012786 // The lowering is pretty easy: we're just emitting the call to _alloca. The
12787 // non-trivial part is impdef of ESP.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012788
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012789 if (Subtarget->isTargetWin64()) {
12790 if (Subtarget->isTargetCygMing()) {
12791 // ___chkstk(Mingw64):
12792 // Clobbers R10, R11, RAX and EFLAGS.
12793 // Updates RSP.
12794 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12795 .addExternalSymbol("___chkstk")
12796 .addReg(X86::RAX, RegState::Implicit)
12797 .addReg(X86::RSP, RegState::Implicit)
12798 .addReg(X86::RAX, RegState::Define | RegState::Implicit)
12799 .addReg(X86::RSP, RegState::Define | RegState::Implicit)
12800 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12801 } else {
12802 // __chkstk(MSVCRT): does not update stack pointer.
12803 // Clobbers R10, R11 and EFLAGS.
12804 // FIXME: RAX(allocated size) might be reused and not killed.
12805 BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA))
12806 .addExternalSymbol("__chkstk")
12807 .addReg(X86::RAX, RegState::Implicit)
12808 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12809 // RAX has the offset to subtracted from RSP.
12810 BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP)
12811 .addReg(X86::RSP)
12812 .addReg(X86::RAX);
12813 }
12814 } else {
12815 const char *StackProbeSymbol =
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012816 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
12817
NAKAMURA Takumia2e07622011-03-24 07:07:00 +000012818 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
12819 .addExternalSymbol(StackProbeSymbol)
12820 .addReg(X86::EAX, RegState::Implicit)
12821 .addReg(X86::ESP, RegState::Implicit)
12822 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
12823 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
12824 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
12825 }
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012826
Dan Gohman14152b42010-07-06 20:24:04 +000012827 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000012828 return BB;
12829}
Chris Lattner52600972009-09-02 05:57:00 +000012830
12831MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000012832X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
12833 MachineBasicBlock *BB) const {
12834 // This is pretty easy. We're taking the value that we received from
12835 // our load from the relocation, sticking it in either RDI (x86-64)
12836 // or EAX and doing an indirect call. The return value will then
12837 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000012838 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000012839 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000012840 DebugLoc DL = MI->getDebugLoc();
12841 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000012842
12843 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000012844 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000012845
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012846 // Get a register mask for the lowered call.
12847 // FIXME: The 32-bit calls have non-standard calling conventions. Use a
12848 // proper register mask.
12849 const uint32_t *RegMask =
12850 getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012851 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000012852 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12853 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000012854 .addReg(X86::RIP)
12855 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012856 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012857 MI->getOperand(3).getTargetFlags())
12858 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000012859 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000012860 addDirectMem(MIB, X86::RDI);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012861 MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher61025492010-06-15 23:08:42 +000012862 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000012863 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12864 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000012865 .addReg(0)
12866 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012867 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000012868 MI->getOperand(3).getTargetFlags())
12869 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012870 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012871 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012872 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012873 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000012874 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
12875 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000012876 .addReg(TII->getGlobalBaseReg(F))
12877 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000012878 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000012879 MI->getOperand(3).getTargetFlags())
12880 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000012881 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000012882 addDirectMem(MIB, X86::EAX);
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +000012883 MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012884 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000012885
Dan Gohman14152b42010-07-06 20:24:04 +000012886 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000012887 return BB;
12888}
12889
12890MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000012891X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012892 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000012893 switch (MI->getOpcode()) {
Craig Topperabb94d02012-02-05 03:43:23 +000012894 default: llvm_unreachable("Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012895 case X86::TAILJMPd64:
12896 case X86::TAILJMPr64:
12897 case X86::TAILJMPm64:
Craig Topper6d1263a2012-02-05 05:38:58 +000012898 llvm_unreachable("TAILJMP64 would not be touched here.");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012899 case X86::TCRETURNdi64:
12900 case X86::TCRETURNri64:
12901 case X86::TCRETURNmi64:
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000012902 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000012903 case X86::WIN_ALLOCA:
12904 return EmitLoweredWinAlloca(MI, BB);
Rafael Espindola151ab3e2011-08-30 19:47:04 +000012905 case X86::SEG_ALLOCA_32:
12906 return EmitLoweredSegAlloca(MI, BB, false);
12907 case X86::SEG_ALLOCA_64:
12908 return EmitLoweredSegAlloca(MI, BB, true);
Eric Christopher30ef0e52010-06-03 04:07:48 +000012909 case X86::TLSCall_32:
12910 case X86::TLSCall_64:
12911 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000012912 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000012913 case X86::CMOV_FR32:
12914 case X86::CMOV_FR64:
12915 case X86::CMOV_V4F32:
12916 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000012917 case X86::CMOV_V2I64:
Bruno Cardoso Lopesd40aa242011-08-09 23:27:13 +000012918 case X86::CMOV_V8F32:
12919 case X86::CMOV_V4F64:
12920 case X86::CMOV_V4I64:
Chris Lattner314a1132010-03-14 18:31:44 +000012921 case X86::CMOV_GR16:
12922 case X86::CMOV_GR32:
12923 case X86::CMOV_RFP32:
12924 case X86::CMOV_RFP64:
12925 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000012926 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000012927
Dale Johannesen849f2142007-07-03 00:53:03 +000012928 case X86::FP32_TO_INT16_IN_MEM:
12929 case X86::FP32_TO_INT32_IN_MEM:
12930 case X86::FP32_TO_INT64_IN_MEM:
12931 case X86::FP64_TO_INT16_IN_MEM:
12932 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000012933 case X86::FP64_TO_INT64_IN_MEM:
12934 case X86::FP80_TO_INT16_IN_MEM:
12935 case X86::FP80_TO_INT32_IN_MEM:
12936 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000012937 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
12938 DebugLoc DL = MI->getDebugLoc();
12939
Evan Cheng60c07e12006-07-05 22:17:51 +000012940 // Change the floating point control register to use "round towards zero"
12941 // mode when truncating to an integer value.
12942 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000012943 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000012944 addFrameReference(BuildMI(*BB, MI, DL,
12945 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012946
12947 // Load the old value of the high byte of the control word...
12948 unsigned OldCW =
Craig Topperc9099502012-04-20 06:31:50 +000012949 F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
Dan Gohman14152b42010-07-06 20:24:04 +000012950 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000012951 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012952
12953 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000012954 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012955 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000012956
12957 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000012958 addFrameReference(BuildMI(*BB, MI, DL,
12959 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000012960
12961 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000012962 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000012963 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000012964
12965 // Get the X86 opcode to use.
12966 unsigned Opc;
12967 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000012968 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000012969 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
12970 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
12971 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
12972 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
12973 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
12974 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000012975 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
12976 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
12977 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000012978 }
12979
12980 X86AddressMode AM;
12981 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000012982 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012983 AM.BaseType = X86AddressMode::RegBase;
12984 AM.Base.Reg = Op.getReg();
12985 } else {
12986 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000012987 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000012988 }
12989 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000012990 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012991 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012992 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000012993 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000012994 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000012995 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000012996 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000012997 AM.GV = Op.getGlobal();
12998 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000012999 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000013000 }
Dan Gohman14152b42010-07-06 20:24:04 +000013001 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000013002 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000013003
13004 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000013005 addFrameReference(BuildMI(*BB, MI, DL,
13006 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000013007
Dan Gohman14152b42010-07-06 20:24:04 +000013008 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000013009 return BB;
13010 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013011 // String/text processing lowering.
13012 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013013 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013014 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013015 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000013016 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000013017 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000013018 case X86::PCMPESTRM128MEM:
Craig Topper63a99ff2012-08-17 07:15:56 +000013019 case X86::VPCMPESTRM128MEM: {
13020 unsigned NumArgs;
13021 bool MemArg;
13022 switch (MI->getOpcode()) {
13023 default: llvm_unreachable("illegal opcode!");
13024 case X86::PCMPISTRM128REG:
13025 case X86::VPCMPISTRM128REG:
13026 NumArgs = 3; MemArg = false; break;
13027 case X86::PCMPISTRM128MEM:
13028 case X86::VPCMPISTRM128MEM:
13029 NumArgs = 3; MemArg = true; break;
13030 case X86::PCMPESTRM128REG:
13031 case X86::VPCMPESTRM128REG:
13032 NumArgs = 5; MemArg = false; break;
13033 case X86::PCMPESTRM128MEM:
13034 case X86::VPCMPESTRM128MEM:
13035 NumArgs = 5; MemArg = true; break;
13036 }
13037 return EmitPCMP(MI, BB, NumArgs, MemArg);
13038 }
Eric Christopherb120ab42009-08-18 22:50:32 +000013039
Eric Christopher228232b2010-11-30 07:20:12 +000013040 // Thread synchronization.
13041 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000013042 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000013043
Eric Christopherb120ab42009-08-18 22:50:32 +000013044 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000013045 case X86::ATOMMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013046 case X86::ATOMMAX32:
Mon P Wang63307c32008-05-05 19:05:59 +000013047 case X86::ATOMUMIN32:
Mon P Wang63307c32008-05-05 19:05:59 +000013048 case X86::ATOMUMAX32:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013049 case X86::ATOMMIN16:
13050 case X86::ATOMMAX16:
13051 case X86::ATOMUMIN16:
13052 case X86::ATOMUMAX16:
13053 case X86::ATOMMIN64:
13054 case X86::ATOMMAX64:
13055 case X86::ATOMUMIN64:
13056 case X86::ATOMUMAX64: {
13057 unsigned Opc;
13058 switch (MI->getOpcode()) {
13059 default: llvm_unreachable("illegal opcode!");
13060 case X86::ATOMMIN32: Opc = X86::CMOVL32rr; break;
13061 case X86::ATOMMAX32: Opc = X86::CMOVG32rr; break;
13062 case X86::ATOMUMIN32: Opc = X86::CMOVB32rr; break;
13063 case X86::ATOMUMAX32: Opc = X86::CMOVA32rr; break;
13064 case X86::ATOMMIN16: Opc = X86::CMOVL16rr; break;
13065 case X86::ATOMMAX16: Opc = X86::CMOVG16rr; break;
13066 case X86::ATOMUMIN16: Opc = X86::CMOVB16rr; break;
13067 case X86::ATOMUMAX16: Opc = X86::CMOVA16rr; break;
13068 case X86::ATOMMIN64: Opc = X86::CMOVL64rr; break;
13069 case X86::ATOMMAX64: Opc = X86::CMOVG64rr; break;
13070 case X86::ATOMUMIN64: Opc = X86::CMOVB64rr; break;
13071 case X86::ATOMUMAX64: Opc = X86::CMOVA64rr; break;
13072 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
13073 }
13074 return EmitAtomicMinMaxWithCustomInserter(MI, BB, Opc);
13075 }
13076
13077 case X86::ATOMAND32:
13078 case X86::ATOMOR32:
13079 case X86::ATOMXOR32:
13080 case X86::ATOMNAND32: {
13081 bool Invert = false;
13082 unsigned RegOpc, ImmOpc;
13083 switch (MI->getOpcode()) {
13084 default: llvm_unreachable("illegal opcode!");
13085 case X86::ATOMAND32:
13086 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; break;
13087 case X86::ATOMOR32:
13088 RegOpc = X86::OR32rr; ImmOpc = X86::OR32ri; break;
13089 case X86::ATOMXOR32:
13090 RegOpc = X86::XOR32rr; ImmOpc = X86::XOR32ri; break;
13091 case X86::ATOMNAND32:
13092 RegOpc = X86::AND32rr; ImmOpc = X86::AND32ri; Invert = true; break;
13093 }
13094 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13095 X86::MOV32rm, X86::LCMPXCHG32,
13096 X86::NOT32r, X86::EAX,
13097 &X86::GR32RegClass, Invert);
13098 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013099
13100 case X86::ATOMAND16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013101 case X86::ATOMOR16:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013102 case X86::ATOMXOR16:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013103 case X86::ATOMNAND16: {
13104 bool Invert = false;
13105 unsigned RegOpc, ImmOpc;
13106 switch (MI->getOpcode()) {
13107 default: llvm_unreachable("illegal opcode!");
13108 case X86::ATOMAND16:
13109 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; break;
13110 case X86::ATOMOR16:
13111 RegOpc = X86::OR16rr; ImmOpc = X86::OR16ri; break;
13112 case X86::ATOMXOR16:
13113 RegOpc = X86::XOR16rr; ImmOpc = X86::XOR16ri; break;
13114 case X86::ATOMNAND16:
13115 RegOpc = X86::AND16rr; ImmOpc = X86::AND16ri; Invert = true; break;
13116 }
13117 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13118 X86::MOV16rm, X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013119 X86::NOT16r, X86::AX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013120 &X86::GR16RegClass, Invert);
13121 }
Dale Johannesen140be2d2008-08-19 18:47:28 +000013122
13123 case X86::ATOMAND8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013124 case X86::ATOMOR8:
Dale Johannesen140be2d2008-08-19 18:47:28 +000013125 case X86::ATOMXOR8:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013126 case X86::ATOMNAND8: {
13127 bool Invert = false;
13128 unsigned RegOpc, ImmOpc;
13129 switch (MI->getOpcode()) {
13130 default: llvm_unreachable("illegal opcode!");
13131 case X86::ATOMAND8:
13132 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; break;
13133 case X86::ATOMOR8:
13134 RegOpc = X86::OR8rr; ImmOpc = X86::OR8ri; break;
13135 case X86::ATOMXOR8:
13136 RegOpc = X86::XOR8rr; ImmOpc = X86::XOR8ri; break;
13137 case X86::ATOMNAND8:
13138 RegOpc = X86::AND8rr; ImmOpc = X86::AND8ri; Invert = true; break;
13139 }
13140 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13141 X86::MOV8rm, X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000013142 X86::NOT8r, X86::AL,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013143 &X86::GR8RegClass, Invert);
13144 }
13145
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013146 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000013147 case X86::ATOMAND64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013148 case X86::ATOMOR64:
Dale Johannesena99e3842008-08-20 00:48:50 +000013149 case X86::ATOMXOR64:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013150 case X86::ATOMNAND64: {
13151 bool Invert = false;
13152 unsigned RegOpc, ImmOpc;
13153 switch (MI->getOpcode()) {
13154 default: llvm_unreachable("illegal opcode!");
13155 case X86::ATOMAND64:
13156 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; break;
13157 case X86::ATOMOR64:
13158 RegOpc = X86::OR64rr; ImmOpc = X86::OR64ri32; break;
13159 case X86::ATOMXOR64:
13160 RegOpc = X86::XOR64rr; ImmOpc = X86::XOR64ri32; break;
13161 case X86::ATOMNAND64:
13162 RegOpc = X86::AND64rr; ImmOpc = X86::AND64ri32; Invert = true; break;
13163 }
13164 return EmitAtomicBitwiseWithCustomInserter(MI, BB, RegOpc, ImmOpc,
13165 X86::MOV64rm, X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000013166 X86::NOT64r, X86::RAX,
Craig Topperacaaa6f2012-08-18 06:39:34 +000013167 &X86::GR64RegClass, Invert);
13168 }
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013169
13170 // This group does 64-bit operations on a 32-bit host.
13171 case X86::ATOMAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013172 case X86::ATOMOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013173 case X86::ATOMXOR6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013174 case X86::ATOMNAND6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013175 case X86::ATOMADD6432:
Dale Johannesen48c1bc22008-10-02 18:53:47 +000013176 case X86::ATOMSUB6432:
Craig Topperacaaa6f2012-08-18 06:39:34 +000013177 case X86::ATOMSWAP6432: {
13178 bool Invert = false;
13179 unsigned RegOpcL, RegOpcH, ImmOpcL, ImmOpcH;
13180 switch (MI->getOpcode()) {
13181 default: llvm_unreachable("illegal opcode!");
13182 case X86::ATOMAND6432:
13183 RegOpcL = RegOpcH = X86::AND32rr;
13184 ImmOpcL = ImmOpcH = X86::AND32ri;
13185 break;
13186 case X86::ATOMOR6432:
13187 RegOpcL = RegOpcH = X86::OR32rr;
13188 ImmOpcL = ImmOpcH = X86::OR32ri;
13189 break;
13190 case X86::ATOMXOR6432:
13191 RegOpcL = RegOpcH = X86::XOR32rr;
13192 ImmOpcL = ImmOpcH = X86::XOR32ri;
13193 break;
13194 case X86::ATOMNAND6432:
13195 RegOpcL = RegOpcH = X86::AND32rr;
13196 ImmOpcL = ImmOpcH = X86::AND32ri;
13197 Invert = true;
13198 break;
13199 case X86::ATOMADD6432:
13200 RegOpcL = X86::ADD32rr; RegOpcH = X86::ADC32rr;
13201 ImmOpcL = X86::ADD32ri; ImmOpcH = X86::ADC32ri;
13202 break;
13203 case X86::ATOMSUB6432:
13204 RegOpcL = X86::SUB32rr; RegOpcH = X86::SBB32rr;
13205 ImmOpcL = X86::SUB32ri; ImmOpcH = X86::SBB32ri;
13206 break;
13207 case X86::ATOMSWAP6432:
13208 RegOpcL = RegOpcH = X86::MOV32rr;
13209 ImmOpcL = ImmOpcH = X86::MOV32ri;
13210 break;
13211 }
13212 return EmitAtomicBit6432WithCustomInserter(MI, BB, RegOpcL, RegOpcH,
13213 ImmOpcL, ImmOpcH, Invert);
13214 }
13215
Dan Gohmand6708ea2009-08-15 01:38:56 +000013216 case X86::VASTART_SAVE_XMM_REGS:
13217 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000013218
13219 case X86::VAARG_64:
13220 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000013221 }
13222}
13223
13224//===----------------------------------------------------------------------===//
13225// X86 Optimization Hooks
13226//===----------------------------------------------------------------------===//
13227
Dan Gohman475871a2008-07-27 21:46:04 +000013228void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000013229 APInt &KnownZero,
13230 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000013231 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000013232 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013233 unsigned BitWidth = KnownZero.getBitWidth();
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013234 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000013235 assert((Opc >= ISD::BUILTIN_OP_END ||
13236 Opc == ISD::INTRINSIC_WO_CHAIN ||
13237 Opc == ISD::INTRINSIC_W_CHAIN ||
13238 Opc == ISD::INTRINSIC_VOID) &&
13239 "Should use MaskedValueIsZero if you don't know whether Op"
13240 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013241
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013242 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013243 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000013244 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013245 case X86ISD::ADD:
13246 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000013247 case X86ISD::ADC:
13248 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013249 case X86ISD::SMUL:
13250 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000013251 case X86ISD::INC:
13252 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000013253 case X86ISD::OR:
13254 case X86ISD::XOR:
13255 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000013256 // These nodes' second result is a boolean.
13257 if (Op.getResNo() == 0)
13258 break;
13259 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013260 case X86ISD::SETCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013261 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000013262 break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013263 case ISD::INTRINSIC_WO_CHAIN: {
13264 unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
13265 unsigned NumLoBits = 0;
13266 switch (IntId) {
13267 default: break;
13268 case Intrinsic::x86_sse_movmsk_ps:
13269 case Intrinsic::x86_avx_movmsk_ps_256:
13270 case Intrinsic::x86_sse2_movmsk_pd:
13271 case Intrinsic::x86_avx_movmsk_pd_256:
13272 case Intrinsic::x86_mmx_pmovmskb:
Craig Topper3738ccd2011-12-27 06:27:23 +000013273 case Intrinsic::x86_sse2_pmovmskb_128:
13274 case Intrinsic::x86_avx2_pmovmskb: {
Evan Cheng7c1780c2011-10-07 17:21:44 +000013275 // High bits of movmskp{s|d}, pmovmskb are known zero.
13276 switch (IntId) {
Craig Topperabb94d02012-02-05 03:43:23 +000013277 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng7c1780c2011-10-07 17:21:44 +000013278 case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break;
13279 case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break;
13280 case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break;
13281 case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break;
13282 case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break;
13283 case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break;
Craig Topper3738ccd2011-12-27 06:27:23 +000013284 case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break;
Evan Cheng7c1780c2011-10-07 17:21:44 +000013285 }
Rafael Espindola26c8dcc2012-04-04 12:51:34 +000013286 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
Evan Cheng7c1780c2011-10-07 17:21:44 +000013287 break;
13288 }
13289 }
13290 break;
13291 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013292 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000013293}
Chris Lattner259e97c2006-01-31 19:43:35 +000013294
Owen Andersonbc146b02010-09-21 20:42:50 +000013295unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
13296 unsigned Depth) const {
13297 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
13298 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
13299 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000013300
Owen Andersonbc146b02010-09-21 20:42:50 +000013301 // Fallback case.
13302 return 1;
13303}
13304
Evan Cheng206ee9d2006-07-07 08:33:52 +000013305/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000013306/// node is a GlobalAddress + offset.
13307bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000013308 const GlobalValue* &GA,
13309 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000013310 if (N->getOpcode() == X86ISD::Wrapper) {
13311 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000013312 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000013313 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000013314 return true;
13315 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000013316 }
Evan Chengad4196b2008-05-12 19:56:52 +000013317 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000013318}
13319
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013320/// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the
13321/// same as extracting the high 128-bit part of 256-bit vector and then
13322/// inserting the result into the low part of a new 256-bit vector
13323static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) {
13324 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013325 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013326
13327 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
Craig Topper66ddd152012-04-27 22:54:43 +000013328 for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013329 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13330 SVOp->getMaskElt(j) >= 0)
13331 return false;
13332
13333 return true;
13334}
13335
13336/// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the
13337/// same as extracting the low 128-bit part of 256-bit vector and then
13338/// inserting the result into the high part of a new 256-bit vector
13339static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) {
13340 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013341 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013342
13343 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
Craig Topper66ddd152012-04-27 22:54:43 +000013344 for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j)
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013345 if (!isUndefOrEqual(SVOp->getMaskElt(i), j) ||
13346 SVOp->getMaskElt(j) >= 0)
13347 return false;
13348
13349 return true;
13350}
13351
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013352/// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
13353static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
Craig Topper12216172012-01-13 08:12:35 +000013354 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013355 const X86Subtarget* Subtarget) {
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013356 DebugLoc dl = N->getDebugLoc();
13357 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
13358 SDValue V1 = SVOp->getOperand(0);
13359 SDValue V2 = SVOp->getOperand(1);
13360 EVT VT = SVOp->getValueType(0);
Craig Topper66ddd152012-04-27 22:54:43 +000013361 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013362
13363 if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
13364 V2.getOpcode() == ISD::CONCAT_VECTORS) {
13365 //
13366 // 0,0,0,...
Benjamin Kramer558cc5a2011-07-22 01:02:57 +000013367 // |
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013368 // V UNDEF BUILD_VECTOR UNDEF
13369 // \ / \ /
13370 // CONCAT_VECTOR CONCAT_VECTOR
13371 // \ /
13372 // \ /
13373 // RESULT: V + zero extended
13374 //
13375 if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
13376 V2.getOperand(1).getOpcode() != ISD::UNDEF ||
13377 V1.getOperand(1).getOpcode() != ISD::UNDEF)
13378 return SDValue();
13379
13380 if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
13381 return SDValue();
13382
13383 // To match the shuffle mask, the first half of the mask should
13384 // be exactly the first vector, and all the rest a splat with the
13385 // first element of the second one.
Craig Topper66ddd152012-04-27 22:54:43 +000013386 for (unsigned i = 0; i != NumElems/2; ++i)
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013387 if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
13388 !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
13389 return SDValue();
13390
Chad Rosier3d1161e2012-01-03 21:05:52 +000013391 // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
13392 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
Chad Rosier42726832012-05-07 18:47:44 +000013393 if (Ld->hasNUsesOfValue(1, 0)) {
13394 SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
13395 SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
13396 SDValue ResNode =
13397 DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2,
13398 Ld->getMemoryVT(),
13399 Ld->getPointerInfo(),
13400 Ld->getAlignment(),
13401 false/*isVolatile*/, true/*ReadMem*/,
13402 false/*WriteMem*/);
13403 return DAG.getNode(ISD::BITCAST, dl, VT, ResNode);
13404 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000013405 }
Chad Rosier3d1161e2012-01-03 21:05:52 +000013406
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013407 // Emit a zeroed vector and insert the desired subvector on its
13408 // first half.
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013409 SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
Craig Topperb14940a2012-04-22 20:55:18 +000013410 SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013411 return DCI.CombineTo(N, InsV);
13412 }
13413
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013414 //===--------------------------------------------------------------------===//
13415 // Combine some shuffles into subvector extracts and inserts:
13416 //
13417
13418 // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
13419 if (isShuffleHigh128VectorInsertLow(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013420 SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl);
13421 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013422 return DCI.CombineTo(N, InsV);
13423 }
13424
13425 // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
13426 if (isShuffleLow128VectorInsertHigh(SVOp)) {
Craig Topperb14940a2012-04-22 20:55:18 +000013427 SDValue V = Extract128BitVector(V1, 0, DAG, dl);
13428 SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl);
Bruno Cardoso Lopesef8d6992011-08-11 21:50:44 +000013429 return DCI.CombineTo(N, InsV);
13430 }
13431
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013432 return SDValue();
13433}
13434
13435/// PerformShuffleCombine - Performs several different shuffle combines.
Dan Gohman475871a2008-07-27 21:46:04 +000013436static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013437 TargetLowering::DAGCombinerInfo &DCI,
13438 const X86Subtarget *Subtarget) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000013439 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000013440 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000013441
Mon P Wanga0fd0d52010-12-19 23:55:53 +000013442 // Don't create instructions with illegal types after legalize types has run.
13443 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13444 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
13445 return SDValue();
13446
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013447 // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
Craig Topper7a9a28b2012-08-12 02:23:29 +000013448 if (Subtarget->hasAVX() && VT.is256BitVector() &&
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000013449 N->getOpcode() == ISD::VECTOR_SHUFFLE)
Elena Demikhovsky0f1ead42012-02-02 09:20:18 +000013450 return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013451
13452 // Only handle 128 wide vector from here on.
Craig Topper7a9a28b2012-08-12 02:23:29 +000013453 if (!VT.is128BitVector())
Bruno Cardoso Lopes74dad552011-07-22 00:15:00 +000013454 return SDValue();
13455
13456 // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
13457 // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
13458 // consecutive, non-overlapping, and in the right order.
Nate Begemanfdea31a2010-03-24 20:49:50 +000013459 SmallVector<SDValue, 16> Elts;
13460 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000013461 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000013462
Nate Begemanfdea31a2010-03-24 20:49:50 +000013463 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000013464}
Evan Chengd880b972008-05-09 21:53:03 +000013465
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013466
Craig Topperc16f8512012-04-25 06:39:39 +000013467/// DCI, PerformTruncateCombine - Converts truncate operation to
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013468/// a sequence of vector shuffle operations.
13469/// It is possible when we truncate 256-bit vector to 128-bit vector
13470
Chad Rosiera20e1e72012-08-01 18:39:17 +000013471SDValue X86TargetLowering::PerformTruncateCombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013472 DAGCombinerInfo &DCI) const {
13473 if (!DCI.isBeforeLegalizeOps())
13474 return SDValue();
13475
Craig Topper3ef43cf2012-04-24 06:36:35 +000013476 if (!Subtarget->hasAVX())
13477 return SDValue();
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013478
13479 EVT VT = N->getValueType(0);
13480 SDValue Op = N->getOperand(0);
13481 EVT OpVT = Op.getValueType();
13482 DebugLoc dl = N->getDebugLoc();
13483
13484 if ((VT == MVT::v4i32) && (OpVT == MVT::v4i64)) {
13485
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013486 if (Subtarget->hasAVX2()) {
13487 // AVX2: v4i64 -> v4i32
13488
13489 // VPERMD
13490 static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13491
13492 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v8i32, Op);
13493 Op = DAG.getVectorShuffle(MVT::v8i32, dl, Op, DAG.getUNDEF(MVT::v8i32),
13494 ShufMask);
13495
Craig Topperd63fa652012-04-22 18:51:37 +000013496 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Op,
13497 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013498 }
13499
13500 // AVX: v4i64 -> v4i32
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013501 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013502 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013503
13504 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013505 DAG.getIntPtrConstant(2));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013506
13507 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13508 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13509
13510 // PSHUFD
Craig Topper9e401f22012-04-21 18:58:38 +000013511 static const int ShufMask1[] = {0, 2, 0, 0};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013512
Craig Toppercacafd42012-08-14 08:18:43 +000013513 SDValue Undef = DAG.getUNDEF(VT);
13514 OpLo = DAG.getVectorShuffle(VT, dl, OpLo, Undef, ShufMask1);
13515 OpHi = DAG.getVectorShuffle(VT, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013516
13517 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013518 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013519
Elena Demikhovsky73252572012-02-01 10:33:05 +000013520 return DAG.getVectorShuffle(VT, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013521 }
Craig Topperd63fa652012-04-22 18:51:37 +000013522
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013523 if ((VT == MVT::v8i16) && (OpVT == MVT::v8i32)) {
13524
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013525 if (Subtarget->hasAVX2()) {
13526 // AVX2: v8i32 -> v8i16
13527
13528 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v32i8, Op);
Craig Topperd63fa652012-04-22 18:51:37 +000013529
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013530 // PSHUFB
13531 SmallVector<SDValue,32> pshufbMask;
13532 for (unsigned i = 0; i < 2; ++i) {
13533 pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8));
13534 pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8));
13535 pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8));
13536 pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8));
13537 pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8));
13538 pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8));
13539 pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8));
13540 pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8));
13541 for (unsigned j = 0; j < 8; ++j)
13542 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
13543 }
Craig Topperd63fa652012-04-22 18:51:37 +000013544 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v32i8,
13545 &pshufbMask[0], 32);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013546 Op = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, Op, BV);
13547
13548 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4i64, Op);
13549
13550 static const int ShufMask[] = {0, 2, -1, -1};
Craig Topperd63fa652012-04-22 18:51:37 +000013551 Op = DAG.getVectorShuffle(MVT::v4i64, dl, Op, DAG.getUNDEF(MVT::v4i64),
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013552 &ShufMask[0]);
13553
Craig Topperd63fa652012-04-22 18:51:37 +000013554 Op = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Op,
13555 DAG.getIntPtrConstant(0));
Elena Demikhovsky1da58672012-04-22 09:39:03 +000013556
13557 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
13558 }
13559
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013560 SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013561 DAG.getIntPtrConstant(0));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013562
13563 SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i32, Op,
Craig Topperd63fa652012-04-22 18:51:37 +000013564 DAG.getIntPtrConstant(4));
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013565
13566 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLo);
13567 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpHi);
13568
13569 // PSHUFB
Craig Topper9e401f22012-04-21 18:58:38 +000013570 static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13,
13571 -1, -1, -1, -1, -1, -1, -1, -1};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013572
Craig Toppercacafd42012-08-14 08:18:43 +000013573 SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13574 OpLo = DAG.getVectorShuffle(MVT::v16i8, dl, OpLo, Undef, ShufMask1);
13575 OpHi = DAG.getVectorShuffle(MVT::v16i8, dl, OpHi, Undef, ShufMask1);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013576
13577 OpLo = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpLo);
13578 OpHi = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, OpHi);
13579
13580 // MOVLHPS
Craig Topper9e401f22012-04-21 18:58:38 +000013581 static const int ShufMask2[] = {0, 1, 4, 5};
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013582
Elena Demikhovsky73252572012-02-01 10:33:05 +000013583 SDValue res = DAG.getVectorShuffle(MVT::v4i32, dl, OpLo, OpHi, ShufMask2);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013584 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, res);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000013585 }
13586
13587 return SDValue();
13588}
13589
Craig Topper89f4e662012-03-20 07:17:59 +000013590/// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
13591/// specific shuffle of a load can be folded into a single element load.
13592/// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
13593/// shuffles have been customed lowered so we need to handle those here.
13594static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
13595 TargetLowering::DAGCombinerInfo &DCI) {
13596 if (DCI.isBeforeLegalizeOps())
13597 return SDValue();
13598
13599 SDValue InVec = N->getOperand(0);
13600 SDValue EltNo = N->getOperand(1);
13601
13602 if (!isa<ConstantSDNode>(EltNo))
13603 return SDValue();
13604
13605 EVT VT = InVec.getValueType();
13606
13607 bool HasShuffleIntoBitcast = false;
13608 if (InVec.getOpcode() == ISD::BITCAST) {
13609 // Don't duplicate a load with other uses.
13610 if (!InVec.hasOneUse())
13611 return SDValue();
13612 EVT BCVT = InVec.getOperand(0).getValueType();
13613 if (BCVT.getVectorNumElements() != VT.getVectorNumElements())
13614 return SDValue();
13615 InVec = InVec.getOperand(0);
13616 HasShuffleIntoBitcast = true;
13617 }
13618
13619 if (!isTargetShuffle(InVec.getOpcode()))
13620 return SDValue();
13621
13622 // Don't duplicate a load with other uses.
13623 if (!InVec.hasOneUse())
13624 return SDValue();
13625
13626 SmallVector<int, 16> ShuffleMask;
13627 bool UnaryShuffle;
Craig Topperd978c542012-05-06 19:46:21 +000013628 if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask,
13629 UnaryShuffle))
Craig Topper89f4e662012-03-20 07:17:59 +000013630 return SDValue();
13631
13632 // Select the input vector, guarding against out of range extract vector.
13633 unsigned NumElems = VT.getVectorNumElements();
13634 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
13635 int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
13636 SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
13637 : InVec.getOperand(1);
13638
13639 // If inputs to shuffle are the same for both ops, then allow 2 uses
13640 unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
13641
13642 if (LdNode.getOpcode() == ISD::BITCAST) {
13643 // Don't duplicate a load with other uses.
13644 if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
13645 return SDValue();
13646
13647 AllowedUses = 1; // only allow 1 load use if we have a bitcast
13648 LdNode = LdNode.getOperand(0);
13649 }
13650
13651 if (!ISD::isNormalLoad(LdNode.getNode()))
13652 return SDValue();
13653
13654 LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
13655
13656 if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
13657 return SDValue();
13658
13659 if (HasShuffleIntoBitcast) {
13660 // If there's a bitcast before the shuffle, check if the load type and
13661 // alignment is valid.
13662 unsigned Align = LN0->getAlignment();
13663 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13664 unsigned NewAlign = TLI.getTargetData()->
13665 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
13666
13667 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
13668 return SDValue();
13669 }
13670
13671 // All checks match so transform back to vector_shuffle so that DAG combiner
13672 // can finish the job
13673 DebugLoc dl = N->getDebugLoc();
13674
13675 // Create shuffle node taking into account the case that its a unary shuffle
13676 SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1);
13677 Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl,
13678 InVec.getOperand(0), Shuffle,
13679 &ShuffleMask[0]);
13680 Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
13681 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
13682 EltNo);
13683}
13684
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000013685/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
13686/// generation and convert it from being a bunch of shuffles and extracts
13687/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013688static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
Craig Topper89f4e662012-03-20 07:17:59 +000013689 TargetLowering::DAGCombinerInfo &DCI) {
13690 SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI);
13691 if (NewOp.getNode())
13692 return NewOp;
13693
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013694 SDValue InputVector = N->getOperand(0);
13695
13696 // Only operate on vectors of 4 elements, where the alternative shuffling
13697 // gets to be more expensive.
13698 if (InputVector.getValueType() != MVT::v4i32)
13699 return SDValue();
13700
13701 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
13702 // single use which is a sign-extend or zero-extend, and all elements are
13703 // used.
13704 SmallVector<SDNode *, 4> Uses;
13705 unsigned ExtractedElements = 0;
13706 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
13707 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
13708 if (UI.getUse().getResNo() != InputVector.getResNo())
13709 return SDValue();
13710
13711 SDNode *Extract = *UI;
13712 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13713 return SDValue();
13714
13715 if (Extract->getValueType(0) != MVT::i32)
13716 return SDValue();
13717 if (!Extract->hasOneUse())
13718 return SDValue();
13719 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
13720 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
13721 return SDValue();
13722 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
13723 return SDValue();
13724
13725 // Record which element was extracted.
13726 ExtractedElements |=
13727 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
13728
13729 Uses.push_back(Extract);
13730 }
13731
13732 // If not all the elements were used, this may not be worthwhile.
13733 if (ExtractedElements != 15)
13734 return SDValue();
13735
13736 // Ok, we've now decided to do the transformation.
13737 DebugLoc dl = InputVector.getDebugLoc();
13738
13739 // Store the value to a temporary stack slot.
13740 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000013741 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
13742 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013743
13744 // Replace each use (extract) with a load of the appropriate element.
13745 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
13746 UE = Uses.end(); UI != UE; ++UI) {
13747 SDNode *Extract = *UI;
13748
Nadav Rotem86694292011-05-17 08:31:57 +000013749 // cOMpute the element's address.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013750 SDValue Idx = Extract->getOperand(1);
13751 unsigned EltSize =
13752 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
13753 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
Craig Topper89f4e662012-03-20 07:17:59 +000013754 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013755 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
13756
Nadav Rotem86694292011-05-17 08:31:57 +000013757 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
Chris Lattner51abfe42010-09-21 06:02:19 +000013758 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013759
13760 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000013761 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000013762 ScalarAddr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000013763 false, false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000013764
13765 // Replace the exact with the load.
13766 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
13767 }
13768
13769 // The replacement was made in place; don't return anything.
13770 return SDValue();
13771}
13772
Duncan Sands6bcd2192011-09-17 16:49:39 +000013773/// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
13774/// nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000013775static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotemcc616562012-01-15 19:27:55 +000013776 TargetLowering::DAGCombinerInfo &DCI,
Chris Lattner47b4ce82009-03-11 05:48:52 +000013777 const X86Subtarget *Subtarget) {
13778 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000013779 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000013780 // Get the LHS/RHS of the select.
13781 SDValue LHS = N->getOperand(1);
13782 SDValue RHS = N->getOperand(2);
Bruno Cardoso Lopes149f29f2011-09-20 22:34:45 +000013783 EVT VT = LHS.getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +000013784
Dan Gohman670e5392009-09-21 18:03:22 +000013785 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000013786 // instructions match the semantics of the common C idiom x<y?x:y but not
13787 // x<=y?x:y, because of how they handle negative zero (which can be
13788 // ignored in unsafe-math mode).
Benjamin Kramer2c2ccbf2011-09-22 03:27:22 +000013789 if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
13790 VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) &&
Craig Topper1accb7e2012-01-10 06:54:16 +000013791 (Subtarget->hasSSE2() ||
13792 (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013793 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013794
Chris Lattner47b4ce82009-03-11 05:48:52 +000013795 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000013796 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000013797 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
13798 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013799 switch (CC) {
13800 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013801 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013802 // Converting this to a min would handle NaNs incorrectly, and swapping
13803 // the operands would cause it to handle comparisons between positive
13804 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013805 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013806 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013807 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13808 break;
13809 std::swap(LHS, RHS);
13810 }
Dan Gohman670e5392009-09-21 18:03:22 +000013811 Opcode = X86ISD::FMIN;
13812 break;
13813 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013814 // Converting this to a min would handle comparisons between positive
13815 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013816 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013817 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
13818 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013819 Opcode = X86ISD::FMIN;
13820 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013821 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013822 // Converting this to a min would handle both negative zeros and NaNs
13823 // incorrectly, but we can swap the operands to fix both.
13824 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013825 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013826 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013827 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013828 Opcode = X86ISD::FMIN;
13829 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013830
Dan Gohman670e5392009-09-21 18:03:22 +000013831 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013832 // Converting this to a max would handle comparisons between positive
13833 // and negative zero incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013834 if (!DAG.getTarget().Options.UnsafeFPMath &&
Evan Chengdd5663c2011-08-04 18:38:15 +000013835 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013836 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013837 Opcode = X86ISD::FMAX;
13838 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000013839 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013840 // Converting this to a max would handle NaNs incorrectly, and swapping
13841 // the operands would cause it to handle comparisons between positive
13842 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013843 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013844 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013845 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
13846 break;
13847 std::swap(LHS, RHS);
13848 }
Dan Gohman670e5392009-09-21 18:03:22 +000013849 Opcode = X86ISD::FMAX;
13850 break;
13851 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013852 // Converting this to a max would handle both negative zeros and NaNs
13853 // incorrectly, but we can swap the operands to fix both.
13854 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013855 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013856 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013857 case ISD::SETGE:
13858 Opcode = X86ISD::FMAX;
13859 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000013860 }
Dan Gohman670e5392009-09-21 18:03:22 +000013861 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000013862 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
13863 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000013864 switch (CC) {
13865 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000013866 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013867 // Converting this to a min would handle comparisons between positive
13868 // and negative zero incorrectly, and swapping the operands would
13869 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013870 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013871 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000013872 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013873 break;
13874 std::swap(LHS, RHS);
13875 }
Dan Gohman670e5392009-09-21 18:03:22 +000013876 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000013877 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013878 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000013879 // Converting this to a min would handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013880 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013881 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
13882 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013883 Opcode = X86ISD::FMIN;
13884 break;
13885 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000013886 // Converting this to a min would handle both negative zeros and NaNs
13887 // incorrectly, but we can swap the operands to fix both.
13888 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013889 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013890 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013891 case ISD::SETGE:
13892 Opcode = X86ISD::FMIN;
13893 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013894
Dan Gohman670e5392009-09-21 18:03:22 +000013895 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000013896 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000013897 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013898 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013899 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000013900 break;
Dan Gohman670e5392009-09-21 18:03:22 +000013901 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000013902 // Converting this to a max would handle comparisons between positive
13903 // and negative zero incorrectly, and swapping the operands would
13904 // cause it to handle NaNs incorrectly.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000013905 if (!DAG.getTarget().Options.UnsafeFPMath &&
Dan Gohmane8326932010-02-24 06:52:40 +000013906 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000013907 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000013908 break;
13909 std::swap(LHS, RHS);
13910 }
Dan Gohman670e5392009-09-21 18:03:22 +000013911 Opcode = X86ISD::FMAX;
13912 break;
13913 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000013914 // Converting this to a max would handle both negative zeros and NaNs
13915 // incorrectly, but we can swap the operands to fix both.
13916 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000013917 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013918 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000013919 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000013920 Opcode = X86ISD::FMAX;
13921 break;
13922 }
Chris Lattner83e6c992006-10-04 06:57:07 +000013923 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000013924
Chris Lattner47b4ce82009-03-11 05:48:52 +000013925 if (Opcode)
13926 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000013927 }
Eric Christopherfd179292009-08-27 18:07:15 +000013928
Chris Lattnerd1980a52009-03-12 06:52:53 +000013929 // If this is a select between two integer constants, try to do some
13930 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000013931 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
13932 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000013933 // Don't do this for crazy integer types.
13934 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
13935 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000013936 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000013937 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000013938
Chris Lattnercee56e72009-03-13 05:53:31 +000013939 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000013940 // Efficiently invertible.
13941 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
13942 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
13943 isa<ConstantSDNode>(Cond.getOperand(1))))) {
13944 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000013945 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000013946 }
Eric Christopherfd179292009-08-27 18:07:15 +000013947
Chris Lattnerd1980a52009-03-12 06:52:53 +000013948 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000013949 if (FalseC->getAPIntValue() == 0 &&
13950 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000013951 if (NeedsCondInvert) // Invert the condition if needed.
13952 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
13953 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000013954
Chris Lattnerd1980a52009-03-12 06:52:53 +000013955 // Zero extend the condition if needed.
13956 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000013957
Chris Lattnercee56e72009-03-13 05:53:31 +000013958 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000013959 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000013960 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000013961 }
Eric Christopherfd179292009-08-27 18:07:15 +000013962
Chris Lattner97a29a52009-03-13 05:22:11 +000013963 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000013964 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +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 Lattner97a29a52009-03-13 05:22:11 +000013969 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000013970 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
13971 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000013972 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000013973 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000013974 }
Eric Christopherfd179292009-08-27 18:07:15 +000013975
Chris Lattnercee56e72009-03-13 05:53:31 +000013976 // Optimize cases that will turn into an LEA instruction. This requires
13977 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000013978 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000013979 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000013980 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000013981
Chris Lattnercee56e72009-03-13 05:53:31 +000013982 bool isFastMultiplier = false;
13983 if (Diff < 10) {
13984 switch ((unsigned char)Diff) {
13985 default: break;
13986 case 1: // result = add base, cond
13987 case 2: // result = lea base( , cond*2)
13988 case 3: // result = lea base(cond, cond*2)
13989 case 4: // result = lea base( , cond*4)
13990 case 5: // result = lea base(cond, cond*4)
13991 case 8: // result = lea base( , cond*8)
13992 case 9: // result = lea base(cond, cond*8)
13993 isFastMultiplier = true;
13994 break;
13995 }
13996 }
Eric Christopherfd179292009-08-27 18:07:15 +000013997
Chris Lattnercee56e72009-03-13 05:53:31 +000013998 if (isFastMultiplier) {
13999 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
14000 if (NeedsCondInvert) // Invert the condition if needed.
14001 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
14002 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014003
Chris Lattnercee56e72009-03-13 05:53:31 +000014004 // Zero extend the condition if needed.
14005 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14006 Cond);
14007 // Scale the condition by the difference.
14008 if (Diff != 1)
14009 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14010 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000014011
Chris Lattnercee56e72009-03-13 05:53:31 +000014012 // Add the base if non-zero.
14013 if (FalseC->getAPIntValue() != 0)
14014 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14015 SDValue(FalseC, 0));
14016 return Cond;
14017 }
Eric Christopherfd179292009-08-27 18:07:15 +000014018 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014019 }
14020 }
Eric Christopherfd179292009-08-27 18:07:15 +000014021
Evan Cheng56f582d2012-01-04 01:41:39 +000014022 // Canonicalize max and min:
14023 // (x > y) ? x : y -> (x >= y) ? x : y
14024 // (x < y) ? x : y -> (x <= y) ? x : y
14025 // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
14026 // the need for an extra compare
14027 // against zero. e.g.
14028 // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
14029 // subl %esi, %edi
14030 // testl %edi, %edi
14031 // movl $0, %eax
14032 // cmovgl %edi, %eax
14033 // =>
14034 // xorl %eax, %eax
14035 // subl %esi, $edi
14036 // cmovsl %eax, %edi
14037 if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
14038 DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
14039 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
14040 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
14041 switch (CC) {
14042 default: break;
14043 case ISD::SETLT:
14044 case ISD::SETGT: {
14045 ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
14046 Cond = DAG.getSetCC(Cond.getDebugLoc(), Cond.getValueType(),
14047 Cond.getOperand(0), Cond.getOperand(1), NewCC);
14048 return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
14049 }
14050 }
14051 }
14052
Nadav Rotemcc616562012-01-15 19:27:55 +000014053 // If we know that this node is legal then we know that it is going to be
14054 // matched by one of the SSE/AVX BLEND instructions. These instructions only
14055 // depend on the highest bit in each word. Try to use SimplifyDemandedBits
14056 // to simplify previous instructions.
14057 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14058 if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
Nadav Rotembdcae382012-06-07 20:53:48 +000014059 !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) {
Nadav Rotemcc616562012-01-15 19:27:55 +000014060 unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits();
Nadav Rotembdcae382012-06-07 20:53:48 +000014061
14062 // Don't optimize vector selects that map to mask-registers.
14063 if (BitWidth == 1)
14064 return SDValue();
14065
Nadav Rotemcc616562012-01-15 19:27:55 +000014066 assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
14067 APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
14068
14069 APInt KnownZero, KnownOne;
14070 TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
14071 DCI.isBeforeLegalizeOps());
14072 if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
14073 TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO))
14074 DCI.CommitTargetLoweringOpt(TLO);
14075 }
14076
Dan Gohman475871a2008-07-27 21:46:04 +000014077 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000014078}
14079
Michael Liao2a33cec2012-08-10 19:58:13 +000014080// Check whether a boolean test is testing a boolean value generated by
14081// X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
14082// code.
14083//
14084// Simplify the following patterns:
14085// (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
14086// (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
14087// to (Op EFLAGS Cond)
14088//
14089// (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
14090// (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
14091// to (Op EFLAGS !Cond)
14092//
14093// where Op could be BRCOND or CMOV.
14094//
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014095static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014096 // Quit if not CMP and SUB with its value result used.
14097 if (Cmp.getOpcode() != X86ISD::CMP &&
14098 (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
14099 return SDValue();
14100
14101 // Quit if not used as a boolean value.
14102 if (CC != X86::COND_E && CC != X86::COND_NE)
14103 return SDValue();
14104
14105 // Check CMP operands. One of them should be 0 or 1 and the other should be
14106 // an SetCC or extended from it.
14107 SDValue Op1 = Cmp.getOperand(0);
14108 SDValue Op2 = Cmp.getOperand(1);
14109
14110 SDValue SetCC;
14111 const ConstantSDNode* C = 0;
14112 bool needOppositeCond = (CC == X86::COND_E);
14113
14114 if ((C = dyn_cast<ConstantSDNode>(Op1)))
14115 SetCC = Op2;
14116 else if ((C = dyn_cast<ConstantSDNode>(Op2)))
14117 SetCC = Op1;
14118 else // Quit if all operands are not constants.
14119 return SDValue();
14120
14121 if (C->getZExtValue() == 1)
14122 needOppositeCond = !needOppositeCond;
14123 else if (C->getZExtValue() != 0)
14124 // Quit if the constant is neither 0 or 1.
14125 return SDValue();
14126
14127 // Skip 'zext' node.
14128 if (SetCC.getOpcode() == ISD::ZERO_EXTEND)
14129 SetCC = SetCC.getOperand(0);
14130
14131 // Quit if not SETCC.
14132 // FIXME: So far we only handle the boolean value generated from SETCC. If
14133 // there is other ways to generate boolean values, we need handle them here
14134 // as well.
14135 if (SetCC.getOpcode() != X86ISD::SETCC)
14136 return SDValue();
14137
14138 // Set the condition code or opposite one if necessary.
14139 CC = X86::CondCode(SetCC.getConstantOperandVal(0));
14140 if (needOppositeCond)
14141 CC = X86::GetOppositeBranchCondition(CC);
14142
14143 return SetCC.getOperand(1);
14144}
14145
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014146/// checkFlaggedOrCombine - DAG combination on X86ISD::OR, i.e. with EFLAGS
14147/// updated. If only flag result is used and the result is evaluated from a
14148/// series of element extraction, try to combine it into a PTEST.
14149static SDValue checkFlaggedOrCombine(SDValue Or, X86::CondCode &CC,
14150 SelectionDAG &DAG,
14151 const X86Subtarget *Subtarget) {
14152 SDNode *N = Or.getNode();
14153 DebugLoc DL = N->getDebugLoc();
14154
14155 // Only SSE4.1 and beyond supports PTEST or like.
14156 if (!Subtarget->hasSSE41())
14157 return SDValue();
14158
14159 if (N->getOpcode() != X86ISD::OR)
14160 return SDValue();
14161
14162 // Quit if the value result of OR is used.
14163 if (N->hasAnyUseOfValue(0))
14164 return SDValue();
14165
14166 // Quit if not used as a boolean value.
14167 if (CC != X86::COND_E && CC != X86::COND_NE)
14168 return SDValue();
14169
14170 SmallVector<SDValue, 8> Opnds;
14171 SDValue VecIn;
14172 EVT VT = MVT::Other;
14173 unsigned Mask = 0;
14174
14175 // Recognize a special case where a vector is casted into wide integer to
14176 // test all 0s.
14177 Opnds.push_back(N->getOperand(0));
14178 Opnds.push_back(N->getOperand(1));
14179
14180 for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
14181 SmallVector<SDValue, 8>::const_iterator I = Opnds.begin() + Slot;
14182 // BFS traverse all OR'd operands.
14183 if (I->getOpcode() == ISD::OR) {
14184 Opnds.push_back(I->getOperand(0));
14185 Opnds.push_back(I->getOperand(1));
14186 // Re-evaluate the number of nodes to be traversed.
Michael Liao95c22a32012-08-28 23:42:17 +000014187 e += 2; // 2 more nodes (LHS and RHS) are pushed.
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014188 continue;
14189 }
14190
14191 // Quit if a non-EXTRACT_VECTOR_ELT
14192 if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
14193 return SDValue();
14194
14195 // Quit if without a constant index.
14196 SDValue Idx = I->getOperand(1);
14197 if (!isa<ConstantSDNode>(Idx))
14198 return SDValue();
14199
14200 // Check if all elements are extracted from the same vector.
14201 SDValue ExtractedFromVec = I->getOperand(0);
14202 if (VecIn.getNode() == 0) {
14203 VT = ExtractedFromVec.getValueType();
14204 // FIXME: only 128-bit vector is supported so far.
14205 if (!VT.is128BitVector())
14206 return SDValue();
14207 VecIn = ExtractedFromVec;
14208 } else if (VecIn != ExtractedFromVec)
14209 return SDValue();
14210
14211 // Record the constant index.
14212 Mask |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
14213 }
14214
14215 assert(VT.is128BitVector() && "Only 128-bit vector PTEST is supported so far.");
14216
14217 // Quit if not all elements are used.
14218 if (Mask != (1U << VT.getVectorNumElements()) - 1U)
14219 return SDValue();
14220
14221 return DAG.getNode(X86ISD::PTEST, DL, MVT::i32, VecIn, VecIn);
14222}
14223
Chris Lattnerd1980a52009-03-12 06:52:53 +000014224/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
14225static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014226 TargetLowering::DAGCombinerInfo &DCI,
14227 const X86Subtarget *Subtarget) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014228 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000014229
Chris Lattnerd1980a52009-03-12 06:52:53 +000014230 // If the flag operand isn't dead, don't touch this CMOV.
14231 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
14232 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000014233
Evan Chengb5a55d92011-05-24 01:48:22 +000014234 SDValue FalseOp = N->getOperand(0);
14235 SDValue TrueOp = N->getOperand(1);
14236 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
14237 SDValue Cond = N->getOperand(3);
Michael Liao2a33cec2012-08-10 19:58:13 +000014238
Evan Chengb5a55d92011-05-24 01:48:22 +000014239 if (CC == X86::COND_E || CC == X86::COND_NE) {
14240 switch (Cond.getOpcode()) {
14241 default: break;
14242 case X86ISD::BSR:
14243 case X86ISD::BSF:
14244 // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
14245 if (DAG.isKnownNeverZero(Cond.getOperand(0)))
14246 return (CC == X86::COND_E) ? FalseOp : TrueOp;
14247 }
14248 }
14249
Michael Liao2a33cec2012-08-10 19:58:13 +000014250 SDValue Flags;
14251
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014252 Flags = checkBoolTestSetCCCombine(Cond, CC);
Michael Liao9eac20a2012-08-11 23:47:06 +000014253 if (Flags.getNode() &&
14254 // Extra check as FCMOV only supports a subset of X86 cond.
Michael Liao7859f432012-09-06 07:11:22 +000014255 (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
Michael Liaodbf8b5b2012-08-28 03:34:40 +000014256 SDValue Ops[] = { FalseOp, TrueOp,
14257 DAG.getConstant(CC, MVT::i8), Flags };
14258 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14259 Ops, array_lengthof(Ops));
14260 }
14261
14262 Flags = checkFlaggedOrCombine(Cond, CC, DAG, Subtarget);
14263 if (Flags.getNode()) {
Michael Liao2a33cec2012-08-10 19:58:13 +000014264 SDValue Ops[] = { FalseOp, TrueOp,
14265 DAG.getConstant(CC, MVT::i8), Flags };
14266 return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(),
14267 Ops, array_lengthof(Ops));
14268 }
14269
Chris Lattnerd1980a52009-03-12 06:52:53 +000014270 // If this is a select between two integer constants, try to do some
14271 // optimizations. Note that the operands are ordered the opposite of SELECT
14272 // operands.
Evan Chengb5a55d92011-05-24 01:48:22 +000014273 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
14274 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000014275 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
14276 // larger than FalseC (the false value).
Chris Lattnerd1980a52009-03-12 06:52:53 +000014277 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
14278 CC = X86::GetOppositeBranchCondition(CC);
14279 std::swap(TrueC, FalseC);
14280 }
Eric Christopherfd179292009-08-27 18:07:15 +000014281
Chris Lattnerd1980a52009-03-12 06:52:53 +000014282 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000014283 // This is efficient for any integer data type (including i8/i16) and
14284 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000014285 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014286 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14287 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014288
Chris Lattnerd1980a52009-03-12 06:52:53 +000014289 // Zero extend the condition if needed.
14290 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014291
Chris Lattnerd1980a52009-03-12 06:52:53 +000014292 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
14293 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000014294 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000014295 if (N->getNumValues() == 2) // Dead flag value?
14296 return DCI.CombineTo(N, Cond, SDValue());
14297 return Cond;
14298 }
Eric Christopherfd179292009-08-27 18:07:15 +000014299
Chris Lattnercee56e72009-03-13 05:53:31 +000014300 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
14301 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000014302 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000014303 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14304 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000014305
Chris Lattner97a29a52009-03-13 05:22:11 +000014306 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000014307 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
14308 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000014309 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14310 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000014311
Chris Lattner97a29a52009-03-13 05:22:11 +000014312 if (N->getNumValues() == 2) // Dead flag value?
14313 return DCI.CombineTo(N, Cond, SDValue());
14314 return Cond;
14315 }
Eric Christopherfd179292009-08-27 18:07:15 +000014316
Chris Lattnercee56e72009-03-13 05:53:31 +000014317 // Optimize cases that will turn into an LEA instruction. This requires
14318 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000014319 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000014320 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014321 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000014322
Chris Lattnercee56e72009-03-13 05:53:31 +000014323 bool isFastMultiplier = false;
14324 if (Diff < 10) {
14325 switch ((unsigned char)Diff) {
14326 default: break;
14327 case 1: // result = add base, cond
14328 case 2: // result = lea base( , cond*2)
14329 case 3: // result = lea base(cond, cond*2)
14330 case 4: // result = lea base( , cond*4)
14331 case 5: // result = lea base(cond, cond*4)
14332 case 8: // result = lea base( , cond*8)
14333 case 9: // result = lea base(cond, cond*8)
14334 isFastMultiplier = true;
14335 break;
14336 }
14337 }
Eric Christopherfd179292009-08-27 18:07:15 +000014338
Chris Lattnercee56e72009-03-13 05:53:31 +000014339 if (isFastMultiplier) {
14340 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000014341 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
14342 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000014343 // Zero extend the condition if needed.
14344 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
14345 Cond);
14346 // Scale the condition by the difference.
14347 if (Diff != 1)
14348 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
14349 DAG.getConstant(Diff, Cond.getValueType()));
14350
14351 // Add the base if non-zero.
14352 if (FalseC->getAPIntValue() != 0)
14353 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
14354 SDValue(FalseC, 0));
14355 if (N->getNumValues() == 2) // Dead flag value?
14356 return DCI.CombineTo(N, Cond, SDValue());
14357 return Cond;
14358 }
Eric Christopherfd179292009-08-27 18:07:15 +000014359 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000014360 }
14361 }
14362 return SDValue();
14363}
14364
14365
Evan Cheng0b0cd912009-03-28 05:57:29 +000014366/// PerformMulCombine - Optimize a single multiply with constant into two
14367/// in order to implement it with two cheaper instructions, e.g.
14368/// LEA + SHL, LEA + LEA.
14369static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
14370 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000014371 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
14372 return SDValue();
14373
Owen Andersone50ed302009-08-10 22:56:29 +000014374 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000014375 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000014376 return SDValue();
14377
14378 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
14379 if (!C)
14380 return SDValue();
14381 uint64_t MulAmt = C->getZExtValue();
14382 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
14383 return SDValue();
14384
14385 uint64_t MulAmt1 = 0;
14386 uint64_t MulAmt2 = 0;
14387 if ((MulAmt % 9) == 0) {
14388 MulAmt1 = 9;
14389 MulAmt2 = MulAmt / 9;
14390 } else if ((MulAmt % 5) == 0) {
14391 MulAmt1 = 5;
14392 MulAmt2 = MulAmt / 5;
14393 } else if ((MulAmt % 3) == 0) {
14394 MulAmt1 = 3;
14395 MulAmt2 = MulAmt / 3;
14396 }
14397 if (MulAmt2 &&
14398 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
14399 DebugLoc DL = N->getDebugLoc();
14400
14401 if (isPowerOf2_64(MulAmt2) &&
14402 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
14403 // If second multiplifer is pow2, issue it first. We want the multiply by
14404 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
14405 // is an add.
14406 std::swap(MulAmt1, MulAmt2);
14407
14408 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000014409 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014410 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000014411 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000014412 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014413 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000014414 DAG.getConstant(MulAmt1, VT));
14415
Eric Christopherfd179292009-08-27 18:07:15 +000014416 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000014417 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000014418 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000014419 else
Evan Cheng73f24c92009-03-30 21:36:47 +000014420 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000014421 DAG.getConstant(MulAmt2, VT));
14422
14423 // Do not add new nodes to DAG combiner worklist.
14424 DCI.CombineTo(N, NewMul, false);
14425 }
14426 return SDValue();
14427}
14428
Evan Chengad9c0a32009-12-15 00:53:42 +000014429static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
14430 SDValue N0 = N->getOperand(0);
14431 SDValue N1 = N->getOperand(1);
14432 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
14433 EVT VT = N0.getValueType();
14434
14435 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
14436 // since the result of setcc_c is all zero's or all ones.
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014437 if (VT.isInteger() && !VT.isVector() &&
14438 N1C && N0.getOpcode() == ISD::AND &&
Evan Chengad9c0a32009-12-15 00:53:42 +000014439 N0.getOperand(1).getOpcode() == ISD::Constant) {
14440 SDValue N00 = N0.getOperand(0);
14441 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
14442 ((N00.getOpcode() == ISD::ANY_EXTEND ||
14443 N00.getOpcode() == ISD::ZERO_EXTEND) &&
14444 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
14445 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
14446 APInt ShAmt = N1C->getAPIntValue();
14447 Mask = Mask.shl(ShAmt);
14448 if (Mask != 0)
14449 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
14450 N00, DAG.getConstant(Mask, VT));
14451 }
14452 }
14453
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014454
14455 // Hardware support for vector shifts is sparse which makes us scalarize the
14456 // vector operations in many cases. Also, on sandybridge ADD is faster than
14457 // shl.
14458 // (shl V, 1) -> add V,V
14459 if (isSplatVector(N1.getNode())) {
14460 assert(N0.getValueType().isVector() && "Invalid vector shift type");
14461 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0));
14462 // We shift all of the values by one. In many cases we do not have
14463 // hardware support for this operation. This is better expressed as an ADD
14464 // of two values.
14465 if (N1C && (1 == N1C->getZExtValue())) {
14466 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N0);
14467 }
14468 }
14469
Evan Chengad9c0a32009-12-15 00:53:42 +000014470 return SDValue();
14471}
Evan Cheng0b0cd912009-03-28 05:57:29 +000014472
Nate Begeman740ab032009-01-26 00:52:55 +000014473/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
14474/// when possible.
14475static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
Mon P Wang845b1892012-02-01 22:15:20 +000014476 TargetLowering::DAGCombinerInfo &DCI,
Nate Begeman740ab032009-01-26 00:52:55 +000014477 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000014478 EVT VT = N->getValueType(0);
Nadav Rotemfb0dfbb2011-10-30 13:24:22 +000014479 if (N->getOpcode() == ISD::SHL) {
14480 SDValue V = PerformSHLCombine(N, DAG);
14481 if (V.getNode()) return V;
14482 }
Evan Chengad9c0a32009-12-15 00:53:42 +000014483
Nate Begeman740ab032009-01-26 00:52:55 +000014484 // On X86 with SSE2 support, we can transform this to a vector shift if
14485 // all elements are shifted by the same amount. We can't do this in legalize
14486 // because the a constant vector is typically transformed to a constant pool
14487 // so we have no knowledge of the shift amount.
Craig Topper1accb7e2012-01-10 06:54:16 +000014488 if (!Subtarget->hasSSE2())
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014489 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014490
Craig Topper7be5dfd2011-11-12 09:58:49 +000014491 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
14492 (!Subtarget->hasAVX2() ||
14493 (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014494 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000014495
Mon P Wang3becd092009-01-28 08:12:05 +000014496 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000014497 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000014498 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000014499 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000014500 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
14501 unsigned NumElts = VT.getVectorNumElements();
14502 unsigned i = 0;
14503 for (; i != NumElts; ++i) {
14504 SDValue Arg = ShAmtOp.getOperand(i);
14505 if (Arg.getOpcode() == ISD::UNDEF) continue;
14506 BaseShAmt = Arg;
14507 break;
14508 }
Craig Topper37c26772012-01-17 04:44:50 +000014509 // Handle the case where the build_vector is all undef
14510 // FIXME: Should DAG allow this?
14511 if (i == NumElts)
14512 return SDValue();
14513
Mon P Wang3becd092009-01-28 08:12:05 +000014514 for (; i != NumElts; ++i) {
14515 SDValue Arg = ShAmtOp.getOperand(i);
14516 if (Arg.getOpcode() == ISD::UNDEF) continue;
14517 if (Arg != BaseShAmt) {
14518 return SDValue();
14519 }
14520 }
14521 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000014522 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000014523 SDValue InVec = ShAmtOp.getOperand(0);
14524 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
14525 unsigned NumElts = InVec.getValueType().getVectorNumElements();
14526 unsigned i = 0;
14527 for (; i != NumElts; ++i) {
14528 SDValue Arg = InVec.getOperand(i);
14529 if (Arg.getOpcode() == ISD::UNDEF) continue;
14530 BaseShAmt = Arg;
14531 break;
14532 }
14533 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
14534 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000014535 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000014536 if (C->getZExtValue() == SplatIdx)
14537 BaseShAmt = InVec.getOperand(1);
14538 }
14539 }
Mon P Wang845b1892012-02-01 22:15:20 +000014540 if (BaseShAmt.getNode() == 0) {
14541 // Don't create instructions with illegal types after legalize
14542 // types has run.
14543 if (!DAG.getTargetLoweringInfo().isTypeLegal(EltVT) &&
14544 !DCI.isBeforeLegalize())
14545 return SDValue();
14546
Mon P Wangefa42202009-09-03 19:56:25 +000014547 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
14548 DAG.getIntPtrConstant(0));
Mon P Wang845b1892012-02-01 22:15:20 +000014549 }
Mon P Wang3becd092009-01-28 08:12:05 +000014550 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014551 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000014552
Mon P Wangefa42202009-09-03 19:56:25 +000014553 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000014554 if (EltVT.bitsGT(MVT::i32))
14555 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
14556 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000014557 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000014558
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014559 // The shift amount is identical so we can do a vector shift.
14560 SDValue ValOp = N->getOperand(0);
14561 switch (N->getOpcode()) {
14562 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000014563 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014564 case ISD::SHL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014565 switch (VT.getSimpleVT().SimpleTy) {
14566 default: return SDValue();
14567 case MVT::v2i64:
14568 case MVT::v4i32:
14569 case MVT::v8i16:
14570 case MVT::v4i64:
14571 case MVT::v8i32:
14572 case MVT::v16i16:
14573 return getTargetVShiftNode(X86ISD::VSHLI, DL, VT, ValOp, BaseShAmt, DAG);
14574 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014575 case ISD::SRA:
Craig Toppered2e13d2012-01-22 19:15:14 +000014576 switch (VT.getSimpleVT().SimpleTy) {
14577 default: return SDValue();
14578 case MVT::v4i32:
14579 case MVT::v8i16:
14580 case MVT::v8i32:
14581 case MVT::v16i16:
14582 return getTargetVShiftNode(X86ISD::VSRAI, DL, VT, ValOp, BaseShAmt, DAG);
14583 }
Nate Begemanc2fd67f2009-01-26 03:15:31 +000014584 case ISD::SRL:
Craig Toppered2e13d2012-01-22 19:15:14 +000014585 switch (VT.getSimpleVT().SimpleTy) {
14586 default: return SDValue();
14587 case MVT::v2i64:
14588 case MVT::v4i32:
14589 case MVT::v8i16:
14590 case MVT::v4i64:
14591 case MVT::v8i32:
14592 case MVT::v16i16:
14593 return getTargetVShiftNode(X86ISD::VSRLI, DL, VT, ValOp, BaseShAmt, DAG);
14594 }
Nate Begeman740ab032009-01-26 00:52:55 +000014595 }
Nate Begeman740ab032009-01-26 00:52:55 +000014596}
14597
Nate Begemanb65c1752010-12-17 22:55:37 +000014598
Stuart Hastings865f0932011-06-03 23:53:54 +000014599// CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..))
14600// where both setccs reference the same FP CMP, and rewrite for CMPEQSS
14601// and friends. Likewise for OR -> CMPNEQSS.
14602static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
14603 TargetLowering::DAGCombinerInfo &DCI,
14604 const X86Subtarget *Subtarget) {
14605 unsigned opcode;
14606
14607 // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
14608 // we're requiring SSE2 for both.
Craig Topper1accb7e2012-01-10 06:54:16 +000014609 if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
Stuart Hastings865f0932011-06-03 23:53:54 +000014610 SDValue N0 = N->getOperand(0);
14611 SDValue N1 = N->getOperand(1);
14612 SDValue CMP0 = N0->getOperand(1);
14613 SDValue CMP1 = N1->getOperand(1);
14614 DebugLoc DL = N->getDebugLoc();
14615
14616 // The SETCCs should both refer to the same CMP.
14617 if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
14618 return SDValue();
14619
14620 SDValue CMP00 = CMP0->getOperand(0);
14621 SDValue CMP01 = CMP0->getOperand(1);
14622 EVT VT = CMP00.getValueType();
14623
14624 if (VT == MVT::f32 || VT == MVT::f64) {
14625 bool ExpectingFlags = false;
14626 // Check for any users that want flags:
14627 for (SDNode::use_iterator UI = N->use_begin(),
14628 UE = N->use_end();
14629 !ExpectingFlags && UI != UE; ++UI)
14630 switch (UI->getOpcode()) {
14631 default:
14632 case ISD::BR_CC:
14633 case ISD::BRCOND:
14634 case ISD::SELECT:
14635 ExpectingFlags = true;
14636 break;
14637 case ISD::CopyToReg:
14638 case ISD::SIGN_EXTEND:
14639 case ISD::ZERO_EXTEND:
14640 case ISD::ANY_EXTEND:
14641 break;
14642 }
14643
14644 if (!ExpectingFlags) {
14645 enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
14646 enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
14647
14648 if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
14649 X86::CondCode tmp = cc0;
14650 cc0 = cc1;
14651 cc1 = tmp;
14652 }
14653
14654 if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) ||
14655 (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
14656 bool is64BitFP = (CMP00.getValueType() == MVT::f64);
14657 X86ISD::NodeType NTOperator = is64BitFP ?
14658 X86ISD::FSETCCsd : X86ISD::FSETCCss;
14659 // FIXME: need symbolic constants for these magic numbers.
14660 // See X86ATTInstPrinter.cpp:printSSECC().
14661 unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
14662 SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01,
14663 DAG.getConstant(x86cc, MVT::i8));
14664 SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32,
14665 OnesOrZeroesF);
14666 SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI,
14667 DAG.getConstant(1, MVT::i32));
14668 SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed);
14669 return OneBitOfTruth;
14670 }
14671 }
14672 }
14673 }
14674 return SDValue();
14675}
14676
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014677/// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
14678/// so it can be folded inside ANDNP.
14679static bool CanFoldXORWithAllOnes(const SDNode *N) {
14680 EVT VT = N->getValueType(0);
14681
14682 // Match direct AllOnes for 128 and 256-bit vectors
14683 if (ISD::isBuildVectorAllOnes(N))
14684 return true;
14685
14686 // Look through a bit convert.
14687 if (N->getOpcode() == ISD::BITCAST)
14688 N = N->getOperand(0).getNode();
14689
14690 // Sometimes the operand may come from a insert_subvector building a 256-bit
14691 // allones vector
Craig Topper7a9a28b2012-08-12 02:23:29 +000014692 if (VT.is256BitVector() &&
Bill Wendling456a9252011-08-04 00:32:58 +000014693 N->getOpcode() == ISD::INSERT_SUBVECTOR) {
14694 SDValue V1 = N->getOperand(0);
14695 SDValue V2 = N->getOperand(1);
14696
14697 if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
14698 V1.getOperand(0).getOpcode() == ISD::UNDEF &&
14699 ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
14700 ISD::isBuildVectorAllOnes(V2.getNode()))
14701 return true;
14702 }
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014703
14704 return false;
14705}
14706
Nate Begemanb65c1752010-12-17 22:55:37 +000014707static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
14708 TargetLowering::DAGCombinerInfo &DCI,
14709 const X86Subtarget *Subtarget) {
14710 if (DCI.isBeforeLegalizeOps())
14711 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014712
Stuart Hastings865f0932011-06-03 23:53:54 +000014713 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14714 if (R.getNode())
14715 return R;
14716
Craig Topper54a11172011-10-14 07:06:56 +000014717 EVT VT = N->getValueType(0);
14718
Craig Topperb4c94572011-10-21 06:55:01 +000014719 // Create ANDN, BLSI, and BLSR instructions
14720 // BLSI is X & (-X)
14721 // BLSR is X & (X-1)
Craig Topper54a11172011-10-14 07:06:56 +000014722 if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) {
14723 SDValue N0 = N->getOperand(0);
14724 SDValue N1 = N->getOperand(1);
14725 DebugLoc DL = N->getDebugLoc();
14726
14727 // Check LHS for not
14728 if (N0.getOpcode() == ISD::XOR && isAllOnes(N0.getOperand(1)))
14729 return DAG.getNode(X86ISD::ANDN, DL, VT, N0.getOperand(0), N1);
14730 // Check RHS for not
14731 if (N1.getOpcode() == ISD::XOR && isAllOnes(N1.getOperand(1)))
14732 return DAG.getNode(X86ISD::ANDN, DL, VT, N1.getOperand(0), N0);
14733
Craig Topperb4c94572011-10-21 06:55:01 +000014734 // Check LHS for neg
14735 if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 &&
14736 isZero(N0.getOperand(0)))
14737 return DAG.getNode(X86ISD::BLSI, DL, VT, N1);
14738
14739 // Check RHS for neg
14740 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 &&
14741 isZero(N1.getOperand(0)))
14742 return DAG.getNode(X86ISD::BLSI, DL, VT, N0);
14743
14744 // Check LHS for X-1
14745 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14746 isAllOnes(N0.getOperand(1)))
14747 return DAG.getNode(X86ISD::BLSR, DL, VT, N1);
14748
14749 // Check RHS for X-1
14750 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14751 isAllOnes(N1.getOperand(1)))
14752 return DAG.getNode(X86ISD::BLSR, DL, VT, N0);
14753
Craig Topper54a11172011-10-14 07:06:56 +000014754 return SDValue();
14755 }
14756
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014757 // Want to form ANDNP nodes:
14758 // 1) In the hopes of then easily combining them with OR and AND nodes
14759 // to form PBLEND/PSIGN.
14760 // 2) To match ANDN packed intrinsics
Bruno Cardoso Lopes466b0222011-07-13 21:36:51 +000014761 if (VT != MVT::v2i64 && VT != MVT::v4i64)
Nate Begemanb65c1752010-12-17 22:55:37 +000014762 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014763
Nate Begemanb65c1752010-12-17 22:55:37 +000014764 SDValue N0 = N->getOperand(0);
14765 SDValue N1 = N->getOperand(1);
14766 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014767
Nate Begemanb65c1752010-12-17 22:55:37 +000014768 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014769 if (N0.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014770 //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
14771 CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014772 return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
Nate Begemanb65c1752010-12-17 22:55:37 +000014773
14774 // Check RHS for vnot
14775 if (N1.getOpcode() == ISD::XOR &&
Bruno Cardoso Lopes863bd9d2011-07-25 23:05:32 +000014776 //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
14777 CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
Bruno Cardoso Lopesc1af4772011-07-13 21:36:47 +000014778 return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014779
Nate Begemanb65c1752010-12-17 22:55:37 +000014780 return SDValue();
14781}
14782
Evan Cheng760d1942010-01-04 21:22:48 +000014783static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000014784 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000014785 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000014786 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000014787 return SDValue();
14788
Stuart Hastings865f0932011-06-03 23:53:54 +000014789 SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget);
14790 if (R.getNode())
14791 return R;
14792
Evan Cheng760d1942010-01-04 21:22:48 +000014793 EVT VT = N->getValueType(0);
Evan Cheng760d1942010-01-04 21:22:48 +000014794
Evan Cheng760d1942010-01-04 21:22:48 +000014795 SDValue N0 = N->getOperand(0);
14796 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014797
Nate Begemanb65c1752010-12-17 22:55:37 +000014798 // look for psign/blend
Craig Topper1666cb62011-11-19 07:07:26 +000014799 if (VT == MVT::v2i64 || VT == MVT::v4i64) {
Craig Topperd0a31172012-01-10 06:37:29 +000014800 if (!Subtarget->hasSSSE3() ||
Craig Topper1666cb62011-11-19 07:07:26 +000014801 (VT == MVT::v4i64 && !Subtarget->hasAVX2()))
14802 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014803
Craig Topper1666cb62011-11-19 07:07:26 +000014804 // Canonicalize pandn to RHS
14805 if (N0.getOpcode() == X86ISD::ANDNP)
14806 std::swap(N0, N1);
Lang Hames9ffaa6a2012-01-10 22:53:20 +000014807 // or (and (m, y), (pandn m, x))
Craig Topper1666cb62011-11-19 07:07:26 +000014808 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
14809 SDValue Mask = N1.getOperand(0);
14810 SDValue X = N1.getOperand(1);
14811 SDValue Y;
14812 if (N0.getOperand(0) == Mask)
14813 Y = N0.getOperand(1);
14814 if (N0.getOperand(1) == Mask)
14815 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014816
Craig Topper1666cb62011-11-19 07:07:26 +000014817 // Check to see if the mask appeared in both the AND and ANDNP and
14818 if (!Y.getNode())
14819 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014820
Craig Topper1666cb62011-11-19 07:07:26 +000014821 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
Craig Topper1666cb62011-11-19 07:07:26 +000014822 // Look through mask bitcast.
Nadav Rotem4ac90812012-04-01 19:31:22 +000014823 if (Mask.getOpcode() == ISD::BITCAST)
14824 Mask = Mask.getOperand(0);
14825 if (X.getOpcode() == ISD::BITCAST)
14826 X = X.getOperand(0);
14827 if (Y.getOpcode() == ISD::BITCAST)
14828 Y = Y.getOperand(0);
14829
Craig Topper1666cb62011-11-19 07:07:26 +000014830 EVT MaskVT = Mask.getValueType();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014831
Craig Toppered2e13d2012-01-22 19:15:14 +000014832 // Validate that the Mask operand is a vector sra node.
Craig Topper1666cb62011-11-19 07:07:26 +000014833 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
14834 // there is no psrai.b
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014835 if (Mask.getOpcode() != X86ISD::VSRAI)
Craig Toppered2e13d2012-01-22 19:15:14 +000014836 return SDValue();
Craig Topper1666cb62011-11-19 07:07:26 +000014837
14838 // Check that the SRA is all signbits.
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014839 SDValue SraC = Mask.getOperand(1);
Craig Topper1666cb62011-11-19 07:07:26 +000014840 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
14841 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
14842 if ((SraAmt + 1) != EltBits)
14843 return SDValue();
14844
14845 DebugLoc DL = N->getDebugLoc();
14846
14847 // Now we know we at least have a plendvb with the mask val. See if
14848 // we can form a psignb/w/d.
14849 // psign = x.type == y.type == mask.type && y = sub(0, x);
Craig Topper1666cb62011-11-19 07:07:26 +000014850 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
14851 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
Craig Toppered2e13d2012-01-22 19:15:14 +000014852 X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
14853 assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
14854 "Unsupported VT for PSIGN");
Craig Topper7fb8b0c2012-01-23 06:46:22 +000014855 Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
Craig Toppered2e13d2012-01-22 19:15:14 +000014856 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Craig Topper1666cb62011-11-19 07:07:26 +000014857 }
14858 // PBLENDVB only available on SSE 4.1
Craig Topperd0a31172012-01-10 06:37:29 +000014859 if (!Subtarget->hasSSE41())
Craig Topper1666cb62011-11-19 07:07:26 +000014860 return SDValue();
14861
14862 EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
14863
14864 X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X);
14865 Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y);
14866 Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask);
Nadav Rotem18197d72011-11-30 10:13:37 +000014867 Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
Craig Topper1666cb62011-11-19 07:07:26 +000014868 return DAG.getNode(ISD::BITCAST, DL, VT, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000014869 }
14870 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014871
Craig Topper1666cb62011-11-19 07:07:26 +000014872 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
14873 return SDValue();
14874
Nate Begemanb65c1752010-12-17 22:55:37 +000014875 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000014876 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
14877 std::swap(N0, N1);
14878 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
14879 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000014880 if (!N0.hasOneUse() || !N1.hasOneUse())
14881 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000014882
14883 SDValue ShAmt0 = N0.getOperand(1);
14884 if (ShAmt0.getValueType() != MVT::i8)
14885 return SDValue();
14886 SDValue ShAmt1 = N1.getOperand(1);
14887 if (ShAmt1.getValueType() != MVT::i8)
14888 return SDValue();
14889 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
14890 ShAmt0 = ShAmt0.getOperand(0);
14891 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
14892 ShAmt1 = ShAmt1.getOperand(0);
14893
14894 DebugLoc DL = N->getDebugLoc();
14895 unsigned Opc = X86ISD::SHLD;
14896 SDValue Op0 = N0.getOperand(0);
14897 SDValue Op1 = N1.getOperand(0);
14898 if (ShAmt0.getOpcode() == ISD::SUB) {
14899 Opc = X86ISD::SHRD;
14900 std::swap(Op0, Op1);
14901 std::swap(ShAmt0, ShAmt1);
14902 }
14903
Evan Cheng8b1190a2010-04-28 01:18:01 +000014904 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000014905 if (ShAmt1.getOpcode() == ISD::SUB) {
14906 SDValue Sum = ShAmt1.getOperand(0);
14907 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000014908 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
14909 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
14910 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
14911 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000014912 return DAG.getNode(Opc, DL, VT,
14913 Op0, Op1,
14914 DAG.getNode(ISD::TRUNCATE, DL,
14915 MVT::i8, ShAmt0));
14916 }
14917 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
14918 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
14919 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000014920 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000014921 return DAG.getNode(Opc, DL, VT,
14922 N0.getOperand(0), N1.getOperand(0),
14923 DAG.getNode(ISD::TRUNCATE, DL,
14924 MVT::i8, ShAmt0));
14925 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000014926
Evan Cheng760d1942010-01-04 21:22:48 +000014927 return SDValue();
14928}
14929
Manman Ren92363622012-06-07 22:39:10 +000014930// Generate NEG and CMOV for integer abs.
14931static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
14932 EVT VT = N->getValueType(0);
14933
14934 // Since X86 does not have CMOV for 8-bit integer, we don't convert
14935 // 8-bit integer abs to NEG and CMOV.
14936 if (VT.isInteger() && VT.getSizeInBits() == 8)
14937 return SDValue();
14938
14939 SDValue N0 = N->getOperand(0);
14940 SDValue N1 = N->getOperand(1);
14941 DebugLoc DL = N->getDebugLoc();
14942
14943 // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
14944 // and change it to SUB and CMOV.
14945 if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
14946 N0.getOpcode() == ISD::ADD &&
14947 N0.getOperand(1) == N1 &&
14948 N1.getOpcode() == ISD::SRA &&
14949 N1.getOperand(0) == N0.getOperand(0))
14950 if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
14951 if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
14952 // Generate SUB & CMOV.
14953 SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
14954 DAG.getConstant(0, VT), N0.getOperand(0));
14955
14956 SDValue Ops[] = { N0.getOperand(0), Neg,
14957 DAG.getConstant(X86::COND_GE, MVT::i8),
14958 SDValue(Neg.getNode(), 1) };
14959 return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue),
14960 Ops, array_lengthof(Ops));
14961 }
14962 return SDValue();
14963}
14964
Craig Topper3738ccd2011-12-27 06:27:23 +000014965// PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes
Craig Topperb4c94572011-10-21 06:55:01 +000014966static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
14967 TargetLowering::DAGCombinerInfo &DCI,
14968 const X86Subtarget *Subtarget) {
14969 if (DCI.isBeforeLegalizeOps())
14970 return SDValue();
14971
Manman Ren45d53b82012-06-08 18:58:26 +000014972 if (Subtarget->hasCMov()) {
14973 SDValue RV = performIntegerAbsCombine(N, DAG);
14974 if (RV.getNode())
14975 return RV;
14976 }
Manman Ren92363622012-06-07 22:39:10 +000014977
14978 // Try forming BMI if it is available.
14979 if (!Subtarget->hasBMI())
14980 return SDValue();
14981
Craig Topperb4c94572011-10-21 06:55:01 +000014982 EVT VT = N->getValueType(0);
14983
14984 if (VT != MVT::i32 && VT != MVT::i64)
14985 return SDValue();
14986
Craig Topper3738ccd2011-12-27 06:27:23 +000014987 assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions");
14988
Craig Topperb4c94572011-10-21 06:55:01 +000014989 // Create BLSMSK instructions by finding X ^ (X-1)
14990 SDValue N0 = N->getOperand(0);
14991 SDValue N1 = N->getOperand(1);
14992 DebugLoc DL = N->getDebugLoc();
14993
14994 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 &&
14995 isAllOnes(N0.getOperand(1)))
14996 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1);
14997
14998 if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 &&
14999 isAllOnes(N1.getOperand(1)))
15000 return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0);
15001
15002 return SDValue();
15003}
15004
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015005/// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
15006static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015007 TargetLowering::DAGCombinerInfo &DCI,
15008 const X86Subtarget *Subtarget) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015009 LoadSDNode *Ld = cast<LoadSDNode>(N);
15010 EVT RegVT = Ld->getValueType(0);
15011 EVT MemVT = Ld->getMemoryVT();
15012 DebugLoc dl = Ld->getDebugLoc();
15013 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15014
15015 ISD::LoadExtType Ext = Ld->getExtensionType();
15016
Nadav Rotemca6f2962011-09-18 19:00:23 +000015017 // If this is a vector EXT Load then attempt to optimize it using a
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015018 // shuffle. We need SSE4 for the shuffles.
15019 // TODO: It is possible to support ZExt by zeroing the undef values
15020 // during the shuffle phase or after the shuffle.
Eli Friedmanda813f42011-12-28 21:24:44 +000015021 if (RegVT.isVector() && RegVT.isInteger() &&
15022 Ext == ISD::EXTLOAD && Subtarget->hasSSE41()) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015023 assert(MemVT != RegVT && "Cannot extend to the same type");
15024 assert(MemVT.isVector() && "Must load a vector from memory");
15025
15026 unsigned NumElems = RegVT.getVectorNumElements();
15027 unsigned RegSz = RegVT.getSizeInBits();
15028 unsigned MemSz = MemVT.getSizeInBits();
15029 assert(RegSz > MemSz && "Register size must be greater than the mem size");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015030
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015031 // All sizes must be a power of two.
15032 if (!isPowerOf2_32(RegSz * MemSz * NumElems))
15033 return SDValue();
15034
15035 // Attempt to load the original value using scalar loads.
15036 // Find the largest scalar type that divides the total loaded size.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015037 MVT SclrLoadTy = MVT::i8;
15038 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15039 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15040 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015041 if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015042 SclrLoadTy = Tp;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015043 }
15044 }
15045
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015046 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15047 if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15048 (64 <= MemSz))
15049 SclrLoadTy = MVT::f64;
15050
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015051 // Calculate the number of scalar loads that we need to perform
15052 // in order to load our vector from memory.
15053 unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015054
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015055 // Represent our vector as a sequence of elements which are the
15056 // largest scalar that we can load.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015057 EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy,
15058 RegSz/SclrLoadTy.getSizeInBits());
15059
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015060 // Represent the data using the same element type that is stored in
15061 // memory. In practice, we ''widen'' MemVT.
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015062 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15063 RegSz/MemVT.getScalarType().getSizeInBits());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015064
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015065 assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15066 "Invalid vector type");
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015067
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015068 // We can't shuffle using an illegal type.
15069 if (!TLI.isTypeLegal(WideVecVT))
15070 return SDValue();
15071
15072 SmallVector<SDValue, 8> Chains;
15073 SDValue Ptr = Ld->getBasePtr();
15074 SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8,
15075 TLI.getPointerTy());
15076 SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15077
15078 for (unsigned i = 0; i < NumLoads; ++i) {
15079 // Perform a single load.
15080 SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(),
15081 Ptr, Ld->getPointerInfo(),
15082 Ld->isVolatile(), Ld->isNonTemporal(),
15083 Ld->isInvariant(), Ld->getAlignment());
15084 Chains.push_back(ScalarLoad.getValue(1));
15085 // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15086 // another round of DAGCombining.
15087 if (i == 0)
15088 Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15089 else
15090 Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15091 ScalarLoad, DAG.getIntPtrConstant(i));
15092
15093 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15094 }
15095
15096 SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15097 Chains.size());
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015098
15099 // Bitcast the loaded value to a vector of the original element type, in
15100 // the size of the target vector type.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015101 SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015102 unsigned SizeRatio = RegSz/MemSz;
15103
15104 // Redistribute the loaded elements into the different locations.
15105 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015106 for (unsigned i = 0; i != NumElems; ++i)
15107 ShuffleVec[i*SizeRatio] = i;
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015108
15109 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015110 DAG.getUNDEF(WideVecVT),
15111 &ShuffleVec[0]);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015112
15113 // Bitcast to the requested type.
15114 Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff);
15115 // Replace the original load with the new sequence
15116 // and return the new chain.
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015117 return DCI.CombineTo(N, Shuff, TF, true);
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015118 }
15119
15120 return SDValue();
15121}
15122
Chris Lattner149a4e52008-02-22 02:09:43 +000015123/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015124static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000015125 const X86Subtarget *Subtarget) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015126 StoreSDNode *St = cast<StoreSDNode>(N);
15127 EVT VT = St->getValue().getValueType();
15128 EVT StVT = St->getMemoryVT();
15129 DebugLoc dl = St->getDebugLoc();
Nadav Rotem5e742a32011-08-11 16:41:21 +000015130 SDValue StoredVal = St->getOperand(1);
15131 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15132
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015133 // If we are saving a concatenation of two XMM registers, perform two stores.
Nadav Rotem87d35e82012-05-19 20:30:08 +000015134 // On Sandy Bridge, 256-bit memory operations are executed by two
15135 // 128-bit ports. However, on Haswell it is better to issue a single 256-bit
15136 // memory operation.
Craig Topper7a9a28b2012-08-12 02:23:29 +000015137 if (VT.is256BitVector() && !Subtarget->hasAVX2() &&
Craig Topperb4a8aef2012-04-27 21:05:09 +000015138 StoredVal.getNode()->getOpcode() == ISD::CONCAT_VECTORS &&
15139 StoredVal.getNumOperands() == 2) {
Nadav Rotem5e742a32011-08-11 16:41:21 +000015140 SDValue Value0 = StoredVal.getOperand(0);
15141 SDValue Value1 = StoredVal.getOperand(1);
15142
15143 SDValue Stride = DAG.getConstant(16, TLI.getPointerTy());
15144 SDValue Ptr0 = St->getBasePtr();
15145 SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
15146
15147 SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
15148 St->getPointerInfo(), St->isVolatile(),
15149 St->isNonTemporal(), St->getAlignment());
15150 SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
15151 St->getPointerInfo(), St->isVolatile(),
15152 St->isNonTemporal(), St->getAlignment());
15153 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
15154 }
Nadav Rotem614061b2011-08-10 19:30:14 +000015155
15156 // Optimize trunc store (of multiple scalars) to shuffle and store.
15157 // First, pack all of the elements in one place. Next, store to memory
15158 // in fewer chunks.
15159 if (St->isTruncatingStore() && VT.isVector()) {
15160 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15161 unsigned NumElems = VT.getVectorNumElements();
15162 assert(StVT != VT && "Cannot truncate to the same type");
15163 unsigned FromSz = VT.getVectorElementType().getSizeInBits();
15164 unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
15165
15166 // From, To sizes and ElemCount must be pow of two
15167 if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015168 // We are going to use the original vector elt for storing.
Nadav Rotem64ac73b2011-09-21 17:14:40 +000015169 // Accumulated smaller vector elements must be a multiple of the store size.
Nadav Rotem9c6cdf42011-09-21 08:45:10 +000015170 if (0 != (NumElems * FromSz) % ToSz) return SDValue();
Nadav Rotem91e43fd2011-09-18 10:39:32 +000015171
Nadav Rotem614061b2011-08-10 19:30:14 +000015172 unsigned SizeRatio = FromSz / ToSz;
15173
15174 assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
15175
15176 // Create a type on which we perform the shuffle
15177 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
15178 StVT.getScalarType(), NumElems*SizeRatio);
15179
15180 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
15181
15182 SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue());
15183 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Craig Topper31a207a2012-05-04 06:39:13 +000015184 for (unsigned i = 0; i != NumElems; ++i)
15185 ShuffleVec[i] = i * SizeRatio;
Nadav Rotem614061b2011-08-10 19:30:14 +000015186
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000015187 // Can't shuffle using an illegal type.
15188 if (!TLI.isTypeLegal(WideVecVT))
15189 return SDValue();
Nadav Rotem614061b2011-08-10 19:30:14 +000015190
15191 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
Craig Topperdf966f62012-04-22 19:17:57 +000015192 DAG.getUNDEF(WideVecVT),
15193 &ShuffleVec[0]);
Nadav Rotem614061b2011-08-10 19:30:14 +000015194 // At this point all of the data is stored at the bottom of the
15195 // register. We now need to save it to mem.
15196
15197 // Find the largest store unit
15198 MVT StoreType = MVT::i8;
15199 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
15200 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
15201 MVT Tp = (MVT::SimpleValueType)tp;
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015202 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
Nadav Rotem614061b2011-08-10 19:30:14 +000015203 StoreType = Tp;
15204 }
15205
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015206 // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15207 if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
15208 (64 <= NumElems * ToSz))
15209 StoreType = MVT::f64;
15210
Nadav Rotem614061b2011-08-10 19:30:14 +000015211 // Bitcast the original vector into a vector of store-size units
15212 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
Nadav Rotem5cd95e12012-07-11 13:27:05 +000015213 StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
Nadav Rotem614061b2011-08-10 19:30:14 +000015214 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
15215 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff);
15216 SmallVector<SDValue, 8> Chains;
15217 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
15218 TLI.getPointerTy());
15219 SDValue Ptr = St->getBasePtr();
15220
15221 // Perform one or more big stores into memory.
Craig Topper31a207a2012-05-04 06:39:13 +000015222 for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
Nadav Rotem614061b2011-08-10 19:30:14 +000015223 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
15224 StoreType, ShuffWide,
15225 DAG.getIntPtrConstant(i));
15226 SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
15227 St->getPointerInfo(), St->isVolatile(),
15228 St->isNonTemporal(), St->getAlignment());
15229 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15230 Chains.push_back(Ch);
15231 }
15232
15233 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0],
15234 Chains.size());
15235 }
15236
15237
Chris Lattner149a4e52008-02-22 02:09:43 +000015238 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
15239 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000015240 // A preferable solution to the general problem is to figure out the right
15241 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000015242
15243 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng536e6672009-03-12 05:59:15 +000015244 if (VT.getSizeInBits() != 64)
15245 return SDValue();
15246
Devang Patel578efa92009-06-05 21:57:13 +000015247 const Function *F = DAG.getMachineFunction().getFunction();
15248 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Nick Lewycky8a8d4792011-12-02 22:16:29 +000015249 bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps
Craig Topper1accb7e2012-01-10 06:54:16 +000015250 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000015251 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000015252 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000015253 isa<LoadSDNode>(St->getValue()) &&
15254 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
15255 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015256 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015257 LoadSDNode *Ld = 0;
15258 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000015259 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000015260 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015261 // Must be a store of a load. We currently handle two cases: the load
15262 // is a direct child, and it's under an intervening TokenFactor. It is
15263 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000015264 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000015265 Ld = cast<LoadSDNode>(St->getChain());
15266 else if (St->getValue().hasOneUse() &&
15267 ChainVal->getOpcode() == ISD::TokenFactor) {
Chad Rosierc2348d52012-02-01 18:45:51 +000015268 for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000015269 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000015270 TokenFactorIndex = i;
15271 Ld = cast<LoadSDNode>(St->getValue());
15272 } else
15273 Ops.push_back(ChainVal->getOperand(i));
15274 }
15275 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000015276
Evan Cheng536e6672009-03-12 05:59:15 +000015277 if (!Ld || !ISD::isNormalLoad(Ld))
15278 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015279
Evan Cheng536e6672009-03-12 05:59:15 +000015280 // If this is not the MMX case, i.e. we are just turning i64 load/store
15281 // into f64 load/store, avoid the transformation if there are multiple
15282 // uses of the loaded value.
15283 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
15284 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000015285
Evan Cheng536e6672009-03-12 05:59:15 +000015286 DebugLoc LdDL = Ld->getDebugLoc();
15287 DebugLoc StDL = N->getDebugLoc();
15288 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
15289 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
15290 // pair instead.
15291 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000015292 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000015293 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
15294 Ld->getPointerInfo(), Ld->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015295 Ld->isNonTemporal(), Ld->isInvariant(),
15296 Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015297 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000015298 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000015299 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000015300 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000015301 Ops.size());
15302 }
Evan Cheng536e6672009-03-12 05:59:15 +000015303 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000015304 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015305 St->isVolatile(), St->isNonTemporal(),
15306 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000015307 }
Evan Cheng536e6672009-03-12 05:59:15 +000015308
15309 // Otherwise, lower to two pairs of 32-bit loads / stores.
15310 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015311 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
15312 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015313
Owen Anderson825b72b2009-08-11 20:47:22 +000015314 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015315 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015316 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015317 Ld->isInvariant(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000015318 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000015319 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000015320 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000015321 Ld->isInvariant(),
Evan Cheng536e6672009-03-12 05:59:15 +000015322 MinAlign(Ld->getAlignment(), 4));
15323
15324 SDValue NewChain = LoLd.getValue(1);
15325 if (TokenFactorIndex != -1) {
15326 Ops.push_back(LoLd);
15327 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000015328 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000015329 Ops.size());
15330 }
15331
15332 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000015333 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
15334 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000015335
15336 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015337 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000015338 St->isVolatile(), St->isNonTemporal(),
15339 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000015340 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000015341 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000015342 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000015343 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000015344 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000015345 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000015346 }
Dan Gohman475871a2008-07-27 21:46:04 +000015347 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000015348}
15349
Duncan Sands17470be2011-09-22 20:15:48 +000015350/// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal"
15351/// and return the operands for the horizontal operation in LHS and RHS. A
15352/// horizontal operation performs the binary operation on successive elements
15353/// of its first operand, then on successive elements of its second operand,
15354/// returning the resulting values in a vector. For example, if
15355/// A = < float a0, float a1, float a2, float a3 >
15356/// and
15357/// B = < float b0, float b1, float b2, float b3 >
15358/// then the result of doing a horizontal operation on A and B is
15359/// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
15360/// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
15361/// A horizontal-op B, for some already available A and B, and if so then LHS is
15362/// set to A, RHS to B, and the routine returns 'true'.
15363/// Note that the binary operation should have the property that if one of the
15364/// operands is UNDEF then the result is UNDEF.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015365static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
Duncan Sands17470be2011-09-22 20:15:48 +000015366 // Look for the following pattern: if
15367 // A = < float a0, float a1, float a2, float a3 >
15368 // B = < float b0, float b1, float b2, float b3 >
15369 // and
15370 // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
15371 // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
15372 // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
15373 // which is A horizontal-op B.
15374
15375 // At least one of the operands should be a vector shuffle.
15376 if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
15377 RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
15378 return false;
15379
15380 EVT VT = LHS.getValueType();
Craig Topperf8363302011-12-02 08:18:41 +000015381
15382 assert((VT.is128BitVector() || VT.is256BitVector()) &&
15383 "Unsupported vector type for horizontal add/sub");
15384
15385 // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
15386 // operate independently on 128-bit lanes.
Craig Topperb72039c2011-11-30 09:10:50 +000015387 unsigned NumElts = VT.getVectorNumElements();
15388 unsigned NumLanes = VT.getSizeInBits()/128;
15389 unsigned NumLaneElts = NumElts / NumLanes;
Craig Topperf8363302011-12-02 08:18:41 +000015390 assert((NumLaneElts % 2 == 0) &&
15391 "Vector type should have an even number of elements in each lane");
15392 unsigned HalfLaneElts = NumLaneElts/2;
Duncan Sands17470be2011-09-22 20:15:48 +000015393
15394 // View LHS in the form
15395 // LHS = VECTOR_SHUFFLE A, B, LMask
15396 // If LHS is not a shuffle then pretend it is the shuffle
15397 // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
15398 // NOTE: in what follows a default initialized SDValue represents an UNDEF of
15399 // type VT.
15400 SDValue A, B;
Craig Topperb72039c2011-11-30 09:10:50 +000015401 SmallVector<int, 16> LMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015402 if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15403 if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
15404 A = LHS.getOperand(0);
15405 if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
15406 B = LHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015407 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
15408 std::copy(Mask.begin(), Mask.end(), LMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015409 } else {
15410 if (LHS.getOpcode() != ISD::UNDEF)
15411 A = LHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015412 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015413 LMask[i] = i;
15414 }
15415
15416 // Likewise, view RHS in the form
15417 // RHS = VECTOR_SHUFFLE C, D, RMask
15418 SDValue C, D;
Craig Topperb72039c2011-11-30 09:10:50 +000015419 SmallVector<int, 16> RMask(NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015420 if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
15421 if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
15422 C = RHS.getOperand(0);
15423 if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
15424 D = RHS.getOperand(1);
Benjamin Kramered4c8c62012-01-15 13:16:05 +000015425 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
15426 std::copy(Mask.begin(), Mask.end(), RMask.begin());
Duncan Sands17470be2011-09-22 20:15:48 +000015427 } else {
15428 if (RHS.getOpcode() != ISD::UNDEF)
15429 C = RHS;
Craig Topperb72039c2011-11-30 09:10:50 +000015430 for (unsigned i = 0; i != NumElts; ++i)
Duncan Sands17470be2011-09-22 20:15:48 +000015431 RMask[i] = i;
15432 }
15433
15434 // Check that the shuffles are both shuffling the same vectors.
15435 if (!(A == C && B == D) && !(A == D && B == C))
15436 return false;
15437
15438 // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
15439 if (!A.getNode() && !B.getNode())
15440 return false;
15441
15442 // If A and B occur in reverse order in RHS, then "swap" them (which means
15443 // rewriting the mask).
15444 if (A != C)
Craig Topperbeabc6c2011-12-05 06:56:46 +000015445 CommuteVectorShuffleMask(RMask, NumElts);
Duncan Sands17470be2011-09-22 20:15:48 +000015446
15447 // At this point LHS and RHS are equivalent to
15448 // LHS = VECTOR_SHUFFLE A, B, LMask
15449 // RHS = VECTOR_SHUFFLE A, B, RMask
15450 // Check that the masks correspond to performing a horizontal operation.
Craig Topperf8363302011-12-02 08:18:41 +000015451 for (unsigned i = 0; i != NumElts; ++i) {
Craig Topperbeabc6c2011-12-05 06:56:46 +000015452 int LIdx = LMask[i], RIdx = RMask[i];
Duncan Sands17470be2011-09-22 20:15:48 +000015453
Craig Topperf8363302011-12-02 08:18:41 +000015454 // Ignore any UNDEF components.
Craig Topperbeabc6c2011-12-05 06:56:46 +000015455 if (LIdx < 0 || RIdx < 0 ||
15456 (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
15457 (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
Craig Topperf8363302011-12-02 08:18:41 +000015458 continue;
Duncan Sands17470be2011-09-22 20:15:48 +000015459
Craig Topperf8363302011-12-02 08:18:41 +000015460 // Check that successive elements are being operated on. If not, this is
15461 // not a horizontal operation.
15462 unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs
15463 unsigned LaneStart = (i/NumLaneElts) * NumLaneElts;
Craig Topperbeabc6c2011-12-05 06:56:46 +000015464 int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart;
Craig Topperf8363302011-12-02 08:18:41 +000015465 if (!(LIdx == Index && RIdx == Index + 1) &&
Craig Topperbeabc6c2011-12-05 06:56:46 +000015466 !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
Craig Topperf8363302011-12-02 08:18:41 +000015467 return false;
Duncan Sands17470be2011-09-22 20:15:48 +000015468 }
15469
15470 LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
15471 RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
15472 return true;
15473}
15474
15475/// PerformFADDCombine - Do target-specific dag combines on floating point adds.
15476static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
15477 const X86Subtarget *Subtarget) {
15478 EVT VT = N->getValueType(0);
15479 SDValue LHS = N->getOperand(0);
15480 SDValue RHS = N->getOperand(1);
15481
15482 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015483 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015484 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015485 isHorizontalBinOp(LHS, RHS, true))
15486 return DAG.getNode(X86ISD::FHADD, N->getDebugLoc(), VT, LHS, RHS);
15487 return SDValue();
15488}
15489
15490/// PerformFSUBCombine - Do target-specific dag combines on floating point subs.
15491static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
15492 const X86Subtarget *Subtarget) {
15493 EVT VT = N->getValueType(0);
15494 SDValue LHS = N->getOperand(0);
15495 SDValue RHS = N->getOperand(1);
15496
15497 // Try to synthesize horizontal subs from subs of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015498 if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
Craig Topper138a5c62011-12-02 07:16:01 +000015499 (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
Duncan Sands17470be2011-09-22 20:15:48 +000015500 isHorizontalBinOp(LHS, RHS, false))
15501 return DAG.getNode(X86ISD::FHSUB, N->getDebugLoc(), VT, LHS, RHS);
15502 return SDValue();
15503}
15504
Chris Lattner6cf73262008-01-25 06:14:17 +000015505/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
15506/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015507static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000015508 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
15509 // F[X]OR(0.0, x) -> x
15510 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000015511 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15512 if (C->getValueAPF().isPosZero())
15513 return N->getOperand(1);
15514 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15515 if (C->getValueAPF().isPosZero())
15516 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000015517 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015518}
15519
Nadav Rotemd60cb112012-08-19 13:06:16 +000015520/// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and
15521/// X86ISD::FMAX nodes.
15522static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
15523 assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
15524
15525 // Only perform optimizations if UnsafeMath is used.
15526 if (!DAG.getTarget().Options.UnsafeFPMath)
15527 return SDValue();
15528
15529 // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
Craig Topper8365e9b2012-09-01 06:33:50 +000015530 // into FMINC and FMAXC, which are Commutative operations.
Nadav Rotemd60cb112012-08-19 13:06:16 +000015531 unsigned NewOp = 0;
15532 switch (N->getOpcode()) {
15533 default: llvm_unreachable("unknown opcode");
15534 case X86ISD::FMIN: NewOp = X86ISD::FMINC; break;
15535 case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break;
15536 }
15537
15538 return DAG.getNode(NewOp, N->getDebugLoc(), N->getValueType(0),
15539 N->getOperand(0), N->getOperand(1));
15540}
15541
15542
Chris Lattneraf723b92008-01-25 05:46:26 +000015543/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000015544static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000015545 // FAND(0.0, x) -> 0.0
15546 // FAND(x, 0.0) -> 0.0
15547 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
15548 if (C->getValueAPF().isPosZero())
15549 return N->getOperand(0);
15550 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
15551 if (C->getValueAPF().isPosZero())
15552 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000015553 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000015554}
15555
Dan Gohmane5af2d32009-01-29 01:59:02 +000015556static SDValue PerformBTCombine(SDNode *N,
15557 SelectionDAG &DAG,
15558 TargetLowering::DAGCombinerInfo &DCI) {
15559 // BT ignores high bits in the bit index operand.
15560 SDValue Op1 = N->getOperand(1);
15561 if (Op1.hasOneUse()) {
15562 unsigned BitWidth = Op1.getValueSizeInBits();
15563 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
15564 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000015565 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
15566 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000015567 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000015568 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
15569 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
15570 DCI.CommitTargetLoweringOpt(TLO);
15571 }
15572 return SDValue();
15573}
Chris Lattner83e6c992006-10-04 06:57:07 +000015574
Eli Friedman7a5e5552009-06-07 06:52:44 +000015575static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
15576 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015577 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000015578 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000015579 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000015580 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000015581 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000015582 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000015583 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000015584 }
15585 return SDValue();
15586}
15587
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015588static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
15589 TargetLowering::DAGCombinerInfo &DCI,
15590 const X86Subtarget *Subtarget) {
15591 if (!DCI.isBeforeLegalizeOps())
15592 return SDValue();
15593
Craig Topper3ef43cf2012-04-24 06:36:35 +000015594 if (!Subtarget->hasAVX())
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015595 return SDValue();
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015596
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015597 EVT VT = N->getValueType(0);
15598 SDValue Op = N->getOperand(0);
15599 EVT OpVT = Op.getValueType();
15600 DebugLoc dl = N->getDebugLoc();
15601
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015602 if ((VT == MVT::v4i64 && OpVT == MVT::v4i32) ||
15603 (VT == MVT::v8i32 && OpVT == MVT::v8i16)) {
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015604
Craig Topper3ef43cf2012-04-24 06:36:35 +000015605 if (Subtarget->hasAVX2())
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015606 return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015607
15608 // Optimize vectors in AVX mode
15609 // Sign extend v8i16 to v8i32 and
15610 // v4i32 to v4i64
15611 //
15612 // Divide input vector into two parts
15613 // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15614 // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15615 // concat the vectors to original VT
15616
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015617 unsigned NumElems = OpVT.getVectorNumElements();
Craig Toppercacafd42012-08-14 08:18:43 +000015618 SDValue Undef = DAG.getUNDEF(OpVT);
15619
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015620 SmallVector<int,8> ShufMask1(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015621 for (unsigned i = 0; i != NumElems/2; ++i)
15622 ShufMask1[i] = i;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015623
Craig Toppercacafd42012-08-14 08:18:43 +000015624 SDValue OpLo = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask1[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015625
15626 SmallVector<int,8> ShufMask2(NumElems, -1);
Craig Topper3ef43cf2012-04-24 06:36:35 +000015627 for (unsigned i = 0; i != NumElems/2; ++i)
15628 ShufMask2[i] = i + NumElems/2;
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015629
Craig Toppercacafd42012-08-14 08:18:43 +000015630 SDValue OpHi = DAG.getVectorShuffle(OpVT, dl, Op, Undef, &ShufMask2[0]);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015631
Craig Topper3ef43cf2012-04-24 06:36:35 +000015632 EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
Elena Demikhovskyf6020402012-02-08 08:37:26 +000015633 VT.getVectorNumElements()/2);
15634
Craig Topper3ef43cf2012-04-24 06:36:35 +000015635 OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000015636 OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi);
15637
15638 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15639 }
15640 return SDValue();
15641}
15642
Michael Liaof6c24ee2012-08-10 14:39:24 +000015643static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015644 const X86Subtarget* Subtarget) {
15645 DebugLoc dl = N->getDebugLoc();
15646 EVT VT = N->getValueType(0);
15647
Craig Topperb1bdd7d2012-08-30 06:56:15 +000015648 // Let legalize expand this if it isn't a legal type yet.
15649 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
15650 return SDValue();
15651
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015652 EVT ScalarVT = VT.getScalarType();
Craig Topperbf404372012-08-31 15:40:30 +000015653 if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) ||
15654 (!Subtarget->hasFMA() && !Subtarget->hasFMA4()))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015655 return SDValue();
15656
15657 SDValue A = N->getOperand(0);
15658 SDValue B = N->getOperand(1);
15659 SDValue C = N->getOperand(2);
15660
15661 bool NegA = (A.getOpcode() == ISD::FNEG);
15662 bool NegB = (B.getOpcode() == ISD::FNEG);
15663 bool NegC = (C.getOpcode() == ISD::FNEG);
15664
Michael Liaof6c24ee2012-08-10 14:39:24 +000015665 // Negative multiplication when NegA xor NegB
15666 bool NegMul = (NegA != NegB);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015667 if (NegA)
15668 A = A.getOperand(0);
15669 if (NegB)
15670 B = B.getOperand(0);
15671 if (NegC)
15672 C = C.getOperand(0);
15673
15674 unsigned Opcode;
15675 if (!NegMul)
Craig Topperbf404372012-08-31 15:40:30 +000015676 Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015677 else
Craig Topperbf404372012-08-31 15:40:30 +000015678 Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
15679
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000015680 return DAG.getNode(Opcode, dl, VT, A, B, C);
15681}
15682
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015683static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
Craig Topperc16f8512012-04-25 06:39:39 +000015684 TargetLowering::DAGCombinerInfo &DCI,
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015685 const X86Subtarget *Subtarget) {
Evan Cheng2e489c42009-12-16 00:53:11 +000015686 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
15687 // (and (i32 x86isd::setcc_carry), 1)
15688 // This eliminates the zext. This transformation is necessary because
15689 // ISD::SETCC is always legalized to i8.
15690 DebugLoc dl = N->getDebugLoc();
15691 SDValue N0 = N->getOperand(0);
15692 EVT VT = N->getValueType(0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015693 EVT OpVT = N0.getValueType();
15694
Evan Cheng2e489c42009-12-16 00:53:11 +000015695 if (N0.getOpcode() == ISD::AND &&
15696 N0.hasOneUse() &&
15697 N0.getOperand(0).hasOneUse()) {
15698 SDValue N00 = N0.getOperand(0);
15699 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
15700 return SDValue();
15701 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
15702 if (!C || C->getZExtValue() != 1)
15703 return SDValue();
15704 return DAG.getNode(ISD::AND, dl, VT,
15705 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
15706 N00.getOperand(0), N00.getOperand(1)),
15707 DAG.getConstant(1, VT));
15708 }
Craig Topperd0cf5652012-04-21 18:13:35 +000015709
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015710 // Optimize vectors in AVX mode:
15711 //
15712 // v8i16 -> v8i32
15713 // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32.
15714 // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32.
15715 // Concat upper and lower parts.
15716 //
15717 // v4i32 -> v4i64
15718 // Use vpunpckldq for 4 lower elements v4i32 -> v2i64.
15719 // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64.
15720 // Concat upper and lower parts.
15721 //
Craig Topperc16f8512012-04-25 06:39:39 +000015722 if (!DCI.isBeforeLegalizeOps())
15723 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015724
Craig Topperc16f8512012-04-25 06:39:39 +000015725 if (!Subtarget->hasAVX())
15726 return SDValue();
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015727
Craig Topperc16f8512012-04-25 06:39:39 +000015728 if (((VT == MVT::v8i32) && (OpVT == MVT::v8i16)) ||
15729 ((VT == MVT::v4i64) && (OpVT == MVT::v4i32))) {
Elena Demikhovsky1da58672012-04-22 09:39:03 +000015730
Craig Topperc16f8512012-04-25 06:39:39 +000015731 if (Subtarget->hasAVX2())
15732 return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, N0);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015733
Craig Topperc16f8512012-04-25 06:39:39 +000015734 SDValue ZeroVec = getZeroVector(OpVT, Subtarget, DAG, dl);
15735 SDValue OpLo = getUnpackl(DAG, dl, OpVT, N0, ZeroVec);
15736 SDValue OpHi = getUnpackh(DAG, dl, OpVT, N0, ZeroVec);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015737
Craig Topperc16f8512012-04-25 06:39:39 +000015738 EVT HVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(),
15739 VT.getVectorNumElements()/2);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015740
Craig Topperc16f8512012-04-25 06:39:39 +000015741 OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo);
15742 OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi);
15743
15744 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
Elena Demikhovsky28d7e712012-01-24 13:54:13 +000015745 }
15746
Evan Cheng2e489c42009-12-16 00:53:11 +000015747 return SDValue();
15748}
15749
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015750// Optimize x == -y --> x+y == 0
15751// x != -y --> x+y != 0
15752static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) {
15753 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
15754 SDValue LHS = N->getOperand(0);
Chad Rosiera20e1e72012-08-01 18:39:17 +000015755 SDValue RHS = N->getOperand(1);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000015756
15757 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
15758 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0)))
15759 if (C->getAPIntValue() == 0 && LHS.hasOneUse()) {
15760 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15761 LHS.getValueType(), RHS, LHS.getOperand(1));
15762 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15763 addV, DAG.getConstant(0, addV.getValueType()), CC);
15764 }
15765 if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
15766 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0)))
15767 if (C->getAPIntValue() == 0 && RHS.hasOneUse()) {
15768 SDValue addV = DAG.getNode(ISD::ADD, N->getDebugLoc(),
15769 RHS.getValueType(), LHS, RHS.getOperand(1));
15770 return DAG.getSetCC(N->getDebugLoc(), N->getValueType(0),
15771 addV, DAG.getConstant(0, addV.getValueType()), CC);
15772 }
15773 return SDValue();
15774}
15775
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015776// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015777static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
15778 TargetLowering::DAGCombinerInfo &DCI,
15779 const X86Subtarget *Subtarget) {
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015780 DebugLoc DL = N->getDebugLoc();
Michael Liao2a33cec2012-08-10 19:58:13 +000015781 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
15782 SDValue EFLAGS = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015783
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015784 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
15785 // a zext and produces an all-ones bit which is more useful than 0/1 in some
15786 // cases.
Michael Liao2a33cec2012-08-10 19:58:13 +000015787 if (CC == X86::COND_B)
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015788 return DAG.getNode(ISD::AND, DL, MVT::i8,
15789 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
Michael Liao2a33cec2012-08-10 19:58:13 +000015790 DAG.getConstant(CC, MVT::i8), EFLAGS),
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015791 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015792
Michael Liao2a33cec2012-08-10 19:58:13 +000015793 SDValue Flags;
15794
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015795 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15796 if (Flags.getNode()) {
15797 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15798 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15799 }
15800
15801 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015802 if (Flags.getNode()) {
15803 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15804 return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
15805 }
15806
15807 return SDValue();
15808}
15809
15810// Optimize branch condition evaluation.
15811//
15812static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
15813 TargetLowering::DAGCombinerInfo &DCI,
15814 const X86Subtarget *Subtarget) {
15815 DebugLoc DL = N->getDebugLoc();
15816 SDValue Chain = N->getOperand(0);
15817 SDValue Dest = N->getOperand(1);
15818 SDValue EFLAGS = N->getOperand(3);
15819 X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
15820
15821 SDValue Flags;
15822
Michael Liaodbf8b5b2012-08-28 03:34:40 +000015823 Flags = checkBoolTestSetCCCombine(EFLAGS, CC);
15824 if (Flags.getNode()) {
15825 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15826 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15827 Flags);
15828 }
15829
15830 Flags = checkFlaggedOrCombine(EFLAGS, CC, DAG, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000015831 if (Flags.getNode()) {
15832 SDValue Cond = DAG.getConstant(CC, MVT::i8);
15833 return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
15834 Flags);
15835 }
15836
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015837 return SDValue();
15838}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015839
Craig Topper7fd5e162012-04-24 06:02:29 +000015840static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG) {
Nadav Rotema3540772012-04-23 21:53:37 +000015841 SDValue Op0 = N->getOperand(0);
15842 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015843
15844 // UINT_TO_FP(v4i8) -> SINT_TO_FP(ZEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015845 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015846 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015847 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015848 SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
15849 // Notice that we use SINT_TO_FP because we know that the high bits
15850 // are zero and SINT_TO_FP is better supported by the hardware.
15851 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15852 }
15853
15854 return SDValue();
15855}
15856
Benjamin Kramer1396c402011-06-18 11:09:41 +000015857static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
15858 const X86TargetLowering *XTLI) {
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015859 SDValue Op0 = N->getOperand(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015860 EVT InVT = Op0->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015861
15862 // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32))
Craig Topper7fd5e162012-04-24 06:02:29 +000015863 if (InVT == MVT::v8i8 || InVT == MVT::v4i8) {
Nadav Rotema3540772012-04-23 21:53:37 +000015864 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015865 MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015866 SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
15867 return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P);
15868 }
15869
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015870 // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
15871 // a 32-bit target where SSE doesn't support i64->FP operations.
15872 if (Op0.getOpcode() == ISD::LOAD) {
15873 LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
15874 EVT VT = Ld->getValueType(0);
15875 if (!Ld->isVolatile() && !N->getValueType(0).isVector() &&
15876 ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
15877 !XTLI->getSubtarget()->is64Bit() &&
15878 !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
Benjamin Kramer1396c402011-06-18 11:09:41 +000015879 SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0),
15880 Ld->getChain(), Op0, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000015881 DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
15882 return FILDChain;
15883 }
15884 }
15885 return SDValue();
15886}
15887
Craig Topper7fd5e162012-04-24 06:02:29 +000015888static SDValue PerformFP_TO_SINTCombine(SDNode *N, SelectionDAG &DAG) {
15889 EVT VT = N->getValueType(0);
Nadav Rotema3540772012-04-23 21:53:37 +000015890
15891 // v4i8 = FP_TO_SINT() -> v4i8 = TRUNCATE (V4i32 = FP_TO_SINT()
Nadav Rotema3540772012-04-23 21:53:37 +000015892 if (VT == MVT::v8i8 || VT == MVT::v4i8) {
15893 DebugLoc dl = N->getDebugLoc();
Craig Topper7fd5e162012-04-24 06:02:29 +000015894 MVT DstVT = VT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32;
Nadav Rotema3540772012-04-23 21:53:37 +000015895 SDValue I = DAG.getNode(ISD::FP_TO_SINT, dl, DstVT, N->getOperand(0));
15896 return DAG.getNode(ISD::TRUNCATE, dl, VT, I);
15897 }
15898
15899 return SDValue();
15900}
15901
Chris Lattner23a01992010-12-20 01:37:09 +000015902// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
15903static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
15904 X86TargetLowering::DAGCombinerInfo &DCI) {
15905 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
15906 // the result is either zero or one (depending on the input carry bit).
15907 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
15908 if (X86::isZeroNode(N->getOperand(0)) &&
15909 X86::isZeroNode(N->getOperand(1)) &&
15910 // We don't have a good way to replace an EFLAGS use, so only do this when
15911 // dead right now.
15912 SDValue(N, 1).use_empty()) {
15913 DebugLoc DL = N->getDebugLoc();
15914 EVT VT = N->getValueType(0);
15915 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
15916 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
15917 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
15918 DAG.getConstant(X86::COND_B,MVT::i8),
15919 N->getOperand(2)),
15920 DAG.getConstant(1, VT));
15921 return DCI.CombineTo(N, Res1, CarryOut);
15922 }
15923
15924 return SDValue();
15925}
15926
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015927// fold (add Y, (sete X, 0)) -> adc 0, Y
15928// (add Y, (setne X, 0)) -> sbb -1, Y
15929// (sub (sete X, 0), Y) -> sbb 0, Y
15930// (sub (setne X, 0), Y) -> adc -1, Y
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015931static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015932 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000015933
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015934 // Look through ZExts.
15935 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
15936 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
15937 return SDValue();
15938
15939 SDValue SetCC = Ext.getOperand(0);
15940 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
15941 return SDValue();
15942
15943 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
15944 if (CC != X86::COND_E && CC != X86::COND_NE)
15945 return SDValue();
15946
15947 SDValue Cmp = SetCC.getOperand(1);
15948 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000015949 !X86::isZeroNode(Cmp.getOperand(1)) ||
15950 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000015951 return SDValue();
15952
15953 SDValue CmpOp0 = Cmp.getOperand(0);
15954 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
15955 DAG.getConstant(1, CmpOp0.getValueType()));
15956
15957 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
15958 if (CC == X86::COND_NE)
15959 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
15960 DL, OtherVal.getValueType(), OtherVal,
15961 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
15962 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
15963 DL, OtherVal.getValueType(), OtherVal,
15964 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
15965}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000015966
Craig Topper54f952a2011-11-19 09:02:40 +000015967/// PerformADDCombine - Do target-specific dag combines on integer adds.
15968static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
15969 const X86Subtarget *Subtarget) {
15970 EVT VT = N->getValueType(0);
15971 SDValue Op0 = N->getOperand(0);
15972 SDValue Op1 = N->getOperand(1);
15973
15974 // Try to synthesize horizontal adds from adds of shuffles.
Craig Topperd0a31172012-01-10 06:37:29 +000015975 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Toppere6cf4a02012-01-13 05:04:25 +000015976 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
Craig Topper54f952a2011-11-19 09:02:40 +000015977 isHorizontalBinOp(Op0, Op1, true))
15978 return DAG.getNode(X86ISD::HADD, N->getDebugLoc(), VT, Op0, Op1);
15979
15980 return OptimizeConditionalInDecrement(N, DAG);
15981}
15982
15983static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
15984 const X86Subtarget *Subtarget) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015985 SDValue Op0 = N->getOperand(0);
15986 SDValue Op1 = N->getOperand(1);
15987
15988 // X86 can't encode an immediate LHS of a sub. See if we can push the
15989 // negation into a preceding instruction.
15990 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015991 // If the RHS of the sub is a XOR with one use and a constant, invert the
15992 // immediate. Then add one to the LHS of the sub so we can turn
15993 // X-Y -> X+~Y+1, saving one register.
15994 if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
15995 isa<ConstantSDNode>(Op1.getOperand(1))) {
Nick Lewycky726ebd62011-08-23 19:01:24 +000015996 APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000015997 EVT VT = Op0.getValueType();
15998 SDValue NewXor = DAG.getNode(ISD::XOR, Op1.getDebugLoc(), VT,
15999 Op1.getOperand(0),
16000 DAG.getConstant(~XorC, VT));
16001 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, NewXor,
Nick Lewycky726ebd62011-08-23 19:01:24 +000016002 DAG.getConstant(C->getAPIntValue()+1, VT));
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016003 }
16004 }
16005
Craig Topper54f952a2011-11-19 09:02:40 +000016006 // Try to synthesize horizontal adds from adds of shuffles.
16007 EVT VT = N->getValueType(0);
Craig Topperd0a31172012-01-10 06:37:29 +000016008 if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
Craig Topperb72039c2011-11-30 09:10:50 +000016009 (Subtarget->hasAVX2() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
16010 isHorizontalBinOp(Op0, Op1, true))
Craig Topper54f952a2011-11-19 09:02:40 +000016011 return DAG.getNode(X86ISD::HSUB, N->getDebugLoc(), VT, Op0, Op1);
16012
Benjamin Kramer162ee5c2011-07-26 22:42:13 +000016013 return OptimizeConditionalInDecrement(N, DAG);
16014}
16015
Dan Gohman475871a2008-07-27 21:46:04 +000016016SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000016017 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000016018 SelectionDAG &DAG = DCI.DAG;
16019 switch (N->getOpcode()) {
16020 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000016021 case ISD::EXTRACT_VECTOR_ELT:
Craig Topper89f4e662012-03-20 07:17:59 +000016022 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
Duncan Sands6bcd2192011-09-17 16:49:39 +000016023 case ISD::VSELECT:
Nadav Rotemcc616562012-01-15 19:27:55 +000016024 case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016025 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget);
Craig Topper54f952a2011-11-19 09:02:40 +000016026 case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget);
16027 case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget);
Chris Lattner23a01992010-12-20 01:37:09 +000016028 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000016029 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000016030 case ISD::SHL:
16031 case ISD::SRA:
Mon P Wang845b1892012-02-01 22:15:20 +000016032 case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000016033 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000016034 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Craig Topperb4c94572011-10-21 06:55:01 +000016035 case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget);
Nadav Rotem2dd83eb2012-07-10 13:25:08 +000016036 case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000016037 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Craig Topper7fd5e162012-04-24 06:02:29 +000016038 case ISD::UINT_TO_FP: return PerformUINT_TO_FPCombine(N, DAG);
Stuart Hastingsf99a4b82011-06-06 23:15:58 +000016039 case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this);
Craig Topper7fd5e162012-04-24 06:02:29 +000016040 case ISD::FP_TO_SINT: return PerformFP_TO_SINTCombine(N, DAG);
Duncan Sands17470be2011-09-22 20:15:48 +000016041 case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget);
16042 case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000016043 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000016044 case X86ISD::FOR: return PerformFORCombine(N, DAG);
Nadav Rotemd60cb112012-08-19 13:06:16 +000016045 case X86ISD::FMIN:
16046 case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG);
Chris Lattneraf723b92008-01-25 05:46:26 +000016047 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000016048 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000016049 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Elena Demikhovsky1da58672012-04-22 09:39:03 +000016050 case ISD::ANY_EXTEND:
Craig Topperc16f8512012-04-25 06:39:39 +000016051 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovskydcabc7b2012-02-02 09:10:43 +000016052 case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget);
Elena Demikhovsky3ae98152012-02-01 07:56:44 +000016053 case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG, DCI);
Chad Rosiera73b6fc2012-04-27 22:33:25 +000016054 case ISD::SETCC: return PerformISDSETCCCombine(N, DAG);
Michael Liaodbf8b5b2012-08-28 03:34:40 +000016055 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Michael Liao2a33cec2012-08-10 19:58:13 +000016056 case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget);
Craig Topperb3982da2011-12-31 23:50:21 +000016057 case X86ISD::SHUFP: // Handle all target specific shuffles
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000016058 case X86ISD::PALIGN:
Craig Topper34671b82011-12-06 08:21:25 +000016059 case X86ISD::UNPCKH:
16060 case X86ISD::UNPCKL:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000016061 case X86ISD::MOVHLPS:
16062 case X86ISD::MOVLHPS:
16063 case X86ISD::PSHUFD:
16064 case X86ISD::PSHUFHW:
16065 case X86ISD::PSHUFLW:
16066 case X86ISD::MOVSS:
16067 case X86ISD::MOVSD:
Craig Topper316cd2a2011-11-30 06:25:25 +000016068 case X86ISD::VPERMILP:
Craig Topperec24e612011-11-30 07:47:51 +000016069 case X86ISD::VPERM2X128:
Bruno Cardoso Lopes50b37c72011-08-15 21:45:54 +000016070 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +000016071 case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +000016072 }
16073
Dan Gohman475871a2008-07-27 21:46:04 +000016074 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000016075}
16076
Evan Chenge5b51ac2010-04-17 06:13:15 +000016077/// isTypeDesirableForOp - Return true if the target has native support for
16078/// the specified value type and it is 'desirable' to use the type for the
16079/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
16080/// instruction encodings are longer and some i16 instructions are slow.
16081bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
16082 if (!isTypeLegal(VT))
16083 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016084 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000016085 return true;
16086
16087 switch (Opc) {
16088 default:
16089 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000016090 case ISD::LOAD:
16091 case ISD::SIGN_EXTEND:
16092 case ISD::ZERO_EXTEND:
16093 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016094 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000016095 case ISD::SRL:
16096 case ISD::SUB:
16097 case ISD::ADD:
16098 case ISD::MUL:
16099 case ISD::AND:
16100 case ISD::OR:
16101 case ISD::XOR:
16102 return false;
16103 }
16104}
16105
16106/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000016107/// beneficial for dag combiner to promote the specified node. If true, it
16108/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000016109bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016110 EVT VT = Op.getValueType();
16111 if (VT != MVT::i16)
16112 return false;
16113
Evan Cheng4c26e932010-04-19 19:29:22 +000016114 bool Promote = false;
16115 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016116 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000016117 default: break;
16118 case ISD::LOAD: {
16119 LoadSDNode *LD = cast<LoadSDNode>(Op);
16120 // If the non-extending load has a single use and it's not live out, then it
16121 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016122 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
16123 Op.hasOneUse()*/) {
16124 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
16125 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
16126 // The only case where we'd want to promote LOAD (rather then it being
16127 // promoted as an operand is when it's only use is liveout.
16128 if (UI->getOpcode() != ISD::CopyToReg)
16129 return false;
16130 }
16131 }
Evan Cheng4c26e932010-04-19 19:29:22 +000016132 Promote = true;
16133 break;
16134 }
16135 case ISD::SIGN_EXTEND:
16136 case ISD::ZERO_EXTEND:
16137 case ISD::ANY_EXTEND:
16138 Promote = true;
16139 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016140 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000016141 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000016142 SDValue N0 = Op.getOperand(0);
16143 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000016144 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000016145 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016146 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000016147 break;
16148 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000016149 case ISD::ADD:
16150 case ISD::MUL:
16151 case ISD::AND:
16152 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000016153 case ISD::XOR:
16154 Commute = true;
16155 // fallthrough
16156 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000016157 SDValue N0 = Op.getOperand(0);
16158 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000016159 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016160 return false;
16161 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000016162 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016163 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000016164 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000016165 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000016166 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016167 }
16168 }
16169
16170 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000016171 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000016172}
16173
Evan Cheng60c07e12006-07-05 22:17:51 +000016174//===----------------------------------------------------------------------===//
16175// X86 Inline Assembly Support
16176//===----------------------------------------------------------------------===//
16177
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016178namespace {
16179 // Helper to match a string separated by whitespace.
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016180 bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016181 s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016182
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016183 for (unsigned i = 0, e = args.size(); i != e; ++i) {
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016184 StringRef piece(*args[i]);
16185 if (!s.startswith(piece)) // Check if the piece matches.
16186 return false;
16187
16188 s = s.substr(piece.size());
16189 StringRef::size_type pos = s.find_first_not_of(" \t");
16190 if (pos == 0) // We matched a prefix.
16191 return false;
16192
16193 s = s.substr(pos);
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016194 }
16195
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016196 return s.empty();
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016197 }
Benjamin Kramer0581ed72011-12-18 20:51:31 +000016198 const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={};
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016199}
16200
Chris Lattnerb8105652009-07-20 17:51:36 +000016201bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
16202 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000016203
16204 std::string AsmStr = IA->getAsmString();
16205
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016206 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
16207 if (!Ty || Ty->getBitWidth() % 16 != 0)
16208 return false;
16209
Chris Lattnerb8105652009-07-20 17:51:36 +000016210 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000016211 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000016212 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000016213
16214 switch (AsmPieces.size()) {
16215 default: return false;
16216 case 1:
Chris Lattner7a2bdde2011-04-15 05:18:47 +000016217 // FIXME: this should verify that we are targeting a 486 or better. If not,
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016218 // we will turn this bswap into something that will be lowered to logical
16219 // ops instead of emitting the bswap asm. For now, we don't support 486 or
16220 // lower so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000016221 // bswap $0
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016222 if (matchAsm(AsmPieces[0], "bswap", "$0") ||
16223 matchAsm(AsmPieces[0], "bswapl", "$0") ||
16224 matchAsm(AsmPieces[0], "bswapq", "$0") ||
16225 matchAsm(AsmPieces[0], "bswap", "${0:q}") ||
16226 matchAsm(AsmPieces[0], "bswapl", "${0:q}") ||
16227 matchAsm(AsmPieces[0], "bswapq", "${0:q}")) {
Chris Lattnerb8105652009-07-20 17:51:36 +000016228 // No need to check constraints, nothing other than the equivalent of
16229 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000016230 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016231 }
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016232
Chris Lattnerb8105652009-07-20 17:51:36 +000016233 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000016234 if (CI->getType()->isIntegerTy(16) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016235 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016236 (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") ||
16237 matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) {
Dan Gohman0ef701e2010-03-04 19:58:08 +000016238 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000016239 const std::string &ConstraintsStr = IA->getConstraintString();
16240 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000016241 std::sort(AsmPieces.begin(), AsmPieces.end());
16242 if (AsmPieces.size() == 4 &&
16243 AsmPieces[0] == "~{cc}" &&
16244 AsmPieces[1] == "~{dirflag}" &&
16245 AsmPieces[2] == "~{flags}" &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016246 AsmPieces[3] == "~{fpsr}")
16247 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016248 }
16249 break;
16250 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000016251 if (CI->getType()->isIntegerTy(32) &&
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016252 IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016253 matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") &&
16254 matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") &&
16255 matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) {
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016256 AsmPieces.clear();
16257 const std::string &ConstraintsStr = IA->getConstraintString();
16258 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
16259 std::sort(AsmPieces.begin(), AsmPieces.end());
16260 if (AsmPieces.size() == 4 &&
16261 AsmPieces[0] == "~{cc}" &&
16262 AsmPieces[1] == "~{dirflag}" &&
16263 AsmPieces[2] == "~{flags}" &&
16264 AsmPieces[3] == "~{fpsr}")
16265 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000016266 }
Evan Cheng55d42002011-01-08 01:24:27 +000016267
16268 if (CI->getType()->isIntegerTy(64)) {
16269 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
16270 if (Constraints.size() >= 2 &&
16271 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
16272 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
16273 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer2ea4cdb2011-12-18 19:59:20 +000016274 if (matchAsm(AsmPieces[0], "bswap", "%eax") &&
16275 matchAsm(AsmPieces[1], "bswap", "%edx") &&
16276 matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx"))
Benjamin Kramere6cddb72011-12-17 14:36:05 +000016277 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000016278 }
16279 }
16280 break;
16281 }
16282 return false;
16283}
16284
16285
16286
Chris Lattnerf4dff842006-07-11 02:54:03 +000016287/// getConstraintType - Given a constraint letter, return the type of
16288/// constraint it is for this target.
16289X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000016290X86TargetLowering::getConstraintType(const std::string &Constraint) const {
16291 if (Constraint.size() == 1) {
16292 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000016293 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000016294 case 'q':
16295 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000016296 case 'f':
16297 case 't':
16298 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000016299 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000016300 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000016301 case 'Y':
Eric Christopher31b5f002011-07-07 22:29:07 +000016302 case 'l':
Chris Lattner4234f572007-03-25 02:14:49 +000016303 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000016304 case 'a':
16305 case 'b':
16306 case 'c':
16307 case 'd':
16308 case 'S':
16309 case 'D':
16310 case 'A':
16311 return C_Register;
16312 case 'I':
16313 case 'J':
16314 case 'K':
16315 case 'L':
16316 case 'M':
16317 case 'N':
16318 case 'G':
16319 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000016320 case 'e':
16321 case 'Z':
16322 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000016323 default:
16324 break;
16325 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000016326 }
Chris Lattner4234f572007-03-25 02:14:49 +000016327 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000016328}
16329
John Thompson44ab89e2010-10-29 17:29:13 +000016330/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000016331/// This object must already have been set up with the operand type
16332/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000016333TargetLowering::ConstraintWeight
16334 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000016335 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000016336 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016337 Value *CallOperandVal = info.CallOperandVal;
16338 // If we don't have a value, we can't do a match,
16339 // but allow it at the lowest weight.
16340 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000016341 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +000016342 Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000016343 // Look at the constraint type.
16344 switch (*constraint) {
16345 default:
John Thompson44ab89e2010-10-29 17:29:13 +000016346 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
16347 case 'R':
16348 case 'q':
16349 case 'Q':
16350 case 'a':
16351 case 'b':
16352 case 'c':
16353 case 'd':
16354 case 'S':
16355 case 'D':
16356 case 'A':
16357 if (CallOperandVal->getType()->isIntegerTy())
16358 weight = CW_SpecificReg;
16359 break;
16360 case 'f':
16361 case 't':
16362 case 'u':
16363 if (type->isFloatingPointTy())
16364 weight = CW_SpecificReg;
16365 break;
16366 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000016367 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000016368 weight = CW_SpecificReg;
16369 break;
16370 case 'x':
16371 case 'Y':
Craig Topper1accb7e2012-01-10 06:54:16 +000016372 if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
Eric Christopher55487552012-01-07 01:02:09 +000016373 ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasAVX()))
John Thompson44ab89e2010-10-29 17:29:13 +000016374 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016375 break;
16376 case 'I':
16377 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
16378 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000016379 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016380 }
16381 break;
John Thompson44ab89e2010-10-29 17:29:13 +000016382 case 'J':
16383 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16384 if (C->getZExtValue() <= 63)
16385 weight = CW_Constant;
16386 }
16387 break;
16388 case 'K':
16389 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16390 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
16391 weight = CW_Constant;
16392 }
16393 break;
16394 case 'L':
16395 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16396 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
16397 weight = CW_Constant;
16398 }
16399 break;
16400 case 'M':
16401 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16402 if (C->getZExtValue() <= 3)
16403 weight = CW_Constant;
16404 }
16405 break;
16406 case 'N':
16407 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16408 if (C->getZExtValue() <= 0xff)
16409 weight = CW_Constant;
16410 }
16411 break;
16412 case 'G':
16413 case 'C':
16414 if (dyn_cast<ConstantFP>(CallOperandVal)) {
16415 weight = CW_Constant;
16416 }
16417 break;
16418 case 'e':
16419 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16420 if ((C->getSExtValue() >= -0x80000000LL) &&
16421 (C->getSExtValue() <= 0x7fffffffLL))
16422 weight = CW_Constant;
16423 }
16424 break;
16425 case 'Z':
16426 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
16427 if (C->getZExtValue() <= 0xffffffff)
16428 weight = CW_Constant;
16429 }
16430 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000016431 }
16432 return weight;
16433}
16434
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016435/// LowerXConstraint - try to replace an X constraint, which matches anything,
16436/// with another that has more specific requirements based on the type of the
16437/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000016438const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000016439LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000016440 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
16441 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000016442 if (ConstraintVT.isFloatingPoint()) {
Craig Topper1accb7e2012-01-10 06:54:16 +000016443 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000016444 return "Y";
Craig Topper1accb7e2012-01-10 06:54:16 +000016445 if (Subtarget->hasSSE1())
Chris Lattner5e764232008-04-26 23:02:14 +000016446 return "x";
16447 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016448
Chris Lattner5e764232008-04-26 23:02:14 +000016449 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000016450}
16451
Chris Lattner48884cd2007-08-25 00:47:38 +000016452/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
16453/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000016454void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +000016455 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000016456 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000016457 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000016458 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000016459
Eric Christopher100c8332011-06-02 23:16:42 +000016460 // Only support length 1 constraints for now.
16461 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +000016462
Eric Christopher100c8332011-06-02 23:16:42 +000016463 char ConstraintLetter = Constraint[0];
16464 switch (ConstraintLetter) {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016465 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000016466 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000016467 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016468 if (C->getZExtValue() <= 31) {
16469 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016470 break;
16471 }
Devang Patel84f7fd22007-03-17 00:13:28 +000016472 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016473 return;
Evan Cheng364091e2008-09-22 23:57:37 +000016474 case 'J':
16475 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016476 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000016477 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16478 break;
16479 }
16480 }
16481 return;
16482 case 'K':
16483 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000016484 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000016485 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16486 break;
16487 }
16488 }
16489 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000016490 case 'N':
16491 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000016492 if (C->getZExtValue() <= 255) {
16493 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000016494 break;
16495 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000016496 }
Chris Lattner48884cd2007-08-25 00:47:38 +000016497 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000016498 case 'e': {
16499 // 32-bit signed value
16500 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000016501 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
16502 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016503 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016504 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000016505 break;
16506 }
16507 // FIXME gcc accepts some relocatable values here too, but only in certain
16508 // memory models; it's complicated.
16509 }
16510 return;
16511 }
16512 case 'Z': {
16513 // 32-bit unsigned 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->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016517 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
16518 break;
16519 }
16520 }
16521 // FIXME gcc accepts some relocatable values here too, but only in certain
16522 // memory models; it's complicated.
16523 return;
16524 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016525 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016526 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000016527 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000016528 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000016529 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000016530 break;
16531 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016532
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016533 // In any sort of PIC mode addresses need to be computed at runtime by
16534 // adding in a register or some sort of table lookup. These can't
16535 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000016536 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000016537 return;
16538
Chris Lattnerdc43a882007-05-03 16:52:29 +000016539 // If we are in non-pic codegen mode, we allow the address of a global (with
16540 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000016541 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016542 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000016543
Chris Lattner49921962009-05-08 18:23:14 +000016544 // Match either (GA), (GA+C), (GA+C1+C2), etc.
16545 while (1) {
16546 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
16547 Offset += GA->getOffset();
16548 break;
16549 } else if (Op.getOpcode() == ISD::ADD) {
16550 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16551 Offset += C->getZExtValue();
16552 Op = Op.getOperand(0);
16553 continue;
16554 }
16555 } else if (Op.getOpcode() == ISD::SUB) {
16556 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
16557 Offset += -C->getZExtValue();
16558 Op = Op.getOperand(0);
16559 continue;
16560 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016561 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016562
Chris Lattner49921962009-05-08 18:23:14 +000016563 // Otherwise, this isn't something we can handle, reject it.
16564 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000016565 }
Eric Christopherfd179292009-08-27 18:07:15 +000016566
Dan Gohman46510a72010-04-15 01:51:59 +000016567 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016568 // If we require an extra load to get this address, as in PIC mode, we
16569 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000016570 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
16571 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000016572 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000016573
Devang Patel0d881da2010-07-06 22:08:15 +000016574 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
16575 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000016576 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016577 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000016578 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016579
Gabor Greifba36cb52008-08-28 21:40:38 +000016580 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000016581 Ops.push_back(Result);
16582 return;
16583 }
Dale Johannesen1784d162010-06-25 21:55:36 +000016584 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000016585}
16586
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016587std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000016588X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000016589 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000016590 // First, see if this is a constraint that directly corresponds to an LLVM
16591 // register class.
16592 if (Constraint.size() == 1) {
16593 // GCC Constraint Letters
16594 switch (Constraint[0]) {
16595 default: break;
Eric Christopherd176af82011-06-29 17:23:50 +000016596 // TODO: Slight differences here in allocation order and leaving
16597 // RIP in the class. Do they matter any more here than they do
16598 // in the normal allocation?
16599 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
16600 if (Subtarget->is64Bit()) {
Craig Topperc9099502012-04-20 06:31:50 +000016601 if (VT == MVT::i32 || VT == MVT::f32)
16602 return std::make_pair(0U, &X86::GR32RegClass);
16603 if (VT == MVT::i16)
16604 return std::make_pair(0U, &X86::GR16RegClass);
16605 if (VT == MVT::i8 || VT == MVT::i1)
16606 return std::make_pair(0U, &X86::GR8RegClass);
16607 if (VT == MVT::i64 || VT == MVT::f64)
16608 return std::make_pair(0U, &X86::GR64RegClass);
16609 break;
Eric Christopherd176af82011-06-29 17:23:50 +000016610 }
16611 // 32-bit fallthrough
16612 case 'Q': // Q_REGS
Nick Lewycky9bf45d02011-07-08 00:19:27 +000016613 if (VT == MVT::i32 || VT == MVT::f32)
Craig Topperc9099502012-04-20 06:31:50 +000016614 return std::make_pair(0U, &X86::GR32_ABCDRegClass);
16615 if (VT == MVT::i16)
16616 return std::make_pair(0U, &X86::GR16_ABCDRegClass);
16617 if (VT == MVT::i8 || VT == MVT::i1)
16618 return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
16619 if (VT == MVT::i64)
16620 return std::make_pair(0U, &X86::GR64_ABCDRegClass);
Eric Christopherd176af82011-06-29 17:23:50 +000016621 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016622 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000016623 case 'l': // INDEX_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016624 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016625 return std::make_pair(0U, &X86::GR8RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016626 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016627 return std::make_pair(0U, &X86::GR16RegClass);
Eric Christopher2bbecd82011-05-19 21:33:47 +000016628 if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016629 return std::make_pair(0U, &X86::GR32RegClass);
16630 return std::make_pair(0U, &X86::GR64RegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016631 case 'R': // LEGACY_REGS
Eric Christopher5427ede2011-07-14 20:13:52 +000016632 if (VT == MVT::i8 || VT == MVT::i1)
Craig Topperc9099502012-04-20 06:31:50 +000016633 return std::make_pair(0U, &X86::GR8_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016634 if (VT == MVT::i16)
Craig Topperc9099502012-04-20 06:31:50 +000016635 return std::make_pair(0U, &X86::GR16_NOREXRegClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000016636 if (VT == MVT::i32 || !Subtarget->is64Bit())
Craig Topperc9099502012-04-20 06:31:50 +000016637 return std::make_pair(0U, &X86::GR32_NOREXRegClass);
16638 return std::make_pair(0U, &X86::GR64_NOREXRegClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000016639 case 'f': // FP Stack registers.
16640 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
16641 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000016642 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016643 return std::make_pair(0U, &X86::RFP32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016644 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Craig Topperc9099502012-04-20 06:31:50 +000016645 return std::make_pair(0U, &X86::RFP64RegClass);
16646 return std::make_pair(0U, &X86::RFP80RegClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000016647 case 'y': // MMX_REGS if MMX allowed.
16648 if (!Subtarget->hasMMX()) break;
Craig Topperc9099502012-04-20 06:31:50 +000016649 return std::make_pair(0U, &X86::VR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016650 case 'Y': // SSE_REGS if SSE2 allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016651 if (!Subtarget->hasSSE2()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000016652 // FALL THROUGH.
Eric Christopher55487552012-01-07 01:02:09 +000016653 case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
Craig Topper1accb7e2012-01-10 06:54:16 +000016654 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000016655
Owen Anderson825b72b2009-08-11 20:47:22 +000016656 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000016657 default: break;
16658 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016659 case MVT::f32:
16660 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +000016661 return std::make_pair(0U, &X86::FR32RegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000016662 case MVT::f64:
16663 case MVT::i64:
Craig Topperc9099502012-04-20 06:31:50 +000016664 return std::make_pair(0U, &X86::FR64RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016665 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000016666 case MVT::v16i8:
16667 case MVT::v8i16:
16668 case MVT::v4i32:
16669 case MVT::v2i64:
16670 case MVT::v4f32:
16671 case MVT::v2f64:
Craig Topperc9099502012-04-20 06:31:50 +000016672 return std::make_pair(0U, &X86::VR128RegClass);
Eric Christopher55487552012-01-07 01:02:09 +000016673 // AVX types.
16674 case MVT::v32i8:
16675 case MVT::v16i16:
16676 case MVT::v8i32:
16677 case MVT::v4i64:
16678 case MVT::v8f32:
16679 case MVT::v4f64:
Craig Topperc9099502012-04-20 06:31:50 +000016680 return std::make_pair(0U, &X86::VR256RegClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000016681 }
Chris Lattnerad043e82007-04-09 05:11:28 +000016682 break;
16683 }
16684 }
Scott Michelfdc40a02009-02-17 22:15:04 +000016685
Chris Lattnerf76d1802006-07-31 23:26:50 +000016686 // Use the default implementation in TargetLowering to convert the register
16687 // constraint into a member of a register class.
16688 std::pair<unsigned, const TargetRegisterClass*> Res;
16689 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000016690
16691 // Not found as a standard register?
16692 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016693 // Map st(0) -> st(7) -> ST0
16694 if (Constraint.size() == 7 && Constraint[0] == '{' &&
16695 tolower(Constraint[1]) == 's' &&
16696 tolower(Constraint[2]) == 't' &&
16697 Constraint[3] == '(' &&
16698 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
16699 Constraint[5] == ')' &&
16700 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000016701
Chris Lattner56d77c72009-09-13 22:41:48 +000016702 Res.first = X86::ST0+Constraint[4]-'0';
Craig Topperc9099502012-04-20 06:31:50 +000016703 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016704 return Res;
16705 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016706
Chris Lattner56d77c72009-09-13 22:41:48 +000016707 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016708 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000016709 Res.first = X86::ST0;
Craig Topperc9099502012-04-20 06:31:50 +000016710 Res.second = &X86::RFP80RegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016711 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000016712 }
Chris Lattner56d77c72009-09-13 22:41:48 +000016713
16714 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000016715 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000016716 Res.first = X86::EFLAGS;
Craig Topperc9099502012-04-20 06:31:50 +000016717 Res.second = &X86::CCRRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016718 return Res;
16719 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000016720
Dale Johannesen330169f2008-11-13 21:52:36 +000016721 // 'A' means EAX + EDX.
16722 if (Constraint == "A") {
16723 Res.first = X86::EAX;
Craig Topperc9099502012-04-20 06:31:50 +000016724 Res.second = &X86::GR32_ADRegClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000016725 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000016726 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000016727 return Res;
16728 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016729
Chris Lattnerf76d1802006-07-31 23:26:50 +000016730 // Otherwise, check to see if this is a register class of the wrong value
16731 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
16732 // turn into {ax},{dx}.
16733 if (Res.second->hasType(VT))
16734 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016735
Chris Lattnerf76d1802006-07-31 23:26:50 +000016736 // All of the single-register GCC register classes map their values onto
16737 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
16738 // really want an 8-bit or 32-bit register, map to the appropriate register
16739 // class and return the appropriate register.
Craig Topperc9099502012-04-20 06:31:50 +000016740 if (Res.second == &X86::GR16RegClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000016741 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016742 unsigned DestReg = 0;
16743 switch (Res.first) {
16744 default: break;
16745 case X86::AX: DestReg = X86::AL; break;
16746 case X86::DX: DestReg = X86::DL; break;
16747 case X86::CX: DestReg = X86::CL; break;
16748 case X86::BX: DestReg = X86::BL; break;
16749 }
16750 if (DestReg) {
16751 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016752 Res.second = &X86::GR8RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016753 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016754 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016755 unsigned DestReg = 0;
16756 switch (Res.first) {
16757 default: break;
16758 case X86::AX: DestReg = X86::EAX; break;
16759 case X86::DX: DestReg = X86::EDX; break;
16760 case X86::CX: DestReg = X86::ECX; break;
16761 case X86::BX: DestReg = X86::EBX; break;
16762 case X86::SI: DestReg = X86::ESI; break;
16763 case X86::DI: DestReg = X86::EDI; break;
16764 case X86::BP: DestReg = X86::EBP; break;
16765 case X86::SP: DestReg = X86::ESP; break;
16766 }
16767 if (DestReg) {
16768 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016769 Res.second = &X86::GR32RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016770 }
Owen Anderson825b72b2009-08-11 20:47:22 +000016771 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016772 unsigned DestReg = 0;
16773 switch (Res.first) {
16774 default: break;
16775 case X86::AX: DestReg = X86::RAX; break;
16776 case X86::DX: DestReg = X86::RDX; break;
16777 case X86::CX: DestReg = X86::RCX; break;
16778 case X86::BX: DestReg = X86::RBX; break;
16779 case X86::SI: DestReg = X86::RSI; break;
16780 case X86::DI: DestReg = X86::RDI; break;
16781 case X86::BP: DestReg = X86::RBP; break;
16782 case X86::SP: DestReg = X86::RSP; break;
16783 }
16784 if (DestReg) {
16785 Res.first = DestReg;
Craig Topperc9099502012-04-20 06:31:50 +000016786 Res.second = &X86::GR64RegClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000016787 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000016788 }
Craig Topperc9099502012-04-20 06:31:50 +000016789 } else if (Res.second == &X86::FR32RegClass ||
16790 Res.second == &X86::FR64RegClass ||
16791 Res.second == &X86::VR128RegClass) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000016792 // Handle references to XMM physical registers that got mapped into the
16793 // wrong class. This can happen with constraints like {xmm0} where the
16794 // target independent register mapper will just pick the first match it can
16795 // find, ignoring the required type.
Eli Friedman52d418d2012-06-25 23:42:33 +000016796
16797 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +000016798 Res.second = &X86::FR32RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016799 else if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +000016800 Res.second = &X86::FR64RegClass;
16801 else if (X86::VR128RegClass.hasType(VT))
16802 Res.second = &X86::VR128RegClass;
Eli Friedman52d418d2012-06-25 23:42:33 +000016803 else if (X86::VR256RegClass.hasType(VT))
16804 Res.second = &X86::VR256RegClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000016805 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000016806
Chris Lattnerf76d1802006-07-31 23:26:50 +000016807 return Res;
16808}